1 | xemplar = require '../'
|
2 | should = require 'should'
|
3 | require 'mocha'
|
4 |
|
5 | tests =
|
6 | match: (regex, {sample, expected, idx}) ->
|
7 | throw "Missing sample: #{regex}" unless typeof sample is 'string'
|
8 | throw "Missing expected: #{regex}" unless expected?
|
9 | (done) ->
|
10 | res = sample.match regex
|
11 | if res?
|
12 | res = res[idx or 0] unless Array.isArray expected
|
13 | res.should.eql expected
|
14 | else
|
15 | expected.should.eql res
|
16 | done()
|
17 |
|
18 | exec: (regex, {sample, expected, idx}) ->
|
19 | throw "Missing sample: #{regex}" unless typeof sample is 'string'
|
20 | throw "Missing expected: #{regex}" unless expected?
|
21 | (done) ->
|
22 | res = regex.exec sample
|
23 | if res?
|
24 | res = (match for match in res)
|
25 | res = res[idx or 0] unless Array.isArray expected
|
26 | res.should.eql expected
|
27 | else
|
28 | expected.should.eql res
|
29 | done()
|
30 |
|
31 | test: (regex, {shouldntMatch, shouldMatch}) ->
|
32 | throw "Missing shouldntMatch: #{regex}" unless shouldntMatch?
|
33 | throw "Missing shouldMatch: #{regex}" unless shouldMatch?
|
34 | (done) ->
|
35 | m.should.not.match regex for m in shouldntMatch
|
36 | m.should.match regex for m in shouldMatch
|
37 | done()
|
38 |
|
39 | split: (regex, {sample, expected}) ->
|
40 | throw "Missing sample: #{regex}" unless typeof sample is 'string'
|
41 | throw "Missing expected: #{regex}" unless expected?
|
42 | (done) ->
|
43 | expected.should.eql sample.split regex
|
44 | done()
|
45 |
|
46 | replace: (regex, {sample, replaceWith, expected}) ->
|
47 | throw "Missing sample: #{regex}" unless typeof sample is 'string'
|
48 | throw "Missing replaceWith: #{regex}" unless typeof replaceWith is 'string'
|
49 | throw "Missing expected: #{regex}" unless expected?
|
50 | (done) ->
|
51 | expected.should.eql sample.replace regex, replaceWith
|
52 | done()
|
53 |
|
54 | runTest = (exp) -> ->
|
55 | throw "Invalid RegExp: #{regex}" unless exp?
|
56 | throw "Missing test: #{regex}" unless exp.test?
|
57 | it "should #{type} properly", tests[type] exp.clone(test.flags), test for type, test of exp.test
|
58 |
|
59 | findTests = (obj) -> ->
|
60 | for k,v of obj
|
61 | do (k,v) ->
|
62 | describe k, (if v instanceof RegExp and v.test then runTest(v) else findTests(v))
|
63 | return
|
64 |
|
65 | findTests(xemplar)() |
\ | No newline at end of file |