This repository has been archived by the owner on Mar 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
93 lines (69 loc) · 2.63 KB
/
test.js
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
90
91
92
93
"use strict";
const jsHP = require("node-hp");
const assert = require("assert");
// some vars
var bsScript = 'Hi<$write(" you!")';
var bsResult = 'Hi you!';
// tests
const tests = {
"basic & write": function () {
assert(jsHP(bsScript)+"" === bsResult, "bad string result");
},
"load & write": function () {
var load = jsHP.load(bsScript);
assert(!load.error, "load has error");
assert(load.script === bsScript);
assert(load()+"" === bsResult, "bad string result");
},
"reduce fast": function () {
var load = jsHP.load("reduce fast");
assert(!load.error, "load has error");
assert(load.script === "reduce fast");
assert(load.fast === "reduce fast");
assert(load()+"" === "reduce fast", "bad string result");
},
"return value": function () {
assert(jsHP("<$return 7").return === 7);
assert(jsHP("<$ write('hi')").return === undefined);
},
"run time error": function () {
var result = jsHP("<$ return a");
assert(result.error instanceof ReferenceError);
assert(result+"" === 'ReferenceError: a is not defined');
},
"compile time error": function () {
var load = jsHP.load("<$ 99(");
assert(load.error instanceof SyntaxError);
assert(load.script === "<$ 99(");
assert(load.fast+"" === 'SyntaxError: Unexpected token ;');
assert(load()+"" === 'SyntaxError: Unexpected token ;', "bad string result");
},
"different brackets": function () {
assert(jsHP('Hi<#write(" you!")?>', null, {brackets: ["<#","?>"]})+""
=== 'Hi you!', "bad string result");
},
"variable bracket script numbers": function () {
assert(jsHP("a<$$>a<$$>a")+"" === "aaa");
assert(jsHP("a<$$>a<$")+"" === "aa");
assert(jsHP("a$>a<$")+"" === "a$>a");
},
"script as Buffer": function () {
assert(jsHP(Buffer.from(bsScript))+"" === bsResult, "bad string result");
},
"test _file, _dir, not given": function () {
assert(jsHP("<$write(_file)")+"" === process.cwd()+"/.");
assert(jsHP("<$write(_dir)")+"" === process.cwd());
},
"test _file, _dir, given": function () {
var s = {file: "/hans/folder/script.js"};
assert(jsHP("<$write(_file)", null, s)+"" === "/hans/folder/script.js");
assert(jsHP("<$write(_dir)", null, s)+"" === "/hans/folder");
}
}
Object.keys(tests).forEach(function (name, index) {
var error;
try {tests[name]()}
catch (err) {error = err}
console.log("Case", (index+1)+":", !error, "-", name);
if (error) console.log(error);
});