diff src/clojure/contrib/test_contrib/test_io.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/contrib/test_contrib/test_io.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,96 @@
     1.4 +(ns clojure.contrib.test-io
     1.5 +  (:refer-clojure :exclude (spit))
     1.6 +  (:use clojure.test clojure.contrib.io)
     1.7 +  (:import (java.io File FileInputStream BufferedInputStream)
     1.8 +           (java.net URL URI)))
     1.9 +
    1.10 +(deftest file-str-backslash
    1.11 +  (is (= (java.io.File.
    1.12 +          (str "C:" java.io.File/separator
    1.13 +               "Documents" java.io.File/separator
    1.14 +               "file.txt"))
    1.15 +         (file-str "C:\\Documents\\file.txt"))))
    1.16 +
    1.17 +(deftest test-as-file
    1.18 +  (testing "strings"
    1.19 +    (is (= (File. "foo") (as-file "foo"))))
    1.20 +  (testing "Files"
    1.21 +    (is (= (File. "bar") (as-file (File. "bar"))))))
    1.22 +
    1.23 +(deftest test-as-url
    1.24 +  (are [result expr] (= result expr)
    1.25 +       (URL. "http://foo") (as-url (URL. "http://foo"))
    1.26 +       (URL. "http://foo") (as-url "http://foo")
    1.27 +       (URL. "http://foo") (as-url (URI. "http://foo"))
    1.28 +       (URL. "file:/foo") (as-url (File. "/foo"))))
    1.29 +
    1.30 +(deftest test-delete-file
    1.31 +  (let [file (File/createTempFile "test" "deletion")
    1.32 +        not-file (File. (str (java.util.UUID/randomUUID)))]
    1.33 +    (delete-file (.getAbsolutePath file))
    1.34 +    (is (not (.exists file)))
    1.35 +    (is (thrown? ArithmeticException (/ 1 0)))
    1.36 +    (is (thrown? java.io.IOException (delete-file not-file)))
    1.37 +    (is (delete-file not-file :silently))))
    1.38 +
    1.39 +(deftest test-relative-path-string
    1.40 +  (testing "strings"
    1.41 +    (is (= "foo" (relative-path-string "foo"))))
    1.42 +  (testing "absolute path strings are forbidden"
    1.43 +    (is (thrown? IllegalArgumentException (relative-path-string (str File/separator "baz")))))
    1.44 +  (testing "relative File paths"
    1.45 +    (is (= "bar" (relative-path-string (File. "bar")))))
    1.46 +  (testing "absolute File paths are forbidden"
    1.47 +    (is (thrown? IllegalArgumentException (relative-path-string (File. (str File/separator "quux")))))))
    1.48 +
    1.49 +(defn stream-should-have [stream expected-bytes msg]
    1.50 +  (let [actual-bytes (byte-array (alength expected-bytes))]
    1.51 +    (.read stream actual-bytes)
    1.52 +    (is (= -1 (.read stream)) (str msg " : should be end of stream"))
    1.53 +    (is (= (seq expected-bytes) (seq actual-bytes)) (str msg " : byte arrays should match"))))
    1.54 +
    1.55 +(deftest test-input-stream
    1.56 +  (let [file (File/createTempFile "test-input-stream" "txt")
    1.57 +        bytes (.getBytes "foobar")]
    1.58 +    (spit file "foobar")
    1.59 +    (doseq [[expr msg]
    1.60 +            [[file File]
    1.61 +             [(FileInputStream. file) FileInputStream]
    1.62 +             [(BufferedInputStream. (FileInputStream. file)) BufferedInputStream]
    1.63 +             [(.. file toURI) URI]
    1.64 +             [(.. file toURI toURL) URL]
    1.65 +             [(.. file toURI toURL toString) "URL as String"]
    1.66 +             [(.. file toString) "File as String"]]]
    1.67 +      (with-open [s (input-stream expr)]
    1.68 +        (stream-should-have s bytes msg)))))
    1.69 +
    1.70 +(deftest test-streams-buffering
    1.71 +  (let [data (.getBytes "")]
    1.72 +    (is (instance? java.io.BufferedReader (reader data)))
    1.73 +    (is (instance? java.io.BufferedWriter (writer (java.io.ByteArrayOutputStream.))))
    1.74 +    (is (instance? java.io.BufferedInputStream (input-stream data)))
    1.75 +    (is (instance? java.io.BufferedOutputStream (output-stream (java.io.ByteArrayOutputStream.))))))
    1.76 +
    1.77 +(deftest test-streams-defaults
    1.78 +  (let [f (File/createTempFile "clojure.contrib" "test-reader-writer")
    1.79 +        content "test\u2099ing"]
    1.80 +    (try
    1.81 +      (is (thrown? Exception (reader (Object.))))
    1.82 +      (is (thrown? Exception (writer (Object.))))
    1.83 +
    1.84 +      (are [write-to read-from] (= content (do
    1.85 +                                             (spit write-to content)
    1.86 +                                             (slurp* (or read-from write-to))))
    1.87 +        f nil
    1.88 +        (.getAbsolutePath f) nil
    1.89 +        (.toURL f) nil
    1.90 +        (.toURI f) nil
    1.91 +        (java.io.FileOutputStream. f) f
    1.92 +        (java.io.OutputStreamWriter. (java.io.FileOutputStream. f) "UTF-8") f
    1.93 +        f (java.io.FileInputStream. f)
    1.94 +        f (java.io.InputStreamReader. (java.io.FileInputStream. f) "UTF-8"))
    1.95 +
    1.96 +      (is (= content (slurp* (.getBytes content "UTF-8"))))
    1.97 +      (is (= content (slurp* (.toCharArray content))))
    1.98 +      (finally
    1.99 +        (.delete f)))))