utemplates.nim 910 B

1234567891011121314151617181920212223242526272829303132
  1. import unittest
  2. template t(a: int): string = "int"
  3. template t(a: string): string = "string"
  4. test "templates can be overloaded":
  5. check t(10) == "int"
  6. check t("test") == "string"
  7. test "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. test "templates can be redefined multiple times":
  15. template customAssert(cond: bool, msg: string): typed {.immediate, dirty.} =
  16. if not cond: fail(msg)
  17. template assertion_failed(body: typed) {.immediate, dirty.} =
  18. template fail(msg: string): typed = body
  19. assertion_failed: check msg == "first fail path"
  20. customAssert false, "first fail path"
  21. assertion_failed: check msg == "second fail path"
  22. customAssert false, "second fail path"