diff options
Diffstat (limited to 'tests/exception')
-rw-r--r-- | tests/exception/m21261.nim | 1 | ||||
-rw-r--r-- | tests/exception/m22469.nim | 4 | ||||
-rw-r--r-- | tests/exception/t13115.nim | 31 | ||||
-rw-r--r-- | tests/exception/t18620.nim | 17 | ||||
-rw-r--r-- | tests/exception/t20613.nim | 8 | ||||
-rw-r--r-- | tests/exception/t21261.nim | 9 | ||||
-rw-r--r-- | tests/exception/t22008.nim | 8 | ||||
-rw-r--r-- | tests/exception/t22469.nim | 16 | ||||
-rw-r--r-- | tests/exception/t9657.nim | 7 | ||||
-rw-r--r-- | tests/exception/tcpp_imported_exc.nim | 30 | ||||
-rw-r--r-- | tests/exception/tcpp_imported_exc2.nim | 10 | ||||
-rw-r--r-- | tests/exception/texcas.nim | 5 | ||||
-rw-r--r-- | tests/exception/texception_inference.nim | 32 | ||||
-rw-r--r-- | tests/exception/texceptionbreak.nim | 8 | ||||
-rw-r--r-- | tests/exception/texceptions.nim | 13 | ||||
-rw-r--r-- | tests/exception/texceptions2.nim | 130 | ||||
-rw-r--r-- | tests/exception/tindexerrorformatbounds.nim | 31 | ||||
-rw-r--r-- | tests/exception/tsetexceptions.nim | 11 | ||||
-rw-r--r-- | tests/exception/twrongexc.nim | 2 |
19 files changed, 314 insertions, 59 deletions
diff --git a/tests/exception/m21261.nim b/tests/exception/m21261.nim new file mode 100644 index 000000000..11b12fb5b --- /dev/null +++ b/tests/exception/m21261.nim @@ -0,0 +1 @@ +raise (ref Exception)(msg: "something") \ No newline at end of file diff --git a/tests/exception/m22469.nim b/tests/exception/m22469.nim new file mode 100644 index 000000000..201698701 --- /dev/null +++ b/tests/exception/m22469.nim @@ -0,0 +1,4 @@ +# ModuleB +echo "First top-level statement of ModuleB" +echo high(int) + 1 +echo "ModuleB last statement" \ No newline at end of file diff --git a/tests/exception/t13115.nim b/tests/exception/t13115.nim new file mode 100644 index 000000000..5db8f9107 --- /dev/null +++ b/tests/exception/t13115.nim @@ -0,0 +1,31 @@ +const msg = "This char is `" & '\0' & "` and works fine!" + +when defined nim_t13115: + # bug #13115 + template fn = + raise newException(Exception, msg) + when defined nim_t13115_static: + static: fn() + fn() +else: + import std/[osproc,strformat,os,strutils] + proc main = + const nim = getCurrentCompilerExe() + const file = currentSourcePath + for b in "c js cpp".split: + # save CI time by avoiding mostly redundant combinations as far as this bug is concerned + var opts = case b + of "c": @["", "-d:nim_t13115_static", "-d:danger", "-d:debug"] + of "js": @["", "-d:nim_t13115_static"] + else: @[""] + + for opt in opts: + let cmd = fmt"{nim} r -b:{b} -d:nim_t13115 {opt} --hints:off {file}" + let (outp, exitCode) = execCmdEx(cmd) + when defined windows: + # `\0` not preserved on windows + doAssert "` and works fine!" in outp, cmd & "\n" & msg + else: + doAssert msg in outp, cmd & "\n" & msg + doAssert exitCode == 1 + main() diff --git a/tests/exception/t18620.nim b/tests/exception/t18620.nim new file mode 100644 index 000000000..ee23f8bac --- /dev/null +++ b/tests/exception/t18620.nim @@ -0,0 +1,17 @@ +discard """ + matrix: "--gc:arc; --gc:refc" +""" + +proc hello() = + raise newException(ValueError, "You are wrong") + +var flag = false + +try: + hello() +except ValueError as e: + flag = true + doAssert len(getStackTraceEntries(e)) > 0 + doAssert len(getStackTraceEntries(e)) > 0 + +doAssert flag diff --git a/tests/exception/t20613.nim b/tests/exception/t20613.nim new file mode 100644 index 000000000..6edb69415 --- /dev/null +++ b/tests/exception/t20613.nim @@ -0,0 +1,8 @@ +discard """ + matrix: "; --panics:on" +""" + +func test = + if 0 > 10: + raiseAssert "hey" +test() diff --git a/tests/exception/t21261.nim b/tests/exception/t21261.nim new file mode 100644 index 000000000..84817d854 --- /dev/null +++ b/tests/exception/t21261.nim @@ -0,0 +1,9 @@ +discard """ + exitcode: 1 + outputsub: ''' +m21261.nim(1) m21261 +Error: unhandled exception: something [Exception] +''' +""" + +import m21261 \ No newline at end of file diff --git a/tests/exception/t22008.nim b/tests/exception/t22008.nim new file mode 100644 index 000000000..c0758e7b4 --- /dev/null +++ b/tests/exception/t22008.nim @@ -0,0 +1,8 @@ +template detect(v: untyped) = + doAssert typeof(v) is int + +detect: + try: + raise (ref ValueError)() + except ValueError: + 42 \ No newline at end of file diff --git a/tests/exception/t22469.nim b/tests/exception/t22469.nim new file mode 100644 index 000000000..a76c74967 --- /dev/null +++ b/tests/exception/t22469.nim @@ -0,0 +1,16 @@ +discard """ + exitcode: 1 + output: ''' +First top-level statement of ModuleB +m22469.nim(3) m22469 +fatal.nim(53) sysFatal +Error: unhandled exception: over- or underflow [OverflowDefect] +''' +""" + +# bug #22469 + +# ModuleA +import m22469 +echo "ModuleA about to have exception" +echo high(int) + 1 diff --git a/tests/exception/t9657.nim b/tests/exception/t9657.nim index 0b6e128e0..6ac525a70 100644 --- a/tests/exception/t9657.nim +++ b/tests/exception/t9657.nim @@ -1,9 +1,12 @@ discard """ action: run exitcode: 1 - target: "c" + targets: "c cpp" + disabled: "openbsd" + disabled: "netbsd" """ -# todo: remove `target: "c"` workaround once #10343 is properly fixed + close stdmsg const m = "exception!" +# see #10343 for details on this test discard writeBuffer(stdmsg, cstring(m), m.len) diff --git a/tests/exception/tcpp_imported_exc.nim b/tests/exception/tcpp_imported_exc.nim index c8349f7d5..55a58440f 100644 --- a/tests/exception/tcpp_imported_exc.nim +++ b/tests/exception/tcpp_imported_exc.nim @@ -1,6 +1,8 @@ discard """ +matrix: "--mm:refc" targets: "cpp" -output: '''caught as std::exception +output: ''' +caught as std::exception expected finally1 finally2 @@ -12,9 +14,10 @@ finally 2 expected cpp exception caught ''' +disabled: "windows" # pending bug #18011 """ -type +type std_exception* {.importcpp: "std::exception", header: "<exception>".} = object std_runtime_error* {.importcpp: "std::runtime_error", header: "<stdexcept>".} = object std_string* {.importcpp: "std::string", header: "<string>".} = object @@ -25,7 +28,7 @@ proc constructRuntimeError(s: stdstring): std_runtime_error {.importcpp: "std::r proc what(ex: std_runtime_error): cstring {.importcpp: "((char *)#.what())".} -proc myexception = +proc myexception = raise constructRuntimeError(constructStdString("cpp_exception")) try: @@ -41,17 +44,17 @@ except std_exception: doAssert(getCurrentException() == nil) -proc earlyReturn = +proc earlyReturn = try: try: - myexception() + myexception() finally: echo "finally1" except: return finally: echo "finally2" - + earlyReturn() doAssert(getCurrentException() == nil) @@ -74,7 +77,7 @@ doAssert(getCurrentException() == nil) # raise by pointer and also generic type type - std_vector {.importcpp"std::vector", header"<vector>".} [T] = object + std_vector[T] {.importcpp"std::vector", header"<vector>".} = object proc newVector[T](len: int): ptr std_vector[T] {.importcpp: "new std::vector<'1>(@)".} proc deleteVector[T](v: ptr std_vector[T]) {.importcpp: "delete @; @ = NIM_NIL;".} @@ -118,16 +121,15 @@ try: echo "finally 2" except: echo "expected" - - + doAssert(getCurrentException() == nil) try: - try: - myexception() - except std_runtime_error as ex: - echo "cpp exception caught" - raise newException(ValueError, "rewritten " & $ex.what()) + try: + myexception() + except std_runtime_error as ex: + echo "cpp exception caught" + raise newException(ValueError, "rewritten " & $ex.what()) except: doAssert(getCurrentExceptionMsg() == "rewritten cpp_exception") diff --git a/tests/exception/tcpp_imported_exc2.nim b/tests/exception/tcpp_imported_exc2.nim new file mode 100644 index 000000000..ff299ea3d --- /dev/null +++ b/tests/exception/tcpp_imported_exc2.nim @@ -0,0 +1,10 @@ +discard """ +targets: "cpp" +output: "" +""" +#issue #14369 case 2 +type RuntimeError {.requiresInit, importcpp: "std::runtime_error", header: "<stdexcept>".} = object + +proc initRuntimeError(a: cstring): RuntimeError {.importcpp: "std::runtime_error(@)", constructor.} +try: raise initRuntimeError("foo2") +except: discard diff --git a/tests/exception/texcas.nim b/tests/exception/texcas.nim index 7108e334c..ad6819f11 100644 --- a/tests/exception/texcas.nim +++ b/tests/exception/texcas.nim @@ -1,8 +1,9 @@ discard """ targets: "c cpp" - output: '''Hello + output: ''' Hello - ''' +Hello +''' """ proc test[T]() = try: diff --git a/tests/exception/texception_inference.nim b/tests/exception/texception_inference.nim new file mode 100644 index 000000000..7dd01cca1 --- /dev/null +++ b/tests/exception/texception_inference.nim @@ -0,0 +1,32 @@ +discard """ + output: '''good''' + cmd: "nim c --gc:orc -d:release $file" +""" + +type + Raising[T, E] = object + +proc foo[T, Errors](x: proc (x: Raising[T, Errors])) {.raises: Errors.} = + discard + +proc callback(x: Raising[int, ValueError]) = + echo "callback" + +proc xy() {.raises: [ValueError].} = + foo callback + +proc x[E]() {.raises: [E, IOError].} = + raise newException(E, "text here") + +try: + x[ValueError]() +except ValueError: + echo "good" + +proc callback2(x: Raising[int, IOError]) = + discard + +proc foo2[T, OtherErrors](x: proc(x: Raising[T, OtherErrors])) {.raises: [ValueError, OtherErrors].} = + discard + +foo2 callback2 diff --git a/tests/exception/texceptionbreak.nim b/tests/exception/texceptionbreak.nim index 6548192c6..b8ce7eead 100644 --- a/tests/exception/texceptionbreak.nim +++ b/tests/exception/texceptionbreak.nim @@ -29,16 +29,16 @@ echo "2" try: raise newException(OSError, "Problem") except OSError: - block: - break + block label: + break label echo "3" # Fourth Variety -block: +block label: try: raise newException(OSError, "Problem") except OSError: - break + break label echo "4" diff --git a/tests/exception/texceptions.nim b/tests/exception/texceptions.nim index 7bce32837..62d24c934 100644 --- a/tests/exception/texceptions.nim +++ b/tests/exception/texceptions.nim @@ -1,5 +1,8 @@ discard """ + disabled: "windows" # no sigsetjmp() there + matrix: "-d:nimStdSetjmp; -d:nimSigSetjmp; -d:nimRawSetjmp; -d:nimBuiltinSetjmp" output: ''' + BEFORE FINALLY @@ -16,7 +19,7 @@ FINALLY echo "" -proc no_expcetion = +proc no_exception = try: echo "BEFORE" @@ -27,7 +30,7 @@ proc no_expcetion = finally: echo "FINALLY" -try: no_expcetion() +try: no_exception() except: echo "RECOVER" echo "" @@ -89,10 +92,10 @@ block: result.add(1212) try: try: - raise newException(AssertionError, "a") + raise newException(AssertionDefect, "a") finally: result.add(42) - except AssertionError: + except AssertionDefect: result.add(99) finally: result.add(10) @@ -104,7 +107,7 @@ block: result.add(-1) except ValueError: result.add(-1) - except IndexError: + except IndexDefect: result.add(2) except: result.add(3) diff --git a/tests/exception/texceptions2.nim b/tests/exception/texceptions2.nim new file mode 100644 index 000000000..97fd856a0 --- /dev/null +++ b/tests/exception/texceptions2.nim @@ -0,0 +1,130 @@ +discard """ + disabled: "posix" # already covered by texceptions.nim + matrix: "-d:nimStdSetjmp; -d:nimRawSetjmp; -d:nimBuiltinSetjmp" + output: ''' + +BEFORE +FINALLY + +BEFORE +EXCEPT +FINALLY +RECOVER + +BEFORE +EXCEPT: IOError: hi +FINALLY +''' +""" + +echo "" + +proc no_exception = + try: + echo "BEFORE" + + except: + echo "EXCEPT" + raise + + finally: + echo "FINALLY" + +try: no_exception() +except: echo "RECOVER" + +echo "" + +proc reraise_in_except = + try: + echo "BEFORE" + raise newException(IOError, "") + + except IOError: + echo "EXCEPT" + raise + + finally: + echo "FINALLY" + +try: reraise_in_except() +except: echo "RECOVER" + +echo "" + +proc return_in_except = + try: + echo "BEFORE" + raise newException(IOError, "hi") + + except: + echo "EXCEPT: ", getCurrentException().name, ": ", getCurrentExceptionMsg() + return + + finally: + echo "FINALLY" + +try: return_in_except() +except: echo "RECOVER" + +block: #10417 + proc moo() {.noreturn.} = discard + + let bar = + try: + 1 + except: + moo() + + doAssert(bar == 1) + +# Make sure the VM handles the exceptions correctly +block: + proc fun1(): seq[int] = + try: + try: + raise newException(ValueError, "xx") + except: + doAssert("xx" == getCurrentExceptionMsg()) + raise newException(KeyError, "yy") + except: + doAssert("yy" == getCurrentExceptionMsg()) + result.add(1212) + try: + try: + raise newException(AssertionDefect, "a") + finally: + result.add(42) + except AssertionDefect: + result.add(99) + finally: + result.add(10) + result.add(4) + result.add(0) + try: + result.add(1) + except KeyError: + result.add(-1) + except ValueError: + result.add(-1) + except IndexDefect: + result.add(2) + except: + result.add(3) + + try: + try: + result.add(1) + return + except: + result.add(-1) + finally: + result.add(2) + except KeyError: + doAssert(false) + finally: + result.add(3) + + let x1 = fun1() + const x2 = fun1() + doAssert(x1 == x2) diff --git a/tests/exception/tindexerrorformatbounds.nim b/tests/exception/tindexerrorformatbounds.nim deleted file mode 100644 index 7563c5ffa..000000000 --- a/tests/exception/tindexerrorformatbounds.nim +++ /dev/null @@ -1,31 +0,0 @@ -import os, osproc, strutils - -const characters = "abcdefghijklmnopqrstuvwxyz" -var s: string - -# # chcks.nim:23 -# # test formatErrorIndexBound returns correct bounds -block: - s = characters - try: - discard s[0..999] - except IndexError: - let msg = getCurrentExceptionMsg() - let expected = "index $# not in 0 .. $#" % [$len(s), $(len(s)-1)] - doAssert msg.contains expected, $(msg, expected) - -block: - try: - discard paramStr(999) - except IndexError: - let msg = getCurrentExceptionMsg() - let expected = "index 999 not in 0 .. 0" - doAssert msg.contains expected, $(msg, expected) - -block: - const nim = getCurrentCompilerExe() - for i in 1..4: - let (outp, errC) = execCmdEx("$# e tests/exception/testindexerroroutput.nims test$#" % [nim, $i]) - let expected = "index 3 not in 0 .. 2" - doAssert errC != 0 - doAssert outp.contains expected, $(outp, errC, expected, i) diff --git a/tests/exception/tsetexceptions.nim b/tests/exception/tsetexceptions.nim new file mode 100644 index 000000000..0e353f43e --- /dev/null +++ b/tests/exception/tsetexceptions.nim @@ -0,0 +1,11 @@ +discard """ + targets: "c cpp js" + joinable: false +""" + +# refs https://github.com/nim-lang/Nim/pull/18247#issuecomment-860877161 + +let ex = newException(CatchableError, "test") +setCurrentException(ex) +doAssert getCurrentException().msg == ex.msg +doAssert getCurrentExceptionMsg() == ex.msg diff --git a/tests/exception/twrongexc.nim b/tests/exception/twrongexc.nim index 9c656f3d9..d229c5749 100644 --- a/tests/exception/twrongexc.nim +++ b/tests/exception/twrongexc.nim @@ -4,5 +4,5 @@ discard """ """ try: raise newException(ValueError, "") -except OverflowError: +except OverflowDefect: echo("Error caught") |