utemplates.nim 921 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import unittest
  2. template t(a: int): string = "int"
  3. template t(a: string): string = "string"
  4. block: # templates can be overloaded
  5. check t(10) == "int"
  6. check t("test") == "string"
  7. block: # previous definitions can be further overloaded or hidden in local scopes
  8. template t(a: bool): string = "bool"
  9. check t(true) == "bool"
  10. check t(10) == "int"
  11. template t(a: int): string = "inner int"
  12. check t(10) == "inner int"
  13. check t("test") == "string"
  14. block: # templates can be redefined multiple times
  15. template customAssert(cond: bool, msg: string): typed {.dirty.} =
  16. if not cond: fail(msg)
  17. template assertionFailed(body: untyped) {.dirty.} =
  18. template fail(msg: string): typed {.redefine.} =
  19. body
  20. assertionFailed:
  21. check(msg == "first fail path")
  22. customAssert false, "first fail path"
  23. assertionFailed:
  24. check(msg == "second fail path")
  25. customAssert false, "second fail path"