assert_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. const assert = require('../../testing/assert');
  19. const AssertionError = require('assert').AssertionError;
  20. const assertTrue = require('assert').ok;
  21. const assertEqual = require('assert').equal;
  22. const assertThrows = require('assert').throws;
  23. const fail = require('assert').fail;
  24. describe('assert', function() {
  25. describe('atLeast', function() {
  26. it('compares subject >= value', function() {
  27. assert(1).atLeast(0);
  28. assert(1).atLeast(1);
  29. assertThrows(() => assert(1).atLeast(2));
  30. });
  31. it('accepts failure message', function() {
  32. assertThrows(
  33. () => assert(1).atLeast(2, 'hi there!'),
  34. (error) => error.message.indexOf('hi there') != -1);
  35. });
  36. it('fails if given a non-numeric subject', function() {
  37. assertThrows(() => assert('a').atLeast(1));
  38. });
  39. it('fails if given a non-numeric bound', function() {
  40. assertThrows(() => assert(1).atLeast('a'));
  41. });
  42. it('waits for promised subject', function() {
  43. return assert(Promise.resolve(123)).atLeast(100);
  44. });
  45. it('waits for promised subject (with failure)', function() {
  46. return assert(Promise.resolve(100))
  47. .atLeast(123)
  48. .then(() => fail('should have failed'), function(e) {
  49. assertInstanceOf(AssertionError, e);
  50. assertEqual('100 >= 123', e.message);
  51. });
  52. });
  53. });
  54. describe('atMost', function() {
  55. it('compares subject <= value', function() {
  56. assertThrows(() => assert(1).atMost(0));
  57. assert(1).atMost(1);
  58. assert(1).atMost(2);
  59. });
  60. it('accepts failure message', function() {
  61. assertThrows(
  62. () => assert(1).atMost(0, 'hi there!'),
  63. (error) => error.message.indexOf('hi there!') != -1);
  64. });
  65. it('fails if given a non-numeric subject', function() {
  66. assertThrows(() => assert(1).atMost('a'));
  67. });
  68. it('fails if given a non-numeric bound', function() {
  69. assertThrows(() => assert('a').atMost(1));
  70. });
  71. it('waits for promised subject', function() {
  72. return assert(Promise.resolve(100)).atMost(123);
  73. });
  74. it('waits for promised subject (with failure)', function() {
  75. return assert(Promise.resolve(123))
  76. .atMost(100)
  77. .then(() => fail('should have failed'), function(e) {
  78. assertInstanceOf(AssertionError, e);
  79. assertEqual('123 <= 100', e.message);
  80. });
  81. });
  82. });
  83. describe('greaterThan', function() {
  84. it('compares subject > value', function() {
  85. assertThrows(() => assert(1).greaterThan(1));
  86. assertThrows(() => assert(1).greaterThan(2));
  87. assert(2).greaterThan(1);
  88. });
  89. it('accepts failure message', function() {
  90. assertThrows(
  91. () => assert(0).greaterThan(1, 'hi there!'),
  92. (error) => error.message.indexOf('hi there!') != -1);
  93. });
  94. it('fails if given a non-numeric subject', function() {
  95. assertThrows(() => assert('a').atMost(1));
  96. });
  97. it('fails if given a non-numeric bound', function() {
  98. assertThrows(() => assert(1).atMost('a'));
  99. });
  100. it('waits for promised subject', function() {
  101. return assert(Promise.resolve(123)).greaterThan(100);
  102. });
  103. it('waits for promised subject (with failure)', function() {
  104. return assert(Promise.resolve(100))
  105. .greaterThan(123)
  106. .then(() => fail('should have failed'), function(e) {
  107. assertInstanceOf(AssertionError, e);
  108. assertEqual('100 > 123', e.message);
  109. });
  110. });
  111. });
  112. describe('lessThan', function() {
  113. it('compares subject < value', function() {
  114. assertThrows(() => assert(1).lessThan(0));
  115. assertThrows(() => assert(1).lessThan(1));
  116. assert(1).lessThan(2);
  117. });
  118. it('accepts failure message', function() {
  119. assertThrows(
  120. () => assert(1).lessThan(0, 'hi there!'),
  121. (error) => error.message.indexOf('hi there!') != -1);
  122. });
  123. it('fails if given a non-numeric subject', function() {
  124. assertThrows(() => assert('a').lessThan(1));
  125. });
  126. it('fails if given a non-numeric bound', function() {
  127. assertThrows(() => assert(1).lessThan('a'));
  128. });
  129. it('waits for promised subject', function() {
  130. return assert(Promise.resolve(100)).lessThan(123);
  131. });
  132. it('waits for promised subject (with failure)', function() {
  133. return assert(Promise.resolve(123))
  134. .lessThan(100)
  135. .then(() => fail('should have failed'), function(e) {
  136. assertInstanceOf(AssertionError, e);
  137. assertEqual('123 < 100', e.message);
  138. });
  139. });
  140. });
  141. describe('closeTo', function() {
  142. it('accepts values within epislon of target', function() {
  143. assert(123).closeTo(123, 0);
  144. assert(123).closeTo(124, 1);
  145. assert(125).closeTo(124, 1);
  146. assertThrows(() => assert(123).closeTo(125, .1));
  147. assertThrows(() => assert(1./3).closeTo(.8, .01));
  148. });
  149. it('waits for promised values', function() {
  150. let p = new Promise(resolve => setTimeout(() => resolve(123), 10));
  151. return assert(p).closeTo(124, 1);
  152. });
  153. });
  154. describe('instanceOf', function() {
  155. it('works with direct instances', function() {
  156. assert(Error('foo')).instanceOf(Error);
  157. });
  158. it('works with sub-types', function() {
  159. assert(TypeError('foo')).instanceOf(Error);
  160. });
  161. it('parent types are not instances of sub-types', function() {
  162. assertThrows(() => assert(Error('foo')).instanceOf(TypeError));
  163. });
  164. });
  165. describe('isNull', function() {
  166. it('normal case', function() {
  167. assert(null).isNull();
  168. assertThrows(() => assert(1).isNull());
  169. });
  170. it('handles promised values', function() {
  171. let p = new Promise(function(f) {
  172. setTimeout(() => f(null), 10);
  173. });
  174. return assert(p).isNull();
  175. });
  176. it('does not match on undefined', function() {
  177. assertThrows(() => assert(void(0)).isNull());
  178. })
  179. });
  180. describe('isUndefined', function() {
  181. it('normal case', function() {
  182. assert(void(0)).isUndefined();
  183. assertThrows(() => assert(1).isUndefined());
  184. });
  185. it('handles promised values', function() {
  186. let p = new Promise(function(f) {
  187. setTimeout(() => f(void(0)), 10);
  188. });
  189. return assert(p).isUndefined();
  190. });
  191. it('does not match on null', function() {
  192. assertThrows(() => assert(null).isUndefined());
  193. })
  194. });
  195. describe('contains', function() {
  196. it('works with strings', function() {
  197. assert('abc').contains('a');
  198. assert('abc').contains('ab');
  199. assert('abc').contains('abc');
  200. assert('abc').contains('bc');
  201. assert('abc').contains('c');
  202. assertThrows(() => assert('abc').contains('d'));
  203. });
  204. it('works with arrays', function() {
  205. assert([1, 2, 3]).contains(1);
  206. assert([1, 2, 3]).contains(2);
  207. assert([1, 2, 3]).contains(3);
  208. assertThrows(() => assert([1, 2]).contains(3));
  209. });
  210. it('works with maps', function() {
  211. let m = new Map;
  212. m.set(1, 2);
  213. assert(m).contains(1);
  214. assertThrows(() => assert(m).contains(2));
  215. });
  216. it('works with sets', function() {
  217. let s = new Set;
  218. s.add(1);
  219. assert(s).contains(1);
  220. assertThrows(() => assert(s).contains(2));
  221. });
  222. it('requires an array, string, map, or set subject', function() {
  223. assertThrows(() => assert(123).contains('a'));
  224. });
  225. });
  226. describe('endsWith', function() {
  227. it('works', function() {
  228. assert('abc').endsWith('abc');
  229. assert('abc').endsWith('bc');
  230. assert('abc').endsWith('c');
  231. assertThrows(() => assert('abc').endsWith('d'));
  232. })
  233. });
  234. describe('startsWith', function() {
  235. it('works', function() {
  236. assert('abc').startsWith('abc');
  237. assert('abc').startsWith('ab');
  238. assert('abc').startsWith('a');
  239. assertThrows(() => assert('abc').startsWith('d'));
  240. })
  241. });
  242. describe('matches', function() {
  243. it('requires a regex value', function() {
  244. assertThrows(() => assert('abc').matches(1234));
  245. });
  246. it('requires a string value', function() {
  247. assertThrows(() => assert(1234).matches(/abc/));
  248. });
  249. it('requires a string value (promise case)', function() {
  250. return assert(Promise.resolve(1234))
  251. .matches(/abc/)
  252. .then(fail, function(error) {
  253. assertEqual(
  254. 'Expected a string matching /abc/, got <1234> (number)',
  255. error.message);
  256. });
  257. });
  258. it('applies regex', function() {
  259. assert('abc').matches(/abc/);
  260. assertThrows(() => assert('def').matches(/abc/));
  261. });
  262. });
  263. describe('isTrue', function() {
  264. it('only accepts booleans', function() {
  265. assertThrows(() => assert(123).isTrue());
  266. });
  267. it('accepts true values', function() {
  268. assert(true).isTrue();
  269. assert(Boolean('abc')).isTrue();
  270. return assert(Promise.resolve(true)).isTrue();
  271. });
  272. it('rejects false values', function() {
  273. assertThrows(() => assert(false).isTrue());
  274. assertThrows(() => assert(Boolean(0)).isTrue());
  275. return assert(Promise.resolve(false)).isTrue()
  276. .then(fail, function() {/*no-op, ok*/});
  277. });
  278. });
  279. describe('isFalse', function() {
  280. it('only accepts booleans', function() {
  281. assertThrows(() => assert(123).isFalse());
  282. })
  283. it('accepts false values', function() {
  284. assert(false).isFalse();
  285. assert(Boolean('')).isFalse();
  286. return assert(Promise.resolve(false)).isFalse();
  287. });
  288. it('rejects true values', function() {
  289. assertThrows(() => assert(true).isFalse());
  290. assertThrows(() => assert(Boolean(1)).isFalse());
  291. return assert(Promise.resolve(true)).isFalse()
  292. .then(fail, function() {/*no-op, ok*/});
  293. });
  294. });
  295. describe('isEqualTo', function() {
  296. it('is strict equality', function() {
  297. assert('abc').isEqualTo('abc');
  298. assert('abc').equals('abc');
  299. assert('abc').equalTo('abc');
  300. assertThrows(() => assert('1').isEqualTo(1));
  301. });
  302. });
  303. describe('notEqualTo', function() {
  304. it('tests strict equality', function() {
  305. assert('1').notEqualTo(1);
  306. assert(1).notEqualTo('1');
  307. assertThrows(() => assert('abc').notEqualTo('abc'));
  308. });
  309. });
  310. function assertInstanceOf(ctor, value) {
  311. assertTrue(value instanceof ctor);
  312. }
  313. });