Sha256: 0b200d120e928daffeb097a99f047ca7d1c40e0b0f257c44a8ccf0c02369904b

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require File.dirname(__FILE__) + '/helper'

class MatchTest < Test::Unit::TestCase

  Double = Grammar.new {
    include MatchTest::TestGrammar
    root :double
    rule :double do
      one_or_more(:num)
    end
  }

  Sentence = Grammar.new {
    include MatchTest::TestGrammar
    root :sentence
    rule :word do
      one_or_more(:alpha)
    end
    rule :sentence do
      [ :word, zero_or_more([ ' ', :word ]) ]
    end
  }

  def test_string
    match = Match.new('hello')
    assert_equal('hello', match.text)
    assert_equal(5, match.length)
  end

  def test_array_string
    match1 = Match.new('a')
    match2 = Match.new('b')
    match = Match.new([match1, match2])
    assert_equal('ab', match.text)
    assert_equal(2, match.length)
  end

  def test_match_data
    match = Match.new('hello world'.match(/^(\w+) /))
    assert_equal('hello ', match.text)
    assert_equal(6, match.length)
  end

  def test_array_match_data
    match1 = Match.new('hello')
    match2 = Match.new(' world'.match(/.+/))
    match = Match.new([match1, match2])
    assert_equal('hello world', match.text)
    assert_equal(11, match.length)
  end

  def test_matches
    match = Double.parse('123')
    assert(match)
    assert_equal(3, match.matches.length)
    assert_equal(3, match.find(:num).length)
  end

  def test_match
    match = Double.parse('456')
    assert(match)
    assert_equal(3, match.matches.length)

    num = match.first(:num)
    assert(num)
    assert_equal('4', num.text)
    assert_equal(4, num.value)
  end

  def test_matches_deep
    match = Sentence.parse('one two three four')
    assert(match)
    assert_equal(15, match.find(:alpha).length)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
citrus-1.0.0 test/match_test.rb