From 221292436afe23651c4ab7b8db3b749dc921865b Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Wed, 21 Mar 2012 13:15:16 +0100 Subject: [PATCH] add expectWithTrim to all test cases and protect numericIf against failure --- lib/OpenLayers/BaseTypes.js | 2 +- tests/BaseTypes.html | 58 +++++++++++++++---------------------- 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/lib/OpenLayers/BaseTypes.js b/lib/OpenLayers/BaseTypes.js index 0856ad22a6..da5562725f 100644 --- a/lib/OpenLayers/BaseTypes.js +++ b/lib/OpenLayers/BaseTypes.js @@ -197,7 +197,7 @@ OpenLayers.String = { */ numericIf: function(value, trimWhitespace) { var originalValue = value; - if (trimWhitespace === true) { + if (trimWhitespace === true && value != null && value.replace) { value = value.replace(/^\s*|\s*$/g, ""); } return OpenLayers.String.isNumeric(value) ? parseFloat(value) : originalValue; diff --git a/tests/BaseTypes.html b/tests/BaseTypes.html index 3f1f87cfd2..cc391003da 100644 --- a/tests/BaseTypes.html +++ b/tests/BaseTypes.html @@ -201,38 +201,30 @@ function test_Number_numericIf(t) { var cases = [ - {value: "3", expect: 3}, - {value: "+3", expect: 3}, - {value: "-3", expect: -3}, - {value: "3.0", expect: 3}, - {value: "+3.0", expect: 3}, - {value: "-3.0", expect: -3}, - {value: "6.02e23", expect: 6.02e23}, - {value: "+1.0e-100", expect: 1e-100}, - {value: "-1.0e+100", expect: -1e100}, - {value: "1E100", expect: 1e100}, - {value: null, expect: null}, - {value: true, expect: true}, - {value: false, expect: false}, - {value: undefined, expect: undefined}, - {value: "", expect: ""}, - {value: "3 ", expect: "3 "}, - {value: " 3", expect: " 3"}, - {value: "1e", expect: "1e"}, - {value: "1+e", expect: "1+e"}, - {value: "1-e", expect: "1-e"}, + {value: "3", expect: 3, expectWithTrim: 3}, + {value: "+3", expect: 3, expectWithTrim: 3}, + {value: "-3", expect: -3, expectWithTrim: -3}, + {value: "3.0", expect: 3, expectWithTrim: 3}, + {value: "+3.0", expect: 3, expectWithTrim: 3}, + {value: "-3.0", expect: -3, expectWithTrim: -3}, + {value: "6.02e23", expect: 6.02e23, expectWithTrim: 6.02e23}, + {value: "+1.0e-100", expect: 1e-100, expectWithTrim: 1e-100}, + {value: "-1.0e+100", expect: -1e100, expectWithTrim: -1e100}, + {value: "1E100", expect: 1e100, expectWithTrim: 1e100}, + {value: null, expect: null, expectWithTrim: null}, + {value: true, expect: true, expectWithTrim: true}, + {value: false, expect: false, expectWithTrim: false}, + {value: undefined, expect: undefined, expectWithTrim: undefined}, + {value: "", expect: "", expectWithTrim: ""}, + {value: "3 ", expect: "3 ", expectWithTrim: 3}, + {value: " 3", expect: " 3", expectWithTrim: 3}, + {value: "1e", expect: "1e", expectWithTrim: "1e"}, + {value: "1+e", expect: "1+e", expectWithTrim: "1+e"}, + {value: "1-e", expect: "1-e", expectWithTrim: "1-e"}, {value: " 27 ", expect: " 27 ", expectWithTrim: 27}, {value: " abc ", expect: " abc ", expectWithTrim: " abc "} ]; - var count = 0; - for (var i=0, ii=cases.length; i