From b6b6382e0b4f86759d07a1be150a826b9e0fe054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=83=E7=A7=92=E4=B8=8D=E8=A7=89=E6=A2=A6?= <7822577+7sDream@users.noreply.github.com> Date: Mon, 28 May 2018 10:24:04 +0800 Subject: [PATCH] Added: type name output when exception raised from unittest (#7869) --- changelog.md | 2 ++ lib/pure/unittest.nim | 10 +++++++--- tests/stdlib/tunittestexceptiontype.nim | 10 ++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 tests/stdlib/tunittestexceptiontype.nim diff --git a/changelog.md b/changelog.md index 57b54c71ef..813c4caf84 100644 --- a/changelog.md +++ b/changelog.md @@ -76,6 +76,8 @@ - The `terminal` module now exports additional procs for generating ANSI color codes as strings. - Added the parameter ``val`` for the ``CritBitTree[int].inc`` proc. +- An exception raised from ``test`` block of ``unittest`` now show its type in + error message ### Language additions diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim index 917251a6cd..d804ba7c83 100644 --- a/lib/pure/unittest.nim +++ b/lib/pure/unittest.nim @@ -462,6 +462,8 @@ template suite*(name, body) {.dirty.} = finally: suiteEnded() +template exceptionTypeName(e: typed): string = $e.name + template test*(name, body) {.dirty.} = ## Define a single test case identified by `name`. ## @@ -476,7 +478,7 @@ template test*(name, body) {.dirty.} = ## .. code-block:: ## ## [OK] roses are red - bind shouldRun, checkpoints, formatters, ensureInitialized, testEnded + bind shouldRun, checkpoints, formatters, ensureInitialized, testEnded, exceptionTypeName ensureInitialized() @@ -495,8 +497,10 @@ template test*(name, body) {.dirty.} = except: when not defined(js): - checkpoint("Unhandled exception: " & getCurrentExceptionMsg()) - var stackTrace {.inject.} = getCurrentException().getStackTrace() + let e = getCurrentException() + let eTypeDesc = "[" & exceptionTypeName(e) & "]" + checkpoint("Unhandled exception: " & getCurrentExceptionMsg() & " " & eTypeDesc) + var stackTrace {.inject.} = e.getStackTrace() fail() finally: diff --git a/tests/stdlib/tunittestexceptiontype.nim b/tests/stdlib/tunittestexceptiontype.nim new file mode 100644 index 0000000000..e05a254099 --- /dev/null +++ b/tests/stdlib/tunittestexceptiontype.nim @@ -0,0 +1,10 @@ +discard """ + exitcode: 1 + outputsub: '''exception type is [ValueError]''' +""" + +import unittest + +suite "exception from test": + test "show exception type": + raise newException(ValueError, "exception type is")