From 7c1563f9ec26314c207f47ddb1b138fa76dedda8 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Thu, 11 Apr 2013 19:05:18 -0700 Subject: [PATCH] Add unit test for 'vg.comparator'. --- test/_package-test.js | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/test/_package-test.js b/test/_package-test.js index cbc51a27f7..9c469e68ba 100644 --- a/test/_package-test.js +++ b/test/_package-test.js @@ -204,6 +204,77 @@ suite.addBatch({ assert.equal(vg.accessor(1)(['a', 'b']), 'b'); } }, + 'comparator': { + 'when called without argument, the comparator': { + 'should always return 0': function (vg) { + assert.equal(vg.comparator()('a', 'b'), 0); + } + }, + 'when sort argument contains single String without prefix, the comparator': { + topic: function (vg) { + return vg.comparator(['p']); + }, + 'should return 1 if resolved property of 1st arg is greater than resolved property of 2nd arg': function (comparator) { + assert.equal(comparator({'p': 1}, {'p': 0}), 1); + }, + 'should return 1 if resolved property of 2nd arg is greater than resolved property of 1st arg': function (comparator) { + assert.equal(comparator({'p': 0}, {'p': 1}), -1); + }, + 'should return 0 if resolved property of 1st arg is equal to resolved property of 2nd arg': function (comparator) { + assert.equal(comparator({'p': 1}, {'p': 1}), 0); + } + }, + 'when sort argument contains single String with "+" prefix, the comparator': { + topic: function (vg) { + return vg.comparator(['+p']); + }, + 'should return 1 if resolved property of 1st arg is greater than resolved property of 2nd arg': function (comparator) { + assert.equal(comparator({'p': 1}, {'p': 0}), 1); + }, + 'should return 1 if resolved property of 2nd arg is greater than resolved property of 1st arg': function (comparator) { + assert.equal(comparator({'p': 0}, {'p': 1}), -1); + }, + 'should return 0 if resolved property of 1st arg is equal to resolved property of 2nd arg': function (comparator) { + assert.equal(comparator({'p': 1}, {'p': 1}), 0); + } + }, + 'when sort argument contains single String with "-" prefix, the comparator': { + topic: function (vg) { + return vg.comparator(['-p']); + }, + 'should return -1 if resolved property of 1st arg is greater than resolved property of 2nd arg': function (comparator) { + assert.equal(comparator({'p': 1}, {'p': 0}), -1); + }, + 'should return 1 if resolved property of 2nd arg is greater than resolved property of 1st arg': function (comparator) { + assert.equal(comparator({'p': 0}, {'p': 1}), 1); + }, + 'should return 0 if resolved property of 1st arg is equal to resolved property of 2nd arg': function (comparator) { + assert.equal(comparator({'p': 1}, {'p': 1}), 0); + } + }, + 'when sort argument contains two Strings (without prefix), the comparator': { + topic: function (vg) { + return vg.comparator(['p', 'q']); + }, + 'should return 1 if 1st resolved property of 1st arg is greater than 1st resolved property of 2nd arg': function (comparator) { + assert.equal(comparator({'p': 1}, {'p': 0}), 1); + }, + 'should return -1 if 1st resolved property of 2nd arg is greater than 1st resolved property of 1st arg': function (comparator) { + assert.equal(comparator({'p': 0}, {'p': 1}), -1); + }, + 'when 1st resolved properties of both arguments are equal': { + 'should return 1 if 2nd resolved property of 1st arg is greater than 2nd resolved property of 2nd arg': function (comparator) { + assert.equal(comparator({'p': 1, 'q': 2}, {'p': 1, 'q': -2}), 1); + }, + 'should return -1 1st if 2nd resolved property of 2nd arg is greater than 2nd resolved property of 1st arg': function (comparator) { + assert.equal(comparator({'p': 1, 'q': -2}, {'p': 1, 'q': 2}), -1); + }, + 'should return 0 if both 2nd resolved properties are equal': function (comparator) { + assert.equal(comparator({'p': 1, 'q': 5}, {'p': 1, 'q': 5}), 0); + } + } + } + }, 'array': { 'array(null) should return an empty array': function (vg) { assert.deepEqual(vg.array(null), []);