diff --git a/package-lock.json b/package-lock.json index 510aed1e..ed886bb4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7175,6 +7175,15 @@ "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=", "dev": true }, + "set-tz": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/set-tz/-/set-tz-0.2.0.tgz", + "integrity": "sha512-LDEBBYjFt57i83bqWN5MFpff++56Q7VfY/m0/GpSfF9BgmjqnLO3Izaf44PDC5dsUDPy4kx1ZV2QDtuBq7MP+g==", + "dev": true, + "requires": { + "windows-iana": "^3.0.0" + } + }, "set-value": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/set-value/-/set-value-2.0.1.tgz", @@ -8747,6 +8756,12 @@ } } }, + "windows-iana": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/windows-iana/-/windows-iana-3.1.0.tgz", + "integrity": "sha512-rCPf3AakAAgvapnbYVvG2bQyI3g6EDbPpjDJ72fdAu+XTzB1qvX4ZC6OnZ0I2+thaspjTb+8KwdyhdBl8Lt/QA==", + "dev": true + }, "word-wrap": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", diff --git a/package.json b/package.json index eeef3380..6bc88ee6 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "prettier": "^1.19.1", "raw-loader": "^4.0.0", "rimraf": "^3.0.0", + "set-tz": "^0.2.0", "version-bump-prompt": "^5.0.6", "webpack": "^4.41.4", "webpack-cli": "^3.3.10", diff --git a/test/examples.test.js b/test/examples.test.js index a0e9972f..0cb20dbd 100644 --- a/test/examples.test.js +++ b/test/examples.test.js @@ -7,7 +7,6 @@ var { compile, evalCompiled } = require("../src/parser"); var lib = utils.loadlib(); const exampleDir = path.resolve(__dirname, "../examples/"); -const outputDir = path.resolve(__dirname, "../test/temp/examples/"); const python = getPythonExecutable(); const ignoreExamples = [ @@ -67,11 +66,6 @@ function runAll(lang, options) { } describe("examples", () => { - before(() => { - fs.removeSync(outputDir); - fs.ensureDirSync(outputDir); - }); - describe("javascript", () => { runAll("js"); }); diff --git a/test/hanzi2num.test.js b/test/hanzi2num.test.js deleted file mode 100644 index 6ac55ba8..00000000 --- a/test/hanzi2num.test.js +++ /dev/null @@ -1,349 +0,0 @@ -var assert = require("assert"); -var { num2hanzi, hanzi2num, hanzi2numstr } = require("../src/hanzi2num"); - -describe("hanzi2num", () => { - describe("num2hanzi(0.53212121222)", () => { - it("should translate float number to hanzi correctly", () => { - assert.strictEqual( - num2hanzi(0.53212121222), - "五分三釐二毫一絲二忽一微二纖一沙二塵二埃二渺" - ); - }); - }); - - describe("num2hanzi(0.5)", () => { - it("should translate float number to hanzi correctly", () => { - assert.strictEqual(num2hanzi(0.5), "五分"); - }); - }); - - describe("hanzi2num(num2hanzi(0.532))", () => { - it("should translate float number and hanzi back and forth", () => { - assert.strictEqual(hanzi2num(num2hanzi(0.532)), 0.532); - }); - }); - - describe('num2hanzi(hanzi2num("二十一京二千三百四十五兆六千七百八十億零九百萬零二百五十有一"))', () => { - it("should translate float number and hanzi back and forth", () => { - assert.strictEqual( - num2hanzi( - hanzi2num( - "二十一京二千三百四十五兆六千七百八十億零九百萬零二百五十有一" - ) - ), - "二十一京二千三百四十五兆六千七百八十億零九百萬零二百六十" - ); - }); - }); - - describe("num2hanzi(-(1e+10 + 99))", () => { - it("should translate small negative float numner to hanzi", () => { - assert.strictEqual(num2hanzi(-(1e10 + 99)), "負一百億零九十九"); - }); - }); - - describe("num2hanzi(0)", () => { - it("should translate 0 to hanzi", () => { - assert.strictEqual(num2hanzi(0), "零"); - }); - }); - - describe("num2hanzi(-0.765433)", () => { - it("should translate negative float number", () => { - assert.strictEqual(num2hanzi(-0.765433), "負七分六釐五毫四絲三忽三微"); - }); - }); - - describe('hanzi2num("負一又二分三釐四毫五絲六忽七微")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual( - hanzi2num("負一又二分三釐四毫五絲六忽七微"), - -1.234567 - ); - }); - }); - - describe('hanzi2num("一沙一塵")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一沙一塵"), 1.1e-8); - }); - }); - - describe('hanzi2numstr("二十一京二千三百四十五兆六千七百八十億零九百萬零二百五十有一")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual( - hanzi2numstr( - "二十一京二千三百四十五兆六千七百八十億零九百萬零二百五十有一" - ), - "212345678009000251" - ); - }); - }); - - describe('hanzi2numstr("一極零二")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual( - hanzi2numstr("一極零二"), - "1000000000000000000000000000000000000000000000002" - ); - }); - }); - - describe('hanzi2numstr("一極零二又三漠")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual( - hanzi2numstr("一極零二又三漠"), - "1000000000000000000000000000000000000000000000002.000000000003" - ); - }); - }); - - describe('hanzi2numstr("一極零二京")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual( - hanzi2numstr("一極零二京"), - "1.00000000000000000000000000000002e+48" - ); - }); - }); - - describe('hanzi2num("極")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("極"), 1e48); - }); - }); - - describe('hanzi2num("三十二又一分")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("三十二又一分"), 32.1); - }); - }); - - describe('hanzi2num("二分七釐三毫一絲二忽三微四纖五沙三塵一埃")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual( - hanzi2num("二分七釐三毫一絲二忽三微四纖五沙三塵一埃"), - 0.2731234531 - ); - }); - }); - - //渺、埃、尘 (塵)、沙、纤(纖)、微 - describe('hanzi2num("一微")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一微"), 1e-6); - }); - }); - - /* - describe('hanzi2num("一纤")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一纤"), 1e-7); - }); - }); - */ - - describe('hanzi2num("一纖")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一纖"), 1e-7); - }); - }); - - describe('hanzi2num("一沙")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一沙"), 1e-8); - }); - }); - - describe('hanzi2num("一塵")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一塵"), 1e-9); - }); - }); - - /* - describe('hanzi2num("一尘")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一尘"), 1e-9); - }); - }); - */ - - describe('hanzi2num("一埃")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一埃"), 1e-10); - }); - }); - - describe('hanzi2num("一渺")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一渺"), 1e-11); - }); - }); - // ended 渺、埃、尘、沙、纤、微 - - describe('hanzi2num("一漠")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一漠"), 1e-12); - }); - }); - - describe('hanzi2num("三千萬埃")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("三千萬埃"), 0.003); - }); - }); - - describe('hanzi2num("三十二")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("三十二"), 32); - }); - }); - - describe('hanzi2num("極極極極極極")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("極極極極極極"), 1e288); - }); - }); - - describe('hanzi2num("一萬")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一萬"), 10000); - }); - }); - - describe('hanzi2num("零")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("零"), 0); - }); - }); - - describe('hanzi2num("二百五")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("二百五"), 205); - }); - }); - - describe('hanzi2num("二百零五")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("二百零五"), 205); - }); - }); - - describe('hanzi2num("三千七")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("三千七"), 3007); - }); - }); - - describe('hanzi2num("五萬三千七")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("五萬三千七"), 53007); - }); - }); - - describe('hanzi2num("五萬三千零七")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("五萬三千零七"), 53007); - }); - }); - - describe('hanzi2num("十萬")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("十萬"), 1e5); - }); - }); - - describe('hanzi2num("萬十")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("萬十"), 10010); - }); - }); - - describe('hanzi2num("萬億")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("萬億"), 1e12); - }); - }); - - describe('hanzi2num("億萬")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("億萬"), 100010000); - }); - }); - - describe('hanzi2num("三·一四一五九二六五三五八九七九三")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual( - hanzi2num("三·一四一五九二六五三五八九七九三"), - Math.PI - ); - }); - }); - - describe('hanzi2num("一二京三四五六兆七〇〇〇億")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一二京三四五六兆七〇〇〇億"), 1.234567e17); - }); - }); - - describe('hanzi2num("一二京三四五六兆七〇〇〇〇億")', () => { - it("should reject invalid hanzi", () => { - assert.ok(Number.isNaN(hanzi2num("一二京三四五六兆七〇〇〇〇億"))); - }); - }); - - describe('hanzi2num("一百又五絲")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一百又五絲"), 100.0005); - }); - }); - - describe('hanzi2num("一百零五絲")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一百零五絲"), 0.0105); - }); - }); - - describe('hanzi2num("一千又五百")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一千又五百"), 1500); - }); - }); - - describe('hanzi2num("一千零五百")', () => { - it("should reject invalid hanzi", () => { - assert.ok(Number.isNaN(hanzi2num("一千零五百"))); - }); - }); - - describe('hanzi2num("五毫絲")', () => { - it("should reject invalid hanzi", () => { - assert.ok(Number.isNaN(hanzi2num("五毫絲"))); - }); - }); - - describe('hanzi2num("五又十分")', () => { - it("should reject invalid hanzi", () => { - assert.ok(Number.isNaN(hanzi2num("五又十分"))); - }); - }); - - describe('hanzi2num("十分")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("十分"), 1); - }); - }); - - describe('hanzi2num("一分十")', () => { - it("should reject invalid hanzi", () => { - assert.ok(Number.isNaN(hanzi2num("一分十"))); - }); - }); - - describe('hanzi2num("一分十絲")', () => { - it("should translate hanzi to number correctly", () => { - assert.strictEqual(hanzi2num("一分十絲"), 0.101); - }); - }); -}); diff --git a/test/numbers.test.js b/test/numbers.test.js new file mode 100644 index 00000000..7d60cbef --- /dev/null +++ b/test/numbers.test.js @@ -0,0 +1,243 @@ +var assert = require("assert"); +var { num2hanzi, hanzi2num, hanzi2numstr } = require("../src/hanzi2num"); + +describe("number conventions", () => { + describe("hanzi2num", () => { + it("負一又二分三釐四毫五絲六忽七微", () => { + assert.strictEqual( + hanzi2num("負一又二分三釐四毫五絲六忽七微"), + -1.234567 + ); + }); + + it("一沙一塵", () => { + assert.strictEqual(hanzi2num("一沙一塵"), 1.1e-8); + }); + + it("極", () => { + assert.strictEqual(hanzi2num("極"), 1e48); + }); + + it("三十二又一分", () => { + assert.strictEqual(hanzi2num("三十二又一分"), 32.1); + }); + + it("二分七釐三毫一絲二忽三微四纖五沙三塵一埃", () => { + assert.strictEqual( + hanzi2num("二分七釐三毫一絲二忽三微四纖五沙三塵一埃"), + 0.2731234531 + ); + }); + + //渺、埃、尘 (塵)、沙、纤(纖)、微 + it("一微", () => { + assert.strictEqual(hanzi2num("一微"), 1e-6); + }); + + it("一纖", () => { + assert.strictEqual(hanzi2num("一纖"), 1e-7); + }); + + it("一沙", () => { + assert.strictEqual(hanzi2num("一沙"), 1e-8); + }); + + it("一塵", () => { + assert.strictEqual(hanzi2num("一塵"), 1e-9); + }); + + it("一埃", () => { + assert.strictEqual(hanzi2num("一埃"), 1e-10); + }); + + it("一渺", () => { + assert.strictEqual(hanzi2num("一渺"), 1e-11); + }); + // ended 渺、埃、尘、沙、纤、微 + + it("一漠", () => { + assert.strictEqual(hanzi2num("一漠"), 1e-12); + }); + + it("三千萬埃", () => { + assert.strictEqual(hanzi2num("三千萬埃"), 0.003); + }); + + it("三十二", () => { + assert.strictEqual(hanzi2num("三十二"), 32); + }); + + it("極極極極極極", () => { + assert.strictEqual(hanzi2num("極極極極極極"), 1e288); + }); + + it("一萬", () => { + assert.strictEqual(hanzi2num("一萬"), 10000); + }); + + it("零", () => { + assert.strictEqual(hanzi2num("零"), 0); + }); + + it("二百五", () => { + assert.strictEqual(hanzi2num("二百五"), 205); + }); + + it("二百零五", () => { + assert.strictEqual(hanzi2num("二百零五"), 205); + }); + + it("三千七", () => { + assert.strictEqual(hanzi2num("三千七"), 3007); + }); + + it("五萬三千七", () => { + assert.strictEqual(hanzi2num("五萬三千七"), 53007); + }); + + it("五萬三千零七", () => { + assert.strictEqual(hanzi2num("五萬三千零七"), 53007); + }); + + it("十萬", () => { + assert.strictEqual(hanzi2num("十萬"), 1e5); + }); + + it("萬十", () => { + assert.strictEqual(hanzi2num("萬十"), 10010); + }); + + it("萬億", () => { + assert.strictEqual(hanzi2num("萬億"), 1e12); + }); + + it("億萬", () => { + assert.strictEqual(hanzi2num("億萬"), 100010000); + }); + + it("三·一四一五九二六五三五八九七九三", () => { + assert.strictEqual( + hanzi2num("三·一四一五九二六五三五八九七九三"), + Math.PI + ); + }); + + it("一二京三四五六兆七〇〇〇億", () => { + assert.strictEqual(hanzi2num("一二京三四五六兆七〇〇〇億"), 1.234567e17); + }); + + it("一二京三四五六兆七〇〇〇〇億", () => { + assert.ok(Number.isNaN(hanzi2num("一二京三四五六兆七〇〇〇〇億"))); + }); + + it("一百又五絲", () => { + assert.strictEqual(hanzi2num("一百又五絲"), 100.0005); + }); + + it("一百零五絲", () => { + assert.strictEqual(hanzi2num("一百零五絲"), 0.0105); + }); + + it("一千又五百", () => { + assert.strictEqual(hanzi2num("一千又五百"), 1500); + }); + + it("一千零五百", () => { + assert.ok(Number.isNaN(hanzi2num("一千零五百"))); + }); + + it("五毫絲", () => { + assert.ok(Number.isNaN(hanzi2num("五毫絲"))); + }); + + it("五又十分", () => { + assert.ok(Number.isNaN(hanzi2num("五又十分"))); + }); + + it("十分", () => { + assert.strictEqual(hanzi2num("十分"), 1); + }); + + it("一分十", () => { + assert.ok(Number.isNaN(hanzi2num("一分十"))); + }); + + it("一分十絲", () => { + assert.strictEqual(hanzi2num("一分十絲"), 0.101); + }); + }); + + describe("num2hanzi", () => { + it("(0.53212121222)", () => { + assert.strictEqual( + num2hanzi(0.53212121222), + "五分三釐二毫一絲二忽一微二纖一沙二塵二埃二渺" + ); + }); + + it("(0.5)", () => { + assert.strictEqual(num2hanzi(0.5), "五分"); + }); + + it("(-(1e+10 + 99))", () => { + assert.strictEqual(num2hanzi(-(1e10 + 99)), "負一百億零九十九"); + }); + + it("(0)", () => { + assert.strictEqual(num2hanzi(0), "零"); + }); + + it("(-0.765433)", () => { + assert.strictEqual(num2hanzi(-0.765433), "負七分六釐五毫四絲三忽三微"); + }); + }); + + describe("hanzi2numstr", () => { + it("二十一京二千三百四十五兆六千七百八十億零九百萬零二百五十有一", () => { + assert.strictEqual( + hanzi2numstr( + "二十一京二千三百四十五兆六千七百八十億零九百萬零二百五十有一" + ), + "212345678009000251" + ); + }); + + it("一極零二", () => { + assert.strictEqual( + hanzi2numstr("一極零二"), + "1000000000000000000000000000000000000000000000002" + ); + }); + + it("一極零二又三漠", () => { + assert.strictEqual( + hanzi2numstr("一極零二又三漠"), + "1000000000000000000000000000000000000000000000002.000000000003" + ); + }); + + it("一極零二京", () => { + assert.strictEqual( + hanzi2numstr("一極零二京"), + "1.00000000000000000000000000000002e+48" + ); + }); + }); + + describe("hanzi2num & num2hanzi", () => { + it("hanzi2num(num2hanzi(0.532))", () => { + assert.strictEqual(hanzi2num(num2hanzi(0.532)), 0.532); + }); + + it('num2hanzi(hanzi2num("二十一京二千三百四十五兆六千七百八十億零九百萬零二百五十有一"))', () => { + assert.strictEqual( + num2hanzi( + hanzi2num( + "二十一京二千三百四十五兆六千七百八十億零九百萬零二百五十有一" + ) + ), + "二十一京二千三百四十五兆六千七百八十億零九百萬零二百六十" + ); + }); + }); +}); diff --git a/test/stdlib.calendar.test.js b/test/stdlib.calendar.test.js new file mode 100644 index 00000000..cefaca46 --- /dev/null +++ b/test/stdlib.calendar.test.js @@ -0,0 +1,26 @@ +require("set-tz")("UTC"); + +const { createTestUtil } = require("./utils"); + +const { expectOutput } = createTestUtil({ + prefix: "吾嘗觀「「曆法」」之書。", + suffix: "書之" +}); + +describe("stdlib", () => { + describe("calendar", () => { + it("言彼之時刻", () => { + expectOutput( + "方悟「言彼之時刻」之義。施「言彼之時刻」於四千七百一十四", + "丑初一刻三分三十四秒" + ); + }); + + it("言彼之日時", () => { + expectOutput( + "方悟「言彼之日時」之義。施「言彼之日時」於四千七百一十四", + "西元一九六九年己酉年十一月二十四日辛巳日丑初一刻三分三十四秒" + ); + }); + }); +}); diff --git a/test/stdlib.math.test.js b/test/stdlib.math.test.js new file mode 100644 index 00000000..18eff399 --- /dev/null +++ b/test/stdlib.math.test.js @@ -0,0 +1,14 @@ +const { createTestUtil } = require("./utils"); + +const { expectOutput } = createTestUtil({ + prefix: "吾嘗觀「「算經」」之書。", + suffix: "書之" +}); + +describe("stdlib", () => { + describe("math", () => { + it("取整", () => { + expectOutput("方悟「取整」之義。施「取整」於一百又五絲", "一百"); + }); + }); +}); diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 00000000..ad12f5f5 --- /dev/null +++ b/test/utils.js @@ -0,0 +1,29 @@ +const { execute } = require("../src/parser"); +const { expect } = require("chai"); +var utils = require("../tools/utils"); + +var lib = utils.loadlib(); + +function createTestUtil(options = {}) { + const { prefix = "", suffix = "書之", compileOptions = {} } = options; + + function expectOutput(source, expected) { + let output = ""; + execute(prefix + source + suffix, { + lang: "js", + scoped: true, + lib, + logCallback: () => 0, + ...compileOptions, + output: (...args) => (output += args.join(" ") + "\n") + }); + + expect(output.trim()).eq(expected.trim()); + } + + return { expectOutput }; +} + +module.exports = { + createTestUtil +};