2018-09-26 17:55:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2019-05-29 11:14:21 -04:00
|
|
|
RSpec.describe Capybara::Selector::RegexpDisassembler, :aggregate_failures do
|
2018-09-26 17:55:56 -04:00
|
|
|
it 'handles strings' do
|
|
|
|
verify_strings(
|
|
|
|
/abcdef/ => %w[abcdef],
|
|
|
|
/abc def/ => ['abc def']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles escaped characters' do
|
|
|
|
verify_strings(
|
2018-10-15 21:48:15 -04:00
|
|
|
/abc\\def/ => %w[abc\def],
|
|
|
|
/abc\.def/ => %w[abc.def],
|
|
|
|
/\nabc/ => ["\nabc"],
|
|
|
|
%r{abc/} => %w[abc/],
|
|
|
|
/ab\++cd/ => %w[ab+ cd]
|
2018-09-26 17:55:56 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles wildcards' do
|
|
|
|
verify_strings(
|
|
|
|
/abc.*def/ => %w[abc def],
|
|
|
|
/.*def/ => %w[def],
|
|
|
|
/abc./ => %w[abc],
|
|
|
|
/abc.*/ => %w[abc],
|
|
|
|
/abc.def/ => %w[abc def],
|
2018-11-06 16:32:08 -05:00
|
|
|
/abc.def.ghi/ => %w[abc def ghi],
|
|
|
|
/abc.abcd.abcde/ => %w[abcde],
|
|
|
|
/.*/ => []
|
2018-09-26 17:55:56 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-11-06 16:32:08 -05:00
|
|
|
it 'ignores optional characters for substrings' do
|
|
|
|
{
|
2018-09-26 17:55:56 -04:00
|
|
|
/abc*def/ => %w[ab def],
|
|
|
|
/abc*/ => %w[ab],
|
2018-11-06 16:32:08 -05:00
|
|
|
/c*/ => [],
|
2018-09-26 17:55:56 -04:00
|
|
|
/abc?def/ => %w[ab def],
|
|
|
|
/abc?/ => %w[ab],
|
|
|
|
/abc?def?/ => %w[ab de],
|
2018-11-06 16:32:08 -05:00
|
|
|
/abc?def?g/ => %w[ab de g],
|
|
|
|
/d?/ => []
|
|
|
|
}.each do |regexp, expected|
|
2019-07-23 16:03:00 -04:00
|
|
|
expect(described_class.new(regexp).substrings).to eq expected
|
2018-11-06 16:32:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles optional characters for #alternated_substrings' do
|
|
|
|
verify_alternated_strings(
|
2019-11-28 17:41:13 -05:00
|
|
|
{
|
|
|
|
/abc*def/ => [%w[ab def]],
|
|
|
|
/abc*/ => [%w[ab]],
|
|
|
|
/c*/ => [],
|
|
|
|
/abc?def/ => [%w[abdef], %w[abcdef]],
|
|
|
|
/abc?/ => [%w[ab]],
|
|
|
|
/abc?def?/ => [%w[abde], %w[abcde]],
|
|
|
|
/abc?def?g/ => [%w[abdeg], %w[abdefg], %w[abcdeg], %w[abcdefg]],
|
|
|
|
/d?/ => []
|
|
|
|
}
|
2018-09-26 17:55:56 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles character classes' do
|
|
|
|
verify_strings(
|
|
|
|
/abc[a-z]/ => %w[abc],
|
|
|
|
/abc[a-z]def[0-9]g/ => %w[abc def g],
|
|
|
|
/[0-9]abc/ => %w[abc],
|
2018-10-30 15:04:40 -04:00
|
|
|
/[0-9]+/ => [],
|
2018-09-26 17:55:56 -04:00
|
|
|
/abc[0-9&&[^7]]/ => %w[abc]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles posix bracket expressions' do
|
|
|
|
verify_strings(
|
|
|
|
/abc[[:alpha:]]/ => %w[abc],
|
|
|
|
/[[:digit:]]abc/ => %w[abc],
|
|
|
|
/abc[[:print:]]def/ => %w[abc def]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles repitition' do
|
|
|
|
verify_strings(
|
|
|
|
/abc{3}/ => %w[abccc],
|
|
|
|
/abc{3}d/ => %w[abcccd],
|
|
|
|
/abc{0}/ => %w[ab],
|
|
|
|
/abc{,2}/ => %w[ab],
|
|
|
|
/abc{2,}/ => %w[abcc],
|
|
|
|
/def{1,5}/ => %w[def],
|
|
|
|
/abc+def/ => %w[abc def],
|
|
|
|
/ab(cde){,4}/ => %w[ab],
|
|
|
|
/(ab){,2}cd/ => %w[cd],
|
|
|
|
/(abc){2,3}/ => %w[abcabc],
|
|
|
|
/(abc){3}/ => %w[abcabcabc],
|
|
|
|
/ab{2,3}cd/ => %w[abb cd],
|
|
|
|
/(ab){2,3}cd/ => %w[abab cd]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles non-greedy repetition' do
|
|
|
|
verify_strings(
|
|
|
|
/abc.*?/ => %w[abc],
|
|
|
|
/abc+?/ => %w[abc],
|
|
|
|
/abc*?cde/ => %w[ab cde],
|
|
|
|
/(abc)+?def/ => %w[abc def],
|
|
|
|
/ab(cde)*?fg/ => %w[ab fg]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-10-30 15:04:40 -04:00
|
|
|
it 'ignores alternation for #substrings' do
|
|
|
|
{
|
2018-09-26 17:55:56 -04:00
|
|
|
/abc|def/ => [],
|
|
|
|
/ab(?:c|d)/ => %w[ab],
|
2018-10-30 15:04:40 -04:00
|
|
|
/ab(c|d|e)fg/ => %w[ab fg],
|
|
|
|
/ab?(c|d)fg/ => %w[a fg],
|
|
|
|
/ab(c|d)ef/ => %w[ab ef],
|
|
|
|
/ab(cd?|ef)g/ => %w[ab g],
|
|
|
|
/ab(cd|ef*)g/ => %w[ab g],
|
|
|
|
/ab|cd*/ => [],
|
|
|
|
/cd(?:ef|gh)|xyz/ => [],
|
|
|
|
/(cd(?:ef|gh)|xyz)/ => [],
|
|
|
|
/cd(ef|gh)+/ => %w[cd],
|
|
|
|
/cd(ef|gh)?/ => %w[cd],
|
|
|
|
/cd(ef|gh)?ij/ => %w[cd ij],
|
|
|
|
/cd(ef|gh)+ij/ => %w[cd ij],
|
|
|
|
/cd(ef|gh){2}ij/ => %w[cd ij],
|
2018-11-08 14:04:31 -05:00
|
|
|
/(cd(ef|g*))/ => %w[cd],
|
|
|
|
/ab(cd){0,2}ef/ => %w[ab ef],
|
|
|
|
/ab(cd){0,1}ef/ => %w[ab ef],
|
|
|
|
/ab(cd|cd)ef/ => %w[ab ef],
|
|
|
|
/ab(cd|cd)?ef/ => %w[ab ef],
|
|
|
|
/ab\\?cd/ => %w[ab cd]
|
2018-10-30 15:04:40 -04:00
|
|
|
}.each do |regexp, expected|
|
2019-07-23 16:03:00 -04:00
|
|
|
expect(described_class.new(regexp).substrings).to eq expected
|
2018-10-30 15:04:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-06 16:32:08 -05:00
|
|
|
it 'handles alternation for #alternated_substrings' do
|
2018-10-30 15:04:40 -04:00
|
|
|
verify_alternated_strings(
|
2019-11-28 17:41:13 -05:00
|
|
|
{
|
|
|
|
/abc|def/ => [%w[abc], %w[def]],
|
|
|
|
/ab(?:c|d)/ => [%w[abc], %w[abd]],
|
|
|
|
/ab(c|d|e)fg/ => [%w[abcfg], %w[abdfg], %w[abefg]],
|
|
|
|
/ab?(c|d)fg/ => [%w[acfg], %w[adfg], %w[abcfg], %w[abdfg]],
|
|
|
|
/ab(c|d)ef/ => [%w[abcef], %w[abdef]],
|
|
|
|
/ab(cd?|ef)g/ => [%w[abcg], %w[abcdg], %w[abefg]],
|
|
|
|
/ab(cd|ef*)g/ => [%w[abcdg], %w[abe g]],
|
|
|
|
/ab|cd*/ => [%w[ab], %w[c]],
|
|
|
|
/cd(?:ef|gh)|xyz/ => [%w[cdef], %w[cdgh], %w[xyz]],
|
|
|
|
/(cd(?:ef|gh)|xyz)/ => [%w[cdef], %w[cdgh], %w[xyz]],
|
|
|
|
/cd(ef|gh)+/ => [%w[cdef], %w[cdgh]],
|
|
|
|
/cd(ef|gh)?/ => [%w[cd]],
|
|
|
|
/cd(ef|gh)?ij/ => [%w[cdij], %w[cdefij], %w[cdghij]],
|
|
|
|
/cd(ef|gh)+ij/ => [%w[cdef ij], %w[cdgh ij]],
|
|
|
|
/cd(ef|gh){2}ij/ => [%w[cdefefij], %w[cdefghij], %w[cdghefij], %w[cdghghij]],
|
|
|
|
/(cd(ef|g*))/ => [%w[cd]],
|
|
|
|
/a|b*/ => [],
|
|
|
|
/ab(?:c|d?)/ => [%w[ab]],
|
|
|
|
/ab(c|d)|a*/ => [],
|
|
|
|
/(abc)?(d|e)/ => [%w[d], %w[e]],
|
|
|
|
/(abc*de)?(d|e)/ => [%w[d], %w[e]],
|
|
|
|
/(abc*de)?(d|e?)/ => [],
|
|
|
|
/(abc)?(d|e?)/ => [],
|
|
|
|
/ab(cd){0,2}ef/ => [%w[ab ef]],
|
|
|
|
/ab(cd){0,1}ef/ => [%w[abef], %w[abcdef]],
|
|
|
|
/ab(cd|cd)ef/ => [%w[abcdef]],
|
|
|
|
/ab(cd|cd)?ef/ => [%w[abef], %w[abcdef]],
|
|
|
|
/ab\\?cd/ => [%w[abcd], %w[ab\cd]]
|
|
|
|
}
|
2018-09-26 17:55:56 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles grouping' do
|
|
|
|
verify_strings(
|
|
|
|
/(abc)/ => %w[abc],
|
|
|
|
/(abc)?/ => [],
|
|
|
|
/ab(cde)/ => %w[abcde],
|
|
|
|
/(abc)de/ => %w[abcde],
|
|
|
|
/ab(cde)fg/ => %w[abcdefg],
|
|
|
|
/ab(?<name>cd)ef/ => %w[abcdef],
|
|
|
|
/gh(?>ij)kl/ => %w[ghijkl],
|
|
|
|
/m(n.*p)q/ => %w[mn pq],
|
|
|
|
/(?:ab(cd)*){2,3}/ => %w[ab],
|
|
|
|
/(ab(cd){3})?/ => [],
|
|
|
|
/(ab(cd)+){2}/ => %w[abcd]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles meta characters' do
|
|
|
|
verify_strings(
|
|
|
|
/abc\d/ => %w[abc],
|
|
|
|
/abc\wdef/ => %w[abc def],
|
|
|
|
/\habc/ => %w[abc]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles character properties' do
|
|
|
|
verify_strings(
|
|
|
|
/ab\p{Alpha}cd/ => %w[ab cd],
|
|
|
|
/ab\p{Blank}/ => %w[ab],
|
|
|
|
/\p{Digit}cd/ => %w[cd]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles backreferences' do
|
|
|
|
verify_strings(
|
|
|
|
/a(?<group>abc).\k<group>.+/ => %w[aabc]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles subexpressions' do
|
|
|
|
verify_strings(
|
|
|
|
/\A(?<paren>a\g<paren>*b)+\z/ => %w[a b]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-07-24 11:25:48 -04:00
|
|
|
it 'ignores negative lookaheads' do
|
|
|
|
verify_strings(
|
|
|
|
/^(?!.*\bContributing Editor\b).*$/ => %w[],
|
|
|
|
/abc(?!.*def).*/ => %w[abc],
|
|
|
|
/(?!.*def)abc/ => %w[abc],
|
|
|
|
/abc(?!.*def.*).*ghi/ => %w[abc ghi],
|
|
|
|
/abc(?!.*bcd)def/ => %w[abcdef]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-09-26 17:55:56 -04:00
|
|
|
it 'handles anchors' do
|
|
|
|
verify_strings(
|
|
|
|
/^abc/ => %w[abc],
|
|
|
|
/def$/ => %w[def],
|
|
|
|
/^abc$/ => %w[abc]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_strings(hsh)
|
|
|
|
hsh.each do |regexp, expected|
|
|
|
|
expect(Capybara::Selector::RegexpDisassembler.new(regexp).substrings).to eq expected
|
|
|
|
end
|
2018-10-30 15:04:40 -04:00
|
|
|
verify_alternated_strings(hsh, wrap: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_alternated_strings(hsh, wrap: false)
|
|
|
|
hsh.each do |regexp, expected|
|
2018-11-06 16:32:08 -05:00
|
|
|
expected = [expected] if wrap && (expected != [])
|
2018-10-30 15:04:40 -04:00
|
|
|
expect(Capybara::Selector::RegexpDisassembler.new(regexp).alternated_substrings).to eq expected
|
|
|
|
end
|
2018-09-26 17:55:56 -04:00
|
|
|
end
|
|
|
|
end
|