Skip to content

Commit ca8ab65

Browse files
committed
test: add mocha for utils
1 parent aff3f10 commit ca8ab65

File tree

9 files changed

+366
-17
lines changed

9 files changed

+366
-17
lines changed

days/day2.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ const day2 = (input) => {
2727
copyCoord = { ...coords[1] };
2828

2929
[0, 1].forEach((index) =>
30-
utils.coordMove(coords[index], letter)
30+
coords[index] = utils.coordMove(coords[index], letter)
3131
);
3232

3333
['x', 'y'].forEach((axis) =>
3434
coords[0][axis] = utils.makeValueCorrect(coords[0][axis], 0, 2)
3535
);
3636

37-
if (!utils.coordBetween(coords[1], 0, 4, 0, 4) || !pads[1][coords[1].y][coords[1].x]) {
37+
if (!utils.coordBetween(coords[1], { x: 0, y: 0 }, { x: 4, y: 4 }) || !pads[1][coords[1].y][coords[1].x]) {
3838
coords[1] = { ...copyCoord };
3939
}
4040
}

package-lock.json

+188
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
"js-md5": "^0.7.3",
88
"lodash": "^4.17.11"
99
},
10-
"devDependencies": {},
10+
"devDependencies": {
11+
"mocha": "^5.2.0"
12+
},
1113
"scripts": {
12-
"test": "node test"
14+
"test": "mocha test/utils/*.js && node test",
15+
"test-utils-only": "mocha test/utils/*.js"
1316
},
1417
"repository": {
1518
"type": "git",

test/utils/math.js

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
const assert = require('assert');
2+
const utils = require('../../utils/math');
3+
4+
describe('math', function () {
5+
describe('backToPositive', () => {
6+
it('goes back to 0 if it goes 1 by 1', () => {
7+
assert.equal(utils.backToPositive(-5, 1), 0);
8+
});
9+
10+
it('goes back to the first positive value', () => {
11+
assert.equal(utils.backToPositive(-105, 51), 48);
12+
});
13+
});
14+
15+
describe('coordBetween', () => {
16+
it('returns true if it is within the ranges', () => {
17+
assert.equal(utils.coordBetween(
18+
{ x: 5, y: 6 },
19+
{ x: 0, y: 1 },
20+
{ x: 6, y: 7},
21+
), true);
22+
});
23+
24+
it('returns true if it is on the first point', () => {
25+
assert.equal(utils.coordBetween(
26+
{ x: 0, y: 1 },
27+
{ x: 0, y: 1 },
28+
{ x: 6, y: 7},
29+
), true);
30+
});
31+
32+
it('returns true if it is on the last point', () => {
33+
assert.equal(utils.coordBetween(
34+
{ x: 6, y: 7 },
35+
{ x: 0, y: 1 },
36+
{ x: 6, y: 7},
37+
), true);
38+
});
39+
40+
it('returns false if it is beyond the first point', () => {
41+
assert.equal(utils.coordBetween(
42+
{ x: 0, y: 0 },
43+
{ x: 0, y: 1 },
44+
{ x: 6, y: 7},
45+
), false);
46+
});
47+
48+
it('returns false if it is beyond the last point', () => {
49+
assert.equal(utils.coordBetween(
50+
{ x: 7, y: 7 },
51+
{ x: 0, y: 1 },
52+
{ x: 6, y: 7},
53+
), false);
54+
});
55+
});
56+
57+
describe('coordMove', () => {
58+
it('goes up', () => {
59+
assert.deepEqual(utils.coordMove({ x: 5, y: 6 }, 'U'), { x: 5, y: 5 });
60+
});
61+
62+
it('goes down', () => {
63+
assert.deepEqual(utils.coordMove({ x: 5, y: 6 }, 'D'), { x: 5, y: 7 });
64+
});
65+
66+
it('goes left', () => {
67+
assert.deepEqual(utils.coordMove({ x: 5, y: 6 }, 'L'), { x: 4, y: 6 });
68+
});
69+
70+
it('goes right', () => {
71+
assert.deepEqual(utils.coordMove({ x: 5, y: 6 }, 'R'), { x: 6, y: 6 });
72+
});
73+
});
74+
75+
describe('distanceFromOrigin', () => {
76+
it('handles positive values', () => {
77+
assert.equal(utils.distanceFromOrigin({ x: 5, y: 6 }), 11);
78+
});
79+
80+
it('handles negative values', () => {
81+
assert.equal(utils.distanceFromOrigin({ x: -5, y: -6 }), 11);
82+
});
83+
});
84+
85+
describe('makeValueCorrect', () => {
86+
it('returns the lowest extremity if the value is below', () => {
87+
assert.equal(utils.makeValueCorrect(-1, 0, 5), 0);
88+
});
89+
90+
it('returns the highest extremity if the value is above', () => {
91+
assert.equal(utils.makeValueCorrect(6, 0, 5), 5);
92+
});
93+
94+
it('returns the same value if the value is between the extremities', () => {
95+
assert.equal(utils.makeValueCorrect(3, 0, 5), 3);
96+
});
97+
});
98+
99+
describe('minMax', () => {
100+
it('returns the same array if it is already sorted', () => {
101+
assert.deepEqual(utils.minMax([0, 1]), [0, 1]);
102+
});
103+
104+
it('returns the same array if both values are the same', () => {
105+
assert.deepEqual(utils.minMax([2, 2]), [2, 2]);
106+
});
107+
108+
it('returns the reversed array if the two values are switched', () => {
109+
assert.deepEqual(utils.minMax([2, 1]), [1, 2]);
110+
});
111+
});
112+
113+
describe('valueBetween', () => {
114+
it('returns true if it is between two values', () => {
115+
assert.equal(utils.valueBetween(-3, -5, -1), true);
116+
});
117+
118+
it('returns true if it is at the lowest extremity', () => {
119+
assert.equal(utils.valueBetween(-5, -5, -1), true);
120+
});
121+
122+
it('returns true if it is at the highest extremity', () => {
123+
assert.equal(utils.valueBetween(-1, -5, -1), true);
124+
});
125+
126+
it('returns false if it is lower than the lowest extremity', () => {
127+
assert.equal(utils.valueBetween(-6, -5, -1), false);
128+
});
129+
130+
it('returns false if it is greater than the highest extremity', () => {
131+
assert.equal(utils.valueBetween(0, -5, -1), false);
132+
});
133+
});
134+
});

utils/constants.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const LOWERCASE_A_VALUE = 97;
2+
const NUMBER_OF_LETTERS = 26;
3+
4+
module.exports = {
5+
LOWERCASE_A_VALUE,
6+
NUMBER_OF_LETTERS
7+
};

0 commit comments

Comments
 (0)