1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/hustcc-variable-type

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
test.js 12 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
hustcc Отправлено 19.10.2017 05:43 5c21ee1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
var VT = require('./');
var expect = require('expect');
describe('variable-type', function() {
it(' - bool', function() {
expect(VT.bool.check(true)).toBe(true);
expect(VT.bool.check(false)).toBe(true);
expect(VT.bool.check(undefined)).toBe(false);
expect(VT.bool.check('1')).toBe(false);
expect(VT.bool.check(null)).toBe(false);
});
it(' - func', function() {
expect(VT.func.check(Math.pow)).toBe(true);
expect(VT.func.check(function _test() {})).toBe(true);
expect(VT.func.check(undefined)).toBe(false);
expect(VT.func.check(1.2)).toBe(false);
expect(VT.func.check('1')).toBe(false);
expect(VT.func.check(null)).toBe(false);
});
it(' - number', function() {
expect(VT.number.check(1)).toBe(true);
expect(VT.number.check(1.2)).toBe(true);
expect(VT.number.check(undefined)).toBe(false);
expect(VT.number.check('1')).toBe(false);
expect(VT.number.check(null)).toBe(false);
});
it(' - string', function() {
expect(VT.string.check('1')).toBe(true);
expect(VT.string.check('variable-type')).toBe(true);
expect(VT.string.check(undefined)).toBe(false);
expect(VT.string.check(1)).toBe(false);
expect(VT.string.check(null)).toBe(false);
});
it(' - instanceOf', function() {
expect(VT.instanceOf(Date).check(new Date())).toBe(true);
expect(VT.instanceOf(Date).check(undefined)).toBe(false);
expect(VT.instanceOf(Date).check('1')).toBe(false);
expect(VT.instanceOf(Date).check(null)).toBe(false);
});
it(' - typeOf', function() {
expect(VT.typeOf('date').check(new Date())).toBe(true);
expect(VT.typeOf('number').check(1)).toBe(true);
expect(VT.typeOf('string').check('hustcc')).toBe(true);
expect(VT.typeOf('test').check('hustcc')).toBe(false);
});
it(' - object', function() {
expect(VT.object.check({})).toBe(true);
expect(VT.object.check(undefined)).toBe(false);
expect(VT.object.check('1')).toBe(false);
expect(VT.object.check(null)).toBe(false);
});
it(' - array', function() {
expect(VT.array.check([])).toBe(true);
expect(VT.array.check(undefined)).toBe(false);
expect(VT.array.check('1')).toBe(false);
expect(VT.array.check(null)).toBe(false);
});
it(' - undefined', function() {
expect(VT.undefined.check(undefined)).toBe(true);
expect(VT.undefined.check(null)).toBe(false);
expect(VT.undefined.check(1)).toBe(false);
expect(VT.undefined.check('hustcc')).toBe(false);
});
it(' - null', function() {
expect(VT.null.check(undefined)).toBe(false);
expect(VT.null.check(null)).toBe(true);
expect(VT.null.check(1)).toBe(false);
expect(VT.null.check('hustcc')).toBe(false);
});
it(' - oneOf', function() {
expect(VT.oneOf(['a', 1]).check('a')).toBe(true);
expect(VT.oneOf(['a', 1]).check(1)).toBe(true);
expect(VT.oneOf(['a', 1]).check('1')).toBe(false);
expect(VT.oneOf(['a', 1]).check(undefined)).toBe(false);
expect(VT.oneOf(['a', 1]).check(new Date())).toBe(false);
// except cases
expect(VT.oneOf(['a', 1]).check(null)).toBe(false);
expect(VT.oneOf(['a', 1]).check(undefined)).toBe(false);
expect(VT.oneOf(['a', 1]).check(new Date())).toBe(false);
});
it(' - in', function() {
expect(VT.in(['a', 1]).check('a')).toBe(true);
expect(VT.in(['a', 1]).check(1)).toBe(true);
expect(VT.in(['a', 1]).check('1')).toBe(false);
expect(VT.in(['a', 1]).check(undefined)).toBe(false);
expect(VT.in(['a', 1]).check(new Date())).toBe(false);
// except cases
expect(VT.in(['a', 1]).check(null)).toBe(false);
expect(VT.in(['a', 1]).check(undefined)).toBe(false);
expect(VT.in(['a', 1]).check(new Date())).toBe(false);
});
it(' - any', function() {
expect(VT.any.check('a')).toBe(true);
expect(VT.any.check(1)).toBe(true);
expect(VT.any.check(undefined)).toBe(true);
expect(VT.any.check(new Date())).toBe(true);
});
it(' - oneOfType', function() {
expect(VT.oneOfType([
VT.number
]).check('a')).toBe(false);
expect(VT.oneOfType([
VT.number,
VT.string
]).check('a')).toBe(true);
expect(VT.oneOfType([
VT.number,
VT.string,
VT.instanceOf(Date)
]).check(new Date())).toBe(true);
// except cases
expect(VT.oneOfType([
VT.number
]).check(null)).toBe(false);
expect(VT.oneOfType([
VT.number
]).check(undefined)).toBe(false);
expect(VT.oneOfType([
VT.number
]).check(new Date())).toBe(false);
});
it(' - or', function() {
expect(VT.or([
VT.number
]).check('a')).toBe(false);
expect(VT.or([
VT.number,
VT.string
]).check('a')).toBe(true);
expect(VT.or([
VT.number,
VT.string,
VT.instanceOf(Date)
]).check(new Date())).toBe(true);
// except cases
expect(VT.or([
VT.number
]).check(null)).toBe(false);
expect(VT.or([
VT.number
]).check(undefined)).toBe(false);
expect(VT.or([
VT.number
]).check(new Date())).toBe(false);
});
it(' - and', function() {
expect(VT.and([
VT.number
]).check('a')).toBe(false);
expect(VT.and([
VT.string
]).check('a')).toBe(true);
expect(VT.and([
VT.string,
VT.in(['a', 'b'])
]).check('a')).toBe(true);
// except cases
expect(VT.and([
VT.number
]).check(null)).toBe(false);
expect(VT.and([
VT.number
]).check(undefined)).toBe(false);
expect(VT.and([
VT.number
]).check(new Date())).toBe(false);
});
it(' - not', function() {
expect(VT.not(VT.and([
VT.number
])).check('a')).toBe(true);
expect(VT.not(VT.and([
VT.string
])).check('a')).toBe(false);
expect(VT.not(VT.and([
VT.string,
VT.in(['a', 'b'])
])).check('a')).toBe(false);
// except cases
expect(VT.not(VT.and([
VT.number
])).check(null)).toBe(true);
expect(VT.not(VT.and([
VT.number
])).check(undefined)).toBe(true);
expect(VT.not(VT.and([
VT.number
])).check(undefined)).toBe(true);
expect(VT.not(VT.number).check(new Date())).toBe(true);
});
it(' - apply', function() {
expect(VT.apply(function (v) {
return v.indexOf('hustcc') !== -1;
}).check('hustcc')).toBe(true);
expect(VT.shape({
name: VT.apply(function (v) {
return v[0] === 'H';
})
}).check({
name: 'Hello'
})).toBe(true);
expect(VT.arrayOf(
VT.apply(function (v) {
return v < 4;
})
).check([1, 2, 3])).toBe(true);
expect(VT.arrayOf(
VT.apply(function (v) {
return v > 2;
})
).check([1, 2, 3])).toBe(false);
});
it(' - arrayOf', function() {
expect(VT.arrayOf(VT.number).check([1, 2, 3])).toBe(true);
expect(VT.arrayOf(VT.string).check(['1', '2'])).toBe(true);
expect(VT.arrayOf(VT.instanceOf(Date)).check([new Date()])).toBe(true);
expect(VT.arrayOf(VT.string).check(['1', 2])).toBe(false);
expect(VT.arrayOf(VT.func).check(['1', 2])).toBe(false);
expect(VT.arrayOf(
VT.arrayOf(
VT.or([
VT.number,
VT.string
])
)
).check([
['1', 2, '3'],
['1', 2, '3']
])).toBe(true);
// except cases
expect(VT.arrayOf(VT.func).check(null)).toBe(false);
expect(VT.arrayOf(VT.func).check(undefined)).toBe(false);
expect(VT.arrayOf(VT.func).check(1)).toBe(false);
expect(VT.arrayOf(VT.func).check('hello')).toBe(false);
expect(VT.arrayOf(VT.func).check(new Date())).toBe(false);
});
it(' - shape', function() {
expect(VT.shape({
a: VT.bool,
b: VT.number,
c: VT.string,
d: VT.func,
e: VT.instanceOf(Date),
f: VT.in([1, '1'])
}).check({
a: true,
b: 1,
c: 'str',
d: function() {},
e: new Date(),
f: '1'
})).toBe(true);
expect(VT.shape({
a: VT.bool,
b: VT.number,
c: VT.string,
d: VT.func,
e: VT.instanceOf(Date)
}).check({
a: true,
c: 'str',
e: new Date()
})).toBe(false);
expect(VT.shape({
a: VT.bool,
b: VT.number,
c: VT.string,
d: VT.func,
e: VT.instanceOf(Date)
}).check({
a: true,
c: 123,
e: new Date()
})).toBe(false);
expect(VT.shape({
name: VT.string,
boy: VT.bool,
birthday: VT.instanceOf(Date)
}).check({
name: 'hustcc',
boy: true,
birthday: new Date(1992, 8, 1)
})).toBe(true);
// except case
expect(VT.shape({
name: VT.string
}).check(null)).toBe(false);
expect(VT.shape({
name: VT.string
}).check(undefined)).toBe(false);
expect(VT.shape({
name: VT.string
}).check(new Date())).toBe(false);
});
it(' - recursive', function() {
var types = {
name: VT.string
};
types.children = VT.or([
VT.arrayOf(VT.shape(types)),
VT.undefined
]);
var recursiveTypes = VT.shape(types);
expect(recursiveTypes.check({
name: 'Life',
children: [{
name: 'Animal',
children: [{
name: 'Dog',
children: [{
name: 'Dog1'
}, {
name: 'Dog2'
}]
}, {
name: 'Cat'
}
]}, {
name: 'Plant'
}]
})).toBe(true);
expect(recursiveTypes.check({
name: 'Life',
children: [{
name: 'Animal',
children: [{
name: 'Dog',
children: [{
name: 888
}, {
name: 'Dog2'
}]
}, {
name: 'Cat'
}
]}, {
name: 'Plant'
}]
})).toBe(false);
});
it(' - complex usage', function() {
expect(VT.shape({
a: VT.bool,
b: VT.number,
c: VT.string,
d: VT.func,
e: VT.instanceOf(Date),
f: VT.in([1, '1']),
g: VT.shape({
h: VT.oneOfType([
VT.shape({
i: VT.arrayOf(
VT.oneOfType([
VT.number,
VT.string,
VT.bool,
VT.shape({
j: VT.func,
k: VT.null,
l: VT.undefined
})
])
)
})
])
})
}).check({
a: true,
b: 1,
c: 'str',
d: function() {},
e: new Date(),
f: '1',
g: {
h: {
i: [
'1',
2,
true,
{
j: function() {},
k: null
}
]
}
}
})).toBe(true);
expect(VT.shape({
a: VT.bool,
b: VT.number,
c: VT.string,
d: VT.func,
e: VT.instanceOf(Date),
f: VT.in([1, '1']),
g: VT.shape({
h: VT.oneOfType([
VT.shape({
i: VT.arrayOf(
VT.oneOfType([
VT.number,
VT.string,
VT.bool,
VT.shape({
j: VT.func
})
])
)
})
])
})
}).check({
a: true,
b: 1,
c: 'str',
d: function() {},
e: new Date(),
f: '1',
g: {
h: {
i: [
'1',
2,
true,
{
j: 1
}
]
}
}
})).toBe(false);
});
it(' - optional()', function() {
expect(VT.string.optional().check('hustcc')).toBe(true);
expect(VT.string.optional().check(undefined)).toBe(true);
expect(VT.shape({
name: VT.string,
birthday: VT.string,
sex: VT.string.optional()
}).check({
name: 'hustcc',
birthday: '1992-08-01'
})).toBe(true);
expect(VT.shape({
name: VT.string,
birthday: VT.string,
sex: VT.string.optional()
}).check({
name: 'hustcc',
birthday: '1992-08-01',
sex: '1'
})).toBe(true);
expect(VT.shape({
name: VT.string,
birthday: VT.string,
sex: VT.string.optional()
}).check({
name: 'hustcc',
birthday: '1992-08-01',
sex: 0
})).toBe(false);
});
it(' - single usage', function() {
expect(VT.bool.check(true)).toBe(true);
expect(VT.func.check(Math.pow)).toBe(true);
expect(VT.number.check(123)).toBe(true);
expect(VT.string.check('hustcc')).toBe(true);
expect(VT.object.check({a: 1})).toBe(true);
expect(VT.array.check([1, 2, '3'])).toBe(true);
expect(VT.any.check(true)).toBe(true);
expect(VT.null.check(null)).toBe(true);
expect(VT.undefined.check(undefined)).toBe(true);
});
});

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/hustcc-variable-type.git
git@api.gitlife.ru:oschina-mirror/hustcc-variable-type.git
oschina-mirror
hustcc-variable-type
hustcc-variable-type
master