Carp/test/string.carp

188 lines
5.3 KiB
Plaintext
Raw Normal View History

2017-12-18 18:19:29 +03:00
(use String)
(load "Test.carp")
(use Test)
(defn main []
(with-test test
(assert-equal test
\s
(char-at "lisp" 2)
"char-at works as expected"
)
(assert-equal test
\s
(head "silk")
"head works as expected"
)
(assert-equal test
"ilk"
&(tail "silk")
"tail works as expected"
)
(assert-equal test
"sshello"
&(pad-left 7 \s "hello")
"pad-left works as expected"
)
(assert-equal test
"helloss"
&(pad-right 7 \s "hello")
"pad-right works as expected"
)
(assert-equal test
5
(count-char "lisssssp" \s)
"count-char works as expected"
)
(assert-equal test
"olleh"
&(reverse "hello")
"reverse works as expected"
)
(assert-equal test
false
(empty? "lisp")
"empty? returns false on non-empty string"
)
2018-03-13 13:59:48 +03:00
(assert-true test
(empty? "")
"empty? returns true on empty string"
2017-12-18 18:19:29 +03:00
)
(assert-equal test
&[\e \r \i \k]
&(chars "erik")
"chars works as expected"
)
(assert-equal test
"erik"
2018-03-12 16:56:05 +03:00
&(from-chars &[\e \r \i \k])
"from-chars works as expected"
)
(assert-equal test
"edan"
&(substring "svedang" 2 6)
"substring works as expected"
)
(assert-equal test
"sved"
&(prefix-string "svedang" 4)
"prefix-string works as expected"
)
(assert-equal test
"dang"
&(suffix-string "svedang" 3)
"suffix-string works as expected"
)
2018-03-13 13:59:48 +03:00
(assert-true test
(ends-with? "heller" "ler")
"ends-with? works as expected"
)
(assert-true test
(ends-with? "ller" "ler")
"ends-with? works (regression test for #157)"
)
(assert-true test
(starts-with? "heller" "hell")
"starts-with? works as expected"
)
(assert-true test
(in? "metacarpenter" "carp")
"in? works on matching pattern"
)
(assert-false test
(in? "metabolism" "carp")
"in? works on non-matching pattern"
)
(assert-true test
(upper? "I AM SCREAMING!")
"upper? works on matching pattern"
)
(assert-false test
(upper? "I am not screaming.")
"upper? works on non-matching pattern"
)
(assert-true test
(lower? "i am not screaming.")
"lower? works on matching pattern"
)
(assert-false test
(lower? "I am not screaming!")
"lower? works on non-matching pattern"
)
(assert-true test
(num? "11123456789123")
"num? works on matching pattern"
)
(assert-false test
(num? "111l23456789123")
"num? works on non-matching pattern"
)
(assert-true test
(alpha? "abcdefghijklmz")
"alpha? works on matching pattern"
)
(assert-false test
(alpha? "abcdefghijklm1234567")
"alpha? works on non-matching pattern"
)
(assert-true test
(alphanum? "abcdefghijklm1234567")
"alphanum? works on matching pattern"
)
(assert-false test
(alphanum? "abcdefghijklm1234567?")
"alphanum? works on non-matching pattern"
)
(assert-true test
(hex? "c0ffee")
"hex? works on matching pattern"
)
(assert-false test
(hex? "c0ffeebar")
"hex? works on non-matching pattern"
)
(assert-equal test
"string "
&(trim-left " string ")
"trim-left works as expected"
)
(assert-equal test
" string"
&(trim-right " string ")
"trim-right works as expected"
)
(assert-equal test
"string"
&(trim " string ")
"trim works as expected"
)
(assert-equal test
"string"
&(chomp "string\n")
"chomp works as expected I"
)
(assert-equal test
"string\n"
&(chomp "string\n\n")
"chomp works as expected II"
)
(assert-equal test
2018-03-13 13:59:48 +03:00
"string"
&(chomp "string\r\n")
"chomp works as expected III"
)
(assert-equal test
2018-03-13 13:59:48 +03:00
"string"
&(chomp "string\r")
"chomp works as expected IV"
)
(assert-equal test
2018-03-13 13:59:48 +03:00
"too much whitespace."
&(collapse-whitespace "too much whitespace.")
"collapse-whitespace works as expected"
)
2017-12-18 18:19:29 +03:00
(print-test-results test)
2018-03-13 13:59:48 +03:00
)
)