Class: TestMatchData

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/innate/test/testregexp.rb

Instance Method Summary collapse

Instance Method Details

#test_displayObject



6
7
8
9
10
11
# File 'lib/innate/test/testregexp.rb', line 6

def test_display
  assert_equal '(hello)', /hello/.match('hello').display
  assert_equal '(the(next)word)', /the(next)word/.match('thenextword').display
  assert_equal 'the(next)word', /next/.match('thenextword').display
  assert_equal 'the ((3)rd) wave', /(\d)../.match('the 3rd wave').display
end

#test_display_levelsObject



17
18
19
20
21
# File 'lib/innate/test/testregexp.rb', line 17

def test_display_levels
  assert_equal 'the (ne)(xt) ((le)(ve)l)!', /( )(..)(..) ((le)(..).)/.match('the next level!').display(2)
  assert_equal 'the (ne)(xt) ((le)vel)!', /( )(..)(..) ((le)(..).)/.match('the next level!').display(2, 6)
  assert_equal 'the next (level)!', /( )(..)(..) ((le)(..).)/.match('the next level!').display(4, 5)
end

#test_display_nestedObject



13
14
15
# File 'lib/innate/test/testregexp.rb', line 13

def test_display_nested
  assert_equal 'the(( )(ne)(xt) ((le)(ve)l))!', /( )(..)(..) ((le)(..).)/.match('the next level!').display
end

#test_display_other_delimObject



23
24
25
# File 'lib/innate/test/testregexp.rb', line 23

def test_display_other_delim
  assert_equal 'the->-> <-->ne<-->xt<- ->->le<-->ve<-l<-<-!', /( )(..)(..) ((le)(..).)/.match('the next level!').display(0, nil, '->', '<-')
end

#test_display_problemObject



27
28
29
30
31
# File 'lib/innate/test/testregexp.rb', line 27

def test_display_problem
  r = /^\(\?([imx]*)(-[imx]+)?:(.*?)#{Regexp.escape 'etc'}(.*)\)$/
  m = r.match(/"etc"/mix.to_s)
  assert_equal '((?(mix):(")etc(")))', m.display
end

#test_replace_captureObject



34
35
36
# File 'lib/innate/test/testregexp.rb', line 34

def test_replace_capture
  assert_equal 'a new word', /a(n old) word/.match('an old word').replace_capture(1, ' new')
end

#test_subObject



38
39
40
41
42
# File 'lib/innate/test/testregexp.rb', line 38

def test_sub
  assert_equal 'a new word', /a(n old) word/.match('an old word').sub(' new')
  assert_equal 'a new word', /n old/.match('an old word').sub(' new')
  assert_equal 'a new word', /(?:a)(n ((o)l)d) word/.match('an old word').sub(' new')
end

#test_sub_nilObject



44
45
46
47
48
# File 'lib/innate/test/testregexp.rb', line 44

def test_sub_nil
  assert_equal 'a new word', /a(n old) word/.match('an old word').sub(' new', nil)
  assert_equal 'a new word', /n old/.match('an old word').sub(' new', nil)
  assert_equal 'a new word', /(?:a)(n ((o)l)d) word/.match('an old word').sub(' new', nil)
end

#test_sub_otherObject



50
51
52
53
54
55
56
# File 'lib/innate/test/testregexp.rb', line 50

def test_sub_other
  assert_equal 'a new word', /a(n old) word/.match('an old word').sub(' new', 1)
  assert_equal 'a new word', /n (o)ld/.match('an old word').sub(' new', 0)
  assert_equal 'a new word', /n (o)ld/.match('an old word').sub(' new', 10)
  assert_equal 'a new word', /n old/.match('an old word').sub(' new', 10)
  assert_equal 'an  newd word', /(?:a)(n ((o)l)d) word/.match('an old word').sub(' new', 2)
end