| 1 | let { test } = require('uvu') |
| 2 | let { equal, is, throws } = require('uvu/assert') |
| 3 | |
| 4 | let utils = require('../lib/utils') |
| 5 | |
| 6 | test('raises an error', () => { |
| 7 | throws(() => { |
| 8 | utils.error('A') |
| 9 | }, 'A') |
| 10 | }) |
| 11 | |
| 12 | test('marks an error', () => { |
| 13 | let error = null |
| 14 | try { |
| 15 | utils.error('A') |
| 16 | } catch (e) { |
| 17 | error = e |
| 18 | } |
| 19 | |
| 20 | is(error.autoprefixer, true) |
| 21 | }) |
| 22 | |
| 23 | test('filters doubles in array', () => { |
| 24 | equal(utils.uniq(['1', '1', '2', '3', '3']), ['1', '2', '3']) |
| 25 | }) |
| 26 | |
| 27 | test('removes note', () => { |
| 28 | equal(utils.removeNote('-webkit- note'), '-webkit-') |
| 29 | equal(utils.removeNote('-webkit-'), '-webkit-') |
| 30 | }) |
| 31 | |
| 32 | test('escapes RegExp symbols', () => { |
| 33 | equal(utils.escapeRegexp('^[()\\]'), '\\^\\[\\(\\)\\\\\\]') |
| 34 | }) |
| 35 | |
| 36 | test('generates RegExp that finds tokens in CSS values', () => { |
| 37 | let regexp = utils.regexp('foo') |
| 38 | function check(string) { |
| 39 | return string.match(regexp) !== null |
| 40 | } |
| 41 | |
| 42 | is(check('foo'), true) |
| 43 | is(check('Foo'), true) |
| 44 | is(check('one, foo, two'), true) |
| 45 | is(check('one(),foo(),two()'), true) |
| 46 | |
| 47 | equal('foo(), a, foo'.replace(regexp, '$1b$2'), 'bfoo(), a, bfoo') |
| 48 | |
| 49 | is(check('foob'), false) |
| 50 | is(check('(foo)'), false) |
| 51 | is(check('-a-foo'), false) |
| 52 | }) |
| 53 | |
| 54 | test('escapes string if needed', () => { |
| 55 | let regexp = utils.regexp('(a|b)') |
| 56 | function check(string) { |
| 57 | return string.match(regexp) !== null |
| 58 | } |
| 59 | |
| 60 | is(check('a'), false) |
| 61 | is(check('(a|b)'), true) |
| 62 | |
| 63 | regexp = utils.regexp('(a|b)', false) |
| 64 | is(check('a'), true) |
| 65 | is(check('b'), true) |
| 66 | }) |
| 67 | |
| 68 | test('does save without changes', () => { |
| 69 | let list = utils.editList('a,\nb, c', parsed => parsed) |
| 70 | equal(list, 'a,\nb, c') |
| 71 | }) |
| 72 | |
| 73 | test('changes list', () => { |
| 74 | let list = utils.editList('a, b', (parsed, edit) => { |
| 75 | equal(parsed, ['a', 'b']) |
| 76 | equal(edit, []) |
| 77 | return ['1', '2'] |
| 78 | }) |
| 79 | equal(list, '1, 2') |
| 80 | }) |
| 81 | |
| 82 | test('saves comma', () => { |
| 83 | let list = utils.editList('a,\nb', () => ['1', '2']) |
| 84 | equal(list, '1,\n2') |
| 85 | }) |
| 86 | |
| 87 | test('parse one value', () => { |
| 88 | let list = utils.editList('1', parsed => [parsed[0], '2']) |
| 89 | equal(list, '1, 2') |
| 90 | }) |
| 91 | |
| 92 | test('splits simple selectors into an array', () => { |
| 93 | let arr1 = utils.splitSelector('#foo.bar') |
| 94 | let arr2 = utils.splitSelector('.foo, .bar') |
| 95 | equal(arr1, [[['#foo', '.bar']]]) |
| 96 | equal(arr2, [[['.foo']], [['.bar']]]) |
| 97 | }) |
| 98 | |
| 99 | test('splits complex selectors into an array', () => { |
| 100 | let arr = utils.splitSelector( |
| 101 | '#foo.bar .child-one.mod .child-two.mod, .baz, .hello' |
| 102 | ) |
| 103 | equal(arr, [ |
| 104 | [ |
| 105 | ['#foo', '.bar'], |
| 106 | ['.child-one', '.mod'], |
| 107 | ['.child-two', '.mod'] |
| 108 | ], |
| 109 | [['.baz']], |
| 110 | [['.hello']] |
| 111 | ]) |
| 112 | }) |
| 113 | |
| 114 | test('detects numbers', () => { |
| 115 | equal(utils.isPureNumber(42), true) |
| 116 | equal(utils.isPureNumber('42'), true) |
| 117 | equal(utils.isPureNumber('autoprefixer'), false) |
| 118 | equal(utils.isPureNumber(''), false) |
| 119 | equal(utils.isPureNumber({}), false) |
| 120 | equal(utils.isPureNumber(undefined), false) |
| 121 | equal(utils.isPureNumber(true), false) |
| 122 | }) |
| 123 | |
| 124 | test.run() |