From 06c3caa1c0a188af3c3aec1e8072a92b1cc54206 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Wed, 2 Jan 2013 13:26:36 -0500 Subject: [PATCH] Expand util tests --- js/id/util.js | 4 +++- test/spec/util.js | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/js/id/util.js b/js/id/util.js index 7e2df0d34..f9c515ae5 100644 --- a/js/id/util.js +++ b/js/id/util.js @@ -45,7 +45,9 @@ iD.util.tagText = function(entity) { iD.util.stringQs = function(str) { return str.split('&').reduce(function(obj, pair){ var parts = pair.split('='); - obj[parts[0]] = (null === parts[1]) ? '' : decodeURIComponent(parts[1]); + if (parts.length === 2) { + obj[parts[0]] = (null === parts[1]) ? '' : decodeURIComponent(parts[1]); + } return obj; }, {}); }; diff --git a/test/spec/util.js b/test/spec/util.js index 02d8ca2b1..3b9b547a0 100644 --- a/test/spec/util.js +++ b/test/spec/util.js @@ -6,6 +6,24 @@ describe('Util', function() { expect(iD.util.trueObj([])).to.eql({}); }); + it('#tagText', function() { + expect(iD.util.tagText({})).to.eql(''); + expect(iD.util.tagText({tags:{foo:'bar'}})).to.eql('foo: bar'); + expect(iD.util.tagText({tags:{foo:'bar',two:'three'}})).to.eql('foo: bar\ntwo: three'); + }); + + it('#stringQs', function() { + expect(iD.util.stringQs('foo=bar')).to.eql({foo: 'bar'}); + expect(iD.util.stringQs('foo=bar&one=2')).to.eql({foo: 'bar', one: '2' }); + expect(iD.util.stringQs('')).to.eql({}); + }); + + it('#qsString', function() { + expect(iD.util.qsString({ foo: 'bar' })).to.eql('foo=bar'); + expect(iD.util.qsString({ foo: 'bar', one: 2 })).to.eql('foo=bar&one=2'); + expect(iD.util.qsString({})).to.eql(''); + }); + it('#friendlyName', function() { expect(iD.util.friendlyName({ tags: { name: 'hi' }})).to.equal('hi'); expect(iD.util.friendlyName({ tags: { highway: 'Route 5' }})).to.equal('Route 5'); @@ -13,6 +31,12 @@ describe('Util', function() { }); describe('geo', function() { + describe('#roundCoords', function() { + expect(iD.util.geo.roundCoords([0.1, 1])).to.eql([0, 1]); + expect(iD.util.geo.roundCoords([0, 1])).to.eql([0, 1]); + expect(iD.util.geo.roundCoords([0, 1.1])).to.eql([0, 1]); + }); + describe('#interp', function() { it('interpolates halfway', function() { var a = [0, 0],