From 18dea2bc6c86e09a4553f238db79f87a04e162b4 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Wed, 13 Sep 2017 15:02:26 -0700 Subject: [PATCH] Add an equality assertion that does the right thing for a given property type --- tests/js/asserts.js | 16 ++++++++++++++++ tests/js/object-tests.js | 20 ++++---------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/tests/js/asserts.js b/tests/js/asserts.js index 06852003..cf2178de 100644 --- a/tests/js/asserts.js +++ b/tests/js/asserts.js @@ -19,6 +19,22 @@ 'use strict'; module.exports = { + assertSimilar: function(type, val1, val2, errorMessage, depth) { + depth = depth || 0; + if (type == 'float' || type == 'double') { + this.assertEqualWithTolerance(val1, val2, errorMessage, depth + 1); + } + else if (type == 'data') { + this.assertArraysEqual(new Uint8Array(val1), val2, errorMessage, depth + 1); + } + else if (type == 'date') { + this.assertEqual(val1.getTime(), val2.getTime(), errorMessage, depth + 1); + } + else { + this.assertEqual(val1, val2, errorMessage, depth + 1); + } + }, + assertEqual: function(val1, val2, errorMessage, depth) { if (val1 !== val2) { let message = `'${val1}' does not equal expected value '${val2}'`; diff --git a/tests/js/object-tests.js b/tests/js/object-tests.js index 64c84890..be1f3a58 100644 --- a/tests/js/object-tests.js +++ b/tests/js/object-tests.js @@ -46,22 +46,10 @@ module.exports = { object = realm.create('BasicTypesObject', basicTypesValues); }); - for (var name in schemas.BasicTypes.properties) { - var prop = schemas.BasicTypes.properties[name]; - var type = typeof prop == 'object' ? prop.type : prop; - - if (type == 'float' || type == 'double') { - TestCase.assertEqualWithTolerance(object[name], basicTypesValues[name], 0.000001); - } - else if (type == 'data') { - TestCase.assertArraysEqual(new Uint8Array(object[name]), RANDOM_DATA); - } - else if (type == 'date') { - TestCase.assertEqual(object[name].getTime(), basicTypesValues[name].getTime()); - } - else { - TestCase.assertEqual(object[name], basicTypesValues[name]); - } + for (const name in schemas.BasicTypes.properties) { + const prop = schemas.BasicTypes.properties[name]; + const type = typeof prop == 'object' ? prop.type : prop; + TestCase.assertSimilar(type, object[name], basicTypesValues[name]); } TestCase.assertEqual(object.nonexistent, undefined);