Skip to content

Commit 34b1e6b

Browse files
aliang8kevinbarabash
authored andcommitted
Add fraction and polynomial functions to query.js with snapshot tests
1 parent ec02d6e commit 34b1e6b

File tree

4 files changed

+521
-5
lines changed

4 files changed

+521
-5
lines changed

Diff for: .babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
"plugins": [
44
"transform-object-rest-spread"
55
]
6-
}
6+
}

Diff for: lib/__test__/__snapshots__/query.test.js.snap

+248
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`query getCoefficientsAndConstants 2 1`] = `
4+
{
5+
"others": [
6+
],
7+
"constants": [
8+
{
9+
"value": "2",
10+
"type": "Number"
11+
}
12+
],
13+
"coefficientMap": {
14+
}
15+
}
16+
`;
17+
18+
exports[`query getCoefficientsAndConstants 2 x y z 1`] = `
19+
{
20+
"others": [
21+
],
22+
"constants": [
23+
],
24+
"coefficientMap": {
25+
"x y z": [
26+
{
27+
"value": "2",
28+
"type": "Number"
29+
}
30+
]
31+
}
32+
}
33+
`;
34+
35+
exports[`query getCoefficientsAndConstants 2/3 + 3 + cos(4) 1`] = `
36+
{
37+
"others": [
38+
{
39+
"type": "Apply",
40+
"op": {
41+
"type": "Identifier",
42+
"name": "cos"
43+
},
44+
"args": [
45+
{
46+
"value": "4",
47+
"type": "Number"
48+
}
49+
]
50+
}
51+
],
52+
"constants": [
53+
{
54+
"type": "Apply",
55+
"op": "div",
56+
"args": [
57+
{
58+
"value": "2",
59+
"type": "Number"
60+
},
61+
{
62+
"value": "3",
63+
"type": "Number"
64+
}
65+
]
66+
},
67+
{
68+
"value": "3",
69+
"type": "Number"
70+
}
71+
],
72+
"coefficientMap": {
73+
}
74+
}
75+
`;
76+
77+
exports[`query getCoefficientsAndConstants 2x 1`] = `
78+
{
79+
"others": [
80+
],
81+
"constants": [
82+
],
83+
"coefficientMap": {
84+
"x": [
85+
{
86+
"value": "2",
87+
"type": "Number"
88+
}
89+
]
90+
}
91+
}
92+
`;
93+
94+
exports[`query getCoefficientsAndConstants 2x^2 1`] = `
95+
{
96+
"others": [
97+
],
98+
"constants": [
99+
],
100+
"coefficientMap": {
101+
"x^2": [
102+
{
103+
"value": "2",
104+
"type": "Number"
105+
}
106+
]
107+
}
108+
}
109+
`;
110+
111+
exports[`query getCoefficientsAndConstants 2x^3 + 3 1`] = `
112+
{
113+
"others": [
114+
],
115+
"constants": [
116+
{
117+
"value": "3",
118+
"type": "Number"
119+
}
120+
],
121+
"coefficientMap": {
122+
"x^3": [
123+
{
124+
"value": "2",
125+
"type": "Number"
126+
}
127+
]
128+
}
129+
}
130+
`;
131+
132+
exports[`query getCoefficientsAndConstants 3x^2 * 2x^2 1`] = `
133+
{
134+
"others": [
135+
],
136+
"constants": [
137+
],
138+
"coefficientMap": {
139+
"x^2": [
140+
{
141+
"value": "3",
142+
"type": "Number"
143+
},
144+
{
145+
"value": "2",
146+
"type": "Number"
147+
}
148+
]
149+
}
150+
}
151+
`;
152+
153+
exports[`query getCoefficientsAndConstants x^3 + y^3 + x y z + 3 1`] = `
154+
{
155+
"others": [
156+
],
157+
"constants": [
158+
{
159+
"value": "3",
160+
"type": "Number"
161+
}
162+
],
163+
"coefficientMap": {
164+
"y^3": [
165+
{
166+
"value": "1",
167+
"type": "Number"
168+
}
169+
],
170+
"x^3": [
171+
{
172+
"value": "1",
173+
"type": "Number"
174+
}
175+
],
176+
"x y z": [
177+
{
178+
"value": "1",
179+
"type": "Number"
180+
}
181+
]
182+
}
183+
}
184+
`;
185+
186+
exports[`query getVariableFactorName 2 x y z 1`] = `
187+
[
188+
{
189+
"type": "Identifier",
190+
"name": "x"
191+
},
192+
{
193+
"type": "Identifier",
194+
"name": "y"
195+
},
196+
{
197+
"type": "Identifier",
198+
"name": "z"
199+
}
200+
]
201+
`;
202+
203+
exports[`query getVariableFactorName 2x 1`] = `
204+
[
205+
{
206+
"type": "Identifier",
207+
"name": "x"
208+
}
209+
]
210+
`;
211+
212+
exports[`query getVariableFactorName 2x^2 1`] = `
213+
[
214+
{
215+
"type": "Apply",
216+
"op": "pow",
217+
"args": [
218+
{
219+
"type": "Identifier",
220+
"name": "x"
221+
},
222+
{
223+
"value": "2",
224+
"type": "Number"
225+
}
226+
]
227+
}
228+
]
229+
`;
230+
231+
exports[`query getVariableFactorName x^2 1`] = `
232+
[
233+
{
234+
"type": "Apply",
235+
"op": "pow",
236+
"args": [
237+
{
238+
"type": "Identifier",
239+
"name": "x"
240+
},
241+
{
242+
"value": "2",
243+
"type": "Number"
244+
}
245+
]
246+
}
247+
]
248+
`;

