replace.nim 1020 B

1234567891011121314151617181920212223
  1. include nre
  2. import unittest
  3. suite "replace":
  4. test "replace with 0-length strings":
  5. check("".replace(re"1", proc (v: RegexMatch): string = "1") == "")
  6. check(" ".replace(re"", proc (v: RegexMatch): string = "1") == "1 1")
  7. check("".replace(re"", proc (v: RegexMatch): string = "1") == "1")
  8. test "regular replace":
  9. check("123".replace(re"\d", "foo") == "foofoofoo")
  10. check("123".replace(re"(\d)", "$1$1") == "112233")
  11. check("123".replace(re"(\d)(\d)", "$1$2") == "123")
  12. check("123".replace(re"(\d)(\d)", "$#$#") == "123")
  13. check("123".replace(re"(?<foo>\d)(\d)", "$foo$#$#") == "1123")
  14. check("123".replace(re"(?<foo>\d)(\d)", "${foo}$#$#") == "1123")
  15. test "replacing missing captures should throw instead of segfaulting":
  16. expect IndexDefect: discard "ab".replace(re"(a)|(b)", "$1$2")
  17. expect IndexDefect: discard "b".replace(re"(a)?(b)", "$1$2")
  18. expect KeyError: discard "b".replace(re"(a)?", "${foo}")
  19. expect KeyError: discard "b".replace(re"(?<foo>a)?", "${foo}")