require "minitest/autorun" require "list_matcher" class BasicTest < Minitest::Test def test_simple words = %w(cat dog camel) rx = List::Matcher.pattern words rx = Regexp.new rx words.each do |w| assert rx === w end end def test_word_chars word = (1..255).map(&:chr).select{ |c| /\w/ === c } chars = word + ['+'] rx = List::Matcher.pattern chars assert_equal '[+\w]', rx rx = Regexp.new rx chars.each do |c| assert rx === c end chars = word + ['@'] rx = List::Matcher.pattern chars assert_equal '[@\w]', rx rx = Regexp.new rx chars.each do |c| assert rx === c end end def test_word_chars_case_insensitive word = (1..255).map(&:chr).select{ |c| /\w/ === c } chars = word + ['+'] rx = List::Matcher.pattern chars, case_insensitive: true assert_equal '(?i:[+\w])', rx rx = Regexp.new rx chars.each do |c| assert rx === c end end def test_num_chars words = (0..9).map(&:to_s) rx = List::Matcher.pattern words assert_equal '\d', rx rx = Regexp.new rx words.each do |w| assert rx === w end end def test_space_chars words = (1..255).map(&:chr).select{ |c| c =~ /\s/ } rx = List::Matcher.pattern words assert_equal '\s', rx rx = Regexp.new rx words.each do |w| assert rx === w end end def test_bounds words = %w(cat dog) rx = List::Matcher.pattern words, bound: true assert_equal '(?:\b(?:cat|dog)\b)', rx rx = Regexp.new rx words.each do |w| assert rx === w end end def test_repeats rx = List::Matcher.pattern %w(aaaaaaaaaa) assert_equal '(?:a{10})', rx rx = List::Matcher.pattern %w(bbbaaaaaaaaaabbbaaaaaaaaaa) assert_equal '(?:(?:bbba{10}){2})', rx end def test_opt_suffix words = %w(the them) rx = List::Matcher.pattern words assert_equal '(?:them?)', rx rx = Regexp.new rx words.each do |w| assert rx === w end end def test_opt_prefix words = %w(at cat) rx = List::Matcher.pattern words assert_equal '(?:c?at)', rx rx = Regexp.new rx words.each do |w| assert rx === w end end def test_symbols_string words = ['cat dog'] rx = List::Matcher.pattern words, symbols: { ' ' => '\s++' } assert_equal '(?:cat\s++dog)', rx rx = Regexp.new rx words.each do |w| assert rx === w end end def test_symbols_rx words = %w(year year2000 year1999) rx = List::Matcher.pattern words, symbols: { /(? nil } assert_equal '(?:year(?-mix:(?cat|dog)", rx end def test_dup_bound m = List::Matcher.new bound: false, atomic: false rx = m.pattern %w( cat dog ), bound: true assert_equal '\b(?:cat|dog)\b', rx end def test_dup_bound_string m = List::Matcher.new bound: false, atomic: false rx = m.pattern %w( cat dog ), bound: :string assert_equal '\A(?:cat|dog)\z', rx end def test_dup_bound_line m = List::Matcher.new bound: false, atomic: false rx = m.pattern %w( cat dog ), bound: :line assert_equal '^(?:cat|dog)$', rx end def test_dup_bound_fancy m = List::Matcher.new bound: false, atomic: false rx = m.pattern %w( 1 2 ), bound: { test: /\d/, left: '(? '\s++' } assert_equal 'cat\s++dog', rx end def test_multiline rx = List::Matcher.pattern %w( cat dog ), multiline: true assert_equal '(?m:cat|dog)', rx end def test_dup_multiline m = List::Matcher.new atomic: false rx = m.pattern %w( cat dog ), multiline: true assert_equal '(?m:cat|dog)', rx end def test_name m = List::Matcher.new name: :foo rx = m.pattern %w( cat dog ) assert_equal '(?cat|dog)', rx end def test_vetting_good List::Matcher.pattern %w(cat), symbols: { foo: 'bar' }, vet: true assert true, 'good regexen are vetted appropriately' end def test_vetting_bad assert_raises SyntaxError do List::Matcher.pattern %w(cat), symbols: { foo: '+' }, vet: true end end def test_not_extended m = List::Matcher.new not_extended: true rx = m.pattern [ ' ', '#' ] assert_equal '(?-x:#| )', rx rx = Regexp.new rx assert rx === ' ' assert rx === '#' end end