view src/clojure/contrib/test_contrib/test_properties.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-properties
2 (:refer-clojure :exclude (spit))
3 (:use clojure.test clojure.contrib.properties
4 [clojure.contrib.io :only (spit)])
5 (:import (java.util Properties)
6 (java.io File)))
8 (deftest test-get-system-property
9 (testing "works the same with keywords, symbols, and strings"
10 (is (= (get-system-property "java.home") (get-system-property 'java.home)))
11 (is (= (get-system-property "java.home") (get-system-property :java.home))))
12 (testing "treats second arg as default"
13 (is (= "default" (get-system-property "testing.test-system-property" "default"))))
14 (testing "returns nil for missing properties"
15 (is (nil? (get-system-property "testing.test-system-property")))))
17 (deftest test-set-system-properties
18 (testing "set and then unset a property using keywords"
19 (let [propname :clojure.contrib.java.test-set-system-properties]
20 (is (nil? (get-system-property propname)))
21 (set-system-properties {propname :foo})
22 (is (= "foo") (get-system-property propname))
23 (set-system-properties {propname nil})
24 (is (nil? (get-system-property propname))))))
26 (deftest test-with-system-properties
27 (let [propname :clojure.contrib.java.test-with-system-properties]
28 (testing "sets a property only for the duration of a block"
29 (is (= "foo"
30 (with-system-properties {propname "foo"}
31 (get-system-property propname))))
32 (is (nil? (get-system-property propname)))))
33 (testing "leaves other properties alone"
34 ; TODO: write this test better, using a properties -> map function
35 (let [propname :clojure.contrib.java.test-with-system-properties
36 propcount (count (System/getProperties))]
37 (with-system-properties {propname "foo"}
38 (is (= (inc propcount) (count (System/getProperties)))))
39 (is (= propcount (count (System/getProperties)))))))
41 (deftest test-as-properties
42 (let [expected (doto (Properties.)
43 (.setProperty "a" "b")
44 (.setProperty "c" "d"))]
45 (testing "with a map"
46 (is (= expected
47 (as-properties {:a "b" :c "d"}))))
48 (testing "with a sequence of pairs"
49 (is (= expected
50 (as-properties [[:a :b] [:c :d]]))))))
52 (deftest test-read-properties
53 (let [f (File/createTempFile "test" "properties")]
54 (spit f "a=b\nc=d")
55 (is (= {"a" "b" "c" "d"}
56 (read-properties f)))))
58 (deftest test-write-properties
59 (let [f (File/createTempFile "test" "properties")]
60 (write-properties [['a 'b] ['c 'd]] f)
61 (is (= {"a" "b" "c" "d"}
62 (read-properties f)))))