Skip to content

Instantly share code, notes, and snippets.

@raws
Created May 31, 2010 22:52
Show Gist options
  • Save raws/72e2658ef927e7145fcc to your computer and use it in GitHub Desktop.
Save raws/72e2658ef927e7145fcc to your computer and use it in GitHub Desktop.
begin
require "test_helper"
require "polyglot"
require "treetop"
require "grunt/arguments.rb"
rescue LoadError => e
if require "rubygems"
retry
else
raise e
end
end
Treetop.load(File.join(File.dirname(__FILE__), "..", "..", "lib", "grunt", "arguments"))
module Arguments
class Method
def eval
args.map { |arg| arg.eval }.join(" ")
end
end
end
module Grunt
class ArgumentsTest < Test::Unit::TestCase
def parse(input)
result = @parser.parse(input)
puts @parser.terminal_failures.join("\n") unless result
result.should be_instance_of(Arguments::Arguments)
result
end
custom_matcher :have do |receiver, matcher, args|
count = args[0]
something = matcher.chained_messages[0].name
actual = receiver.send(something).size
matcher.positive_failure_message = "Expected #{receiver.inspect} to have #{count} #{something}, but it had #{actual}"
matcher.negative_failure_message = "Expected #{receiver.inspect} not to have #{count} #{something}, but it did"
actual == count
end
custom_matcher :eval_to do |receiver, matcher, args|
expected = args[0]
actual = receiver.eval
matcher.positive_failure_message = "Expected #{actual.inspect} to be #{expected.inspect}, but it wasn't"
matcher.negative_failure_message = "Expected #{actual.inspect} not to be #{expected.inspect}, but it was"
actual == expected
end
context "An ArgumentsParser instance" do
setup do
@parser = ArgumentsParser.new
end
should "parse words" do
result = parse "foo bar baz"
result.should have(3).args
result.args[0].should eval_to("foo")
result.args[1].should eval_to("bar")
result.args[2].should eval_to("baz")
end
should "parse literal strings with normal characters" do
result = parse "{foo bar baz}"
result.should have(1).args
result.args[0].should eval_to("foo bar baz")
end
should "parse literal strings with strange characters" do
result = parse "{[føo {bar] bäz!}"
result.should have(1).args
result.args[0].should eval_to("[føo {bar] bäz!")
end
should "parse interpolated strings with no interpolated methods" do
result = parse '"foo bar baz"'
result.should have(1).args
result.args[0].should eval_to("foo bar baz")
end
should "parse interpolated strings with interpolated methods" do
result = parse '"foo [bar baz]"'
result.should have(1).args
result = parse '"foo [bar [baz hat]]"'
result.should have(1).args
end
should "parse methods" do
result = parse "[foo bar]"
result.should have(1).args
result.args[0].should eval_to("bar")
result = parse "[foo bar [baz {hat cat}]]"
result.should have(1).args
result.args[0].should eval_to("bar hat cat")
end
should "parse multiple arguments" do
parse("foo").should have(1).args
parse("foo bar").should have(2).args
parse("foo {bar}").should have(2).args
parse("foo [baz {hat}] cat").should have(3).args
parse("[foo bar] {cat in the hat} fat").should have(3).args
parse('fat "cat sat [in the] hat" {which was sitting on the} mat').should have(4).args
end
end # ArgumentsParser instance
context "An Arguments::Argument instance" do
setup do
@parser = ArgumentsParser.new
end
should "provide #args_string for arguments of type Arguments::Method, but not other types" do
result = parse("foo [bar baz hat]")
result.args[1].should respond_to(:args_string)
result.args[1].args_string.should == "baz hat"
lambda { result.args[0].args_string }.should raise_error(NoMethodError)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment