Open Chinese Convert  0.4.3
A project for conversion between Traditional and Simplified Chinese
 All Data Structures Files Functions Variables Groups Pages
test.js
1 var assert = require('assert');
2 var fs = require('fs');
3 var OpenCC = require('./opencc');
4 
5 var configs = [
6  'zhs2zht',
7  'zht2zhs',
8  'mix2zht',
9  'mix2zhs',
10  'zhs2zhtw_p',
11  'zhs2zhtw_vp',
12  'zhtw2zhcn_t',
13  'zhtw2zhcn_s',
14 ];
15 
16 var testSync = function (config, done) {
17  var inputName = 'test/testcases/' + config + '.in';
18  var outputName = 'test/testcases/' + config + '.ans';
19  var configName = config + '.ini';
20  var opencc = new OpenCC(configName);
21  fs.readFile(inputName, 'utf-8', function (err, text) {
22  if (err) return done(err);
23  var converted = opencc.convertSync(text);
24  fs.readFile(outputName, 'utf-8', function (err, answer) {
25  if (err) return done(err);
26  assert.equal(converted, answer);
27  done();
28  });
29  });
30 };
31 
32 var testAsync = function (config, done) {
33  var inputName = 'test/testcases/' + config + '.in';
34  var outputName = 'test/testcases/' + config + '.ans';
35  var configName = config + '.ini';
36  var opencc = new OpenCC(configName);
37  fs.readFile(inputName, 'utf-8', function (err, text) {
38  if (err) return done(err);
39  opencc.convert(text, function (err, converted) {
40  if (err) return done(err);
41  fs.readFile(outputName, 'utf-8', function (err, answer) {
42  if (err) return done(err);
43  assert.equal(converted, answer);
44  done();
45  });
46  });
47  });
48 };
49 
50 describe('Sync API', function () {
51  configs.forEach(function (config) {
52  it(config, function (done) {
53  testSync(config, done);
54  });
55  });
56 });
57 
58 describe('Async API', function () {
59  configs.forEach(function (config) {
60  it(config, function (done) {
61  testAsync(config, done);
62  });
63  });
64 });