|
| 1 | +const { highlight } = require('./index') |
| 2 | + |
| 3 | +const OPTIONS = { |
| 4 | + colors: { |
| 5 | + keyword: '[keyword]', |
| 6 | + function: '[function]', |
| 7 | + number: '[number]', |
| 8 | + string: '[string]', |
| 9 | + special: '[special]', |
| 10 | + bracket: '[bracket]', |
| 11 | + clear: '[clear]' |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +const hlUni = query => highlight(query, OPTIONS) |
| 16 | + |
| 17 | +const hlHtml = query => highlight(query, { |
| 18 | + ...OPTIONS, |
| 19 | + html: true |
| 20 | +}) |
| 21 | + |
| 22 | +describe('unicode', () => { |
| 23 | + it('strings (single quotes)', () => { |
| 24 | + expect(hlUni("'Hello, world!'")) |
| 25 | + .toBe("[string]'Hello, world!'[clear]") |
| 26 | + }) |
| 27 | + |
| 28 | + it('strings (double quotes)', () => { |
| 29 | + expect(hlUni('"Hello, world!"')) |
| 30 | + .toBe('[string]"Hello, world!"[clear]') |
| 31 | + }) |
| 32 | + |
| 33 | + it('integers', () => { |
| 34 | + expect(hlUni('42')) |
| 35 | + .toBe('[number]42[clear]') |
| 36 | + }) |
| 37 | + |
| 38 | + it('keywords (uppercase)', () => { |
| 39 | + expect(hlUni('SELECT')) |
| 40 | + .toBe('[keyword]SELECT[clear]') |
| 41 | + }) |
| 42 | + |
| 43 | + it('keywords (lowercase)', () => { |
| 44 | + expect(hlUni('select')) |
| 45 | + .toBe('[keyword]select[clear]') |
| 46 | + }) |
| 47 | + |
| 48 | + it('special characters', () => { |
| 49 | + expect(hlUni('=')) |
| 50 | + .toBe('[special]=[clear]') |
| 51 | + }) |
| 52 | + |
| 53 | + it('brackets', () => { |
| 54 | + expect(hlUni('(')) |
| 55 | + .toBe('[bracket]([clear]') |
| 56 | + }) |
| 57 | + |
| 58 | + it('numbers within strings', () => { |
| 59 | + expect(hlUni("'34'")) |
| 60 | + .toBe("[string]'34'[clear]") |
| 61 | + }) |
| 62 | + |
| 63 | + it('functions', () => { |
| 64 | + expect(hlUni('COUNT(`id`)')) |
| 65 | + .toBe('[function]COUNT[clear][bracket]([clear][string]`id`[clear][bracket])[clear]') |
| 66 | + }) |
| 67 | + |
| 68 | + it('basic query', () => { |
| 69 | + expect(hlUni("SELECT * FROM `users` WHERE `email` = '[email protected]'")) |
| 70 | + .toBe("[keyword]SELECT[clear] [special]*[clear][keyword] FROM[clear] [string]`users`[clear][keyword] WHERE[clear] [string]`email`[clear] [special]=[clear] [string]'[email protected]'[clear]") |
| 71 | + }) |
| 72 | + |
| 73 | + it('complex query', () => { |
| 74 | + expect(hlUni("SELECT COUNT(id), `id`, `username` FROM `users` WHERE `email` = '[email protected]' AND `foo` = 'BAR' OR 1=1")) |
| 75 | + .toBe("[keyword]SELECT[clear] [function]COUNT[clear][bracket]([clear]id[bracket])[clear][special],[clear] [string]`id`[clear][special],[clear] [string]`username`[clear][keyword] FROM[clear] [string]`users`[clear][keyword] WHERE[clear] [string]`email`[clear] [special]=[clear] [string]'[email protected]'[clear][keyword] AND[clear] [string]`foo`[clear] [special]=[clear] [string]'BAR'[clear][keyword] OR[clear] [number]1[clear][special]=[clear][number]1[clear]") |
| 76 | + }) |
| 77 | +}) |
| 78 | + |
| 79 | +describe('html', () => { |
| 80 | + it('strings (single quotes)', () => { |
| 81 | + expect(hlHtml("'Hello, world!'")) |
| 82 | + .toBe('<span class="sql-hl-string">\'Hello, world!\'</span>') |
| 83 | + }) |
| 84 | + |
| 85 | + it('strings (double quotes)', () => { |
| 86 | + expect(hlHtml('"Hello, world!"')) |
| 87 | + .toBe('<span class="sql-hl-string">"Hello, world!"</span>') |
| 88 | + }) |
| 89 | + |
| 90 | + it('integers', () => { |
| 91 | + expect(hlHtml('42')) |
| 92 | + .toBe('<span class="sql-hl-number">42</span>') |
| 93 | + }) |
| 94 | + |
| 95 | + it('keywords (uppercase)', () => { |
| 96 | + expect(hlHtml('SELECT')) |
| 97 | + .toBe('<span class="sql-hl-keyword">SELECT</span>') |
| 98 | + }) |
| 99 | + |
| 100 | + it('keywords (lowercase)', () => { |
| 101 | + expect(hlHtml('select')) |
| 102 | + .toBe('<span class="sql-hl-keyword">select</span>') |
| 103 | + }) |
| 104 | + |
| 105 | + it('special characters', () => { |
| 106 | + expect(hlHtml('=')) |
| 107 | + .toBe('<span class="sql-hl-special">=</span>') |
| 108 | + }) |
| 109 | + |
| 110 | + it('brackets', () => { |
| 111 | + expect(hlHtml('(')) |
| 112 | + .toBe('<span class="sql-hl-bracket">(</span>') |
| 113 | + }) |
| 114 | + |
| 115 | + it('numbers within strings', () => { |
| 116 | + expect(hlHtml("'34'")) |
| 117 | + .toBe("<span class=\"sql-hl-string\">'34'</span>") |
| 118 | + }) |
| 119 | + |
| 120 | + it('functions', () => { |
| 121 | + expect(hlHtml('COUNT(`id`)')) |
| 122 | + .toBe('<span class="sql-hl-function">COUNT</span><span class="sql-hl-bracket">(</span><span class="sql-hl-string">`id`</span><span class="sql-hl-bracket">)</span>') |
| 123 | + }) |
| 124 | + |
| 125 | + it('basic query', () => { |
| 126 | + expect(hlHtml("SELECT * FROM `users` WHERE `email` = '[email protected]'")) |
| 127 | + .toBe("<span class=\"sql-hl-keyword\">SELECT</span> <span class=\"sql-hl-special\">*</span><span class=\"sql-hl-keyword\"> FROM</span> <span class=\"sql-hl-string\">`users`</span><span class=\"sql-hl-keyword\"> WHERE</span> <span class=\"sql-hl-string\">`email`</span> <span class=\"sql-hl-special\">=</span> <span class=\"sql-hl-string\">'[email protected]'</span>") |
| 128 | + }) |
| 129 | + |
| 130 | + it('complex query', () => { |
| 131 | + expect(hlHtml("SELECT COUNT(id), `id`, `username` FROM `users` WHERE `email` = '[email protected]' AND `foo` = 'BAR' OR 1=1")) |
| 132 | + .toBe("<span class=\"sql-hl-keyword\">SELECT</span> <span class=\"sql-hl-function\">COUNT</span><span class=\"sql-hl-bracket\">(</span>id<span class=\"sql-hl-bracket\">)</span><span class=\"sql-hl-special\">,</span> <span class=\"sql-hl-string\">`id`</span><span class=\"sql-hl-special\">,</span> <span class=\"sql-hl-string\">`username`</span><span class=\"sql-hl-keyword\"> FROM</span> <span class=\"sql-hl-string\">`users`</span><span class=\"sql-hl-keyword\"> WHERE</span> <span class=\"sql-hl-string\">`email`</span> <span class=\"sql-hl-special\">=</span> <span class=\"sql-hl-string\">'[email protected]'</span><span class=\"sql-hl-keyword\"> AND</span> <span class=\"sql-hl-string\">`foo`</span> <span class=\"sql-hl-special\">=</span> <span class=\"sql-hl-string\">'BAR'</span><span class=\"sql-hl-keyword\"> OR</span> <span class=\"sql-hl-number\">1</span><span class=\"sql-hl-special\">=</span><span class=\"sql-hl-number\">1</span>") |
| 133 | + }) |
| 134 | +}) |
0 commit comments