Skip to content

Commit

Permalink
Moved arrow-line-number into file-position.clj
Browse files Browse the repository at this point in the history
  • Loading branch information
marick committed Jan 26, 2011
1 parent ba3d34b commit 52ca2ca
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 47 deletions.
7 changes: 0 additions & 7 deletions src/midje/midje_forms/dissecting.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@
(recur (conj so-far whole-body)
(nthnext remainder (count whole-body)))))))

;; Yeah, it's not tail-recursive. So sue me.
(defn arrow-line-number [arrow-loc]
(try (or (-> arrow-loc zip/left zip/node meta :line)
(-> arrow-loc zip/right zip/node meta :line)
(inc (arrow-line-number (zip/prev arrow-loc))))
(catch Throwable ex nil)))

(defn extract-nested-prerequisite [fake-form]
(-> fake-form second second))

3 changes: 2 additions & 1 deletion src/midje/midje_forms/translating.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
(ns midje.midje-forms.translating
(:use clojure.contrib.def)
(:use [clojure.contrib.seq :only [separate]])
(:use [midje.util thread-safe-var-nesting wrapping form-utils laziness form-utils])
(:use [midje.util thread-safe-var-nesting wrapping form-utils laziness form-utils]
[midje.util.file-position :only [arrow-line-number]])
(:use midje.metaconstants)
(:require [clojure.zip :as zip])
(:use [midje.midje-forms building recognizing dissecting moving-around editing])
Expand Down
11 changes: 10 additions & 1 deletion src/midje/util/file_position.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
;; -*- indent-tabs-mode: nil -*-

(ns midje.util.file-position)
(ns midje.util.file-position
(:require [clojure.zip :as zip]))

(defn user-file-position
"Guesses the file position (basename and line number) that the user is
Expand All @@ -18,3 +19,11 @@
"Guess position, using metadata of given form for the line number."
[form]
(line-number-known (:line (meta form))))

;; Yeah, it's not tail-recursive. So sue me.
(defn arrow-line-number [arrow-loc]
(try (or (-> arrow-loc zip/left zip/node meta :line)
(-> arrow-loc zip/right zip/node meta :line)
(inc (arrow-line-number (zip/prev arrow-loc))))
(catch Throwable ex nil)))

34 changes: 0 additions & 34 deletions test/midje/midje_forms/t_dissecting.clj
Original file line number Diff line number Diff line change
Expand Up @@ -61,40 +61,6 @@



(defn at-line [line-no form] (with-meta form {:line line-no}))

(facts "about determining an arrow sequences line number from nearby forms"

"Typical case is form on left. (f 1) => 5"
(let [form `( ~(at-line 33 '(f 1)) => 5)
loc (-> form zip/seq-zip zip/down)]
loc => loc-is-start-of-arrow-sequence?
(arrow-line-number (zip/right loc)) => 33)

"When form on the left is has no line, check right: ...a... => (exactly 1)"
(let [form `( ...a... => ~(at-line 33 '(exactly 1)))
loc (-> form zip/seq-zip zip/down)]
loc => loc-is-start-of-arrow-sequence?
(arrow-line-number (zip/right loc)) => 33)

"If both sides have line numbers, the left takes precedence: (f 1) => (exactly 1)"
(let [form `( ~(at-line 33 '(f 1)) => ~(at-line 34 '(exactly 1)))
loc (-> form zip/seq-zip zip/down)]
loc => loc-is-start-of-arrow-sequence?
(arrow-line-number (zip/right loc)) => 33)

"If neither side has a line number, look to the left and add 1: (let [a 2] a => b)"
(let [form `( (let ~(at-line 32 '[a 2]) a => b))
loc (-> form zip/seq-zip zip/down zip/down zip/right zip/right)]
loc => loc-is-start-of-arrow-sequence?
(arrow-line-number (zip/right loc)) => 33)

"Default retult is nil."
(let [form '(1 => 2)
loc (-> form zip/seq-zip zip/down)]
loc => loc-is-start-of-arrow-sequence?
(arrow-line-number (zip/right loc)) => nil))


(facts "about extracting nested prerequisites"
(extract-nested-prerequisite '(fake (f (g)) => 3)) => '(g))
Expand Down
42 changes: 38 additions & 4 deletions test/midje/util/t_file_position.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
;; -*- indent-tabs-mode: nil -*-

(ns midje.util.t-file-position
(:use [midje.util.file-position])
(:use [midje.sweet])
(:use [clojure.test])
(:use [midje.test-util]))
(:use [midje.util.file-position]
[midje sweet test-util]
[midje.midje-forms.recognizing :only [loc-is-start-of-arrow-sequence?]])
(:require [clojure.zip :as zip]))

(defn this-file [line-number]
["t_file_position.clj" line-number])
Expand Down Expand Up @@ -65,3 +65,37 @@
(fact "line-number-known is used when you know the line but not the file"
(let [position (line-number-known 33)]
position => ["t_file_position.clj", 33]))

(defn at-line [line-no form] (with-meta form {:line line-no}))

(facts "about determining a line number from forms near an arrow"
"Typical case is form on left. (f 1) => 5"
(let [form `( ~(at-line 33 '(f 1)) => 5)
loc (-> form zip/seq-zip zip/down)]
loc => loc-is-start-of-arrow-sequence?
(arrow-line-number (zip/right loc)) => 33)

"When form on the left is has no line, check right: ...a... => (exactly 1)"
(let [form `( ...a... => ~(at-line 33 '(exactly 1)))
loc (-> form zip/seq-zip zip/down)]
loc => loc-is-start-of-arrow-sequence?
(arrow-line-number (zip/right loc)) => 33)

"If both sides have line numbers, the left takes precedence: (f 1) => (exactly 1)"
(let [form `( ~(at-line 33 '(f 1)) => ~(at-line 34 '(exactly 1)))
loc (-> form zip/seq-zip zip/down)]
loc => loc-is-start-of-arrow-sequence?
(arrow-line-number (zip/right loc)) => 33)

"If neither side has a line number, look to the left and add 1: (let [a 2] a => b)"
(let [form `( (let ~(at-line 32 '[a 2]) a => b))
loc (-> form zip/seq-zip zip/down zip/down zip/right zip/right)]
loc => loc-is-start-of-arrow-sequence?
(arrow-line-number (zip/right loc)) => 33)

"Default result is nil."
(let [form '(1 => 2)
loc (-> form zip/seq-zip zip/down)]
loc => loc-is-start-of-arrow-sequence?
(arrow-line-number (zip/right loc)) => nil))

0 comments on commit 52ca2ca

Please sign in to comment.