diff options
Diffstat (limited to 'tests/assert')
-rw-r--r-- | tests/assert/config.nims | 1 | ||||
-rw-r--r-- | tests/assert/panicoverride.nim | 15 | ||||
-rw-r--r-- | tests/assert/t21195.nim | 6 | ||||
-rw-r--r-- | tests/assert/tassert.nim | 5 | ||||
-rw-r--r-- | tests/assert/tassert2.nim | 111 | ||||
-rw-r--r-- | tests/assert/tassert_c.nim | 40 | ||||
-rw-r--r-- | tests/assert/tfailedassert.nim | 51 | ||||
-rw-r--r-- | tests/assert/tuserassert.nim | 2 |
8 files changed, 175 insertions, 56 deletions
diff --git a/tests/assert/config.nims b/tests/assert/config.nims new file mode 100644 index 000000000..f1cffeb6c --- /dev/null +++ b/tests/assert/config.nims @@ -0,0 +1 @@ +--excessiveStackTrace:on diff --git a/tests/assert/panicoverride.nim b/tests/assert/panicoverride.nim new file mode 100644 index 000000000..53ad64215 --- /dev/null +++ b/tests/assert/panicoverride.nim @@ -0,0 +1,15 @@ +# panicoverride.nim + +proc printf(fmt: cstring) {.varargs, importc, header:"stdio.h".} +proc exit(code: cint) {.importc, header:"stdlib.h".} + +{.push stack_trace: off, profiler:off.} + +proc rawoutput(s: cstring) = + printf("RAW: %s\n", s) + +proc panic(s: cstring) {.noreturn.} = + printf("PANIC: %s\n", s) + exit(0) + +{.pop.} \ No newline at end of file diff --git a/tests/assert/t21195.nim b/tests/assert/t21195.nim new file mode 100644 index 000000000..b690d22a0 --- /dev/null +++ b/tests/assert/t21195.nim @@ -0,0 +1,6 @@ +discard """ + matrix: "--verbosity:0 --os:standalone --mm:none" +""" +# bug #21195 +var n = 11 +assert(n == 12) diff --git a/tests/assert/tassert.nim b/tests/assert/tassert.nim index b3df30bd1..a14fec317 100644 --- a/tests/assert/tassert.nim +++ b/tests/assert/tassert.nim @@ -1,5 +1,4 @@ discard """ - file: "tassert.nim" outputsub: "assertion failure!this shall be always written" exitcode: "1" """ @@ -11,7 +10,7 @@ proc callC() = callA() try: callC() -except EAssertionFailed: +except AssertionDefect: write(stdout, "assertion failure!") except: write(stdout, "unknown exception!") @@ -19,5 +18,3 @@ finally: system.write(stdout, "this shall be always written") assert(false) #OUT assertion failure!this shall be always written - - diff --git a/tests/assert/tassert2.nim b/tests/assert/tassert2.nim new file mode 100644 index 000000000..e32ab72e1 --- /dev/null +++ b/tests/assert/tassert2.nim @@ -0,0 +1,111 @@ +discard """ + output: ''' +`false` first assertion from bar +`false` second assertion from bar +-1 +''' +""" +from strutils import endsWith + +type + TLineInfo = tuple[filename: string, line: int, column: int] + TMyError = object of Exception + lineinfo: TLineInfo + EMyError = ref TMyError + + +# NOTE: when entering newlines, adjust `expectedEnd` outputs + +try: + doAssert(false, "msg1") # doAssert test +except AssertionDefect as e: + assert e.msg.endsWith "tassert2.nim(20, 11) `false` msg1" + +try: + assert false # assert test with no msg +except AssertionDefect as e: + assert e.msg.endsWith "tassert2.nim(25, 3) `false` " + +try: + let a = 1 + doAssert(a+a==1) # assert test with Ast expression + # BUG: const folding would make "1+1==1" appear as `false` in + # assert message +except AssertionDefect as e: + assert e.msg.endsWith "`a + a == 1` " + +try: + let a = 1 + doAssert a+a==1 # ditto with `doAssert` and no parens +except AssertionDefect as e: + assert e.msg.endsWith "`a + a == 1` " + +proc fooStatic() = + # protect against https://github.com/nim-lang/Nim/issues/8758 + static: doAssert(true) +fooStatic() + + + + + +block: + # scope-wide policy to change the failed assert + # exception type in order to include a lineinfo + onFailedAssert(msg): + var e = new(TMyError) + e.msg = msg + e.lineinfo = instantiationInfo(-2) + raise e + + proc foo = + assert(false, "assertion from foo") + + + proc bar: int = + # local overrides that are active only in this proc + onFailedAssert(msg): + echo msg[^32..^1] + + assert(false, "first assertion from bar") + + onFailedAssert(msg): + echo msg[^33..^1] + return -1 + + assert(false, "second assertion from bar") + return 10 + + echo(bar()) + + try: + foo() + except: + let e = EMyError(getCurrentException()) + assert e.msg.endsWith "tassert2.nim(62, 11) `false` assertion from foo" + +block: ## checks for issue https://github.com/nim-lang/Nim/issues/8518 + template fun(a: string): string = + const pattern = a & a + pattern + + try: + doAssert fun("foo1") == fun("foo2"), "mymsg" + except AssertionDefect as e: + # used to expand out the template instantiaiton, sometimes filling hundreds of lines + assert e.msg.endsWith "" + +block: ## checks for issue https://github.com/nim-lang/Nim/issues/9301 + try: + doAssert 1 + 1 == 3 + except AssertionDefect as e: + # used to const fold as false + assert e.msg.endsWith "tassert2.nim(100, 5) `1 + 1 == 3` " + +block: ## checks AST isn't transformed as it used to + let a = 1 + try: + doAssert a > 1 + except AssertionDefect as e: + # used to rewrite as `1 < a` + assert e.msg.endsWith "tassert2.nim(108, 5) `a > 1` " diff --git a/tests/assert/tassert_c.nim b/tests/assert/tassert_c.nim new file mode 100644 index 000000000..e3e3b8147 --- /dev/null +++ b/tests/assert/tassert_c.nim @@ -0,0 +1,40 @@ +discard """ + matrix: "-d:nimPreviewSlimSystem --stackTrace:on --excessiveStackTrace:off" + output: '''true''' +""" +import std/assertions +const expected = """ +tassert_c.nim(35) tassert_c +tassert_c.nim(34) foo +assertions.nim(*) failedAssertImpl +assertions.nim(*) raiseAssert +""" + +proc tmatch(x, p: string): bool = + var i = 0 + var k = 0 + while i < p.len: + if p[i] == '*': + let oldk = k + while k < x.len and x[k] in {'0'..'9'}: inc k + # no digit skipped? + if oldk == k: return false + inc i + elif k < x.len and p[i] == x[k]: + inc i + inc k + else: + return false + while k < x.len and x[k] in {' ', '\L', '\C'}: inc k + result = i >= p.len and k >= x.len + + +try: + proc foo() = + assert(false) + foo() +except AssertionDefect: + let e = getCurrentException() + let trace = e.getStackTrace + if tmatch(trace, expected): echo true + else: echo "wrong trace:\n" & trace diff --git a/tests/assert/tfailedassert.nim b/tests/assert/tfailedassert.nim deleted file mode 100644 index 1e6764471..000000000 --- a/tests/assert/tfailedassert.nim +++ /dev/null @@ -1,51 +0,0 @@ -discard """ - output: ''' -WARNING: false first assertion from bar -ERROR: false second assertion from bar --1 -tests/assert/tfailedassert.nim:27 false assertion from foo -''' -""" - -type - TLineInfo = tuple[filename: string, line: int] - - TMyError = object of Exception - lineinfo: TLineInfo - - EMyError = ref TMyError - -# module-wide policy to change the failed assert -# exception type in order to include a lineinfo -onFailedAssert(msg): - var e = new(TMyError) - e.msg = msg - e.lineinfo = instantiationInfo(-2) - raise e - -proc foo = - assert(false, "assertion from foo") - -proc bar: int = - # local overrides that are active only - # in this proc - onFailedAssert(msg): echo "WARNING: " & msg - - assert(false, "first assertion from bar") - - onFailedAssert(msg): - echo "ERROR: " & msg - return -1 - - assert(false, "second assertion from bar") - return 10 - -echo("") -echo(bar()) - -try: - foo() -except: - let e = EMyError(getCurrentException()) - echo e.lineinfo.filename, ":", e.lineinfo.line, " ", e.msg - diff --git a/tests/assert/tuserassert.nim b/tests/assert/tuserassert.nim index 7bb0a7fc0..274c78921 100644 --- a/tests/assert/tuserassert.nim +++ b/tests/assert/tuserassert.nim @@ -2,7 +2,7 @@ discard """ output: "x == 45ugh" """ -template myAssert(cond: expr) = +template myAssert(cond: untyped) = when 3 <= 3: let c = cond.astToStr if not cond: |