herd_string_test.erl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. -module(herd_string_test).
  2. -include_lib("eunit/include/eunit.hrl").
  3. %% eunit tests
  4. fill_before_test() ->
  5. ?assertEqual(" abcdef", herd_string:fill_before("abcdef", 32, 10)),
  6. ?assertEqual("__abc", herd_string:fill_before("abc", $_, 5)),
  7. ?assertEqual("01", herd_string:fill_before("1", $0, 2)),
  8. ?assertEqual("000000000012345", herd_string:fill_before("12345", $0, 15)),
  9. ?assertEqual("abcdef", herd_string:fill_before("abcdef", 32, 5)),
  10. ?assertEqual("abcdef", herd_string:fill_before("abcdef", 32, 6)),
  11. ?assertEqual(" abcdef", herd_string:fill_before("abcdef", 32, 7)),
  12. ok.
  13. split_test() ->
  14. ?assertEqual(["hello", "there"], herd_string:split("hello there", " ")),
  15. ?assertEqual(["he", "o there"], herd_string:split("hello there", "ll")),
  16. ?assertEqual(["h", "llo th", "r"], herd_string:split("hello there", "e")),
  17. ?assertEqual(["aa", "bbb", "ccc"], herd_string:split("aa___bbb___ccc", "___")),
  18. ?assertEqual(["aa", "bbb", "ccc"], herd_string:split("___aa___bbb___ccc", "___")),
  19. ?assertEqual(["aa", "bbb", "ccc"], herd_string:split("___aa___bbb___ccc___", "___")),
  20. ?assertEqual(["aa", "bbb", "ccc"], herd_string:split("aa___bbb___ccc___", "___")),
  21. ?assertEqual([], herd_string:split("", "")),
  22. ?assertEqual([], herd_string:split("", "aa")),
  23. ?assertEqual(["aa"], herd_string:split("aa", "")),
  24. ok.
  25. replace_test() ->
  26. ?assertEqual("", herd_string:replace("", "old", "new")),
  27. ?assertEqual("aa|bb|cc", herd_string:replace("aa--bb--cc", "--", "|")),
  28. ?assertEqual("nice Cool", herd_string:replace("nice cool", "cool", "Cool")),
  29. ?assertEqual("AAbbcc", herd_string:replace("aabbcc", "aa", "AA")),
  30. ?assertEqual("aaBBcc", herd_string:replace("aabbcc", "bb", "BB")),
  31. ?assertEqual("aabbCC", herd_string:replace("aabbcc", "cc", "CC")),
  32. ?assertEqual("AAbbcc AAbbcc", herd_string:replace("aabbcc aabbcc", "aa", "AA")),
  33. ?assertEqual("aaBBcc aaBBcc", herd_string:replace("aabbcc aabbcc", "bb", "BB")),
  34. ?assertEqual("aabbCC aabbCC", herd_string:replace("aabbcc aabbcc", "cc", "CC")),
  35. ok.
  36. escape_xml_test() ->
  37. ?assertEqual("hello <b>from "Holliwood"</b>!",
  38. herd_string:escape_xml("hello <b>from \"Holliwood\"</b>!")),
  39. ?assertEqual("one &amp; two", herd_string:escape_xml("one & two")),
  40. ok.
  41. escape_json_test() ->
  42. ?assertEqual("hello \\\"there\\\" and\\n here\\n\\ragain\\t ok.",
  43. herd_string:escape_json("hello \"there\" and\n here\n\ragain\t ok.")),
  44. ?assertEqual("\\\\ ok \\\\ ok",
  45. herd_string:escape_json("\\ ok \\ ok")),
  46. ok.