Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var is = ((is = function () {}),
- is.int = function (v) {
- return Number.isSafeInteger(parseInt(v));
- },
- is.float = function (v) {
- return !is.arr(v) && (v - parseFloat(v) + 1) >= 0
- },
- is.arr = function (v) {
- return Object.prototype.toString.call(v) === '[object Array]'
- },
- is.str = function (v) {
- return typeof v === 'string' || v instanceof String;
- },
- is.obj = function (v) {
- return v !== null && typeof v === 'object';
- },
- is.type = function (v, t) {
- return !!v && v.constructor === t;
- },
- is.truthy = function (v) {
- return !!v;
- },
- is.falsy = function (v) {
- return !v;
- },
- is.func = function (v) {
- return v && {}.toString.call(v) === '[object Function]';
- },
- is.json = function (v, j) {
- try {
- j.obj = JSON.parse(v);
- } catch (e) {
- return false;
- }
- return true;
- },
- is.eq = function (a, b) {
- console.log('compare ' + a + ' and ' + b);
- return a === b;
- },
- is.nq = function (a, b) {
- return a !== b;
- },
- is.lt = function (a, b) {
- return a < b;
- },
- is.gt = function (a, b) {
- return a > b;
- },
- is.lte = function (a, b) {
- return a <= b;
- },
- is.gte = function (a, b) {
- return a >= b;
- },
- is.empty = function (v) {
- if (!v) return true;
- if (v[length]) return !v.length;
- if (v[size]) return !v.size;
- return true;
- },
- is.what = function (v) {
- if (typeof v === "undefined") return "undefined";
- if (v === null) return "null";
- return Object.prototype.toString.call(v).match(/^\[object\s(.*)\]$/)[1];
- }, is);
- var fix = ((fix = function () {}),
- fix.date = function (v) {
- return new Date(Date.parse(v + 'Z'));
- }, fix);
- this['tests'] = this['tests'] || [];
- var T = function () {
- let $this = {
- value: {},
- assertion: {
- message: '',
- invert: false
- }
- };
- let eval = (exp, a, b, cb) => {
- try {
- if (!exp) return false;
- if (is.str(exp)) return exp;
- if (is.func(exp)) return exp(a, b, cb);
- return exp;
- } catch (e) {
- cb(e);
- }
- return false;
- }
- let info = (v, t) => {
- return 'Value for key ' + t.value.key + ' on path ' + t.value.path + ' is ' + (v ? JSON.stringify(v) + ' of type ' + is.what(v) : 'undefined');
- }
- let assert = (func, name, cb, param) => {
- if (!$this.value.path) return this;
- if (!($this.assertion.result = !$this.assertion.invert && eval(func, $this.value.value, param, (e) => {
- $this.assertion.message = 'Error during value assertion: ' + e + '. ';
- }))) $this.assertion.message += eval(cb || info, $this.value.value, $this);
- if (!$this.name && !(name = eval(name, $this.value.value, $this))) {
- $this.name = 'Assertion for value ' + $this.value.key + ' on path ' + $this.value.path;
- }
- let result = {
- name: $this.name || ($this.value.key + ' is ' + ($this.assertion.invert ? 'not ' : '') + name),
- value: $this.value,
- result: $this.assertion
- }
- console.log(JSON.stringify(result, null, 2));
- if (!(tests[$this.name] = $this.assertion.result)) {
- let r = {};
- r[$this.name] = $this.assertion.message;
- console.info(JSON.stringify(r))
- }
- $this.assertion.invert = false;
- $this.assertion.message = '';
- return this;
- }
- this.name = (n) => {
- $this.name = eval(n);
- return this;
- }
- this.get = (v, p) => {
- $this.value.path = p;
- $this.value.value = T.get(v, p, (o, x, i, a) => {
- $this.value.key = x;
- });
- return this;
- }
- this.not = () => {
- $this.assertion.invert = true;
- return this;
- }
- this.falsy = (cb) => assert(is.falsy, 'falsy', cb);
- this.truthy = (cb) => assert(is.truthy, 'truthy', cb);
- this.is = (t, cb) => assert(is.type, '' + is.what($this.value.value), cb, t);
- this.empty = (cb) => assert(is.empty, 'empty', cb);
- this.equals = (v, cb) => assert(is.eq, 'equal ' + JSON.stringify(v), cb, v);
- this.lessThan = (v, cb) => assert(is.lt, 'less than ' + JSON.stringify(v), cb, v);
- this.lessEquals = (v, cb) => assert(is.lte, 'less or equals ' + JSON.stringify(v), cb, v);
- this.greaterThan = (v, cb) => assert(is.gt, 'greater than ' + JSON.stringify(v), cb, v);
- this.greaterEquals = (v, cb) => assert(is.gte, 'greater or equals ' + JSON.stringify(v), cb, v);
- this.eval = (func, cb) => assert(func, undefined, cb);
- }
- T.get = function (v, p, c) {
- return (p === '.') ? v : p.split('.').reduce((o, x, i, a) => {
- if (o && c) c(o, x, i, a);
- return !o ? o : o[x];
- }, v);
- }
- var test, body,
- t = test = ((t = function (val, path) {
- return (path) ? new T().get(val, path) : new T().name(val);
- }),
- t.responseTime = (max) => {
- test(this, 'responseTime').lessThan(max || 2000);
- },
- t.responseCode = (code) => {
- test(this, 'responseCode.code').equals(code || 200);
- },
- t.responseBody = () => {
- test(this, 'responseBody')
- .name('responseBody is valid JSON')
- .eval((obj) => {
- body = JSON.parse(obj);
- return true;
- });
- },
- t);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement