File size: 2,278 Bytes
2d8be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const assert = require('assert');
const fs = require('fs');
const util = require('util');

const OpenCC = require('./opencc');

const configs = [
  'hk2s',
  'hk2t',
  'jp2t',
  's2hk',
  's2t',
  's2tw',
  's2twp',
  't2hk',
  't2jp',
  't2s',
  'tw2s',
  'tw2sp',
  'tw2t',
];

const testSync = function (config, done) {
  const inputName = 'test/testcases/' + config + '.in';
  const outputName = 'test/testcases/' + config + '.ans';
  const configName = config + '.json';
  const opencc = new OpenCC(configName);
  const text = fs.readFileSync(inputName, 'utf-8');
  const converted = opencc.convertSync(text);
  const answer = fs.readFileSync(outputName, 'utf-8');
  assert.equal(converted, answer);
  done();
};

const testAsync = function (config, done) {
  const inputName = 'test/testcases/' + config + '.in';
  const outputName = 'test/testcases/' + config + '.ans';
  const configName = config + '.json';
  const opencc = new OpenCC(configName);
  fs.readFile(inputName, 'utf-8', function (err, text) {
    if (err) return done(err);
    opencc.convert(text, function (err, converted) {
      if (err) return done(err);
      fs.readFile(outputName, 'utf-8', function (err, answer) {
        if (err) return done(err);
        assert.equal(converted, answer);
        done();
      });
    });
  });
};

async function testAsyncPromise(config) {
  const inputName = 'test/testcases/' + config + '.in';
  const outputName = 'test/testcases/' + config + '.ans';
  const configName = config + '.json';
  const opencc = new OpenCC(configName);

  const text = await util.promisify(fs.readFile)(inputName, 'utf-8');
  const converted = await opencc.convertPromise(text);
  const answer = await util.promisify(fs.readFile)(outputName, 'utf-8');

  assert.equal(converted, answer);
};

describe('Sync API', function () {
  configs.forEach(function (config) {
    it(config, function (done) {
      testSync(config, done);
    });
  });
});

describe('Async API', function () {
  configs.forEach(function (config) {
    it(config, function (done) {
      testAsync(config, done);
    });
  });
});

describe('Async Promise API', function () {
  configs.forEach(function (config) {
    it(config, function (done) {
      testAsyncPromise(config).then(done);
    });
  });
});