diff options
Diffstat (limited to 'library/src/compute/foundations.rs')
| -rw-r--r-- | library/src/compute/foundations.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/library/src/compute/foundations.rs b/library/src/compute/foundations.rs index bad9f8ab..5127fca3 100644 --- a/library/src/compute/foundations.rs +++ b/library/src/compute/foundations.rs @@ -88,6 +88,9 @@ pub fn panic( /// Fails with an error if the condition is not fulfilled. Does not /// produce any output in the document. /// +/// If you wish to test equality between two values, see +/// [`assert.eq`]($func/assert.eq) and [`assert.ne`]($func/assert.ne). +/// /// ## Example /// ```typ /// #assert(1 < 2, message: "math broke") @@ -97,6 +100,11 @@ pub fn panic( /// Category: foundations /// Returns: #[func] +#[scope( + scope.define("eq", assert_eq); + scope.define("ne", assert_ne); + scope +)] pub fn assert( /// The condition that must be true for the assertion to pass. condition: bool, @@ -115,6 +123,90 @@ pub fn assert( Value::None } +/// Ensure that two values are equal. +/// +/// Fails with an error if the first value is not equal to the second. Does not +/// produce any output in the document. +/// +/// ## Example +/// ```example +/// #assert.eq(10, 10) +/// ``` +/// +/// Display: Assert Equals +/// Category: foundations +/// Returns: +#[func] +pub fn assert_eq( + /// The first value to compare. + left: Value, + + /// The second value to compare. + right: Value, + + /// An optional message to display on error instead of the representations + /// of the compared values. + #[named] + #[default] + message: Option<EcoString>, +) -> Value { + if left != right { + if let Some(message) = message { + bail!(args.span, "equality assertion failed: {}", message); + } else { + bail!( + args.span, + "equality assertion failed: value {:?} was not equal to {:?}", + left, + right + ); + } + } + Value::None +} + +/// Ensure that two values are not equal. +/// +/// Fails with an error if the first value is equal to the second. Does not +/// produce any output in the document. +/// +/// ## Example +/// ```example +/// #assert.ne(3, 4) +/// ``` +/// +/// Display: Assert Not Equals +/// Category: foundations +/// Returns: +#[func] +pub fn assert_ne( + /// The first value to compare. + left: Value, + + /// The second value to compare. + right: Value, + + /// An optional message to display on error instead of the representations + /// of the compared values. + #[named] + #[default] + message: Option<EcoString>, +) -> Value { + if left == right { + if let Some(message) = message { + bail!(args.span, "inequality assertion failed: {}", message); + } else { + bail!( + args.span, + "inequality assertion failed: value {:?} was equal to {:?}", + left, + right + ); + } + } + Value::None +} + /// Evaluate a string as Typst code. /// /// This function should only be used as a last resort. |
