forked from technomancy/slamhound
/
hound_test.clj
112 lines (103 loc) · 4.79 KB
/
hound_test.clj
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
(ns slam.hound-test
(:require [clojure.java.io :as io]
[clojure.test :refer [deftest is testing]]
[slam.hound :refer [reconstruct -main]])
(:import (java.io File StringReader)))
(def basic-ns (str '(ns slamhound.sample
"Testing some things going on here."
(:use [slam.hound.stitch :only [ns-from-map]]
[clojure.test :only [is]]
;; 'testing' isn't used, so gets dropped
[clojure.test :only [deftest testing]])
(:require [clojure.java.io :as io]
[clojure.set :as set])
(:import java.io.File java.io.ByteArrayInputStream
clojure.lang.Compiler$BodyExpr
java.util.UUID)
(:refer-clojure :exclude [compile test])
(:gen-class))
'(set/union #{:a} #{:b})
'(UUID/randomUUID)
'(instance? Compiler$BodyExpr nil)
'(io/copy (ByteArrayInputStream. (.getBytes "remotely"))
(doto (File. "/tmp/remotely-human") .deleteOnExit))
'(deftest ^:unit test-ns-to-map
(is (= (ns-from-map {:ns 'slam.hound}))))))
(deftest ^:integration test-slamhound!!!
(is (= "(ns slamhound.sample
\"Testing some things going on here.\"
(:require [clojure.java.io :as io]
[clojure.set :as set]
[clojure.test :refer :all]
[slam.hound.stitch :refer [ns-from-map]])
(:import (clojure.lang Compiler$BodyExpr)
(java.io ByteArrayInputStream File)
(java.util UUID))
(:refer-clojure :exclude [compile test])
(:gen-class))
"
(reconstruct (StringReader. basic-ns)))))
(deftest ^:integration test-regression
(is (= "(ns foo.bar
(:require [clj-schema.validation :as val]))
"
(reconstruct (StringReader.
(str '(ns foo.bar
(:require [clj-schema.validation :as val]))
'(val/validation-errors [[:name] String]
{:name "Bob"})))))))
(deftest ^:integration test-finds-alias-vars-in-nested-maps-and-sets
(is (= "(ns foo.bar
(:require [clj-schema.validation :as val]))
"
(reconstruct (StringReader.
(str '(ns foo.bar
(:require [clj-schema.validation :as val]))
'#{:x {:a (val/validation-errors
[[:name] String]
{:name "Bob"})}}))))))
(deftest ^:integration test-chooses-referred-vars-preferring-those-referred-to-in-old-ns
(testing "since both clojure.string and clojure.set are present in the original namespace
we must disambiguate further by preferring namespaces that also referred the var
in the old version of the ns form"
(is (= "(ns foo.bar
(:require [clojure.set :refer [union]]
[clojure.string :refer [join]]))
"
(reconstruct (StringReader.
(str '(ns foo.bar
(:require [clojure.string :only [join]]
[clojure.set :refer [union]]))
'(do
(defn f [xs]
(join "," xs))
(defn g [a b]
(union a b))))))))))
(deftest ^:integration test-prefers-requires-as-clauses-from-orig-ns
;; korma.core is on the :dev-dependencies, and was getting erroneously picked
;; for these 2 namespaces
(testing "original ns has a require/refer"
(is (= "(ns foo.bar
(:require [clojure.string :refer [join]]))
"
(reconstruct (StringReader.
(str '(ns foo.bar
(:require [clojure.string :refer [join]]))
'(defn do-it! []
(join "," ["a" "b" "c"]))))))))
(testing "original ns has a use/only"
(is (= "(ns foo.bar
(:require [clojure.string :refer [join]]))
"
(reconstruct (StringReader.
(str '(ns foo.bar
(:use [clojure.string :only [join]]))
'(defn do-it! []
(join "," ["a" "b" "c"])))))))))
(deftest ^:integration test-main
(let [tmp (doto (File/createTempFile "test_namespace_copy" ".clj")
.deleteOnExit)]
(io/copy (io/reader (io/resource "test_namespace.clj")) tmp)
(-main tmp)
(is (= (slurp (io/resource "reconstructed_namespace.clj"))
(slurp tmp)))))