Skip to content

Commit 7829f85

Browse files
committed
update dart to add integer encoding tests
1 parent f9d9df3 commit 7829f85

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

dart/lib/src/open_location_code.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,6 @@ bool isFull(String code) {
254254
/// * [codeLength]: The number of significant digits in the output code, not
255255
/// including any separator characters.
256256
String encode(num latitude, num longitude, {int codeLength = pairCodeLength}) {
257-
if (codeLength < minDigitCount ||
258-
(codeLength < pairCodeLength && codeLength.isOdd)) {
259-
throw ArgumentError('Invalid Open Location Code length: $codeLength');
260-
}
261-
codeLength = min(maxDigitCount, codeLength);
262257
var integers = locationToIntegers(latitude, longitude);
263258
return encodeIntegers(integers[0], integers[1], codeLength);
264259
}
@@ -293,6 +288,11 @@ List<int> locationToIntegers(num latitude, num longitude) {
293288

294289
/// Encode a location into an Open Location Code.
295290
String encodeIntegers(int latVal, int lngVal, int codeLength) {
291+
if (codeLength < minDigitCount ||
292+
(codeLength < pairCodeLength && codeLength.isOdd)) {
293+
throw ArgumentError('Invalid Open Location Code length: $codeLength');
294+
}
295+
codeLength = min(maxDigitCount, codeLength);
296296
List<String> code = List<String>.filled(maxDigitCount + 1, '');
297297
code[separatorPosition] = separator;
298298

dart/test/encode_test.dart

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,48 @@ import 'package:open_location_code/open_location_code.dart' as olc;
1818
import 'package:test/test.dart';
1919
import 'utils.dart';
2020

21-
// code,lat,lng,latLo,lngLo,latHi,lngHi
22-
void checkEncodeDecode(String csvLine) {
21+
void checkEncode(String csvLine) {
2322
var elements = csvLine.split(',');
2423
num lat = double.parse(elements[0]);
2524
num lng = double.parse(elements[1]);
26-
int len = int.parse(elements[2]);
27-
var want = elements[3];
25+
int lat_int = int.parse(elements[2]);
26+
int lng_int = int.parse(elements[3]);
27+
int len = int.parse(elements[4]);
28+
var want = elements[5];
2829
var got = olc.encode(lat, lng, codeLength: len);
2930
expect(got, equals(want));
3031
}
3132

33+
void checkLocationToIntegers(String csvLine) {
34+
var elements = csvLine.split(',');
35+
num lat = double.parse(elements[0]);
36+
num lng = double.parse(elements[1]);
37+
int lat_int = int.parse(elements[2]);
38+
int lng_int = int.parse(elements[3]);
39+
var got = olc.locationToIntegers(lat, lng);
40+
expect(got[0], equals(lat_int));
41+
expect(got[1], equals(lng_int));
42+
}
43+
44+
void checkEncodeIntegers(String csvLine) {
45+
var elements = csvLine.split(',');
46+
int lat_int = int.parse(elements[2]);
47+
int lng_int = int.parse(elements[3]);
48+
int len = int.parse(elements[4]);
49+
var want = elements[5];
50+
var got = olc.encodeIntegers(lat_int, lng_int, len);
51+
expect(got, equals(want));
52+
}
53+
3254
void main() {
33-
test('Check encode decode', () {
34-
csvLinesFromFile('encoding.csv').forEach(checkEncodeDecode);
55+
test('Check encode', () {
56+
csvLinesFromFile('encoding.csv').forEach(checkEncode);
57+
});
58+
test('Check locationToIntegers', () {
59+
csvLinesFromFile('encoding.csv').forEach(checkLocationToIntegers);
60+
});
61+
test('Check encodeIntegers', () {
62+
csvLinesFromFile('encoding.csv').forEach(checkEncodeIntegers);
3563
});
3664

3765
test('MaxCodeLength', () {

0 commit comments

Comments
 (0)