spec/regexp-examples_spec.rb in regexp-examples-0.5.4 vs spec/regexp-examples_spec.rb in regexp-examples-0.6.0
- old
+ new
@@ -1,13 +1,19 @@
RSpec.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
+ begin
+ regexp_examples = regexp.examples(max_group_results: 999)
+ rescue
+ # TODO: Find a nicer way to display this?
+ puts "Error generating examples for /#{regexp.source}/"
+ raise $!
+ end
+ expect(regexp_examples).not_to be_empty, "No examples were generated for regexp: /#{regexp.source}/"
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.
+ # 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
@@ -30,11 +36,11 @@
end
def self.examples_are_empty(*regexps)
regexps.each do |regexp|
it do
- expect(regexp.examples).to be_empty
+ expect(regexp.examples).to be_empty, "Unexpected examples for regexp: /#{regexp.source}/"
end
end
end
context 'returns matching strings' do
@@ -179,12 +185,11 @@
context "for unsupported syntax" do
examples_raise_unsupported_syntax_error(
/\p{L}/,
/\p{Arabic}/,
/\p{^Ll}/,
- /(?<name> ... \g<name>*)/,
- /[[:space:]]/
+ /(?<name> ... \g<name>*)/
)
end
context "for control characters" do
examples_exist_and_match(
@@ -228,13 +233,33 @@
/[^\d\D]{2}/,
/[^\d\D]word/
)
end
- context "comment group" do
+ context "for comment groups" do
examples_exist_and_match(
/a(?#comment)b/,
/a(?#ugly backslashy\ comment\\\))b/
+ )
+ end
+
+ context "for POSIX groups" do
+ examples_exist_and_match(
+ /[[:alnum:]]/,
+ /[[:alpha:]]/,
+ /[[:blank:]]/,
+ /[[:cntrl:]]/,
+ /[[:digit:]]/,
+ /[[:graph:]]/,
+ /[[:lower:]]/,
+ /[[:print:]]/,
+ /[[:punct:]]/,
+ /[[:space:]]/,
+ /[[:upper:]]/,
+ /[[:xdigit:]]/,
+ /[[:word:]]/,
+ /[[:ascii:]]/,
+ /[[:^alnum:]]/ # Negated
)
end
context "exact examples match" do
# More rigorous tests to assert that ALL examples are being listed