Browse Source

tests: add failing tests for typeforce.oneOf, and fix oneOf behaviour

master
Daniel Cousens 8 years ago
parent
commit
9fa4135901
  1. 6
      index.js
  2. 984
      test/fixtures.json
  3. 13
      test/index.js
  4. 1
      test/types.js
  5. 2
      test/values.js

6
index.js

@ -122,7 +122,11 @@ var types = {
function _oneOf (value, strict) {
return types.some(function (type) {
return type(value, strict)
try {
return typeforce(type, value, strict)
} catch (e) {
return false
}
})
}
_oneOf.toJSON = function () { return types.map(tfJSON).join('|') }

984
test/fixtures.json

File diff suppressed because it is too large

13
test/index.js

@ -69,3 +69,16 @@ tape('t.throws can handle TfTypeError', function (t) {
typeforce(failType, 'value')
}, new RegExp('Expected mytype, got undefined'))
})
tape('TfTypeError is caught by typeforce.oneOf', function (t) {
t.plan(1)
t.doesNotThrow(function () {
typeforce.oneOf(failType)('value')
})
})
tape('TfTypeError does not break typeforce.oneOf', function (t) {
t.plan(1)
t.ok(!typeforce.oneOf(failType, typeforce.string)('value'))
})

1
test/types.js

@ -30,6 +30,7 @@ module.exports = {
'{ String: Number }': typeforce.map('Number', 'String'),
'{ Letter: Number }': typeforce.map('Number', Letter),
'{ a: { b: Buffer3 } }': { a: { b: typeforce.BufferN(3) } },
'{ a: Buffer10|Number }': { a: typeforce.oneOf(typeforce.BufferN(10), 'Number') },
'Buffer0': typeforce.BufferN(0),
'Buffer3': typeforce.BufferN(3),
'Buffer10': typeforce.BufferN(10),

2
test/values.js

@ -6,6 +6,8 @@ module.exports = {
'emptyType': new function EmptyType () {}(),
'customType': new function CustomType () { this.x = 2 }(),
'{ a: undefined }': { a: undefined },
'{ a: Buffer3 }': { a: buffer3 },
'{ a: Buffer10 }': { a: buffer10 },
'{ a: { b: Buffer3 } }': { a: { b: buffer3 } },
'{ a: { b: Buffer10 } }': { a: { b: buffer10 } },
'Buffer': new Buffer(0),

Loading…
Cancel
Save