require "./spec_helper" include Parcom describe Tokens do describe ".from_string" do it "constructs a Tokens(Char) from a String" do tokens = Tokens.from_string("abcd") tokens.tokens.should eq("abcd".chars) end end describe "#initialize" do it "wraps an array with the contents of the given iterable" do set = Set{'a', 'b', 'c', 'd'} tokens = Tokens.new(set) tokens.tokens.should eq(set.to_a) arr = "abcd".chars tokens = Tokens.new(arr) tokens.tokens.should eq(arr) end end context do tokens_empty = Tokens.new([] of Char) tokens = Tokens.from_string("abcd") describe "#[]" do it "returns the token at the given index" do tokens[2].should eq('c') expect_raises(IndexError) { tokens_empty[2] } end it "returns a new Tokens similar to Array#[](Int, Int)" do tokens[1, 5].should eq(Tokens.new(['b', 'c', 'd'])) expect_raises(IndexError) { tokens_empty[1, 5] } end it "returns a new Tokens similar to Array#[](Range)" do tokens[1..3].should eq(Tokens.new(['b', 'c', 'd'])) expect_raises(IndexError) { tokens_empty[1..3] } end end describe "#[]?" do it "analogous to `Array#[]?`" do # we should only need to check the nil-returning cases tokens_empty[2]?.should be_nil tokens_empty[1, 5]?.should be_nil tokens_empty[1..3]?.should be_nil end end describe "#empty?" do it "exposes the `#empty?` method of the wrapped array" do tokens.empty?.should be_false tokens_empty.empty?.should be_true end end end end