File: vars.clj

package info (click to toggle)
clojure1.4 1.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,364 kB
  • sloc: java: 25,822; xml: 417; sh: 69; makefile: 44
file content (91 lines) | stat: -rw-r--r-- 2,904 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
;   Copyright (c) Rich Hickey. All rights reserved.
;   The use and distribution terms for this software are covered by the
;   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;   which can be found in the file epl-v10.html at the root of this distribution.
;   By using this software in any fashion, you are agreeing to be bound by
;   the terms of this license.
;   You must not remove this notice, or any other, from this software.

; Author: Frantisek Sodomka, Stephen C. Gilardi


(ns clojure.test-clojure.vars
  (:use clojure.test))

; http://clojure.org/vars

; def
; defn defn- defonce

; declare intern binding find-var var

(def ^:dynamic a)
(deftest test-binding
  (are [x y] (= x y)
      (eval `(binding [a 4] a)) 4     ; regression in Clojure SVN r1370
  ))

; var-get var-set alter-var-root [var? (predicates.clj)]
; with-in-str with-out-str
; with-open

(deftest test-with-local-vars
  (let [factorial (fn [x]
                    (with-local-vars [acc 1, cnt x]
                      (while (> @cnt 0)
                        (var-set acc (* @acc @cnt))
                        (var-set cnt (dec @cnt)))
                      @acc))]
    (is (= (factorial 5) 120))))

(deftest test-with-precision
  (are [x y] (= x y)
       (with-precision 4 (+ 3.5555555M 1)) 4.556M
       (with-precision 6 (+ 3.5555555M 1)) 4.55556M
       (with-precision 6 :rounding CEILING     (+ 3.5555555M 1)) 4.55556M
       (with-precision 6 :rounding FLOOR       (+ 3.5555555M 1)) 4.55555M
       (with-precision 6 :rounding HALF_UP     (+ 3.5555555M 1)) 4.55556M
       (with-precision 6 :rounding HALF_DOWN   (+ 3.5555555M 1)) 4.55556M
       (with-precision 6 :rounding HALF_EVEN   (+ 3.5555555M 1)) 4.55556M
       (with-precision 6 :rounding UP          (+ 3.5555555M 1)) 4.55556M
       (with-precision 6 :rounding DOWN        (+ 3.5555555M 1)) 4.55555M
       (with-precision 6 :rounding UNNECESSARY (+ 3.5555M 1))    4.5555M))

(deftest test-settable-math-context
  (is (=
       (clojure.main/with-bindings
         (set! *math-context* (java.math.MathContext. 8))
         (+ 3.55555555555555M 1))
       4.5555556M)))

; set-validator get-validator

; doc find-doc test

(def stub-me :original)

(deftest test-with-redefs-fn
  (let [p (promise)]
    (with-redefs-fn {#'stub-me :temp}
      (fn []
        (.start (Thread. #(deliver p stub-me)))
        @p))
    (is (= :temp @p))
    (is (= :original stub-me))))

(deftest test-with-redefs
  (let [p (promise)]
    (with-redefs [stub-me :temp]
      (.start (Thread. #(deliver p stub-me)))
      @p)
    (is (= :temp @p))
    (is (= :original stub-me))))

(deftest test-with-redefs-throw
  (let [p (promise)]
    (is (thrown? Exception
      (with-redefs [stub-me :temp]
        (deliver p stub-me)
        (throw (Exception. "simulated failure in with-redefs")))))
    (is (= :temp @p))
    (is (= :original stub-me))))