-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator unit test.html
144 lines (129 loc) · 4.87 KB
/
Calculator unit test.html
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
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>calculator test</title>
<script src="../Calculator/node_modules/mocha/mocha.js"></script>
<script src="../Calculator/node_modules/chai/chai.js"></script>
<script>
function parse(content){
// deal with "expression"
var index = content.lastIndexOf("(");
if(index > -1){
var endIndex = content.indexOf(")",index);
if(endIndex > -1){
var result = parse(content.substring(index + 1,endIndex));
return parse(content.substring(0,index) + result + content.substring(endIndex + 1));
}else{
return "Error: ) expected!";
}
}
// deal with +
index = content.indexOf("+");
if(index > -1){
if(content.substring(index + 1) === "") {
return "Error: bad token!";
}
else{
var left = parse(content.substring(0, index));
var right = parse(content.substring(index + 1));
if(typeof(left) === 'string'){
return left;
}else if(typeof(right) === 'string'){
return right;
}else{
return parse(content.substring(0,index)) + parse(content.substring(index + 1));
}
}
}
index = content.lastIndexOf("-");
if(index > -1){
if(content.substring(index + 1) === ""){
return "Error: bad token!";
}else{
left = parse(content.substring(0, index));
right = parse(content.substring(index + 1));
if(typeof(left) === 'string'){
return left;
}else if(typeof(right) === 'string'){
return right;
}else{
return left - right;
}
}
}
index = content.lastIndexOf("*");
if(index > -1){
if(content.substring(index + 1) === ""){
return "Error: bad token!";
}else {
left = parse(content.substring(0, index));
right = parse(content.substring(index + 1));
if (typeof(left) === 'string') {
return left;
} else if (typeof(right) === 'string') {
return right;
} else {
return left * right;
}
}
}
index = content.lastIndexOf("/");
if(index > -1){
if(content.substring(index + 1) === ""){
return "Error: bad token!";
}else {
left = parse(content.substring(0, index));
right = parse(content.substring(index + 1));
if (typeof(left) === 'string') {
return left;
} else if (typeof(right) === 'string') {
return right;
} else {
if(right === 0){
return 'Error: divided by zero!';
}else{
return left / right;
}
}
}
}
index = content.lastIndexOf('%');
if(index > -1){
if(content.substring(index + 1) === ""){
return "Error: bad token!";
}else {
left = parse(content.substring(0, index));
right = parse(content.substring(index + 1));
if (typeof(left) === 'string') {
return left;
} else if (typeof(right) === 'string') {
return right;
} else {
if(right === 0){
return 'Error: divided by zero!';
}else{
return left % right;
}
}
}
}
if("" === content){
return 0;
}else{
return content - 1 + 1;
}
}
</script>
</head>
<body>
<div id="mocha"><p>Index</p></div>
<div id="message"></div>
<div id="fixtures"></div>
<script src="node_modules/mocha/mocha.js"></script>
<script src="node_modules/chai/chai.js"></script>
<script>mocha.setup("bdd")</script>
<script src="../Calculator/Calculator unit test.js"></script>
<script>mocha.run()</script>
</body>
</html>