view src/clojure/test_clojure/string.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
1 (ns clojure.test-clojure.string
2 (:require [clojure.string :as s])
3 (:use clojure.test))
5 (deftest t-split
6 (is (= ["a" "b"] (s/split "a-b" #"-")))
7 (is (= ["a" "b-c"] (s/split "a-b-c" #"-" 2)))
8 (is (vector? (s/split "abc" #"-"))))
10 (deftest t-reverse
11 (is (= "tab" (s/reverse "bat"))))
13 (deftest t-replace
14 (is (= "faabar" (s/replace "foobar" \o \a)))
15 (is (= "barbarbar" (s/replace "foobarfoo" "foo" "bar")))
16 (is (= "FOObarFOO" (s/replace "foobarfoo" #"foo" s/upper-case))))
18 (deftest t-replace-first
19 (is (= "barbarfoo" (s/replace-first "foobarfoo" "foo" "bar")))
20 (is (= "barbarfoo" (s/replace-first "foobarfoo" #"foo" "bar")))
21 (is (= "z.ology" (s/replace-first "zoology" \o \.)))
22 (is (= "FOObarfoo" (s/replace-first "foobarfoo" #"foo" s/upper-case))))
24 (deftest t-join
25 (are [x coll] (= x (s/join coll))
26 "" nil
27 "" []
28 "1" [1]
29 "12" [1 2])
30 (are [x sep coll] (= x (s/join sep coll))
31 "1,2,3" \, [1 2 3]
32 "" \, []
33 "1" \, [1]
34 "1 and-a 2 and-a 3" " and-a " [1 2 3]))
36 (deftest t-trim-newline
37 (is (= "foo" (s/trim-newline "foo\n")))
38 (is (= "foo" (s/trim-newline "foo\r\n")))
39 (is (= "foo" (s/trim-newline "foo")))
40 (is (= "" (s/trim-newline ""))))
42 (deftest t-capitalize
43 (is (= "Foobar" (s/capitalize "foobar")))
44 (is (= "Foobar" (s/capitalize "FOOBAR"))))
46 (deftest t-triml
47 (is (= "foo " (s/triml " foo ")))
48 (is (= "" (s/triml " "))))
50 (deftest t-trimr
51 (is (= " foo" (s/trimr " foo ")))
52 (is (= "" (s/trimr " "))))
54 (deftest t-trim
55 (is (= "foo" (s/trim " foo \r\n"))))
57 (deftest t-upper-case
58 (is (= "FOOBAR" (s/upper-case "Foobar"))))
60 (deftest t-lower-case
61 (is (= "foobar" (s/lower-case "FooBar"))))
63 (deftest nil-handling
64 (are [f args] (thrown? NullPointerException (apply f args))
65 s/reverse [nil]
66 s/replace [nil #"foo" "bar"]
67 s/replace-first [nil #"foo" "bar"]
68 s/capitalize [nil]
69 s/upper-case [nil]
70 s/lower-case [nil]
71 s/split [nil #"-"]
72 s/split [nil #"-" 1]
73 s/trim [nil]
74 s/triml [nil]
75 s/trimr [nil]
76 s/trim-newline [nil]))
78 (deftest char-sequence-handling
79 (are [result f args] (let [[^CharSequence s & more] args]
80 (= result (apply f (StringBuffer. s) more)))
81 "paz" s/reverse ["zap"]
82 "foo:bar" s/replace ["foo-bar" \- \:]
83 "ABC" s/replace ["abc" #"\w" s/upper-case]
84 "faa" s/replace ["foo" #"o" (StringBuffer. "a")]
85 "baz::quux" s/replace-first ["baz--quux" #"--" "::"]
86 "baz::quux" s/replace-first ["baz--quux" (StringBuffer. "--") (StringBuffer. "::")]
87 "zim-zam" s/replace-first ["zim zam" #" " (StringBuffer. "-")]
88 "Pow" s/capitalize ["POW"]
89 "BOOM" s/upper-case ["boom"]
90 "whimper" s/lower-case ["whimPER"]
91 ["foo" "bar"] s/split ["foo-bar" #"-"]
92 "calvino" s/trim [" calvino "]
93 "calvino " s/triml [" calvino "]
94 " calvino" s/trimr [" calvino "]
95 "the end" s/trim-newline ["the end\r\n\r\r\n"]
96 true s/blank? [" "]
97 ["a" "b"] s/split-lines ["a\nb"]
98 "fa la la" s/escape ["fo lo lo" {\o \a}]))
100 (deftest t-escape
101 (is (= "&lt;foo&amp;bar&gt;"
102 (s/escape "<foo&bar>" {\& "&amp;" \< "&lt;" \> "&gt;"})))
103 (is (= " \\\"foo\\\" "
104 (s/escape " \"foo\" " {\" "\\\""})))
105 (is (= "faabor"
106 (s/escape "foobar" {\a \o, \o \a}))))
108 (deftest t-blank
109 (is (s/blank? nil))
110 (is (s/blank? ""))
111 (is (s/blank? " "))
112 (is (s/blank? " \t \n \r "))
113 (is (not (s/blank? " foo "))))
115 (deftest t-split-lines
116 (let [result (s/split-lines "one\ntwo\r\nthree")]
117 (is (= ["one" "two" "three"] result))
118 (is (vector? result)))
119 (is (= (list "foo") (s/split-lines "foo"))))