t_events.nim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import unittest
  2. import sigui
  3. test "events":
  4. var e: Event[int]
  5. var eh: EventHandler
  6. var capture = 0
  7. e.emit 1
  8. check capture == 0
  9. e.connect eh, proc(v: int) =
  10. capture = v
  11. e.emit 2
  12. check capture == 2
  13. e.emit 3
  14. check capture == 3
  15. e.disconnect eh
  16. e.emit 4
  17. check capture == 3
  18. (proc =
  19. var eh2: EventHandler
  20. e.connect eh2, proc(v: int) =
  21. capture = v
  22. e.emit 5
  23. check capture == 5
  24. )()
  25. e.emit 6
  26. when defined(orc) or defined(arc):
  27. check capture == 5
  28. else:
  29. ## don't check, garbadge collection is unpredictable
  30. test "properties":
  31. var e = 0.property
  32. var eh = EventHandler()
  33. var capture = 0
  34. check e[] == 0
  35. e[] = 1
  36. check e[] == 1
  37. check capture == 0
  38. e.changed.connectTo eh:
  39. capture = e
  40. check capture == 0
  41. e[] = 2
  42. check e[] == 2
  43. check capture == 2
  44. e{} = 3
  45. check e[] == 3
  46. check capture == 2
  47. test "custom properties":
  48. var capture1 = 0
  49. var e = CustomProperty[int](
  50. get: proc(): int = capture1 + 1,
  51. set: proc(v: int) = capture1 = v - 1
  52. )
  53. var eh = EventHandler()
  54. var capture2 = 0
  55. e.changed.connectTo eh:
  56. capture2 = e
  57. check e[] == 1
  58. e[] = 2
  59. check e[] == 2
  60. check capture1 == 1
  61. check capture2 == 2
  62. e{} = 5
  63. check e[] == 5
  64. check capture1 == 4
  65. check capture2 == 2