Browse Source

fix ? handling

master
Mark Vayngrib 9 years ago
parent
commit
8a378080cc
  1. 7
      index.js
  2. 4
      test/fixtures.json
  3. 8
      test/index.js

7
index.js

@ -16,6 +16,9 @@ module.exports = function enforce (type, value) {
}
type = type.slice(1)
if (!type.length) {
return throwDefault('?', value)
}
}
}
@ -91,5 +94,9 @@ module.exports = function enforce (type, value) {
}
}
throwDefault(type, value)
}
function throwDefault(type, value) {
throw new TypeError('Expected ' + type + ', got ' + getName(value) + ' ' + value)
}

4
test/fixtures.json

@ -438,12 +438,12 @@
"value": null
},
{
"expception": "Expected ?, got Number 1234",
"exception": "Expected ?, got Number 1234",
"type": "?",
"value": 1234
},
{
"expception": "Expected ?, got String foobar",
"exception": "Expected ?, got String foobar",
"type": "?",
"value": "foobar"
}

8
test/index.js

@ -22,12 +22,18 @@ describe('typeForce', function () {
})
fixtures.invalid.forEach(function (f) {
assert(f.exception)
var actualValue = f.custom ? CUSTOM_TYPES[f.custom] : f.value
it('fails for ' + JSON.stringify(f.type) + ' with ' + (f.custom ? f.custom : JSON.stringify(f.value)), function () {
assert.throws(function () {
typeForce(f.type, actualValue)
}, new RegExp(f.exception))
}, toRegExp(f.exception))
})
})
})
function toRegExp (str) {
str = str.replace(/\?/g, '\\?')
return new RegExp(str)
}

Loading…
Cancel
Save