Commit 7f7bdb2

mokha <mokha@cisco.com>
2019-05-15 01:46:09
test alternatives
1 parent ffe800f
lib/mpeg/base.rb
@@ -2,8 +2,7 @@ module Mpeg
   class Base
     def parse(string)
       input = Input.new(string)
-      call(input)
-      input.end?
+      call(input) && input.end?
     end
 
     def repeat(min = 0, max = nil)
lib/mpeg/input.rb
@@ -11,7 +11,6 @@ module Mpeg
     def consume(characters)
       position = @scanner.pos
       slice = @scanner.scan(/(.|$){#{characters}}/m)
-
       Slice.new(position, slice)
     end
 
lib/mpeg/slice.rb
@@ -1,6 +1,16 @@
 module Mpeg
   class Slice
     def initialize(position, string)
+      @position = position
+      @string = string
+    end
+
+    def to_s
+      "#{@string}@#{@position}"
+    end
+
+    def inspect
+      to_s
     end
   end
 end
lib/mpeg/str.rb
@@ -6,10 +6,7 @@ module Mpeg
     end
 
     def call(input)
-      if input.matches?(@regex)
-        input.consume(@length)
-        return true
-      end
+      return input.consume(@length) if input.matches?(@regex)
       false
     end
   end
spec/alternative_spec.rb
@@ -0,0 +1,19 @@
+RSpec.describe Mpeg::Alternative do
+  context "jello | hello" do
+    subject { Mpeg::Str.new('jello') | Mpeg::Str.new('hello') }
+
+    specify { expect(subject.parse("hello")).to be_truthy }
+  end
+
+  context "hello | jello" do
+    subject { Mpeg::Str.new('hello') | Mpeg::Str.new('jello') }
+
+    specify { expect(subject.parse("hello")).to be_truthy }
+  end
+
+  context "fellow | jello" do
+    subject { Mpeg::Str.new('fellow') | Mpeg::Str.new('jello') }
+
+    specify { expect(subject.parse("hello")).to be_falsey }
+  end
+end