File: test_repeat.rb

package info (click to toggle)
ruby-rsec 0.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 272 kB
  • sloc: ruby: 2,130; lisp: 13; makefile: 3
file content (50 lines) | stat: -rw-r--r-- 1,166 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require "#{File.dirname(__FILE__)}/helpers.rb"

class TestRepeat < TC
  def test_maybe
    [:_?, :maybe].each do |m|
      p = seq('v', 'q').send m
      ase [], p.parse('')
      ase INVALID, p.eof.parse('v')
      ase [['v', 'q']], p.parse('vq')

      # with map block
      p = seq('v', 'q').maybe {|x| x.empty? ? 'bad' : 'good' }
      ase 'good', p.parse('vq')
    end
  end

  def test_multiply
    p = ('ce'.r * 3).eof
    ase ['ce','ce','ce'], (p.parse 'cecece')
    ase INVALID, (p.parse 'cece')
    ase INVALID, (p.parse 'cececece')
    
    p = ('ce'.r * 0).eof
    ase [], (p.parse '')
    ase INVALID, (p.parse 'ce')
  end
  
  def test_range
    p = ('ce'.r * (2..3)).eof
    ase INVALID, (p.parse 'ce')
    ase ['ce','ce'], (p.parse 'cece')
    ase INVALID, (p.parse 'cececece')
  end
  
  def test_inf
    p = ('ce'.r * (3..-1)).eof
    ase INVALID,
      (p.parse 'cece')
    ase ['ce','ce','ce'],
      (p.parse 'cecece')
    ase ['ce','ce','ce','ce','ce'],
      (p.parse 'cecececece')
  end

  def test_star
    p = '*'.r.star
    ase [], p.parse('')
    ase %w[* * *], p.parse('***')
  end
end