Diff for: lib/__test__/query.test.js

+94
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
import assert from 'assert'
22
import {parse} from 'math-parser'
3+
import stringify from 'json-stable-stringify'
4+
5+
const reverseAlphabetical = (a, b) => a.key < b.key ? 1 : -1
6+
7+
const serializer = {
8+
print(val) {
9+
return stringify(val, {cmp: reverseAlphabetical, space: ' '})
10+
},
11+
test() {
12+
return true
13+
},
14+
}
15+
16+
expect.addSnapshotSerializer(serializer) // eslint-disable-line
17+
18+
const suite = (name, func, cases) => {
19+
describe(name, () => {
20+
cases.forEach((c) => {
21+
it(c, () => {
22+
const result = func(parse(c))
23+
expect(result).toMatchSnapshot() // eslint-disable-line
24+
})
25+
})
26+
})
27+
}
28+
29+
suite.only = (name, func, cases) => {
30+
describe.only(name, () => {
31+
cases.forEach((c) => {
32+
it(c, () => {
33+
const result = func(parse(c))
34+
expect(result).toMatchSnapshot() // eslint-disable-line
35+
})
36+
})
37+
})
38+
}
339

440
import * as query from '../query'
541

@@ -12,6 +48,24 @@ describe('query', () => {
1248
y = {type: 'Identifier', name: 'y'}
1349
})
1450

51+
suite('getVariableFactorName', query.getVariableFactors, [
52+
'2x',
53+
'x^2',
54+
'2x^2',
55+
'2 x y z'
56+
])
57+
58+
suite('getCoefficientsAndConstants', query.getCoefficientsAndConstants, [
59+
'2',
60+
'2x',
61+
'2x^2',
62+
'2 x y z',
63+
'2x^3 + 3',
64+
'x^3 + y^3 + x y z + 3',
65+
'2/3 + 3 + cos(4)',
66+
'3x^2 * 2x^2'
67+
])
68+
1569
it('isIdentifier', () => {
1670
assert(query.isIdentifier(x))
1771
assert(!query.isIdentifier(a))
@@ -82,6 +136,24 @@ describe('query', () => {
82136
assert(query.isFraction(parse('--(2 / 3)')))
83137
})
84138

139+
it('isConstantFraction', () => {
140+
assert(query.isConstantFraction(parse('2.2 / 3')))
141+
assert(!query.isConstantFraction(parse('x / 3')))
142+
})
143+
144+
it('isIntegerFraction', () => {
145+
assert(!query.isIntegerFraction(parse('2.2 / 3')))
146+
assert(query.isIntegerFraction(parse('2 / 3')))
147+
assert(!query.isIntegerFraction(parse('x / 3')))
148+
})
149+
150+
it('hasConstantBase', () => {
151+
assert(query.hasConstantBase(parse('2^2')))
152+
assert(query.hasConstantBase(parse('(2/3)^2')))
153+
assert(query.hasConstantBase(parse('(-2)^2')))
154+
assert(!query.hasConstantBase(parse('x^2')))
155+
})
156+
85157
it('isDecimal', () => {
86158
assert(query.isDecimal(parse('-2.2')))
87159
assert(query.isDecimal(parse('2.2')))
@@ -130,4 +202,26 @@ describe('query', () => {
130202
assert.deepEqual(query.getDenominator(parse('-(2/3)')), parse('3'))
131203
assert.equal(query.getDenominator(x), null)
132204
})
205+
206+
it('getPolyDegree', () => {
207+
assert.deepEqual(query.getPolyDegree(parse('2')), parse('0'))
208+
assert.deepEqual(query.getPolyDegree(parse('2x')), parse('1'))
209+
assert.deepEqual(query.getPolyDegree(parse('2xyz')), parse('1'))
210+
assert.deepEqual(query.getPolyDegree(parse('2x^2')), parse('2'))
211+
assert.deepEqual(query.getPolyDegree(parse('x^2')), parse('2'))
212+
assert.deepEqual(query.getPolyDegree(parse('-2x^2')), parse('2'))
213+
assert.deepEqual(query.getPolyDegree(parse('-2/3 x^2')), parse('2'))
214+
})
215+
216+
it('getCoefficient', () => {
217+
assert.deepEqual(query.getCoefficient(parse('2')), parse('2'))
218+
assert.deepEqual(query.getCoefficient(parse('2^2')), parse('2^2'))
219+
assert.deepEqual(query.getCoefficient(parse('2^(2/3)')), parse('2^(2/3)'))
220+
assert.deepEqual(query.getCoefficient(parse('2x')), parse('2'))
221+
assert.deepEqual(query.getCoefficient(parse('2x^2')), parse('2'))
222+
assert.deepEqual(query.getCoefficient(parse('x^2')), parse('1'))
223+
assert.deepEqual(query.getCoefficient(parse('-2x^2')), parse('-2'))
224+
assert.deepEqual(query.getCoefficient(parse('2/3 x^2')), parse('2/3'))
225+
assert.deepEqual(query.getCoefficient(parse('-2/3 x^2')), parse('-2/3'))
226+
})
133227
})

0 commit comments

Comments
 (0)