Skip to content

Instantly share code, notes, and snippets.

@jsl
Created August 23, 2013 21:01
Show Gist options
  • Save jsl/6323978 to your computer and use it in GitHub Desktop.
Save jsl/6323978 to your computer and use it in GitHub Desktop.
Tests for a simple logo interpreter in Ruby
# clearscreen
# forward INTEGER
# TO funcname
# do something
# END
# rt INTEGER ; right turn
# lt INTEGER ; left turn
require "minitest/autorun"
module Logo
def self.compile(code)
end
class ForwardNode
end
end
describe "An awesome logo compiler" do
describe "forward command" do
it "should recognize a forward command followed by an integer" do
prog = <<-PROG
forward 100
PROG
Logo.compile(prog).must_equal == [Logo::ForwardNode.new(100)]
end
it "should not recognize a forward command when not followed by an integer" do
prog = <<-PROG
forward horse
PROG
proc { Logo.compile(prog) }.must_raise(StandardError)
end
end
describe "rt" do
it "should recognize a rt followed by an Integer" do
prog = <<-PROG
rt 90
PROG
Logo.compile(prog).must_equal == [Logo::RightTurn.new(90)]
end
end
describe "simple function definitions" do
it "should recognize a function block starting with TO" do
prog = <<-PROG
to square
forward 100
rt 90
forward 100
rt 90
forward 100
rt 90
forward 100
rt 90
end
PROG
Logo.compile(prog).must_equal == [Logo::Function("square", [
Logo::ForwardNode.new(100),
Logo::RightTurn.new(90),
Logo::ForwardNode.new(100),
Logo::RightTurn.new(90),
Logo::ForwardNode.new(100),
Logo::RightTurn.new(90),
Logo::ForwardNode.new(100),
Logo::RightTurn.new(90)])]
end
it "should raise an exception if there is not a following END"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment