-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.html
More file actions
189 lines (164 loc) · 6.25 KB
/
tests.html
File metadata and controls
189 lines (164 loc) · 6.25 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html>
<html>
<head>
<title>PadCalc Tests</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/8.4.0/mocha.min.css" />
</head>
<body>
<div id="mocha"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/8.4.0/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.3.4/chai.min.js"></script>
<script src="script.js"></script>
<script>mocha.setup('bdd')</script>
<script>
const assert = chai.assert;
describe('applyReplacements', () => {
it("when it's an string", () => {
const expression = 'test input';
const expectedOutput = 'test input';
const output = applyReplacements(expression);
assert.equal(output, expectedOutput);
});
it("when has simple operations", () => {
const expression = '3 + (6-4)*4';
const expectedOutput = '3 + (6-4)*4';
const output = applyReplacements(expression);
assert.equal(output, expectedOutput);
});
it("when has variables", () => {
set_variable('x', 3);
set_variable('y', 5);
const expression = 'x + (y-4)*4';
const expectedOutput = '3 + (5-4)*4';
const output = applyReplacements(expression);
assert.equal(output, expectedOutput);
});
it("when has some variables", () => {
const expression = 'x + (z-4)*4';
const expectedOutput = '3 + (z-4)*4';
const output = applyReplacements(expression);
assert.equal(output, expectedOutput);
});
it("when operates over a range", () => {
const expression = 'sum([1,2,3])';
const expectedOutput = '6';
const output = applyReplacements(expression);
assert.equal(output, expectedOutput);
});
it("when uses functions", () => {
const expression = 'sin(2+3)';
const expectedOutput = -0.96;
const output = eval(applyReplacements(expression)).toFixed(2);
assert.equal(output, expectedOutput);
});
it("when concatenates multiple functions", () => {
const expression = 'sin(2-3) + cos(5*3)';
const expectedOutput = -1.6;
const output = eval(applyReplacements(expression)).toFixed(2);
assert.equal(output, expectedOutput);
});
it("with functions as other function arguments", () => {
const expression = 'sum([sin(2-3), cos(5*3)])';
const expectedOutput = -1.6;
const output = eval(applyReplacements(expression)).toFixed(2);
assert.equal(output, expectedOutput);
});
it("radians to degrees and degrees to radians", () => {
const expression = 'deg(2*pi) + rad(180)';
const output = eval(applyReplacements(expression)).toFixed(2);
assert.equal(output, 363.14);
});
});
describe('evaluate', () => {
it("with functions as other function arguments", () => {
const expression = 'sin(cos(5*x))';
const expectedOutput = -0.69;
const output = evaluate(expression).toFixed(2);
assert.equal(output, expectedOutput);
});
it("with chained functions as other function arguments", () => {
const expression = 'abs(sin(cos(5*x))) + log(e)';
const expectedOutput = 1.69;
const output = evaluate(expression).toFixed(2);
assert.equal(output, expectedOutput);
});
it("ignores case", () => {
const expression = 'ABS(Sin(cOS(5*x))) + lOg(e)';
const expectedOutput = 1.69;
const output = evaluate(expression).toFixed(2);
assert.equal(output, expectedOutput);
});
it("with -%", () => {
const expression = '30-15%';
const expectedOutput = 25.50;
const output = evaluate(expression).toFixed(2);
assert.equal(output, expectedOutput);
});
it("with %()", () => {
const expression = '15%(x*10)';
const expectedOutput = 4.50;
const output = evaluate(expression).toFixed(2);
assert.equal(output, expectedOutput);
});
it("with % modulus", () => {
const expression = '5%4';
const expectedOutput = 1;
const output = evaluate(expression).toFixed(2);
assert.equal(output, expectedOutput);
});
});
describe('recalculation', () => {
it("with functions as other function arguments", () => {
const expression = 'sin(cos(5*x))+23=0000-(4*8)-max([1,2,3])=0000+4';
const output = recalculation(expression);
assert.equal(output, 'sin(cos(5*x))+23=22.31-(4*8)-max([1,2,3])=-12.69+4');
});
});
describe('applyStyle', () => {
it("when it's an string", () => {
const input = document.createElement('input');
input.value = "texto";
applyStyle(input);
assert.equal(input.classList.contains("comment"), true);
});
it("when it's an operation", () => {
const input = document.createElement('input');
input.value = "3+4=7";
applyStyle(input);
assert.equal(input.classList.contains("comment"), false);
});
it("when it's an operation with ranges", () => {
const input = document.createElement('input');
input.value = "sum([3,4])=7";
applyStyle(input);
assert.equal(input.classList.contains("comment"), false);
});
it("when the result is a range", () => {
const input = document.createElement('input');
input.value = "5±7=[12,-2]";
applyStyle(input);
assert.equal(input.classList.contains("comment"), false);
});
it("when it's an assignation", () => {
const input = document.createElement('input');
input.value = "3+4=7->x";
applyStyle(input);
assert.equal(input.classList.contains("comment"), false);
});
it("when it's an Infinity", () => {
const input = document.createElement('input');
input.value = "1/0=Infinity";
applyStyle(input);
assert.equal(input.classList.contains("comment"), false);
});
it("when it's an NaN", () => {
const input = document.createElement('input');
input.value = "sqrt(-1)=NaN";
applyStyle(input);
assert.equal(input.classList.contains("comment"), false);
});
});
mocha.run();
</script>
</body>
</html>