-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtesting.test.ts
131 lines (127 loc) · 5.39 KB
/
testing.test.ts
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import {expect} from "chai";
import {assertASTsAreEqual} from "../../src/testing/testing";
import {ASTNode, Attribute, Children, Node, Point, Position, PossiblyNamed} from "../../src";
describe('AssertASTsAreEqual', function() {
it("the very same node instance compared with itself must pass", function () {
const simpleNode1 : Node = new SimpleNode("node");
assertASTsAreEqual(simpleNode1, simpleNode1);
});
it("two different node instances of the same type and with the same values must pass", function () {
const simpleNode1 : Node = new SimpleNode("node");
const simpleNode2 : Node = new SimpleNode("node");
assertASTsAreEqual(simpleNode1, simpleNode2)
});
it("nodes with different positions must pass when considerPosition == false", function () {
const simpleNode1 : Node = new SimpleNode("node");
simpleNode1.position = Position.ofPoint(new Point(1, 0));
const simpleNode2 : Node = new SimpleNode("node");
simpleNode1.position = Position.ofPoint(new Point(2, 0));
assertASTsAreEqual(simpleNode1, simpleNode2)
});
it("nodes with different positions must NOT pass when considerPosition == true", function () {
const simpleNode1 : Node = new SimpleNode("node");
simpleNode1.position = Position.ofPoint(new Point(1, 0));
const simpleNode2 : Node = new SimpleNode("node");
simpleNode2.position = Position.ofPoint(new Point(2, 0));
expect(() =>
assertASTsAreEqual(simpleNode1, simpleNode2, "<root>", true)
).to.throw("position");
});
it("nodes with equal positions must pass when considerPosition == true", function () {
const simpleNode1 : Node = new SimpleNode("node");
simpleNode1.position = Position.ofPoint(new Point(1, 0));
const simpleNode2 : Node = new SimpleNode("node");
simpleNode2.position = Position.ofPoint(new Point(1, 0));
assertASTsAreEqual(simpleNode1, simpleNode2, "<root>", true)
});
it("two different node instances of the same type and with different values must NOT pass", function () {
const simpleNode1 : Node = new SimpleNode("node");
const simpleNode2 : Node = new SimpleNode("different node");
expect(() =>
assertASTsAreEqual(simpleNode1, simpleNode2)
).to.throw("<root>, property name is 'node', but found 'different node'");
});
it("two different node instances of two different types, but with same values must NOT pass", function () {
const node1 : Node = new SimpleNode("node");
const node2 : Node = new AnotherSimpleNode("node");
expect(() =>
assertASTsAreEqual(node1, node2)
).to.throw("nodes are not of the same type");
});
it("two different node instances of two different types, and with different values must NOT pass", function () {
const node1 : Node = new SimpleNode("node");
const node2 : Node = new AnotherSimpleNode("different node");
expect(() =>
assertASTsAreEqual(node1, node2)
).to.throw("nodes are not of the same type");
});
it("two equal trees of height = 3", function () {
const tree1 = new SimpleNode("A", [
new SimpleNode("B"), new SimpleNode("C", [
new SimpleNode("D")
])
]);
const tree2 = new SimpleNode("A", [
new SimpleNode("B"), new SimpleNode("C", [
new SimpleNode("D")
])
]);
assertASTsAreEqual(tree1, tree2)
});
it("two trees of height = 3 with different node on the 3rd level", function () {
const tree1 =
new SimpleNode("A", [
new SimpleNode("B"),
new SimpleNode("C", [
new SimpleNode("D1")
])
]);
const tree2 =
new SimpleNode("A", [
new SimpleNode("B"),
new SimpleNode("C", [
new SimpleNode("D2")
])
]);
expect(() =>
assertASTsAreEqual(tree1, tree2)
).to.throw();
});
it("two different nodes with same property names but one is a Node and the other is a string, then must NOT pass", function () {
const treeWithLegitSubTree = new SimpleNode("A", []);
const treeWithStringSubTree = new NodeWithStringSubTree("A", "sub-tree");
expect(() =>
assertASTsAreEqual(treeWithLegitSubTree, treeWithStringSubTree)
).to.throw();
});
});
@ASTNode("", "SimpleNode")
class SimpleNode extends Node implements PossiblyNamed {
@Attribute() public name?: string;
@Children() public subTree: Node[];
constructor(name?: string, subTree: Node[] = []) {
super();
this.name = name;
this.subTree = subTree;
}
}
@ASTNode("", "AnotherSimpleNode")
class AnotherSimpleNode extends Node implements PossiblyNamed {
@Attribute() public name?: string;
@Children() public subTree: Node[];
constructor(name?: string, subTree: Node[] = []) {
super();
this.name = name;
this.subTree = subTree;
}
}
@ASTNode("", "NodeWithStringSubTree")
class NodeWithStringSubTree extends Node implements PossiblyNamed {
@Attribute() public name?: string;
@Children() public subTree?: string;
constructor(name?: string, subTree?: string) {
super();
this.name = name;
this.subTree = subTree;
}
}