Skip to content

Commit

Permalink
Merge pull request #62 from lvh/regex-improvements
Browse files Browse the repository at this point in the history
Allow named groups in regex generation
  • Loading branch information
gfredericks authored Aug 11, 2019
2 parents 5978a37 + 7141692 commit 36ce86f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
25 changes: 15 additions & 10 deletions src/com/gfredericks/test/chuck/regexes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,21 @@
{: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 @@ -164,7 +164,8 @@
(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
#_"[{&&[}{]}]"
Expand All @@ -174,7 +175,6 @@
"\\c\\Q0" "[^\\x{2f498}]"])

(defspec generator-regression-spec (times 1000)
;; TODO: make a prop in test.chuck that's like for
(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

0 comments on commit 36ce86f

Please sign in to comment.