diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 00000000..63188989 --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,26 @@ +name: Node CI + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [8.x, 10.x, 12.x] + + steps: + - uses: actions/checkout@v1 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - name: npm install, build, and test + run: | + npm ci + npm run build --if-present + npm test + env: + CI: true diff --git "a/examples/calendar/calendar-\345\275\274\345\271\264\344\275\225\345\271\264\350\231\237.wy" "b/examples/calendar/calendar-\345\275\274\345\271\264\344\275\225\345\271\264\350\231\237.wy" new file mode 100644 index 00000000..db73b6f7 --- /dev/null +++ "b/examples/calendar/calendar-\345\275\274\345\271\264\344\275\225\345\271\264\350\231\237.wy" @@ -0,0 +1,4 @@ +吾嘗觀「「曆法」」之書。方悟「言彼之時刻」之義。 + +注曰「「言彼之時刻。同Javascript之new Date(x * 1000).toTimeString(), in Chinese calendar也。」」 +吾有二言。曰「「施「言彼之時刻」於四千七百一十四? 」」。施「言彼之時刻」於四千七百一十四。書之。 diff --git "a/examples/calendar/calendar-\350\250\200\345\275\274\344\271\213\345\271\264\346\234\210\346\227\245.wy" "b/examples/calendar/calendar-\350\250\200\345\275\274\344\271\213\345\271\264\346\234\210\346\227\245.wy" new file mode 100644 index 00000000..fe22c03e --- /dev/null +++ "b/examples/calendar/calendar-\350\250\200\345\275\274\344\271\213\345\271\264\346\234\210\346\227\245.wy" @@ -0,0 +1,4 @@ +吾嘗觀「「曆法」」之書。方悟「言彼之年月日」之義。 + +批曰。「「今有一術。名之曰「言彼之年月日」。欲行是術。必先得一數。曰「時」。」」。 +吾有二言。曰「「施「言彼之年月日」於四千七百一十四 (「時」)?」」。施「言彼之年月日」於四千七百一十四。書之。 diff --git "a/examples/calendar/calendar-\350\250\200\345\275\274\344\271\213\346\227\245\346\231\202.wy" "b/examples/calendar/calendar-\350\250\200\345\275\274\344\271\213\346\227\245\346\231\202.wy" new file mode 100644 index 00000000..274154cd --- /dev/null +++ "b/examples/calendar/calendar-\350\250\200\345\275\274\344\271\213\346\227\245\346\231\202.wy" @@ -0,0 +1,4 @@ +吾嘗觀「「曆法」」之書。方悟「言彼之日時」之義。 + +批曰。「「欲行是術。必先得一數。曰「時」」」。 +吾有二言。曰「「施「言彼之日時」於四千七百一十四? (「時」)」」。施「言彼之日時」於四千七百一十四。書之。 diff --git "a/examples/calendar/calendar-\350\250\200\345\275\274\344\271\213\346\231\202\345\210\273.wy" "b/examples/calendar/calendar-\350\250\200\345\275\274\344\271\213\346\231\202\345\210\273.wy" new file mode 100644 index 00000000..b511687f --- /dev/null +++ "b/examples/calendar/calendar-\350\250\200\345\275\274\344\271\213\346\231\202\345\210\273.wy" @@ -0,0 +1,3 @@ +吾嘗觀「「曆法」」之書。方悟「彼年何年號」之義。 + +吾有二言。曰「「施「彼年何年號」於四千七百一十四? 」」。施「彼年何年號」於四千七百一十四。書之。 diff --git a/examples/calendar/calendar.wy b/examples/calendar/calendar.wy new file mode 100644 index 00000000..9b149ec9 --- /dev/null +++ b/examples/calendar/calendar.wy @@ -0,0 +1,27 @@ +吾嘗觀「「曆法」」之書。方悟「彼月積何月」「彼月何月」「彼年積何年」「彼年何干支」「今年何年」「言今之日時」「今何紀元時」「今年積何年」「言序數」「言百內數」「言年月日」「彼刻何刻」「彼年何年」「彼日何干支」之義。 + +批曰。「「「今年何干支」「今年積何年」。吾有二言。曰「「今年何干支?」」。施「今年何干支」。書之。」」。 +批曰。「「吾有二言。曰「「今年積何年?」」。施「今年積何年」。書之。」」。 + +批曰。「「吾有一數。曰十。名之曰「甲」。」」。 +批曰。「「吾有二言。曰「「施「言序數」於「甲/十」?」」。 施「言序數」於十。書之。 + 吾有二言。曰「「施「言年月日」於「甲/十」?」」。 施「言年月日」於十。書之。 + 施「言今之日時」。書之。 + 施「今年何年」。書之。 + + + 」」。 + +吾有二言。曰「「施「彼年何年」於四千七百一十六?」」。施「彼年何年」於四千七百一十六。書之。 +吾有二言。曰「「施「彼刻何刻」於四千七百一十?」」。施「彼刻何刻」於四千七百一十。書之。 +吾有二言。曰「「施「彼日何干支」於四千七百一十四?」」。施「彼日何干支」於四千七百一十四。書之。 +吾有二言。曰「「施「彼年何干支」於四千七百一十四?」」。施「彼年何干支」於四千七百一十四。書之。 + +吾有一列。名之曰「天地」。 +充「天地」以「彼年積何年」。以「彼月何月」。以「彼月積何月」。「彼日何日」。「彼日何干支」。「彼日積何日」。「彼時何時」。 + 「彼時何小時」。「彼刻何刻」。「彼分何分」。「彼秒何秒」。 + +凡「天地」中之「功夫」。 + 吾有四言。曰「「施「」」。曰「功夫」。曰「「」於四千七百一十四?」」。施「功夫」於四千七百一十四。書之。 +云云。 + diff --git a/test/examples.calendar.test.js b/test/examples.calendar.test.js new file mode 100644 index 00000000..314c4b09 --- /dev/null +++ b/test/examples.calendar.test.js @@ -0,0 +1,117 @@ +var fs = require("fs-extra"); +var path = require("path"); +var utils = require("../tools/utils"); +var execSync = require("child_process").execSync; +var { expect } = require("chai"); +var { compile, evalCompiled } = require("../src/parser"); + +var lib = utils.loadlib(); +const exampleDir = path.resolve(__dirname, "../examples/calendar"); +const outputDir = path.resolve(__dirname, "../test/temp/examples/calendar"); +const python = getPythonExecutable(); + +const ignoreExamples = [ + "divination", // contains randomness + "import", // prints current time + "tree2", // DOM manipulate + "tree" // DOM manipulate +]; + +function getPythonExecutable() { + try { + const output = execSync(`python3 -V`).toString(); + if (output && +output[7] === 3) return "python3"; + } catch (e) {} + try { + const output = execSync(`python -V`).toString(); + if (output && +output[7] === 3) return "python"; + } catch (e) {} + return undefined; +} + +function readOtherExample(x) { + console.log("Entering function readOtherExample: x = " + x); + return fs + .readFileSync( + path.resolve(__dirname, "../examples/.calendar" + x + ".wy"), + "utf-8" + ) + .toString(); +} + +function runExample(lang, name, options = {}) { + //console.log("Entering function runExample: name = " + name); + var code = fs + .readFileSync(path.join(exampleDir, name + ".wy"), "utf-8") + .toString(); + + var compiled = compile(lang, code, { + logCallback: () => {}, + reader: readOtherExample, + lib: lib, + ...options + }); + + if (ignoreExamples.includes(name)) return; + + let output = ""; + + evalCompiled(compiled, { + scoped: true, + lang, + output: (...args) => (output += args.join(" ") + "\n"), + ...options + }); + + console.log("Output from " + name + ".wy script: \n" + output); + expect(output).to.matchSnapshot(); + /* + expect(output).to.equal( + "施「言彼之日時」於四千七百一十四? (「時」) 西元一九六九年己酉年十一月二十四日辛巳日丑初一刻三分三十四秒\n" + + "施「彼年何年」於四千七百一十六? 一千九百六十九\n施「彼刻何刻」於四千七百一十? 一\n" + + "施「彼日何干支」於四千七百一十四? 一十八\n" + + ); + */ +} + +function runCal(lang, options) { + var files = fs.readdirSync(exampleDir).filter(x => x.endsWith(".wy")); + //console.log("Files to be tested: " + files); + + //var files = fs.readdirSync(exampleDir).filter("import.wy"); + for (const file of files) { + //console.log("File being processed: " + file); + + const filename = file.split(".")[0]; + it(filename, () => runExample(lang, filename, options)); + } +} + +describe("===== wen-yan calendar unit test suite =====", () => { + before(() => { + fs.removeSync(outputDir); + fs.ensureDirSync(outputDir); + }); + + describe("javascript", () => { + runCal("js"); + }); + + describe("romanizeIdentifiers", () => { + runCal("js", { romanizeIdentifiers: true }); + }); + + /* FIXME: there are errors for python compiler + if (python) { + describe("python", () => { + runAll("py"); + }) + } + else { + describe("python", () => { + it("skipped", ()=>{}) + }) + } + */ +});