rlm@10
|
1 (ns clojure.contrib.test-string
|
rlm@10
|
2 (:require [clojure.contrib.string :as s])
|
rlm@10
|
3 (:use clojure.test))
|
rlm@10
|
4
|
rlm@10
|
5 (deftest t-codepoints
|
rlm@10
|
6 (is (= (list 102 111 111 65536 98 97 114)
|
rlm@10
|
7 (s/codepoints "foo\uD800\uDC00bar"))
|
rlm@10
|
8 "Handles Unicode supplementary characters"))
|
rlm@10
|
9
|
rlm@10
|
10 (deftest t-escape
|
rlm@10
|
11 (is (= "<foo&bar>"
|
rlm@10
|
12 (s/escape {\& "&" \< "<" \> ">"} "<foo&bar>")))
|
rlm@10
|
13 (is (= " \\\"foo\\\" "
|
rlm@10
|
14 (s/escape {\" "\\\""} " \"foo\" " )))
|
rlm@10
|
15 (is (= "faabor" (s/escape {\a \o, \o \a} "foobar"))))
|
rlm@10
|
16
|
rlm@10
|
17 (deftest t-blank
|
rlm@10
|
18 (is (s/blank? nil))
|
rlm@10
|
19 (is (s/blank? ""))
|
rlm@10
|
20 (is (s/blank? " "))
|
rlm@10
|
21 (is (s/blank? " \t \n \r "))
|
rlm@10
|
22 (is (not (s/blank? " foo "))))
|
rlm@10
|
23
|
rlm@10
|
24 (deftest t-take
|
rlm@10
|
25 (is (= "foo" (s/take 3 "foobar")))
|
rlm@10
|
26 (is (= "foobar" (s/take 7 "foobar")))
|
rlm@10
|
27 (is (= "" (s/take 0 "foo"))))
|
rlm@10
|
28
|
rlm@10
|
29 (deftest t-drop
|
rlm@10
|
30 (is (= "bar" (s/drop 3 "foobar")))
|
rlm@10
|
31 (is (= "" (s/drop 9 "foobar")))
|
rlm@10
|
32 (is (= "foobar" (s/drop 0 "foobar"))))
|
rlm@10
|
33
|
rlm@10
|
34 (deftest t-butlast
|
rlm@10
|
35 (is (= "foob" (s/butlast 2 "foobar")))
|
rlm@10
|
36 (is (= "" (s/butlast 9 "foobar")))
|
rlm@10
|
37 (is (= "foobar" (s/butlast 0 "foobar"))))
|
rlm@10
|
38
|
rlm@10
|
39 (deftest t-tail
|
rlm@10
|
40 (is (= "ar" (s/tail 2 "foobar")))
|
rlm@10
|
41 (is (= "foobar" (s/tail 9 "foobar")))
|
rlm@10
|
42 (is (= "" (s/tail 0 "foobar"))))
|
rlm@10
|
43
|
rlm@10
|
44 (deftest t-repeat
|
rlm@10
|
45 (is (= "foofoofoo" (s/repeat 3 "foo"))))
|
rlm@10
|
46
|
rlm@10
|
47 (deftest t-reverse
|
rlm@10
|
48 (is (= "tab" (s/reverse "bat"))))
|
rlm@10
|
49
|
rlm@10
|
50 (deftest t-replace
|
rlm@10
|
51 (is (= "faabar" (s/replace-char \o \a "foobar")))
|
rlm@10
|
52 (is (= "barbarbar" (s/replace-str "foo" "bar" "foobarfoo")))
|
rlm@10
|
53 (is (= "FOObarFOO" (s/replace-by #"foo" s/upper-case "foobarfoo"))))
|
rlm@10
|
54
|
rlm@10
|
55 (deftest t-replace-first
|
rlm@10
|
56 (is (= "barbarfoo" (s/replace-first-re #"foo" "bar" "foobarfoo")))
|
rlm@10
|
57 (is (= "FOObarfoo" (s/replace-first-by #"foo" s/upper-case "foobarfoo"))))
|
rlm@10
|
58
|
rlm@10
|
59 (deftest t-partition
|
rlm@10
|
60 (is (= (list "" "abc" "123" "def")
|
rlm@10
|
61 (s/partition #"[a-z]+" "abc123def"))))
|
rlm@10
|
62
|
rlm@10
|
63 (deftest t-join
|
rlm@10
|
64 (is (= "1,2,3" (s/join \, [1 2 3])))
|
rlm@10
|
65 (is (= "" (s/join \, [])))
|
rlm@10
|
66 (is (= "1 and-a 2 and-a 3" (s/join " and-a " [1 2 3]))))
|
rlm@10
|
67
|
rlm@10
|
68 (deftest t-chop
|
rlm@10
|
69 (is (= "fo" (s/chop "foo")))
|
rlm@10
|
70 (is (= "") (s/chop "f"))
|
rlm@10
|
71 (is (= "") (s/chop "")))
|
rlm@10
|
72
|
rlm@10
|
73 (deftest t-chomp
|
rlm@10
|
74 (is (= "foo" (s/chomp "foo\n")))
|
rlm@10
|
75 (is (= "foo" (s/chomp "foo\r\n")))
|
rlm@10
|
76 (is (= "foo" (s/chomp "foo")))
|
rlm@10
|
77 (is (= "" (s/chomp ""))))
|
rlm@10
|
78
|
rlm@10
|
79 (deftest t-swap-case
|
rlm@10
|
80 (is (= "fOO!bAR" (s/swap-case "Foo!Bar")))
|
rlm@10
|
81 (is (= "" (s/swap-case ""))))
|
rlm@10
|
82
|
rlm@10
|
83 (deftest t-capitalize
|
rlm@10
|
84 (is (= "Foobar" (s/capitalize "foobar")))
|
rlm@10
|
85 (is (= "Foobar" (s/capitalize "FOOBAR"))))
|
rlm@10
|
86
|
rlm@10
|
87 (deftest t-ltrim
|
rlm@10
|
88 (is (= "foo " (s/ltrim " foo ")))
|
rlm@10
|
89 (is (= "" (s/ltrim " "))))
|
rlm@10
|
90
|
rlm@10
|
91 (deftest t-rtrim
|
rlm@10
|
92 (is (= " foo" (s/rtrim " foo ")))
|
rlm@10
|
93 (is (= "" (s/rtrim " "))))
|
rlm@10
|
94
|
rlm@10
|
95 (deftest t-split-lines
|
rlm@10
|
96 (is (= (list "one" "two" "three")
|
rlm@10
|
97 (s/split-lines "one\ntwo\r\nthree")))
|
rlm@10
|
98 (is (= (list "foo") (s/split-lines "foo"))))
|
rlm@10
|
99
|
rlm@10
|
100 (deftest t-upper-case
|
rlm@10
|
101 (is (= "FOOBAR" (s/upper-case "Foobar"))))
|
rlm@10
|
102
|
rlm@10
|
103 (deftest t-lower-case
|
rlm@10
|
104 (is (= "foobar" (s/lower-case "FooBar"))))
|
rlm@10
|
105
|
rlm@10
|
106 (deftest t-trim
|
rlm@10
|
107 (is (= "foo" (s/trim " foo \r\n"))))
|
rlm@10
|
108
|
rlm@10
|
109 (deftest t-substring
|
rlm@10
|
110 (is (s/substring? "foo" "foobar"))
|
rlm@10
|
111 (is (not (s/substring? "baz" "foobar"))))
|
rlm@10
|
112
|
rlm@10
|
113 (deftest t-get
|
rlm@10
|
114 (is (= \o (s/get "foo" 1))))
|
rlm@10
|
115
|
rlm@10
|
116 (deftest t-as-str
|
rlm@10
|
117 (testing "keyword to string"
|
rlm@10
|
118 (is (= "foo") (s/as-str :foo)))
|
rlm@10
|
119 (testing "symbol to string"
|
rlm@10
|
120 (is (= "foo") (s/as-str 'foo)))
|
rlm@10
|
121 (testing "string to string"
|
rlm@10
|
122 (is (= "foo") (s/as-str "foo")))
|
rlm@10
|
123 (testing "stringifying non-namish things"
|
rlm@10
|
124 (is (= "42") (s/as-str 42))))
|