aboutsummaryrefslogtreecommitdiff
path: root/spec/tokens_spec.cr
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-31 21:48:41 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-31 21:48:41 +1300
commitfdfce3d4c7a672fdff10e91bf5a4808cd4d46c4d (patch)
treeb210f79d12a3018a3db15c1fc64dd347eb9da1f4 /spec/tokens_spec.cr
parent97be6f4ca3f35b0999a36d647716f77c0300d5d7 (diff)
Separate core tests into separate files
Diffstat (limited to 'spec/tokens_spec.cr')
-rw-r--r--spec/tokens_spec.cr63
1 files changed, 63 insertions, 0 deletions
diff --git a/spec/tokens_spec.cr b/spec/tokens_spec.cr
new file mode 100644
index 0000000..ab43ce2
--- /dev/null
+++ b/spec/tokens_spec.cr
@@ -0,0 +1,63 @@
+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
+