diff --git a/Rakefile b/Rakefile index fb960e0..6971711 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,5 @@ require "bundler/gem_tasks" +require 'rake/testtask' task :examples do $LOAD_PATH.unshift File.expand_path('../examples', __FILE__) @@ -7,3 +8,7 @@ task :examples do require 'recursive' end +Rake::TestTask.new do |t| + t.libs << "test" + t.test_files = FileList['test/*_test.rb'] +end diff --git a/lib/parser_combinator.rb b/lib/parser_combinator.rb index d208862..5ca636f 100644 --- a/lib/parser_combinator.rb +++ b/lib/parser_combinator.rb @@ -53,18 +53,20 @@ def flatten Items.new(super()) end - def slice(nth) - if nth.instance_of? Range then - Items.new(super(nth)) - else - super(nth) + def slice(*args) + case args.size + when 1 + nth, = args + if nth.instance_of? Range then + Items.new(super(nth)) + else + super(nth) + end + when 2 + Items.new(super(*args)) end end - def slice(pos, len) - Items.new(super(pos, len)) - end - def take(n) Items.new(super(n)) end diff --git a/test/items_test.rb b/test/items_test.rb new file mode 100644 index 0000000..3fbe11a --- /dev/null +++ b/test/items_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class ItemsTest < Test::Unit::TestCase + def test_slice + items = ParserCombinator::Items.new([:a, :b, :c]) + assert_instance_of ParserCombinator::Items, items.slice(1..2) + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..b4cc915 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,4 @@ +require "test-unit" + +$LOAD_PATH << File.join(__dir__, "../lib") +require "parser_combinator"