Skip to content

Commit

Permalink
Make context string parsing smarter (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewdowney committed Dec 24, 2022
1 parent f5951f6 commit f34cb60
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 23 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ For a project that only uses rich comment tests, you can add an alias to
:exec-args {:dirs #{"src"}}}}}
```

This is what happens when you run this project with:

clj -X:test1

## Changes
v0.0.3
- (@lilactown) Automatically quote result when used with => [#5](https://github.com/matthewdowney/rich-comment-tests/issues/5)
Expand Down
2 changes: 1 addition & 1 deletion bb.edn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{:tasks
{test {:doc "run unit tests"
:task (clojure "-X:test1")}
:task (clojure "-X:test")}

coords {:doc "update the dependency coordinates in README.md"
:requires ([clojure.string :refer [split split-lines replace]]
Expand Down
48 changes: 32 additions & 16 deletions src/com/mjdowney/rich_comment_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,40 @@
z/right))
(filter z/sexpr-able?)))

(defn result-comment?
"A string like \";=> _\" or \";=>> _\" or \";; => _\""
[s]
(re-matches #"\s*;+\s?=>{1,2}.+\n" s))

(defn remove-form-expstr-pairs [[a b & _ :as xs]]
(lazy-seq
(when (seq xs)
(if (and a b ; is a pair
(z/sexpr-able? b) ; starting with a form
(= (z/tag a) :comment) ; followed by expectation string comment
(result-comment? (z/string a)))
(remove-form-expstr-pairs (drop 2 xs))
(cons a (remove-form-expstr-pairs (rest xs)))))))

(defn context-strings
"A series of string comments preceding the test sexpr (until a line break)."
"A series of string comments preceding the test sexpr."
[test-sexpr-zloc]
(let [nodes-preceding-assertion (rest (iterate z/left* test-sexpr-zloc))]
(reverse
(sequence
(comp
; Look for *comments* preceding the assertion, but stop searching if
; we hit a line break
(take-while
#(and (z/whitespace-or-comment? %) (not (z/linebreak? %))))

; Convert to strings and remove any empties
(map z/string)
(remove (comp empty? string/trim)))
nodes-preceding-assertion))))
(->> nodes-preceding-assertion
; take up to the next completely blank line
(take-while (complement z/linebreak?))

; drop any form + expectation string comment pairs
(remove z/whitespace?)
remove-form-expstr-pairs

; take all other comments and remove empties
(filter z/whitespace-or-comment?)
(map z/string)
(remove (comp empty? string/trim))

; finally, put them back in top-to-bottom order
reverse)))

(defn expectation-data
"Parse a string representing the expectation for a test expression and an
Expand All @@ -132,9 +150,7 @@
(+ 1 1)
;; => 2"
[test-sexpr-zloc]
(let [nodes-following-assertion (rest (iterate z/right* test-sexpr-zloc))
; A string like ";=> _" or ";=>> _" or ";; => _"
result-comment? #(re-matches #"\s*;+\s?=>{1,2}.+\n" %)]
(let [nodes-following-assertion (rest (iterate z/right* test-sexpr-zloc))]
(when-let [[fst-line & rest]
(->> nodes-following-assertion
(take-while z/whitespace-or-comment?)
Expand Down
46 changes: 44 additions & 2 deletions test/com/mjdowney/rich_comment_tests_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
(ns com.mjdowney.rich-comment-tests-test
(:require [clojure.test :refer :all]
[com.mjdowney.rich-comment-tests.test-runner :as test-runner]))
(:require [clojure.string :as string]
[clojure.test :refer :all]
[com.mjdowney.rich-comment-tests :as rct]
[com.mjdowney.rich-comment-tests.test-runner :as test-runner]
[matcho.core :as m]
[rewrite-clj.zip :as z]))

(deftest rct-tests
(test-runner/run-tests-in-file-tree! :dirs #{"src"}))

(defn ctx-strings [comment-body]
(let [form (str "^:rct/test\n(comment\n" comment-body "\n)")
>str (comp string/trim string/join)]
(->> (z/of-string form {:track-position? true})
rct/rct-zlocs
(mapcat rct/rct-data-seq)
(map (juxt :test-sexpr (comp >str :context-strings)))
(into {}))))

(deftest context-strings-test
(m/assert
'{(* 0 0) ""

(+ 1 1) ";; Test for\n;; addition"
(+ 2 2) ";; Test for\n;; addition"

(* 1 1) ""
(* 2 3) ""

(* 2 2) ";; Squares\n;; and such"
(* 3 3) ";; Squares\n;; and such\n; 3 squared"}
(ctx-strings
"(* 0 0) ;=> 0
;; Test for
;; addition
(+ 1 1) ;=> 2
(+ 2 2) ;=> 4
(* 1 1) ;=> 1
(* 2 3) ;=> 6
;; Squares
;; and such
(* 2 2) ;=> 4
; 3 squared
(* 3 3) ;=> 9")))

0 comments on commit f34cb60

Please sign in to comment.