tdo.nim 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. discard """
  2. output: '''
  3. true
  4. true
  5. true
  6. true inner B
  7. running with pragma
  8. ran with pragma
  9. '''
  10. """
  11. template withValue(a, b, c, d, e: untyped) =
  12. if c:
  13. d
  14. else:
  15. e
  16. template withValue(a, b, c, d: untyped) =
  17. if c:
  18. d
  19. const
  20. EVENT_READ = 1
  21. EVENT_WRITE = 2
  22. FLAG_HANDLE = 3
  23. EVENT_MASK = 3
  24. var s: string
  25. proc main =
  26. var value = false
  27. var fd = 8888
  28. var event = 0
  29. s.withValue(fd, value) do:
  30. if value:
  31. var oe = (EVENT_MASK)
  32. if (oe xor event) != 0:
  33. if (oe and EVENT_READ) != 0 and (event and EVENT_READ) == 0:
  34. discard
  35. if (oe and EVENT_WRITE) != 0 and (event and EVENT_WRITE) == 0:
  36. discard
  37. if (oe and EVENT_READ) == 0 and (event and EVENT_READ) != 0:
  38. discard
  39. if (oe and EVENT_WRITE) == 0 and (event and EVENT_WRITE) != 0:
  40. discard
  41. else:
  42. raise newException(ValueError, "error")
  43. do:
  44. raise newException(ValueError, "Descriptor is not registered in queue")
  45. proc main2 =
  46. var unused = 8
  47. # test 'then' branch:
  48. s.withValue(unused, true) do:
  49. echo "true"
  50. do:
  51. echo "false"
  52. # test overloading:
  53. s.withValue(unused, false) do:
  54. echo "cannot come here"
  55. # test 'else' branch:
  56. s.withValue(unused, false) do:
  57. echo "false"
  58. do:
  59. echo "true"
  60. # test proper nesting:
  61. s.withValue(unused, false) do:
  62. echo "false"
  63. s.withValue(unused, false) do:
  64. echo "false inner A"
  65. do:
  66. echo "true inner A"
  67. do:
  68. echo "true"
  69. s.withValue(unused, false) do:
  70. echo "false inner B"
  71. do:
  72. echo "true inner B"
  73. main2()
  74. proc withPragma(foo: int, bar: proc() {.raises: [].}) =
  75. echo "running with pragma"
  76. bar()
  77. withPragma(3) do {.raises: [].}:
  78. echo "ran with pragma"
  79. doAssert not (compiles do:
  80. withPragma(3) do {.raises: [].}:
  81. raise newException(Exception))