|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { parseCssVars } from '../parser-vbind-m' |
| 3 | + |
| 4 | +describe('analyze css vbind', () => { |
| 5 | + test('Should be able to parse to extract the v-bind-m value', () => { |
| 6 | + const source = ` |
| 7 | + .test { |
| 8 | + color: v-bind-m(color); |
| 9 | + } |
| 10 | + ` |
| 11 | + const expected = ['color'] |
| 12 | + expect(parseCssVars([source])).toMatchObject(expected) |
| 13 | + }) |
| 14 | + |
| 15 | + test('Should be able to parse single quoted values', () => { |
| 16 | + const source = ` |
| 17 | + .test { |
| 18 | + color: v-bind-m('color'); |
| 19 | + } |
| 20 | + ` |
| 21 | + const expected = ['color'] |
| 22 | + expect(parseCssVars([source])).toMatchObject(expected) |
| 23 | + }) |
| 24 | + |
| 25 | + test('Should be able to parse double quoted values', () => { |
| 26 | + const source = ` |
| 27 | + .test { |
| 28 | + color: v-bind-m("color"); |
| 29 | + } |
| 30 | + ` |
| 31 | + const expected = ['color'] |
| 32 | + expect(parseCssVars([source])).toMatchObject(expected) |
| 33 | + }) |
| 34 | + |
| 35 | + test('Should be able to parse the value of the template string', () => { |
| 36 | + const source = ` |
| 37 | + .test { |
| 38 | + color: v-bind-m(\`\${v}\`); |
| 39 | + background-image: v-bind-m('\`url('\${bgUrl}')\`'); |
| 40 | + } |
| 41 | + ` |
| 42 | + |
| 43 | + const expected = ['`${v}`', '`url(\'${bgUrl}\')`'] |
| 44 | + expect(parseCssVars([source])).toMatchObject(expected) |
| 45 | + }) |
| 46 | + |
| 47 | + test('Should be able to parse extract v-bind-m values in nested', () => { |
| 48 | + const source = ` |
| 49 | + .parent { |
| 50 | + .child { |
| 51 | + color: v-bind-m(color); |
| 52 | + } |
| 53 | + } |
| 54 | + ` |
| 55 | + const expected = ['color'] |
| 56 | + expect(parseCssVars([source])).toMatchObject(expected) |
| 57 | + }) |
| 58 | + |
| 59 | + test('Should be able to parse extract v-bind-m values when ignoring single line comments', () => { |
| 60 | + const source = ` |
| 61 | + .test { |
| 62 | + color: v-bind-m(color); // this is a comment |
| 63 | + } |
| 64 | + ` |
| 65 | + const expected = ['color'] |
| 66 | + expect(parseCssVars([source])).toMatchObject(expected) |
| 67 | + }) |
| 68 | + |
| 69 | + test('Should be able to parse extract v-bind-m values when ignoring multi-line comments', () => { |
| 70 | + const source = ` |
| 71 | + .test { |
| 72 | + color: v-bind-m(color); /* this is a |
| 73 | + multi-line |
| 74 | + comment */ |
| 75 | + } |
| 76 | + ` |
| 77 | + const expected = ['color'] |
| 78 | + expect(parseCssVars([source])).toMatchObject(expected) |
| 79 | + }) |
| 80 | + |
| 81 | + test('Should be able to extract multiple v-bind-m values in analysis', () => { |
| 82 | + const source = ` |
| 83 | + .test { |
| 84 | + color: v-bind-m(color1); |
| 85 | + background-color: v-bind-m(color2); |
| 86 | + } |
| 87 | + ` |
| 88 | + const expected = ['color1', 'color2'] |
| 89 | + expect(parseCssVars([source])).toMatchObject(expected) |
| 90 | + }) |
| 91 | + |
| 92 | + test('Should only analyze to extract unique values', () => { |
| 93 | + const source = ` |
| 94 | + .test { |
| 95 | + color: v-bind-m(color1); |
| 96 | + background-color: v-bind-m(color1); |
| 97 | + } |
| 98 | + ` |
| 99 | + const expected = ['color1'] |
| 100 | + expect(parseCssVars([source])).toMatchObject(expected) |
| 101 | + }) |
| 102 | + |
| 103 | + test('Should be able to parse to extract values inside nested parentheses', () => { |
| 104 | + const source = ` |
| 105 | + .test { |
| 106 | + color: v-bind-m(((color1))); |
| 107 | + } |
| 108 | + ` |
| 109 | + const expected = ['((color1))'] |
| 110 | + expect(parseCssVars([source])).toMatchObject(expected) |
| 111 | + }) |
| 112 | + |
| 113 | + test('Should be able to parse to extract values template string', () => { |
| 114 | + const source = '.test{ color: v-bind-m(`${v}`);\n background-image: v-bind-m("`url(\'${bgUrl}\')`");}' |
| 115 | + const expected = ['`${v}`', "`url('${bgUrl}')`"] |
| 116 | + expect(parseCssVars([source])).toMatchObject(expected) |
| 117 | + }) |
| 118 | + |
| 119 | + test('the right parenthesis is missing', () => { |
| 120 | + const source = ` |
| 121 | + .test { |
| 122 | + v-bind-m(color1; |
| 123 | + } |
| 124 | + ` |
| 125 | + expect(parseCssVars([source])).toMatchObject([]) |
| 126 | + }) |
| 127 | + |
| 128 | + test('the left parenthesis is missing', () => { |
| 129 | + const source = ` |
| 130 | + .test { |
| 131 | + v-bind-m color1); |
| 132 | + } |
| 133 | + ` |
| 134 | + expect(parseCssVars([source])).toMatchObject([]) |
| 135 | + }) |
| 136 | + |
| 137 | + test('should be able to parse incomplete expressions', () => { |
| 138 | + const source = ` |
| 139 | + .test { |
| 140 | + font-weight: v-bind-m("count.toString("); |
| 141 | + font-weight: v-bind-m(xxx); |
| 142 | + } |
| 143 | + ` |
| 144 | + expect(parseCssVars([source])).toMatchObject(['count.toString(', 'xxx']) |
| 145 | + }) |
| 146 | +}) |
0 commit comments