string.lisp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. (import test ())
  2. (import core/string ())
  3. (describe "A string"
  4. (it "has a constant length"
  5. (affirm (eq? (n "") 0)
  6. (eq? (n "foo") 3)))
  7. (it "equals itself"
  8. (affirm (eq? "" "")
  9. (eq? "foo" "foo")
  10. (eq? "foo" '"foo")
  11. (eq? "foo" "f\111\111")))
  12. (it "can be indexed"
  13. (affirm (eq? "a" (char-at "abc" 1))
  14. (eq? "b" (char-at "abc" 2))
  15. (eq? "c" (char-at "abc" -1))
  16. (eq? "" (char-at "abc" 4))
  17. (eq? "" (char-at "abc" -4))
  18. (eq? "" (char-at "" 1))))
  19. (it "can be concatenated"
  20. (affirm (eq? "abc" (.. "a" "b" "c"))
  21. (eq? "foo-bar-baz" (.. "foo" "-" "bar" "-" "baz"))))
  22. (it "can be concatenated from a list"
  23. (affirm (eq? "abc" (concat '("a" "b" "c")))
  24. (eq? "a b c" (concat '("a" "b" "c") " "))
  25. (eq? "bc" (concat (cdr '("a" "b" "c"))))))
  26. (it "can be split"
  27. (affirm (eq? '("foo" "bar" "baz") (split "foo-bar-baz" "-"))
  28. (eq? '("foo" "bar-baz") (split "foo-bar-baz" "-" 1))
  29. (eq? '("foo" "bar" "") (split "foo-bar-" "-"))
  30. (eq? '("" "foo" "bar") (split "-foo-bar" "-"))
  31. (eq? '("foo" "" "bar") (split "foo--bar" "-"))
  32. (eq? '("f" "o" "o") (split "foo" ""))))
  33. (it "can be quoted"
  34. (affirm (eq? "\"foo\"" (quoted "foo"))
  35. (eq? "\"\\9\"" (quoted "\t"))
  36. (eq? "\"\\n\"" (quoted "\n"))))
  37. (it "can be trimmed"
  38. (affirm (eq? "test" (trim "\n\t test \t\r\n")))))