Mercurial > lasercutter
view 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 source
1 (ns clojure.contrib.test-io2 (:refer-clojure :exclude (spit))3 (:use clojure.test clojure.contrib.io)4 (:import (java.io File FileInputStream BufferedInputStream)5 (java.net URL URI)))7 (deftest file-str-backslash8 (is (= (java.io.File.9 (str "C:" java.io.File/separator10 "Documents" java.io.File/separator11 "file.txt"))12 (file-str "C:\\Documents\\file.txt"))))14 (deftest test-as-file15 (testing "strings"16 (is (= (File. "foo") (as-file "foo"))))17 (testing "Files"18 (is (= (File. "bar") (as-file (File. "bar"))))))20 (deftest test-as-url21 (are [result expr] (= result expr)22 (URL. "http://foo") (as-url (URL. "http://foo"))23 (URL. "http://foo") (as-url "http://foo")24 (URL. "http://foo") (as-url (URI. "http://foo"))25 (URL. "file:/foo") (as-url (File. "/foo"))))27 (deftest test-delete-file28 (let [file (File/createTempFile "test" "deletion")29 not-file (File. (str (java.util.UUID/randomUUID)))]30 (delete-file (.getAbsolutePath file))31 (is (not (.exists file)))32 (is (thrown? ArithmeticException (/ 1 0)))33 (is (thrown? java.io.IOException (delete-file not-file)))34 (is (delete-file not-file :silently))))36 (deftest test-relative-path-string37 (testing "strings"38 (is (= "foo" (relative-path-string "foo"))))39 (testing "absolute path strings are forbidden"40 (is (thrown? IllegalArgumentException (relative-path-string (str File/separator "baz")))))41 (testing "relative File paths"42 (is (= "bar" (relative-path-string (File. "bar")))))43 (testing "absolute File paths are forbidden"44 (is (thrown? IllegalArgumentException (relative-path-string (File. (str File/separator "quux")))))))46 (defn stream-should-have [stream expected-bytes msg]47 (let [actual-bytes (byte-array (alength expected-bytes))]48 (.read stream actual-bytes)49 (is (= -1 (.read stream)) (str msg " : should be end of stream"))50 (is (= (seq expected-bytes) (seq actual-bytes)) (str msg " : byte arrays should match"))))52 (deftest test-input-stream53 (let [file (File/createTempFile "test-input-stream" "txt")54 bytes (.getBytes "foobar")]55 (spit file "foobar")56 (doseq [[expr msg]57 [[file File]58 [(FileInputStream. file) FileInputStream]59 [(BufferedInputStream. (FileInputStream. file)) BufferedInputStream]60 [(.. file toURI) URI]61 [(.. file toURI toURL) URL]62 [(.. file toURI toURL toString) "URL as String"]63 [(.. file toString) "File as String"]]]64 (with-open [s (input-stream expr)]65 (stream-should-have s bytes msg)))))67 (deftest test-streams-buffering68 (let [data (.getBytes "")]69 (is (instance? java.io.BufferedReader (reader data)))70 (is (instance? java.io.BufferedWriter (writer (java.io.ByteArrayOutputStream.))))71 (is (instance? java.io.BufferedInputStream (input-stream data)))72 (is (instance? java.io.BufferedOutputStream (output-stream (java.io.ByteArrayOutputStream.))))))74 (deftest test-streams-defaults75 (let [f (File/createTempFile "clojure.contrib" "test-reader-writer")76 content "test\u2099ing"]77 (try78 (is (thrown? Exception (reader (Object.))))79 (is (thrown? Exception (writer (Object.))))81 (are [write-to read-from] (= content (do82 (spit write-to content)83 (slurp* (or read-from write-to))))84 f nil85 (.getAbsolutePath f) nil86 (.toURL f) nil87 (.toURI f) nil88 (java.io.FileOutputStream. f) f89 (java.io.OutputStreamWriter. (java.io.FileOutputStream. f) "UTF-8") f90 f (java.io.FileInputStream. f)91 f (java.io.InputStreamReader. (java.io.FileInputStream. f) "UTF-8"))93 (is (= content (slurp* (.getBytes content "UTF-8"))))94 (is (= content (slurp* (.toCharArray content))))95 (finally96 (.delete f)))))