Skip to content

Commit

Permalink
Add assert(val, pattern, Exception)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyash-b committed Jun 24, 2024
1 parent b666e68 commit 48cf43f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Add `code(Type)`
* Add `flat_map(Eachable1, Fun)`
* Add `COR` - Chain of Responsibility
* Add `assert(val, pattern, Exception)`

### Fixes and improvements
* Fix `AtPath` for non-eachables
Expand Down
2 changes: 1 addition & 1 deletion lib/autoload/globals/AbstractProcess.ngs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ ns {

F compute_status(p:Process) {
guard not(p.sub)
assert(p, {'status': Not(Status::Unset)}, 'Status of a leaf Process can not be Unset')
assert(p, {'status': Not(Status::Unset)}, InvalidArgument('Status of a leaf Process can not be Unset'))
}

global inspect
Expand Down
2 changes: 1 addition & 1 deletion lib/autoload/globals/COR.ngs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ ns {
}

F init(e:Event) {
assert(e, AnyOf(IncomingEvent, OutgoingEvent), 'COR::Event must be either IncomingEvent or OutgoingEvent')
assert(e, AnyOf(IncomingEvent, OutgoingEvent), InvalidArgument('COR::Event must be either IncomingEvent or OutgoingEvent'))
}


Expand Down
26 changes: 19 additions & 7 deletions lib/stdlib.ngs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,17 @@ F assert(val, pattern, msg:Str="Assertion failed") {
val
}

doc Throws e with given message if val does not match the pattern
doc %RET - val
F assert(val, pattern, e:Exception) {
not(val =~ pattern) throws e::{
A.dflt('cause', { AssertFail("Assertion failed").set(val=val, pattern=pattern) })
}
val
}

TEST type MyExc(Exception); { assert(1, Str, MyExc("blah")) }.assert(MyExc)

doc Throws AssertFail with given message if val is falsy
doc val - if evaluates to false, causes AssertFail exception
doc msg - message for the exception, defaults to "Assertion failed"
Expand Down Expand Up @@ -1118,7 +1129,7 @@ section "=~ Matching" {
pat_v is IfExists
}
} else {
assert(pat_v is not IfExists, 'IfExists value only works with keys of types: Str, Int, Real')
assert(pat_v, Not(IfExists), NotImplemented('IfExists value only works with keys of types: Str, Int, Real'))
if x_keys is Null {
x_keys = x.Hash().keys()
}
Expand Down Expand Up @@ -3016,7 +3027,7 @@ F flatten(e:Eachable1) {
guard not(e is Int)
collector
e.each(F(x) {
assert(x is Eachable1 and x is not Int, 'flatten() elements must be Eachable1, excluding Int')
assert(x, AllOf(Eachable1, Not(Int)), InvalidArgument('flatten() elements must be Eachable1, excluding Int'))
x.each(collect)
})
}
Expand Down Expand Up @@ -4723,7 +4734,7 @@ section "Strings and characters" {
doc %EX - Str("x", 3) # 'x '
doc %EX - Str("x",-3) # ' x'
F Str(s:Str, target_width:Int, ch:Str=' ') {
assert(len(ch) == 1)
assert(len(ch), 1, InvalidArgument("ch must be an Str of length 1"))
l = s.len()
pad_on_left = target_width < 0
if pad_on_left {
Expand Down Expand Up @@ -5695,8 +5706,7 @@ section "Path" {

doc Creates temporary file using `mktemp` and arranges deletion of the file upon exit from the NGS script.
F init(t:TmpFsObj, cleanup_policy:CleanupPolicy=RemoveCleanupPolicy()) {
assert(t, {'path': Str}, 'init(TmpFsObj, ...) expects path to be set and to be a string')
assert(t.path, 'init(TmpFsObj, ...) expects path to be non-empty string')
assert(t, {'path': AllOf(Str, Bool.constructors)}, InvalidArgument('init(TmpFsObj, ...) expects path to be non-empty string'))
t.fd = null
t.cleanup_policy = cleanup_policy
debug("file", "Created ${t.Type().name} at ${t.path}")
Expand Down Expand Up @@ -6449,7 +6459,7 @@ F Argv(h:Hash) {
}

if k is Repeat {
assert(k, {'pattern': Str, 'count': NumRange }, 'Argv: Repeat() must repeat a string and count must be NumRange')
assert(k, {'pattern': Str, 'count': NumRange }, InvalidArgument('Argv: Repeat() must repeat a string and count must be NumRange'))
assert(v.len(), k.count, "Switch ${k.pattern} has ${v.len()} values, out of specified range ${k.count}")
v.each(F(elt) {
collect(k.pattern)
Expand Down Expand Up @@ -6523,7 +6533,7 @@ F '$()'(p:Process) {
debug("process", "Parsed command: ${argv}")

if log_opt = p.command.options.get('log') {
assert(log_opt, AnyOf(true, Str), 'log: option must be either true or a string')
assert(log_opt, AnyOf(true, Str), InvalidArgument('log: option must be either true or a string'))
F redir_str(r:CommandRedir) {
(r.fd or '').Str() + r.marker + safe_escape_bash(r.datum)
}
Expand Down Expand Up @@ -8223,6 +8233,8 @@ section "retry() and friends" {
val
}

# TODO: retry_assert(val, pattern, Exception) # needs design

doc Similar to assert(). Caught and ignored by nearest retry() on all but last retry.
doc %STATUS - experimental
F retry_assert(val, msg:Str="Assertion failed") {
Expand Down

0 comments on commit 48cf43f

Please sign in to comment.