summary refs log tree commit diff stats
path: root/tests/assert/tfailedassert.nim
blob: 2638937671f509a2644decebbf624167a30a9d5d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
discard """
  output: '''
WARNING: false first asseertion 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 E_Base
    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 asseertion 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