Skip to content

Commit

Permalink
Merge pull request #7 from JacksonTian/add_unit_test
Browse files Browse the repository at this point in the history
添加单元测试
  • Loading branch information
sofish committed Feb 15, 2014
2 parents 00f6bda + 938bab3 commit d4dc21d
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules
coverage.html

# Linux
!.gitignore
*~
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- 0.8
- 0.6
- "0.10"
- "0.8"
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
TESTS = test/*.js
REPORTER = spec
TIMEOUT = 2000

test:
@NODE_ENV=test ./node_modules/mocha/bin/mocha \
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
$(MOCHA_OPTS) \
$(TESTS)

test-cov:
@$(MAKE) test REPORTER=dot
@$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=html-cov > coverage.html
@$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=travis-cov

test-coveralls:
@$(MAKE) test
@echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID)
@$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js

test-all: test test-cov

.PHONY: test test-cov test-all
2 changes: 1 addition & 1 deletion han.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = exports = require('./lib');
module.exports = require('./lib');
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ var translate = require('./translate')
module.exports = exports = {
letter: translate.letter
, pinyin: translate.pinyin
}
}
43 changes: 34 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,46 @@
"name": "han",
"description": "a module for tanslating Chinese(汉字) into pinyin",
"version": "0.0.7",
"author": {
"name": "Sofish Lin",
"email": "[email protected]"
},
"author": "Sofish Lin <[email protected]>",
"dependencies": {},
"devDependencies": {},
"keywords": ["han", "pinyin", "url"],
"devDependencies": {
"mocha": "*",
"should": "*",
"blanket": "*",
"travis-cov": "*",
"mocha-lcov-reporter": "*",
"coveralls": "*"
},
"keywords": [
"han",
"pinyin",
"url"
],
"repository": {
"type": "git",
"url": "git://github.com/sofish/han.git"
},
"config": {
"blanket": {
"pattern": "han/lib",
"data-cover-flags": {
"debug": false
}
},
"travis-cov": {
"threshold": 98
}
},
"main": "han",
"bin": {},
"scripts": {},
"scripts": {
"test": "make test"
},
"engines": {
"node": "*"
}
},
"bugs": {
"url": "https://github.com/sofish/han/issues"
},
"homepage": "https://github.com/sofish/han",
"license": "MIT"
}
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Han(汉)

[![Build Status](https://travis-ci.org/sofish/han.png)](https://travis-ci.org/sofish/han)
[![Coverage Status](https://coveralls.io/repos/sofish/han/badge.png)](https://coveralls.io/r/sofish/han)

a module for tanslating Chinese(汉字) into pinyin.

Expand Down Expand Up @@ -63,7 +64,7 @@ Beause that a Chinese word always having more than one pronunciation. You may wa
$ npm install han
# or git clone git://github.com/sofish/han.git

$ cd node_modules/han && node testcase.js
$ cd node_modules/han && make test
```

## Translating Messy Code
Expand Down
59 changes: 59 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var han = require('../')
var should = require('should');

describe('han', function () {
it('letter should ok', function () {
han.letter('中文').should.be.equal('zhongwen');
var expect = 'yao-shi-xian-speaker-deck-na-zhong-zhong-wen-zhuan-pin-yin-de';
han.letter('要实现 Speaker Deck 那种中文转拼音的', '-').should.be.equal(expect);
});

it('letter with 特殊符号', function () {
han.letter('中aaaaa中¢∞§¶•誩aa文喳aa').should.be.equal('zhongaaaaa4e2da2221ea7b62022jingaawenzhaaa');
han.letter('中EnglishWords¢∞§¶•ªº文', '-').should.be.equal('zhong-englishwords-221ea7b6-2022aaba-wen');
});

it('letter with empty', function () {
// 空
han.letter('中文', ' ').should.be.equal('zhong wen');
han.letter('中文', '').should.be.equal('zhongwen');
});

it('pinyin should ok', function () {
han.pinyin('My Chinese name is 小鱼(sofish)').should.be.eql([
'My Chinese name is ',
[ 'xǐao' ],
[ 'yú' ],
'(sofish)'
]);
var expect = [
'#$%^&*',
[ 'zhōng', 'zhòng' ],
'23¢∞§¶•ª52849',
[ 'wén', 'wèn' ],
'@#$%^&*(',
[ 'yì' ],
[ 'sī', 'sì', 'sāi' ],
',',
[ 'húan', 'hái', 'xúan' ],
[ 'yǒu', 'yòu' ],
[ 'yīng', 'yāng' ],
[ 'wén', 'wèn' ],
':english'
];
han.pinyin('#$%^&*中23¢∞§¶•ª52849文@#$%^&*(意思,还有英文:english').should.be.eql(expect);
});

it('pinyin with empty', function () {
han.pinyin('').should.be.eql([]);
});

it('pinyin with english only', function () {
han.pinyin('words like 1234567890 only').should.be.eql(['words like 1234567890 only']);
});

it('pinyin with english and 特殊符号', function () {
var expect = ['English words and ∞§¶•ª will always return itself'];
han.pinyin('English words and ∞§¶•ª will always return itself').should.be.eql(expect);
});
});

0 comments on commit d4dc21d

Please sign in to comment.