Skip to content

Commit

Permalink
Merge pull request #42 from typst-community/40-feature-request-not-eq…
Browse files Browse the repository at this point in the history
…-assertion

Add negated equality assertions
  • Loading branch information
jamesrswift authored Jul 24, 2024
2 parents 94163df + d745795 commit b91422f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Added

- Added negated equality assertions for comparative and length. (#40)

## Changed


Expand Down
2 changes: 1 addition & 1 deletion src/assertions.typ
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "./assertions/length.typ" as length
#import "./assertions/comparative.typ": min, max, eq
#import "./assertions/comparative.typ": min, max, eq, neq
#import "./assertions/string.typ": *

/// Asserts that the given value is contained within the provided list. Useful for complicated enumeration types.
Expand Down
18 changes: 14 additions & 4 deletions src/assertions/comparative.typ
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@
}

/// Asserts that tested value is exactly equal to argument
#let eq(rhs) = {
assert-positive-type(rhs, types: (int,), name: "Equality")
#let eq(arg) = {
assert-positive-type(arg, types: (int,), name: "Equality")

return (
condition: (self, it) => it == rhs,
message: (self, it) => "Must be exactly " + str(rhs),
condition: (self, it) => it == arg,
message: (self, it) => "Must be exactly " + str(arg),
)
}

/// Asserts that tested value is not exactly equal to argument
#let neq(arg) = {
assert-positive-type(arg, types: (int,), name: "Equality")

return (
condition: (self, it) => it != arg,
message: (self, it) => "Must not equal " + str(arg),
)
}
18 changes: 14 additions & 4 deletions src/assertions/length.typ
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@
}

/// Asserts that tested value's length is exactly equal to argument
#let equals(rhs) = {
assert-positive-type(rhs, types: (int,), name: "Exact length")
#let equals(arg) = {
assert-positive-type(arg, types: (int,), name: "Exact length")

return (
condition: (self, it) => it.len() == rhs,
message: (self, it) => "Length must equal " + str(rhs),
condition: (self, it) => it.len() == arg,
message: (self, it) => "Length must equal " + str(arg),
)
}

/// Asserts that tested value's length is not equal to argument
#let neq(arg) = {
assert-positive-type(arg, types: (int,), name: "Exact length")

return (
condition: (self, it) => it.len() != rhs,
message: (self, it) => "Length must not equal " + str(rhs),
)
}

0 comments on commit b91422f

Please sign in to comment.