Advertisement
n7mobile

Untitled

Jan 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. var is = ((is = function () {}),
  2. is.int = function (v) {
  3. return Number.isSafeInteger(parseInt(v));
  4. },
  5. is.float = function (v) {
  6. return !is.arr(v) && (v - parseFloat(v) + 1) >= 0
  7. },
  8. is.arr = function (v) {
  9. return Object.prototype.toString.call(v) === '[object Array]'
  10. },
  11. is.str = function (v) {
  12. return typeof v === 'string' || v instanceof String;
  13. },
  14. is.obj = function (v) {
  15. return v !== null && typeof v === 'object';
  16. },
  17. is.type = function (v, t) {
  18. return !!v && v.constructor === t;
  19. },
  20. is.truthy = function (v) {
  21. return !!v;
  22. },
  23. is.falsy = function (v) {
  24. return !v;
  25. },
  26. is.func = function (v) {
  27. return v && {}.toString.call(v) === '[object Function]';
  28. },
  29. is.json = function (v, j) {
  30. try {
  31. j.obj = JSON.parse(v);
  32. } catch (e) {
  33. return false;
  34. }
  35. return true;
  36. },
  37. is.eq = function (a, b) {
  38. console.log('compare ' + a + ' and ' + b);
  39. return a === b;
  40. },
  41. is.nq = function (a, b) {
  42. return a !== b;
  43. },
  44. is.lt = function (a, b) {
  45. return a < b;
  46. },
  47. is.gt = function (a, b) {
  48. return a > b;
  49. },
  50. is.lte = function (a, b) {
  51. return a <= b;
  52. },
  53. is.gte = function (a, b) {
  54. return a >= b;
  55. },
  56. is.empty = function (v) {
  57. if (!v) return true;
  58. if (v[length]) return !v.length;
  59. if (v[size]) return !v.size;
  60. return true;
  61. },
  62. is.what = function (v) {
  63. if (typeof v === "undefined") return "undefined";
  64. if (v === null) return "null";
  65. return Object.prototype.toString.call(v).match(/^\[object\s(.*)\]$/)[1];
  66. }, is);
  67.  
  68. var fix = ((fix = function () {}),
  69. fix.date = function (v) {
  70. return new Date(Date.parse(v + 'Z'));
  71. }, fix);
  72.  
  73. this['tests'] = this['tests'] || [];
  74.  
  75. var T = function () {
  76. let $this = {
  77. value: {},
  78. assertion: {
  79. message: '',
  80. invert: false
  81. }
  82. };
  83.  
  84. let eval = (exp, a, b, cb) => {
  85. try {
  86. if (!exp) return false;
  87. if (is.str(exp)) return exp;
  88. if (is.func(exp)) return exp(a, b, cb);
  89.  
  90. return exp;
  91. } catch (e) {
  92. cb(e);
  93. }
  94. return false;
  95. }
  96.  
  97. let info = (v, t) => {
  98. return 'Value for key ' + t.value.key + ' on path ' + t.value.path + ' is ' + (v ? JSON.stringify(v) + ' of type ' + is.what(v) : 'undefined');
  99. }
  100.  
  101. let assert = (func, name, cb, param) => {
  102. if (!$this.value.path) return this;
  103.  
  104. if (!($this.assertion.result = !$this.assertion.invert && eval(func, $this.value.value, param, (e) => {
  105. $this.assertion.message = 'Error during value assertion: ' + e + '. ';
  106. }))) $this.assertion.message += eval(cb || info, $this.value.value, $this);
  107.  
  108. if (!$this.name && !(name = eval(name, $this.value.value, $this))) {
  109. $this.name = 'Assertion for value ' + $this.value.key + ' on path ' + $this.value.path;
  110. }
  111.  
  112. let result = {
  113. name: $this.name || ($this.value.key + ' is ' + ($this.assertion.invert ? 'not ' : '') + name),
  114. value: $this.value,
  115. result: $this.assertion
  116. }
  117.  
  118. console.log(JSON.stringify(result, null, 2));
  119.  
  120. if (!(tests[$this.name] = $this.assertion.result)) {
  121. let r = {};
  122. r[$this.name] = $this.assertion.message;
  123.  
  124. console.info(JSON.stringify(r))
  125. }
  126.  
  127. $this.assertion.invert = false;
  128. $this.assertion.message = '';
  129.  
  130. return this;
  131. }
  132. this.name = (n) => {
  133. $this.name = eval(n);
  134. return this;
  135. }
  136. this.get = (v, p) => {
  137. $this.value.path = p;
  138. $this.value.value = T.get(v, p, (o, x, i, a) => {
  139. $this.value.key = x;
  140. });
  141. return this;
  142. }
  143. this.not = () => {
  144. $this.assertion.invert = true;
  145. return this;
  146. }
  147. this.falsy = (cb) => assert(is.falsy, 'falsy', cb);
  148. this.truthy = (cb) => assert(is.truthy, 'truthy', cb);
  149. this.is = (t, cb) => assert(is.type, '' + is.what($this.value.value), cb, t);
  150. this.empty = (cb) => assert(is.empty, 'empty', cb);
  151. this.equals = (v, cb) => assert(is.eq, 'equal ' + JSON.stringify(v), cb, v);
  152. this.lessThan = (v, cb) => assert(is.lt, 'less than ' + JSON.stringify(v), cb, v);
  153. this.lessEquals = (v, cb) => assert(is.lte, 'less or equals ' + JSON.stringify(v), cb, v);
  154. this.greaterThan = (v, cb) => assert(is.gt, 'greater than ' + JSON.stringify(v), cb, v);
  155. this.greaterEquals = (v, cb) => assert(is.gte, 'greater or equals ' + JSON.stringify(v), cb, v);
  156. this.eval = (func, cb) => assert(func, undefined, cb);
  157. }
  158.  
  159. T.get = function (v, p, c) {
  160. return (p === '.') ? v : p.split('.').reduce((o, x, i, a) => {
  161. if (o && c) c(o, x, i, a);
  162. return !o ? o : o[x];
  163. }, v);
  164. }
  165.  
  166. var test, body,
  167. t = test = ((t = function (val, path) {
  168. return (path) ? new T().get(val, path) : new T().name(val);
  169. }),
  170. t.responseTime = (max) => {
  171. test(this, 'responseTime').lessThan(max || 2000);
  172. },
  173. t.responseCode = (code) => {
  174. test(this, 'responseCode.code').equals(code || 200);
  175. },
  176. t.responseBody = () => {
  177. test(this, 'responseBody')
  178. .name('responseBody is valid JSON')
  179. .eval((obj) => {
  180. body = JSON.parse(obj);
  181. return true;
  182. });
  183. },
  184. t);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement