Skip to content

Commit 0a5fb26

Browse files
committed
js: add integer encoding tests
1 parent 6e606cb commit 0a5fb26

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

js/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ Unit tests require [gulp](https://www.npmjs.com/package/gulp),
3030
[karma](https://karma-runner.github.io) and
3131
[jasmine](https://jasmine.github.io).
3232

33-
Execute the tests with `npm test`. This will install the
33+
To run the tests the first time use `bash checks.sh` - this will convert the
34+
CSV test files to JSON.
35+
36+
Then you can execute the tests with `npm test`. This will install the
3437
dependencies, run `eslint` and then run the tests as long as there were no
3538
eslint errors.
3639

js/src/openlocationcode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@
345345
* @param {number} longitude
346346
* @return {Array<number>} A tuple of the latitude integer and longitude integer.
347347
*/
348-
var locationToIntegers = function(latitude, longitude) {
348+
var locationToIntegers = OpenLocationCode.locationToIntegers = function(latitude, longitude) {
349349
var latVal = roundAwayFromZero(latitude * FINAL_LAT_PRECISION_);
350350
latVal += LATITUDE_MAX_ * FINAL_LAT_PRECISION_;
351351
if (latVal < 0) {
@@ -377,7 +377,7 @@
377377
* @return {string} A code of the specified length or the default length if not
378378
* specified.
379379
*/
380-
var encodeIntegers = function(latInt, lngInt, codeLength) {
380+
var encodeIntegers = OpenLocationCode.encodeIntegers = function(latInt, lngInt, codeLength) {
381381
// Javascript strings are immutable and it doesn't have a native
382382
// StringBuilder, so we'll use an array.
383383
const code = new Array(MAX_DIGIT_COUNT_ + 1);

js/test/jasmine-tests.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,33 @@ describe("Open Location Code", function() {
77
expect(encTests.length).toBeGreaterThan(1);
88
for (var i = 0; i < encTests.length; i++) {
99
var test = encTests[i];
10-
var code = OpenLocationCode.encode(test[0], test[1], test[2]);
10+
var code = OpenLocationCode.encode(test[0], test[1], test[4]);
1111
// Did we get the same code?
12-
expect(code).toBe(test[3]);
12+
expect(code).toBe(test[5]);
13+
}
14+
});
15+
16+
it("Location to Integers Tests", function() {
17+
jasmine.getFixtures().fixturesPath = 'base/';
18+
var encTests = JSON.parse(jasmine.getFixtures().read("encoding.json"));
19+
expect(encTests.length).toBeGreaterThan(1);
20+
for (var i = 0; i < encTests.length; i++) {
21+
var test = encTests[i];
22+
var got = OpenLocationCode.locationToIntegers(test[0], test[1]);
23+
expect(got[0]).toBe(test[2]);
24+
expect(got[1]).toBe(test[3]);
25+
}
26+
});
27+
28+
it("Integer Encoding Tests", function() {
29+
jasmine.getFixtures().fixturesPath = 'base/';
30+
var encTests = JSON.parse(jasmine.getFixtures().read("encoding.json"));
31+
expect(encTests.length).toBeGreaterThan(1);
32+
for (var i = 0; i < encTests.length; i++) {
33+
var test = encTests[i];
34+
var code = OpenLocationCode.encodeIntegers(test[2], test[3], test[4]);
35+
// Did we get the same code?
36+
expect(code).toBe(test[5]);
1337
}
1438
});
1539

0 commit comments

Comments
 (0)