|
| 1 | +// |
| 2 | +// Copyright 2025 Google LLC |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | + |
| 6 | +@use 'true' as test; |
| 7 | + |
| 8 | +// go/keep-sorted start by_regex='(.+) prefix_order=sass: |
| 9 | +@use 'sass:string'; |
| 10 | +@use 'assert'; |
| 11 | +@use 'throw'; |
| 12 | +// go/keep-sorted end |
| 13 | + |
| 14 | +@include test.describe('assert') { |
| 15 | + // Value types |
| 16 | + $number: 1; |
| 17 | + $string: 'a-string'; |
| 18 | + $color: red; |
| 19 | + $bool: true; |
| 20 | + $null: null; |
| 21 | + $list: ('list', 'of', 'values'); |
| 22 | + $map: ( |
| 23 | + 'map': 'value', |
| 24 | + ); |
| 25 | + |
| 26 | + @include test.describe('is-type()') { |
| 27 | + @include test.it('returns the argument when it matches a single type') { |
| 28 | + @include test.assert-equal(assert.is-type($number, 'number'), $number); |
| 29 | + @include test.assert-equal(assert.is-type($string, 'string'), $string); |
| 30 | + @include test.assert-equal(assert.is-type($bool, 'bool'), $bool); |
| 31 | + @include test.assert-equal(assert.is-type($null, 'null'), $null); |
| 32 | + @include test.assert-equal(assert.is-type($list, 'list'), $list); |
| 33 | + @include test.assert-equal(assert.is-type($map, 'map'), $map); |
| 34 | + } |
| 35 | + |
| 36 | + @include test.it( |
| 37 | + 'returns the argument when it matches one of multiple types' |
| 38 | + ) { |
| 39 | + @include test.assert-equal( |
| 40 | + assert.is-type($number, 'number|string'), |
| 41 | + $number |
| 42 | + ); |
| 43 | + @include test.assert-equal( |
| 44 | + assert.is-type($string, 'number|string'), |
| 45 | + $string |
| 46 | + ); |
| 47 | + @include test.assert-equal(assert.is-type($null, 'list|map|null'), $null); |
| 48 | + @include test.assert-equal(assert.is-type($list, 'list|map|null'), $list); |
| 49 | + @include test.assert-equal(assert.is-type($map, 'list|map|null'), $map); |
| 50 | + } |
| 51 | + |
| 52 | + @include test.it('throws an error when it does not match the type') { |
| 53 | + @include test.assert-true( |
| 54 | + throw.get-error(assert.is-type($number, 'string')), |
| 55 | + 'number should not match "string" type' |
| 56 | + ); |
| 57 | + @include test.assert-true( |
| 58 | + throw.get-error(assert.is-type($string, 'number')), |
| 59 | + 'string should not match "number" type' |
| 60 | + ); |
| 61 | + @include test.assert-true( |
| 62 | + throw.get-error(assert.is-type($null, 'list|map')), |
| 63 | + 'null should not match "list|map" type' |
| 64 | + ); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @include test.describe('not-type()') { |
| 69 | + @include test.it( |
| 70 | + 'returns the argument when it does not match a single type' |
| 71 | + ) { |
| 72 | + @include test.assert-equal(assert.not-type($number, 'string'), $number); |
| 73 | + @include test.assert-equal(assert.not-type($string, 'number'), $string); |
| 74 | + @include test.assert-equal(assert.not-type($bool, 'string'), $bool); |
| 75 | + @include test.assert-equal(assert.not-type($null, 'string'), $null); |
| 76 | + @include test.assert-equal(assert.not-type($list, 'string'), $list); |
| 77 | + @include test.assert-equal(assert.not-type($map, 'string'), $map); |
| 78 | + } |
| 79 | + |
| 80 | + @include test.it( |
| 81 | + 'returns the argument when it does not match one of multiple types' |
| 82 | + ) { |
| 83 | + @include test.assert-equal( |
| 84 | + assert.not-type($number, 'string|map'), |
| 85 | + $number |
| 86 | + ); |
| 87 | + @include test.assert-equal( |
| 88 | + assert.not-type($string, 'number|map'), |
| 89 | + $string |
| 90 | + ); |
| 91 | + @include test.assert-equal(assert.not-type($null, 'list|map'), $null); |
| 92 | + } |
| 93 | + |
| 94 | + @include test.it('throws an error when it matches the type') { |
| 95 | + @include test.assert-true( |
| 96 | + throw.get-error(assert.not-type($number, 'number')), |
| 97 | + 'number should match "number" type and throw' |
| 98 | + ); |
| 99 | + @include test.assert-true( |
| 100 | + throw.get-error(assert.not-type($string, 'string')), |
| 101 | + 'string should match "string" type and throw' |
| 102 | + ); |
| 103 | + @include test.assert-true( |
| 104 | + throw.get-error(assert.not-type($null, 'null')), |
| 105 | + 'null should match "null" type and throw' |
| 106 | + ); |
| 107 | + @include test.assert-true( |
| 108 | + throw.get-error(assert.not-type($number, 'number|string')), |
| 109 | + 'number should match "number|string" type and throw' |
| 110 | + ); |
| 111 | + @include test.assert-true( |
| 112 | + throw.get-error(assert.not-type($string, 'number|string')), |
| 113 | + 'string should match "number|string" type and throw' |
| 114 | + ); |
| 115 | + @include test.assert-true( |
| 116 | + throw.get-error(assert.not-type($null, 'list|map|null')), |
| 117 | + 'null should match "list|map|null" type and throw' |
| 118 | + ); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments