|
| 1 | +// |
| 2 | +// Copyright 2025 Google LLC |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | + |
| 6 | +// Utilities for `sass-true` errors, to support testing error behavior. |
| 7 | + |
| 8 | +// go/keep-sorted start by_regex='(.+) prefix_order=sass: |
| 9 | +@use 'sass:meta'; |
| 10 | +@use 'sass:string'; |
| 11 | +// go/keep-sorted end |
| 12 | + |
| 13 | +@forward 'true' show error; |
| 14 | + |
| 15 | +/// Returns null if none of the given values are error strings, or is returns |
| 16 | +/// an error string if a value has an error. |
| 17 | +/// |
| 18 | +/// This is used to support testing error behavior with `sass-true`, since |
| 19 | +/// `@error` messages cannot be caught at build time. |
| 20 | +/// |
| 21 | +/// @example scss |
| 22 | +/// // A function that may return an "ERROR:" string in a test. |
| 23 | +/// @function get-value($map, $key) { |
| 24 | +/// @if meta.type-of($map) != 'map' { |
| 25 | +/// // Identical to `@error 'ERROR: Arg is not a map'` outside of tests. |
| 26 | +/// @return throw.error('Arg is not a map'); |
| 27 | +/// } |
| 28 | +/// @return map.get($map, $key); |
| 29 | +/// } |
| 30 | +/// |
| 31 | +/// // A function that needs to handle potential errors from other functions. |
| 32 | +/// @function mix-primary-on-surface($values) { |
| 33 | +/// $primary: get-value($values, 'primary'); |
| 34 | +/// $surface: get-value($values, 'surface'); |
| 35 | +/// $error: throw.get-error($primary, $secondary); |
| 36 | +/// @if $error { |
| 37 | +/// // Return early to guard logic against additional errors since |
| 38 | +/// // $primary or $secondary may be a string instead of a color. |
| 39 | +/// @return $error; |
| 40 | +/// } |
| 41 | +/// |
| 42 | +/// @return color.mix($primary, $surface, 10%); |
| 43 | +/// } |
| 44 | +/// |
| 45 | +/// Note: `throw.error()` and `throw.get-error()` are only useful when testing |
| 46 | +/// error behavior using `sass-true`. If you are not testing a function, use |
| 47 | +/// `@error` instead. |
| 48 | +/// |
| 49 | +/// @example scss |
| 50 | +/// // In a `sass-true` test, `throw.get-error()` can be used to assert that |
| 51 | +/// // an error is thrown. |
| 52 | +/// @use 'true' as test with ($catch-errors: true); |
| 53 | +/// |
| 54 | +/// @include test.describe('module.get-value()') { |
| 55 | +/// @include test.it('throws an error if the value is not a map') { |
| 56 | +/// $result: module.get-value('not a map', 'primary'); |
| 57 | +/// @include test.assert-truthy(throw.get-error($result), '$result is an error'); |
| 58 | +/// } |
| 59 | +/// } |
| 60 | +/// |
| 61 | +/// @param {*} $error - The value to check. |
| 62 | +/// @param {list} $errors - Additional values to check. Useful for checking |
| 63 | +/// multiple errors at the same time. |
| 64 | +/// @return {string|null} The error string if any value is an error, or null |
| 65 | +/// otherwise. |
| 66 | +@function get-error($error, $errors...) { |
| 67 | + @if _is-error($error) { |
| 68 | + @return $error; |
| 69 | + } |
| 70 | + |
| 71 | + @each $additional-error in $errors { |
| 72 | + @if _is-error($additional-error) { |
| 73 | + @return $additional-error; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @return false; |
| 78 | +} |
| 79 | + |
| 80 | +@function _is-error($error) { |
| 81 | + @return (meta.type-of($error) == 'string') and |
| 82 | + (string.index($error, 'ERROR') == 1); |
| 83 | +} |
0 commit comments