describe Regexp, "#examples" do def self.examples_exist_and_match(*regexps) regexps.each do |regexp| it do regexp_examples = regexp.examples expect(regexp_examples).not_to be_empty regexp_examples.each { |example| expect(example).to match(/\A(?:#{regexp.source})\z/) } # Note: /\A...\z/ is used, to prevent misleading examples from passing the test. # For example, we don't want things like: # /a*/.examples to include "xyz" # /a|b/.examples to include "bad" end end end context 'returns matching strings' do context "for basic repeaters" do examples_exist_and_match( /a/, /a*/, /a+/, /a?/, /a{1}/, /a{1,}/, /a{1,2}/ ) end context "for basic groups" do examples_exist_and_match( /[a]/, /(a)/, /a|b/, /./ ) end context "for complex char groups (square brackets)" do examples_exist_and_match( /[abc]/, /[a-c]/, /[abc-e]/, /[^a-zA-Z]/, /[\w]/, /[]]/, # TODO: How to suppress annoying warnings on this test? /[\]]/, /[\\]/, /[\\\]]/, /[\n-\r]/, /[\-]/, /[%-+]/ # This regex is "supposed to" match some surprising things!!! ) end context "for complex multi groups" do examples_exist_and_match( /(normal)/, /(?:nocapture)/, /(?namedgroup)/, /(?namedgroup) \k/ ) # TODO: These are not yet implemented # (expect to raise exception) # /(?=lookahead)/, # /(?!neglookahead)/, # /(?<=lookbehind)/, # /(?