|
| 1 | +import { expect, it, describe } from 'vitest' |
| 2 | + |
| 3 | +import { |
| 4 | + anyOf, |
| 5 | + char, |
| 6 | + exactly, |
| 7 | + charIn, |
| 8 | + not, |
| 9 | + maybe, |
| 10 | + oneOrMore, |
| 11 | + word, |
| 12 | + digit, |
| 13 | + whitespace, |
| 14 | + letter, |
| 15 | + tab, |
| 16 | + linefeed, |
| 17 | + carriageReturn, |
| 18 | + charNotIn, |
| 19 | +} from '../src/core/inputs' |
| 20 | + |
| 21 | +describe('inputs', () => { |
| 22 | + it('charIn', () => { |
| 23 | + const input = charIn('foo') |
| 24 | + expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[foo\\]/') |
| 25 | + }) |
| 26 | + it('charNotIn', () => { |
| 27 | + const input = charNotIn('foo') |
| 28 | + expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[\\^foo\\]/') |
| 29 | + }) |
| 30 | + it('anyOf', () => { |
| 31 | + const values = ['foo', 'bar', 'baz'] |
| 32 | + const input = anyOf(...values) |
| 33 | + const regexp = new RegExp(input as any) |
| 34 | + expect(regexp).toMatchInlineSnapshot('/\\(foo\\|bar\\|baz\\)/') |
| 35 | + for (const value of values) { |
| 36 | + expect(regexp.test(value)).toBeTruthy() |
| 37 | + } |
| 38 | + expect(regexp.test('qux')).toBeFalsy() |
| 39 | + }) |
| 40 | + it('char', () => { |
| 41 | + const input = char |
| 42 | + expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\./') |
| 43 | + }) |
| 44 | + it('maybe', () => { |
| 45 | + const input = maybe('foo') |
| 46 | + const regexp = new RegExp(input as any) |
| 47 | + expect(regexp).toMatchInlineSnapshot('/\\(foo\\)\\?/') |
| 48 | + }) |
| 49 | + it('oneOrMore', () => { |
| 50 | + const input = oneOrMore('foo') |
| 51 | + const regexp = new RegExp(input as any) |
| 52 | + expect(regexp).toMatchInlineSnapshot('/\\(foo\\)\\+/') |
| 53 | + }) |
| 54 | + it('exactly', () => { |
| 55 | + const input = exactly('fo?[a-z]{2}/o?') |
| 56 | + expect(new RegExp(input as any)).toMatchInlineSnapshot( |
| 57 | + '/fo\\\\\\?\\\\\\[a-z\\\\\\]\\\\\\{2\\\\\\}\\\\/o\\\\\\?/' |
| 58 | + ) |
| 59 | + }) |
| 60 | + it('word', () => { |
| 61 | + const input = word |
| 62 | + expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\\\w/') |
| 63 | + }) |
| 64 | + it('digit', () => { |
| 65 | + const input = digit |
| 66 | + expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\\\d/') |
| 67 | + }) |
| 68 | + it('whitespace', () => { |
| 69 | + const input = whitespace |
| 70 | + expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\\\s/') |
| 71 | + }) |
| 72 | + it('letter', () => { |
| 73 | + const input = letter |
| 74 | + expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[a-zA-Z\\]/') |
| 75 | + }) |
| 76 | + it('tab', () => { |
| 77 | + const input = tab |
| 78 | + expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\\\t/') |
| 79 | + }) |
| 80 | + it('linefeed', () => { |
| 81 | + const input = linefeed |
| 82 | + expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\\\n/') |
| 83 | + }) |
| 84 | + it('carriageReturn', () => { |
| 85 | + const input = carriageReturn |
| 86 | + expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\\\r/') |
| 87 | + }) |
| 88 | + it('not', () => { |
| 89 | + expect(not.word.toString()).toMatchInlineSnapshot('"\\\\W"') |
| 90 | + expect(not.digit.toString()).toMatchInlineSnapshot('"\\\\D"') |
| 91 | + expect(not.whitespace.toString()).toMatchInlineSnapshot('"\\\\S"') |
| 92 | + expect(not.letter.toString()).toMatchInlineSnapshot('"[^a-zA-Z]"') |
| 93 | + expect(not.tab.toString()).toMatchInlineSnapshot('"[^\\\\t]"') |
| 94 | + expect(not.linefeed.toString()).toMatchInlineSnapshot('"[^\\\\n]"') |
| 95 | + expect(not.carriageReturn.toString()).toMatchInlineSnapshot('"[^\\\\r]"') |
| 96 | + }) |
| 97 | +}) |
| 98 | + |
| 99 | +describe('chained inputs', () => { |
| 100 | + const input = exactly('?') |
| 101 | + it('and', () => { |
| 102 | + const val = input.and('test') |
| 103 | + const regexp = new RegExp(val as any) |
| 104 | + expect(regexp).toMatchInlineSnapshot('/\\\\\\?test/') |
| 105 | + }) |
| 106 | + it('or', () => { |
| 107 | + const val = input.or('test') |
| 108 | + const regexp = new RegExp(val as any) |
| 109 | + expect(regexp).toMatchInlineSnapshot('/\\(\\\\\\?\\|test\\)/') |
| 110 | + }) |
| 111 | + it('after', () => { |
| 112 | + const val = input.after('test') |
| 113 | + const regexp = new RegExp(val as any) |
| 114 | + expect(regexp).toMatchInlineSnapshot('/\\(\\?<=test\\)\\\\\\?/') |
| 115 | + }) |
| 116 | + it('before', () => { |
| 117 | + const val = input.before('test') |
| 118 | + const regexp = new RegExp(val as any) |
| 119 | + expect(regexp).toMatchInlineSnapshot('/\\\\\\?\\(\\?=test\\)/') |
| 120 | + }) |
| 121 | + it('notAfter', () => { |
| 122 | + const val = input.notAfter('test') |
| 123 | + const regexp = new RegExp(val as any) |
| 124 | + expect(regexp).toMatchInlineSnapshot('/\\(\\?<!test\\)\\\\\\?/') |
| 125 | + }) |
| 126 | + it('notBefore', () => { |
| 127 | + const val = input.notBefore('test') |
| 128 | + const regexp = new RegExp(val as any) |
| 129 | + expect(regexp).toMatchInlineSnapshot('/\\\\\\?\\(\\?!test\\)/') |
| 130 | + }) |
| 131 | + it('times', () => { |
| 132 | + const val = input.times(500) |
| 133 | + const regexp = new RegExp(val as any) |
| 134 | + expect(regexp).toMatchInlineSnapshot('/\\(\\\\\\?\\)\\{500\\}/') |
| 135 | + }) |
| 136 | + it('times.any', () => { |
| 137 | + const val = input.times.any() |
| 138 | + const regexp = new RegExp(val as any) |
| 139 | + expect(regexp).toMatchInlineSnapshot('/\\(\\\\\\?\\)\\*/') |
| 140 | + }) |
| 141 | + it('times.atLeast', () => { |
| 142 | + const val = input.times.atLeast(2) |
| 143 | + const regexp = new RegExp(val as any) |
| 144 | + expect(regexp).toMatchInlineSnapshot('/\\(\\\\\\?\\)\\{2,\\}/') |
| 145 | + }) |
| 146 | + it('times.between', () => { |
| 147 | + const val = input.times.between(3, 5) |
| 148 | + const regexp = new RegExp(val as any) |
| 149 | + expect(regexp).toMatchInlineSnapshot('/\\(\\\\\\?\\)\\{3,5\\}/') |
| 150 | + }) |
| 151 | + it('optionally', () => { |
| 152 | + const val = input.optionally() |
| 153 | + const regexp = new RegExp(val as any) |
| 154 | + expect(regexp).toMatchInlineSnapshot('/\\(\\\\\\?\\)\\?/') |
| 155 | + }) |
| 156 | + it('as', () => { |
| 157 | + const val = input.as('test') |
| 158 | + const regexp = new RegExp(val as any) |
| 159 | + expect(regexp).toMatchInlineSnapshot('/\\(\\?<test>\\\\\\?\\)/') |
| 160 | + }) |
| 161 | + it('at.lineStart', () => { |
| 162 | + const val = input.at.lineStart() |
| 163 | + const regexp = new RegExp(val as any) |
| 164 | + expect(regexp).toMatchInlineSnapshot('/\\^\\\\\\?/') |
| 165 | + }) |
| 166 | + it('at.lineEnd', () => { |
| 167 | + const val = input.at.lineEnd() |
| 168 | + const regexp = new RegExp(val as any) |
| 169 | + expect(regexp).toMatchInlineSnapshot('/\\\\\\?\\$/') |
| 170 | + }) |
| 171 | +}) |
0 commit comments