summary refs log tree commit diff stats
path: root/tests/assert/tassert2.nim
blob: 5d849aaad71ccceaf741f920876e069a6ecd4258 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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, 10) `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, 14) `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, 14) `a > 1` "