diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/test_clojure/string.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,120 @@
     1.4 +(ns clojure.test-clojure.string
     1.5 +  (:require [clojure.string :as s])
     1.6 +  (:use clojure.test))
     1.7 +
     1.8 +(deftest t-split
     1.9 +  (is (= ["a" "b"] (s/split "a-b" #"-")))
    1.10 +  (is (= ["a" "b-c"] (s/split "a-b-c" #"-" 2)))
    1.11 +  (is (vector? (s/split "abc" #"-"))))
    1.12 +
    1.13 +(deftest t-reverse
    1.14 +  (is (= "tab" (s/reverse "bat"))))
    1.15 +
    1.16 +(deftest t-replace
    1.17 +  (is (= "faabar" (s/replace "foobar" \o \a)))
    1.18 +  (is (= "barbarbar" (s/replace "foobarfoo" "foo" "bar")))
    1.19 +  (is (= "FOObarFOO" (s/replace "foobarfoo" #"foo" s/upper-case))))
    1.20 +
    1.21 +(deftest t-replace-first
    1.22 +  (is (= "barbarfoo" (s/replace-first "foobarfoo" "foo" "bar")))
    1.23 +  (is (= "barbarfoo" (s/replace-first "foobarfoo" #"foo" "bar")))
    1.24 +  (is (= "z.ology" (s/replace-first "zoology" \o \.)))
    1.25 +  (is (= "FOObarfoo" (s/replace-first "foobarfoo" #"foo" s/upper-case))))
    1.26 +
    1.27 +(deftest t-join
    1.28 +  (are [x coll] (= x (s/join coll))
    1.29 +       "" nil
    1.30 +       "" []
    1.31 +       "1" [1]
    1.32 +       "12" [1 2])
    1.33 +  (are [x sep coll] (= x (s/join sep coll))
    1.34 +       "1,2,3" \, [1 2 3]
    1.35 +       "" \, []
    1.36 +       "1" \, [1]
    1.37 +       "1 and-a 2 and-a 3" " and-a " [1 2 3]))
    1.38 +
    1.39 +(deftest t-trim-newline
    1.40 +  (is (= "foo" (s/trim-newline "foo\n")))
    1.41 +  (is (= "foo" (s/trim-newline "foo\r\n")))
    1.42 +  (is (= "foo" (s/trim-newline "foo")))
    1.43 +  (is (= "" (s/trim-newline ""))))
    1.44 +
    1.45 +(deftest t-capitalize
    1.46 +  (is (= "Foobar" (s/capitalize "foobar")))
    1.47 +  (is (= "Foobar" (s/capitalize "FOOBAR"))))
    1.48 +
    1.49 +(deftest t-triml
    1.50 +  (is (= "foo " (s/triml " foo ")))
    1.51 +  (is (= "" (s/triml "   "))))
    1.52 +
    1.53 +(deftest t-trimr
    1.54 +  (is (= " foo" (s/trimr " foo ")))
    1.55 +  (is (= "" (s/trimr "   "))))
    1.56 +
    1.57 +(deftest t-trim
    1.58 +  (is (= "foo" (s/trim "  foo  \r\n"))))
    1.59 +
    1.60 +(deftest t-upper-case
    1.61 +  (is (= "FOOBAR" (s/upper-case "Foobar"))))
    1.62 +
    1.63 +(deftest t-lower-case
    1.64 +  (is (= "foobar" (s/lower-case "FooBar"))))
    1.65 +
    1.66 +(deftest nil-handling
    1.67 +  (are [f args] (thrown? NullPointerException (apply f args))
    1.68 +       s/reverse [nil]
    1.69 +       s/replace [nil #"foo" "bar"]
    1.70 +       s/replace-first [nil #"foo" "bar"]
    1.71 +       s/capitalize [nil]
    1.72 +       s/upper-case [nil]
    1.73 +       s/lower-case [nil]
    1.74 +       s/split [nil #"-"]
    1.75 +       s/split [nil #"-" 1]
    1.76 +       s/trim [nil]
    1.77 +       s/triml [nil]
    1.78 +       s/trimr [nil]
    1.79 +       s/trim-newline [nil]))
    1.80 +
    1.81 +(deftest char-sequence-handling
    1.82 +  (are [result f args] (let [[^CharSequence s & more] args]
    1.83 +                         (= result (apply f (StringBuffer. s) more)))
    1.84 +       "paz" s/reverse ["zap"]
    1.85 +       "foo:bar" s/replace ["foo-bar" \- \:]
    1.86 +       "ABC" s/replace ["abc" #"\w" s/upper-case]
    1.87 +       "faa" s/replace ["foo" #"o" (StringBuffer. "a")]
    1.88 +       "baz::quux" s/replace-first ["baz--quux" #"--" "::"]
    1.89 +       "baz::quux" s/replace-first ["baz--quux" (StringBuffer. "--") (StringBuffer. "::")]
    1.90 +       "zim-zam" s/replace-first ["zim zam" #" " (StringBuffer. "-")]
    1.91 +       "Pow" s/capitalize ["POW"]
    1.92 +       "BOOM" s/upper-case ["boom"]
    1.93 +       "whimper" s/lower-case ["whimPER"]
    1.94 +       ["foo" "bar"] s/split ["foo-bar" #"-"]
    1.95 +       "calvino" s/trim ["  calvino  "]
    1.96 +       "calvino  " s/triml ["  calvino  "]
    1.97 +       "  calvino" s/trimr ["  calvino  "]
    1.98 +       "the end" s/trim-newline ["the end\r\n\r\r\n"]
    1.99 +       true s/blank? [" "]
   1.100 +       ["a" "b"] s/split-lines ["a\nb"]
   1.101 +       "fa la la" s/escape ["fo lo lo" {\o \a}]))
   1.102 +
   1.103 +(deftest t-escape
   1.104 +  (is (= "&lt;foo&amp;bar&gt;"
   1.105 +         (s/escape "<foo&bar>" {\& "&amp;" \< "&lt;" \> "&gt;"})))
   1.106 +  (is (= " \\\"foo\\\" "
   1.107 +         (s/escape " \"foo\" " {\" "\\\""})))
   1.108 +  (is (= "faabor"
   1.109 +         (s/escape "foobar" {\a \o, \o \a}))))
   1.110 +
   1.111 +(deftest t-blank
   1.112 +  (is (s/blank? nil))
   1.113 +  (is (s/blank? ""))
   1.114 +  (is (s/blank? " "))
   1.115 +  (is (s/blank? " \t \n  \r "))
   1.116 +  (is (not (s/blank? "  foo  "))))
   1.117 +
   1.118 +(deftest t-split-lines
   1.119 +  (let [result (s/split-lines "one\ntwo\r\nthree")]
   1.120 +    (is (= ["one" "two" "three"] result))
   1.121 +    (is (vector? result)))
   1.122 +  (is (= (list "foo") (s/split-lines "foo"))))
   1.123 +