Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow named groups in regex generation #62

Merged
merged 4 commits into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/com/gfredericks/test/chuck/regexes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,24 @@
:DanglingCurlyRepetitions (constantly nil)
:ParenthesizedExpr (fn
([alternation]
{:type :group
:elements [alternation]})
{:type :group
:elements [alternation]})
([group-flags alternation]
(cond-> {:type :group
:elements [alternation]
:flag group-flags}
;; If the flags are just an empty
;; NonCapturingMatchFlags, we can safely
;; ignore it; this should correspond to
;; something like #"foo(?:bar)*"
(not= group-flags
[:GroupFlags [:NonCapturingMatchFlags [:MatchFlagsExpr]]])
(assoc :unsupported #{:flags}))))
(let [parsed {:type :group
:elements [alternation]
:flag group-flags}]
;; :NonCapturingMatchFlags corresponds to #"foo(?:bar)*"
;; :NamedCapturingGroup corresponds to #"foo(?<x>bar)"
;; We can just ignore these for generation.
(if (or
;; e.g., #"(?:bar)" (no flags)
(= group-flags
[:GroupFlags [:NonCapturingMatchFlags [:MatchFlagsExpr]]])
;; e.g., #"(?<x>bar)"
(let [[flag-header [flag-type flag-details]] group-flags]
(= :NamedCapturingGroup flag-type)))
parsed
(assoc parsed :unsupported #{:flags})))))
:SingleExpr identity
:LinebreakMatcher (constantly
{:type :alternation
Expand Down
4 changes: 2 additions & 2 deletions test/com/gfredericks/test/chuck/regexes_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@
(def generator-regression-cases
["[\\c\\u]" "\\c\\Q\u0080" "\\c\\\u0080" "\\v"
"[^aceg\\S]"
"foo(?:bar)*" ; should allow non-capturing groups
"foo(?:bar)*" ;; should allow non-capturing groups
"iddqd(?<x>idkfa)" ;; should allow named groups

;; intersection unsupported for now
#_"[{&&[}{]}]"])

(defspec generator-regression-spec (times 1000)
;; TODO: make a prop in test.chuck that's like for
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced this with #61.

(prop/for-all [[re s] (gen'/for [re-s (gen/elements generator-regression-cases)
:let [re (re-pattern re-s)]
s (regexes/gen-string-from-regex re)]
Expand Down