spec/token_matcher_spec.rb in dentaku-0.2.2 vs spec/token_matcher_spec.rb in dentaku-0.2.3

- old
+ new

@@ -1,5 +1,6 @@ +require 'spec_helper' require 'dentaku/token_matcher' describe Dentaku::TokenMatcher do it 'with single category should match token category' do matcher = described_class.new(:numeric) @@ -48,8 +49,57 @@ cmp = Dentaku::Token.new(:comparator, :lt) matcher.should_not == add matcher.should == mul matcher.should == cmp + end + + describe 'stream matching' do + let(:stream) { token_stream(5, 11, 9, 24, :hello, 8) } + + describe :standard do + let(:standard) { described_class.new(:numeric) } + + it 'should match zero or more occurrences in a token stream' do + substream = standard.match(stream) + substream.should be_matched + substream.length.should eq 1 + substream.map(&:value).should eq [5] + + substream = standard.match(stream, 4) + substream.should be_empty + substream.should_not be_matched + end + end + + describe :star do + let(:star) { described_class.new(:numeric).star } + + it 'should match zero or more occurrences in a token stream' do + substream = star.match(stream) + substream.should be_matched + substream.length.should eq 4 + substream.map(&:value).should eq [5, 11, 9, 24] + + substream = star.match(stream, 4) + substream.should be_empty + substream.should be_matched + end + end + + describe :plus do + let(:plus) { described_class.new(:numeric).plus } + + it 'should match one or more occurrences in a token stream' do + substream = plus.match(stream) + substream.should be_matched + substream.length.should eq 4 + substream.map(&:value).should eq [5, 11, 9, 24] + + substream = plus.match(stream, 4) + substream.should be_empty + substream.should_not be_matched + end + end end end