Skip to content

Commit

Permalink
Don't create Regexp subclass in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Nov 2, 2022
1 parent faee269 commit 5f70ea6
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions test/json_schemer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ def test_it_handles_regex_anchors
end

def test_it_handles_regexp_resolver
new_regexp_class = Class.new(Regexp) do
new_regexp_class = Class.new do
def self.counts
@@counts ||= 0
end
Expand All @@ -922,13 +922,17 @@ def self.counts=(value)
@@counts = value
end

def initialize(*args)
def initialize(pattern)
@regexp = Regexp.new(pattern)
self.class.counts += 1
super
end

def !~(string)
@regexp !~ string
end
end

schema = JSONSchemer.schema({ 'pattern' => '^foo$' }, regexp_resolver: new_regexp_class.method(:new))
schema = JSONSchemer.schema({ 'pattern' => '^foo$' }, regexp_resolver: -> (pattern) { new_regexp_class.new(pattern) })
assert(schema.valid?('foo'))
assert_equal(1, new_regexp_class.counts)
end
Expand Down

0 comments on commit 5f70ea6

Please sign in to comment.