[gnome-maps/wip/mlundblad/unit-tests] WIP: Add unit tests for the utils module
- From: Marcus Lundblad <mlundblad src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps/wip/mlundblad/unit-tests] WIP: Add unit tests for the utils module
- Date: Sun, 10 May 2020 12:46:27 +0000 (UTC)
commit 31b6b1aeddfafb4749d9940b69cb8cc8e4e01427
Author: Marcus Lundblad <ml update uu se>
Date: Tue Apr 28 23:39:58 2020 +0200
WIP: Add unit tests for the utils module
tests/meson.build | 2 +-
tests/utilsTest.js | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 90 insertions(+), 1 deletion(-)
---
diff --git a/tests/meson.build b/tests/meson.build
index 80c3691b..282fd2c8 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -4,7 +4,7 @@ script_conf.set('PACKAGE_VERSION', version)
script_conf.set('libdir', libdir)
script_conf.set('prefix', prefix)
-tests = []
+tests = ['utilsTest']
foreach test : tests
script_conf.set('name', test)
diff --git a/tests/utilsTest.js b/tests/utilsTest.js
new file mode 100644
index 00000000..52a089c6
--- /dev/null
+++ b/tests/utilsTest.js
@@ -0,0 +1,89 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2020 Marcus Lundblad
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+pkg.require({ 'Gdk': '3.0',
+ 'Gtk': '3.0' });
+pkg.initFormat();
+
+const Geocode = imports.gi.GeocodeGlib;
+const GLib = imports.gi.GLib;
+
+const JsUnit = imports.jsUnit;
+
+const Utils = imports.utils;
+
+function main() {
+ osmTypeToStringTest();
+ dashedToCamelCaseTest();
+ getAccuracyDescriptionTest();
+ prettyTimeTest();
+ prettyDistanceTest();
+}
+
+function osmTypeToStringTest() {
+ JsUnit.assertEquals('OSM type node', 'node',
+ Utils.osmTypeToString(Geocode.PlaceOsmType.NODE));
+ JsUnit.assertEquals('OSM type way', 'way',
+ Utils.osmTypeToString(Geocode.PlaceOsmType.WAY));
+ JsUnit.assertEquals('OSM type relation', 'relation',
+ Utils.osmTypeToString(Geocode.PlaceOsmType.RELATION));
+}
+
+function dashedToCamelCaseTest() {
+ JsUnit.assertEquals('foo', Utils.dashedToCamelCase('foo'));
+ JsUnit.assertEquals('fooBar', Utils.dashedToCamelCase('foo-bar'));
+}
+
+function getAccuracyDescriptionTest() {
+ JsUnit.assertEquals('Unknown',
+ Utils.getAccuracyDescription(Geocode.LOCATION_ACCURACY_UNKNOWN));
+ JsUnit.assertEquals('Exact', Utils.getAccuracyDescription(0));
+ // for other distances, the same as prettyDistance()
+ JsUnit.assertEquals(Utils.prettyDistance(100),
+ Utils.getAccuracyDescription(100));
+ JsUnit.assertEquals(Utils.prettyDistance(10000),
+ Utils.getAccuracyDescription(10000));
+}
+
+function prettyTimeTest() {
+ // tests with default locale C, as set above
+ JsUnit.assertEquals('1 h', Utils.prettyTime(3600000));
+ JsUnit.assertEquals('1 h 1 min', Utils.prettyTime(3660000));
+ JsUnit.assertEquals('1 h', Utils.prettyTime(3600001));
+ JsUnit.assertEquals('1 h 10 mins', Utils.prettyTime(4200000));
+ JsUnit.assertEquals('20 mins', Utils.prettyTime(1200000));
+ JsUnit.assertEquals('20 mins', Utils.prettyTime(1201000));
+ JsUnit.assertEquals('1 s', Utils.prettyTime(1000));
+ JsUnit.assertEquals('1 s', Utils.prettyTime(1001));
+}
+
+function prettyDistanceTest() {
+ // tests with metric system, using override mock function
+ Utils.getMeasurementSystem = function() { return Utils.METRIC_SYSTEM; };
+ JsUnit.assertEquals('1 km', Utils.prettyDistance(1000, false));
+ JsUnit.assertEquals('2.4 km', Utils.prettyDistance(2400, false));
+ JsUnit.assertEquals('123 m', Utils.prettyDistance(123, false));
+ JsUnit.assertEquals('1001 m', Utils.prettyDistance(1001, true));
+
+ // tests with imperial system, using override mock function
+ Utils.getMeasurementSystem = function() { return Utils.IMPERIAL_SYSTEM; };
+ JsUnit.assertEquals('1 mi', Utils.prettyDistance(1609, false));
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]