match.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. 'use strict';
  2. var tape = require('../');
  3. var tap = require('tap');
  4. var concat = require('concat-stream');
  5. var stripFullStack = require('./common').stripFullStack;
  6. tap.test('match', function (tt) {
  7. tt.plan(1);
  8. var test = tape.createHarness({ exit: false });
  9. var tc = function (rows) {
  10. tt.same(stripFullStack(rows.toString('utf8')), [
  11. 'TAP version 13',
  12. '# match',
  13. 'not ok 1 The "regexp" argument must be an instance of RegExp. Received type string (\'string\')',
  14. ' ---',
  15. ' operator: match',
  16. ' expected: \'[object RegExp]\'',
  17. ' actual: \'[object String]\'',
  18. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  19. ' stack: |-',
  20. ' Error: The "regexp" argument must be an instance of RegExp. Received type string (\'string\')',
  21. ' [... stack stripped ...]',
  22. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  23. ' [... stack stripped ...]',
  24. ' ...',
  25. 'not ok 2 regex arg must not be a string',
  26. ' ---',
  27. ' operator: match',
  28. ' expected: \'[object RegExp]\'',
  29. ' actual: \'[object String]\'',
  30. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  31. ' stack: |-',
  32. ' Error: regex arg must not be a string',
  33. ' [... stack stripped ...]',
  34. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  35. ' [... stack stripped ...]',
  36. ' ...',
  37. 'not ok 3 The "string" argument must be of type string. Received type object ({ abc: 123 })',
  38. ' ---',
  39. ' operator: match',
  40. ' expected: \'string\'',
  41. ' actual: \'object\'',
  42. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  43. ' stack: |-',
  44. ' Error: The "string" argument must be of type string. Received type object ({ abc: 123 })',
  45. ' [... stack stripped ...]',
  46. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  47. ' [... stack stripped ...]',
  48. ' ...',
  49. 'not ok 4 string arg must not be an object',
  50. ' ---',
  51. ' operator: match',
  52. ' expected: \'string\'',
  53. ' actual: \'object\'',
  54. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  55. ' stack: |-',
  56. ' Error: string arg must not be an object',
  57. ' [... stack stripped ...]',
  58. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  59. ' [... stack stripped ...]',
  60. ' ...',
  61. 'not ok 5 The input did not match the regular expression /abc/. Input: \'string\'',
  62. ' ---',
  63. ' operator: match',
  64. ' expected: /abc/',
  65. ' actual: \'string\'',
  66. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  67. ' stack: |-',
  68. ' Error: The input did not match the regular expression /abc/. Input: \'string\'',
  69. ' [... stack stripped ...]',
  70. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  71. ' [... stack stripped ...]',
  72. ' ...',
  73. 'not ok 6 "string" does not match /abc/',
  74. ' ---',
  75. ' operator: match',
  76. ' expected: /abc/',
  77. ' actual: \'string\'',
  78. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  79. ' stack: |-',
  80. ' Error: "string" does not match /abc/',
  81. ' [... stack stripped ...]',
  82. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  83. ' [... stack stripped ...]',
  84. ' ...',
  85. 'ok 7 The input matched the regular expression /pass$/. Input: \'I will pass\'',
  86. 'ok 8 "I will pass" matches /pass$/',
  87. '',
  88. '1..8',
  89. '# tests 8',
  90. '# pass 2',
  91. '# fail 6',
  92. ''
  93. ]);
  94. };
  95. test.createStream().pipe(concat(tc));
  96. test('match', function (t) {
  97. t.plan(8);
  98. t.match(/abc/, 'string');
  99. t.match(/abc/, 'string', 'regex arg must not be a string');
  100. t.match({ abc: 123 }, /abc/);
  101. t.match({ abc: 123 }, /abc/, 'string arg must not be an object');
  102. t.match('string', /abc/);
  103. t.match('string', /abc/, '"string" does not match /abc/');
  104. t.match('I will pass', /pass$/);
  105. t.match('I will pass', /pass$/, '"I will pass" matches /pass$/');
  106. t.end();
  107. });
  108. });
  109. tap.test('doesNotMatch', function (tt) {
  110. tt.plan(1);
  111. var test = tape.createHarness({ exit: false });
  112. var tc = function (rows) {
  113. tt.same(stripFullStack(rows.toString('utf8')), [
  114. 'TAP version 13',
  115. '# doesNotMatch',
  116. 'not ok 1 The "regexp" argument must be an instance of RegExp. Received type string (\'string\')',
  117. ' ---',
  118. ' operator: doesNotMatch',
  119. ' expected: \'[object RegExp]\'',
  120. ' actual: \'[object String]\'',
  121. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  122. ' stack: |-',
  123. ' Error: The "regexp" argument must be an instance of RegExp. Received type string (\'string\')',
  124. ' [... stack stripped ...]',
  125. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  126. ' [... stack stripped ...]',
  127. ' ...',
  128. 'not ok 2 regex arg must not be a string',
  129. ' ---',
  130. ' operator: doesNotMatch',
  131. ' expected: \'[object RegExp]\'',
  132. ' actual: \'[object String]\'',
  133. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  134. ' stack: |-',
  135. ' Error: regex arg must not be a string',
  136. ' [... stack stripped ...]',
  137. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  138. ' [... stack stripped ...]',
  139. ' ...',
  140. 'not ok 3 The "string" argument must be of type string. Received type object ({ abc: 123 })',
  141. ' ---',
  142. ' operator: doesNotMatch',
  143. ' expected: \'string\'',
  144. ' actual: \'object\'',
  145. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  146. ' stack: |-',
  147. ' Error: The "string" argument must be of type string. Received type object ({ abc: 123 })',
  148. ' [... stack stripped ...]',
  149. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  150. ' [... stack stripped ...]',
  151. ' ...',
  152. 'not ok 4 string arg must not be an object',
  153. ' ---',
  154. ' operator: doesNotMatch',
  155. ' expected: \'string\'',
  156. ' actual: \'object\'',
  157. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  158. ' stack: |-',
  159. ' Error: string arg must not be an object',
  160. ' [... stack stripped ...]',
  161. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  162. ' [... stack stripped ...]',
  163. ' ...',
  164. 'not ok 5 The input was expected to not match the regular expression /string/. Input: \'string\'',
  165. ' ---',
  166. ' operator: doesNotMatch',
  167. ' expected: /string/',
  168. ' actual: \'string\'',
  169. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  170. ' stack: |-',
  171. ' Error: The input was expected to not match the regular expression /string/. Input: \'string\'',
  172. ' [... stack stripped ...]',
  173. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  174. ' [... stack stripped ...]',
  175. ' ...',
  176. 'not ok 6 "string" should not match /string/',
  177. ' ---',
  178. ' operator: doesNotMatch',
  179. ' expected: /string/',
  180. ' actual: \'string\'',
  181. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  182. ' stack: |-',
  183. ' Error: "string" should not match /string/',
  184. ' [... stack stripped ...]',
  185. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  186. ' [... stack stripped ...]',
  187. ' ...',
  188. 'not ok 7 The input was expected to not match the regular expression /pass$/. Input: \'I will pass\'',
  189. ' ---',
  190. ' operator: doesNotMatch',
  191. ' expected: /pass$/',
  192. ' actual: \'I will pass\'',
  193. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  194. ' stack: |-',
  195. ' Error: The input was expected to not match the regular expression /pass$/. Input: \'I will pass\'',
  196. ' [... stack stripped ...]',
  197. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  198. ' [... stack stripped ...]',
  199. ' ...',
  200. 'not ok 8 "I will pass" should not match /pass$/',
  201. ' ---',
  202. ' operator: doesNotMatch',
  203. ' expected: /pass$/',
  204. ' actual: \'I will pass\'',
  205. ' at: Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  206. ' stack: |-',
  207. ' Error: "I will pass" should not match /pass$/',
  208. ' [... stack stripped ...]',
  209. ' at Test.<anonymous> ($TEST/match.js:$LINE:$COL)',
  210. ' [... stack stripped ...]',
  211. ' ...',
  212. 'ok 9 The input did not match the regular expression /pass$/. Input: \'I will fail\'',
  213. 'ok 10 "I will fail" does not match /pass$/',
  214. '',
  215. '1..10',
  216. '# tests 10',
  217. '# pass 2',
  218. '# fail 8',
  219. ''
  220. ]);
  221. };
  222. test.createStream().pipe(concat(tc));
  223. test('doesNotMatch', function (t) {
  224. t.plan(10);
  225. t.doesNotMatch(/abc/, 'string');
  226. t.doesNotMatch(/abc/, 'string', 'regex arg must not be a string');
  227. t.doesNotMatch({ abc: 123 }, /abc/);
  228. t.doesNotMatch({ abc: 123 }, /abc/, 'string arg must not be an object');
  229. t.doesNotMatch('string', /string/);
  230. t.doesNotMatch('string', /string/, '"string" should not match /string/');
  231. t.doesNotMatch('I will pass', /pass$/);
  232. t.doesNotMatch('I will pass', /pass$/, '"I will pass" should not match /pass$/');
  233. t.doesNotMatch('I will fail', /pass$/);
  234. t.doesNotMatch('I will fail', /pass$/, '"I will fail" does not match /pass$/');
  235. t.end();
  236. });
  237. });