Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Feature/calendar unit test #487

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
26 changes: 26 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Node CI
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The npm t should run all the tests. This extra action config seems to be not necessary.


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
4 changes: 4 additions & 0 deletions examples/calendar/calendar-彼年何年號.wy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
吾嘗觀「「曆法」」之書。方悟「言彼之時刻」之義。

注曰「「言彼之時刻。同Javascript之new Date(x * 1000).toTimeString(), in Chinese calendar也。」」
吾有二言。曰「「施「言彼之時刻」於四千七百一十四? 」」。施「言彼之時刻」於四千七百一十四。書之。
4 changes: 4 additions & 0 deletions examples/calendar/calendar-言彼之年月日.wy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
吾嘗觀「「曆法」」之書。方悟「言彼之年月日」之義。

批曰。「「今有一術。名之曰「言彼之年月日」。欲行是術。必先得一數。曰「時」。」」。
吾有二言。曰「「施「言彼之年月日」於四千七百一十四 (「時」)?」」。施「言彼之年月日」於四千七百一十四。書之。
4 changes: 4 additions & 0 deletions examples/calendar/calendar-言彼之日時.wy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
吾嘗觀「「曆法」」之書。方悟「言彼之日時」之義。

批曰。「「欲行是術。必先得一數。曰「時」」」。
吾有二言。曰「「施「言彼之日時」於四千七百一十四? (「時」)」」。施「言彼之日時」於四千七百一十四。書之。
3 changes: 3 additions & 0 deletions examples/calendar/calendar-言彼之時刻.wy
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
吾嘗觀「「曆法」」之書。方悟「彼年何年號」之義。

吾有二言。曰「「施「彼年何年號」於四千七百一十四? 」」。施「彼年何年號」於四千七百一十四。書之。
27 changes: 27 additions & 0 deletions examples/calendar/calendar.wy
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
吾嘗觀「「曆法」」之書。方悟「彼月積何月」「彼月何月」「彼年積何年」「彼年何干支」「今年何年」「言今之日時」「今何紀元時」「今年積何年」「言序數」「言百內數」「言年月日」「彼刻何刻」「彼年何年」「彼日何干支」之義。

批曰。「「「今年何干支」「今年積何年」。吾有二言。曰「「今年何干支?」」。施「今年何干支」。書之。」」。
批曰。「「吾有二言。曰「「今年積何年?」」。施「今年積何年」。書之。」」。

批曰。「「吾有一數。曰十。名之曰「甲」。」」。
批曰。「「吾有二言。曰「「施「言序數」於「甲/十」?」」。 施「言序數」於十。書之。
吾有二言。曰「「施「言年月日」於「甲/十」?」」。 施「言年月日」於十。書之。
施「言今之日時」。書之。
施「今年何年」。書之。


」」。

吾有二言。曰「「施「彼年何年」於四千七百一十六?」」。施「彼年何年」於四千七百一十六。書之。
吾有二言。曰「「施「彼刻何刻」於四千七百一十?」」。施「彼刻何刻」於四千七百一十。書之。
吾有二言。曰「「施「彼日何干支」於四千七百一十四?」」。施「彼日何干支」於四千七百一十四。書之。
吾有二言。曰「「施「彼年何干支」於四千七百一十四?」」。施「彼年何干支」於四千七百一十四。書之。

吾有一列。名之曰「天地」。
充「天地」以「彼年積何年」。以「彼月何月」。以「彼月積何月」。「彼日何日」。「彼日何干支」。「彼日積何日」。「彼時何時」。
「彼時何小時」。「彼刻何刻」。「彼分何分」。「彼秒何秒」。

凡「天地」中之「功夫」。
吾有四言。曰「「施「」」。曰「功夫」。曰「「」於四千七百一十四?」」。施「功夫」於四千七百一十四。書之。
云云。

117 changes: 117 additions & 0 deletions test/examples.calendar.test.js
Original file line number Diff line number Diff line change
@@ -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"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem seems to from this.

-  path.resolve(__dirname, "../examples/.calendar" + x + ".wy"),
+  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", ()=>{})
})
}
*/
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also run npm run test:update and include the snapshots to the commits.