Skip to content

Commit fe617a8

Browse files
sattvikstuarthalloway
authored andcommitted
Add test cases for CLJ-1561 fixes.
These tests are based of Paul Stadig's test cases from <http://github.com/pjstadig/clojure-line-numbers>. In general, these verify the changes made. Signed-off-by: Stuart Halloway <[email protected]>
1 parent 715754d commit fe617a8

File tree

2 files changed

+157
-4
lines changed

2 files changed

+157
-4
lines changed

test/clojure/test_clojure/compilation.clj

+36-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
(ns clojure.test-clojure.compilation
1313
(:import (clojure.lang Compiler Compiler$CompilerException))
1414
(:require [clojure.test.generative :refer (defspec)]
15-
[clojure.data.generators :as gen])
15+
[clojure.data.generators :as gen]
16+
[clojure.test-clojure.compilation.line-number-examples :as line])
1617
(:use clojure.test
1718
[clojure.test-helper :only (should-not-reflect should-print-err-message)]))
1819

@@ -30,7 +31,7 @@
3031

3132
(string? (:doc m)) true
3233
(> (.length (:doc m)) 0) true
33-
34+
3435
(string? (:file m)) true
3536
(> (.length (:file m)) 0) true
3637

@@ -53,7 +54,7 @@
5354
(is (eval `(= Integer/TYPE ~Integer/TYPE)))
5455
(is (eval `(= Long/TYPE ~Long/TYPE)))
5556
(is (eval `(= Short/TYPE ~Short/TYPE)))))
56-
57+
5758
(deftest test-compiler-resolution
5859
(testing "resolve nonexistent class create should return nil (assembla #262)"
5960
(is (nil? (resolve 'NonExistentClass.)))))
@@ -171,7 +172,7 @@
171172
(deftest primitive-return-decl
172173
(should-not-reflect #(loop [k 5] (recur (clojure.test-clojure.compilation/primfn))))
173174
(should-not-reflect #(loop [k 5.0] (recur (clojure.test-clojure.compilation/primfn 0))))
174-
175+
175176
(should-print-err-message #"(?s).*k is not matching primitive.*"
176177
#(loop [k (clojure.test-clojure.compilation/primfn)] (recur :foo))))
177178

@@ -257,6 +258,37 @@
257258
(binding [*compile-path* "target/test-classes"]
258259
(compile 'clojure.test-clojure.compilation.examples))
259260

261+
262+
(deftest test-compiler-line-numbers
263+
(let [fails-on-line-number? (fn [expected function]
264+
(try
265+
(function)
266+
nil
267+
(catch Throwable t
268+
(let [frames (filter #(= "line_number_examples.clj" (.getFileName %))
269+
(.getStackTrace t))
270+
_ (if (zero? (count frames))
271+
(.printStackTrace t)
272+
)
273+
actual (.getLineNumber ^StackTraceElement (first frames))]
274+
(= expected actual)))))]
275+
(is (fails-on-line-number? 13 line/instance-field))
276+
(is (fails-on-line-number? 19 line/instance-field-reflected))
277+
(is (fails-on-line-number? 25 line/instance-field-unboxed))
278+
(is (fails-on-line-number? 32 line/instance-field-assign))
279+
(is (fails-on-line-number? 40 line/instance-field-assign-reflected))
280+
(is (fails-on-line-number? 47 line/static-field-assign))
281+
(is (fails-on-line-number? 54 line/instance-method))
282+
(is (fails-on-line-number? 61 line/instance-method-reflected))
283+
(is (fails-on-line-number? 68 line/instance-method-unboxed))
284+
(is (fails-on-line-number? 74 line/static-method))
285+
(is (fails-on-line-number? 80 line/static-method-reflected))
286+
(is (fails-on-line-number? 86 line/static-method-unboxed))
287+
(is (fails-on-line-number? 92 line/invoke))
288+
(is (fails-on-line-number? 101 line/threading))
289+
(is (fails-on-line-number? 112 line/keyword-invoke))
290+
(is (fails-on-line-number? 119 line/invoke-cast))))
291+
260292
(deftest CLJ-979
261293
(is (= clojure.test_clojure.compilation.examples.X
262294
(class (clojure.test-clojure.compilation.examples/->X))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
(ns clojure.test-clojure.compilation.line-number-examples
2+
"Example code taken from Paul Stadig and updated by Daniel Solano Gómez.
3+
4+
Original source at:
5+
https://github.com/pjstadig/clojure-line-numbers/blob/master/src/clojure_line_numbers/core.clj"
6+
(:import (clojure.lang PersistentHashMap)))
7+
8+
(defrecord Thing [field ^long primitive])
9+
10+
(defn instance-field
11+
"I throw an exception in an instance field form."
12+
[]
13+
(.field
14+
^Thing (identity nil)))
15+
16+
(defn instance-field-reflected
17+
"I throw an exception in an instance field form."
18+
[]
19+
(.field
20+
(identity nil)))
21+
22+
(defn instance-field-unboxed
23+
"I throw an exception in an instance field form."
24+
^long []
25+
(.primitive
26+
^Thing (identity nil)))
27+
28+
(defn instance-field-assign
29+
"I throw an exception in an instance field assignment form."
30+
[]
31+
(set!
32+
(.field
33+
^Thing (identity nil))
34+
(identity nil)))
35+
36+
(defn instance-field-assign-reflected
37+
"I throw an exception in an instance field assignment form."
38+
[]
39+
(set!
40+
(.field
41+
(identity nil))
42+
(identity nil)))
43+
44+
(defn static-field-assign
45+
"I throw an exception in a static field assignment form."
46+
[]
47+
(set!
48+
PersistentHashMap/EMPTY
49+
(identity nil)))
50+
51+
(defn instance-method
52+
"I throw an exception in an instance method form."
53+
[]
54+
(.without
55+
^PersistentHashMap (identity nil)
56+
:key))
57+
58+
(defn instance-method-reflected
59+
"I throw an exception in an instance method form."
60+
[]
61+
(.without
62+
(identity nil)
63+
:key))
64+
65+
(defn instance-method-unboxed
66+
"I throw an exception in an instance method form."
67+
^long []
68+
(.count
69+
^PersistentHashMap (identity nil)))
70+
71+
(defn static-method
72+
"I throw an exception in a static method form."
73+
[]
74+
(PersistentHashMap/create
75+
^java.util.Map (identity nil)))
76+
77+
(defn static-method-reflected
78+
"I throw an exception in a static method form."
79+
[]
80+
(String/copyValueOf
81+
(identity nil)))
82+
83+
(defn static-method-unboxed
84+
"I throw an exception in a static method form."
85+
^long []
86+
(Long/parseLong
87+
^String (identity nil)))
88+
89+
(defn invoke
90+
"I throw an exception in an invoke form."
91+
[]
92+
((identity nil)
93+
(identity nil)))
94+
95+
(defn threading
96+
"I throw an exception in a threading form."
97+
[]
98+
(-> :foo
99+
(identity)
100+
(identity)
101+
((identity nil))
102+
(identity)
103+
(identity)))
104+
105+
(defn keyword-invoke
106+
"I throw an exception in a keyword invoke."
107+
[]
108+
(letfn [(get-map []
109+
(let [t (transient {})]
110+
(persistent! t)
111+
t))]
112+
(:foo
113+
(get-map))))
114+
115+
(defn invoke-cast
116+
"I throw an exception casting to IFn in an invoke form."
117+
[]
118+
;; This code formatting is intentional.
119+
(
120+
(identity 1)
121+
(identity nil)))

0 commit comments

Comments
 (0)