|
| 1 | +// Copyright © 2022, Canonical Ltd |
| 2 | +// |
| 3 | +// This program is free software; you can redistribute it and/or |
| 4 | +// modify it under the terms of the GNU Lesser General Public |
| 5 | +// License as published by the Free Software Foundation; either |
| 6 | +// version 2.1 of the License, or (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | +// Lesser General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU Lesser General Public |
| 14 | +// License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 15 | +// Authors: |
| 16 | +// Marco Trevisan <marco.trevisan@canonical.com> |
| 17 | + |
| 18 | +@import 'sass-utils'; |
| 19 | + |
| 20 | +@function assert($result, $expected: true) { |
| 21 | + @if $result != $expected { |
| 22 | + $result: if($result == null, 'null', $result); |
| 23 | + $expected: if($expected == null, 'null', $expected); |
| 24 | + @error "Assertion failed, expected '" + $expected + "', got '" + $result + "'"; |
| 25 | + } |
| 26 | + |
| 27 | + @return '' |
| 28 | +} |
| 29 | + |
| 30 | +@function test($name, $result, $expected: true) { |
| 31 | + @return 'Running test '+$name + assert($result, $expected); |
| 32 | +} |
| 33 | + |
| 34 | +@function run-test($function, $expected, $args...) { |
| 35 | + @return test($function, $function($args), $expected); |
| 36 | +} |
| 37 | + |
| 38 | +@debug test('str-contains Empty', str-contains('', '')); |
| 39 | +@debug test('str-contains Empty one', str-contains('', 'foo'), false); |
| 40 | +@debug test('str-contains Empty substring', str-contains('foo', ''), true); |
| 41 | +@debug test('str-contains Valid', str-contains('foo bar baz', 'bar'), true); |
| 42 | +@debug test('str-contains Missing', str-contains('foo bar', 'baz'), false); |
| 43 | + |
| 44 | +@debug test('str-starts-with Empty', str-starts-with('', '')); |
| 45 | +@debug test('str-starts-with Empty Both', str-starts-with('foo', '')); |
| 46 | +@debug test('str-starts-with Valid', str-starts-with('foobar', 'foo')); |
| 47 | +@debug test('str-starts-with Valid full', str-starts-with('foobar', 'foobar')); |
| 48 | +@debug test('str-starts-with Invalid', str-starts-with('barfoo', 'foo'), false); |
| 49 | + |
| 50 | +@debug test('str-ends-with Empty', str-ends-with('', '')); |
| 51 | +@debug test('str-ends-with Empty Both', str-ends-with('foo', ''), false); |
| 52 | +@debug test('str-ends-with Valid', str-ends-with('foobar', 'bar')); |
| 53 | +@debug test('str-ends-with Valid full', str-ends-with('foobar', 'foobar')); |
| 54 | +@debug test('str-ends-with Invalid', str-ends-with('foobar', 'foo'), false); |
| 55 | + |
| 56 | +@debug test('str-basename, Empty', str-basename(''), ''); |
| 57 | +@debug test('str-basename, Named', str-basename('foo'), 'foo'); |
| 58 | +@debug test('str-basename, Valid', str-basename('/foo/bar'), 'bar'); |
| 59 | +@debug test('str-basename, Valid', str-basename('/foo/bar/baz'), 'baz'); |
| 60 | +@debug test('str-basename, Valid', str-basename('proto:///foo/bar/baz'), 'baz'); |
| 61 | + |
| 62 | +@debug test('str-extension, Empty', str-extension(''), null); |
| 63 | +@debug test('str-extension, Named', str-extension('foo'), null); |
| 64 | +@debug test('str-extension, Valid', str-extension('/foo.bar'), 'bar'); |
| 65 | +@debug test('str-extension, Valid', str-extension('/foo/bar.baz'), 'baz'); |
| 66 | + |
| 67 | +@debug test('str-dirname, Empty', str-dirname(''), '.'); |
| 68 | +@debug test('str-dirname, Named', str-dirname('foo'), '.'); |
| 69 | +@debug test('str-dirname, Valid', str-dirname('/foo/bar'), '/foo'); |
| 70 | +@debug test('str-dirname, Valid', str-dirname('/foo/bar/baz'), '/foo/bar'); |
| 71 | +@debug test('str-dirname, Valid', str-dirname('proto:///foo/bar/baz'), 'proto:///foo/bar'); |
| 72 | + |
| 73 | +@debug test('list-length, Empty', list-length([]), 0); |
| 74 | +@debug test('list-length, One', list-length([1]), 1); |
| 75 | +@debug test('list-length, Two', list-length([1, '2']), 2); |
| 76 | + |
| 77 | +@debug test('list-nth, Empty', list-nth([], 0), null); |
| 78 | +@debug test('list-nth, Empty', list-nth([], 10), null); |
| 79 | +@debug test('list-nth, Empty', list-nth([], -10), null); |
| 80 | +@debug test('list-nth, One, valid', list-nth([1], 0), 1); |
| 81 | +@debug test('list-nth, One, valid negative', list-nth([1], -1), 1); |
| 82 | +@debug test('list-nth, One, invalid', list-nth([1], 3), null); |
| 83 | +@debug test('list-nth, One, invalid negative', list-nth([1], -3), null); |
| 84 | +@debug test('list-nth, Three, valid', list-nth([1, '2', false], 0), 1); |
| 85 | +@debug test('list-nth, Three, valid negative', list-nth([1, '2', false], -1), false); |
| 86 | +@debug test('list-nth, Three, valid', list-nth([1, '2', false], 1), '2'); |
| 87 | +@debug test('list-nth, Three, valid negative', list-nth([1, '2', false], -2), '2'); |
| 88 | +@debug test('list-nth, Three, valid', list-nth([1, '2', false], 2), false); |
| 89 | +@debug test('list-nth, Three, valid negative', list-nth([1, '2', false], -3), 1); |
| 90 | + |
| 91 | +@debug test('list-index, Empty', list-index([], 0), null); |
| 92 | +@debug test('list-index, Empty', list-index([], 10), null); |
| 93 | +@debug test('list-index, Empty', list-index([], -10), null); |
| 94 | +@debug test('list-index, One, valid', list-index([1], 1), 0); |
| 95 | +@debug test('list-index, One, invalid', list-index([1], 3), null); |
| 96 | +@debug test('list-index, Three, valid', list-index([1, '2', false], 1), 0); |
| 97 | +@debug test('list-index, Three, valid', list-index([1, '2', false], '2'), 1); |
| 98 | +@debug test('list-index, Three, valid', list-index([1, '2', false], false), 2); |
0 commit comments