Class TestLogoParser
In: LogoParser.rb
Parent: Test::Unit::TestCase

Methods

Constants

DELTA = 0.000000000001   Constant delta for approximative testing

Public Instance methods

Test parsing of commands intended for immediate execution

[Source]

# File LogoParser.rb, line 431
    def testactive
      a = LogoTurtle.new
      b = LogoParser.new(a)
      b.parse("forward 100")
      assert_in_delta(100.0, a.posy, DELTA)
      b.parse("fd 100")
      assert_in_delta(200.0, a.posy, DELTA)
      b.parse("backward 90")
      assert_in_delta(110.0, a.posy, DELTA)
      b.parse("bk 90")
      assert_in_delta(20.0, a.posy, DELTA)
      b.parse("right 90")
      assert_in_delta(90.0, a.direction, DELTA)
      b.parse("rt 90")
      assert_in_delta(180.0, a.direction, DELTA)
      b.parse("left 80")
      assert_in_delta(100.0, a.direction, DELTA)
      b.parse("lt 80")
      assert_in_delta(20.0, a.direction, DELTA)
      b.parse("move 50 50")
      assert_in_delta(50.0, a.posx, DELTA)
      assert_in_delta(50.0, a.posy, DELTA)
      b.parse("home")
      assert_in_delta(0.0, a.posx, DELTA)
      assert_in_delta(0.0, a.posy, DELTA)
      b.parse("turn 361")
      assert_in_delta(1.0, a.direction, DELTA)
      b.parse("penup")
      assert(!a.pen)
      b.parse("pendown")
      assert(a.pen)
      b.parse("pu")
      assert(!a.pen)
      b.parse("pd")
      assert(a.pen)
      b.parse("hide")
      assert(!a.visible)
      b.parse("show")
      assert(a.visible)
      b.parse("color red")
      assert_equal("red", a.color)
      b.parse("reset")
      assert_in_delta(0.0, a.posx, DELTA)
      assert_in_delta(0.0, a.posy, DELTA)
      assert_in_delta(0.0, a.direction, DELTA)
      assert_equal("black", a.color)
      b.parse("forward 100 # forward 100")
      b.parse("right 90 ; forward 100")
      assert_in_delta(0.0, a.posx, DELTA)
      assert_in_delta(100.0, a.posy, DELTA)
      assert_in_delta(90.0, a.direction, DELTA)
      b.parse("asdf")
      assert_match(/ERROR: UNKNOWN FUNCTION 'asdf'/, a.messages[-1])
      b.parse("forward :unknown")
      assert_match(/MOVE FORWARD 0.0/, a.messages[-1])
      b.parse("forward forward")
      assert_match(/MOVE FORWARD 0.0/, a.messages[-1])
      assert_in_delta(0.0, a.posx, DELTA)
      assert_in_delta(100.0, a.posy, DELTA)
      assert_in_delta(90.0, a.direction, DELTA)
    end

Test conditional loop

[Source]

# File LogoParser.rb, line 524
    def testconditional
      a = LogoTurtle.new
      b = LogoParser.new(a)
      b.parse(":x = 5 if :x >= 3 then forward 50 end")
      b.parse("if :x == 3 then forward 50 end rt 90")
      assert_in_delta(50.0, a.posy, DELTA)
      b.parse(":x = 1 repeat 9 if :x % 2 == 0 then forward 10 end :x = :x + 1 end")
      assert_in_delta(40.0, a.posx, DELTA)
      b.parse(":y = 1 while :y <= 10 do :y = :y + 1 fd 10 end")
      assert_in_delta(140.0, a.posx, DELTA)
    end

Test functions

[Source]

# File LogoParser.rb, line 510
    def testfunction
      a = LogoTurtle.new
      b = LogoParser.new(a)
      b.parse("def fa forward 100 end")
      b.parse("to fb :va forward :va end")
      b.parse("def fc :va :vb repeat :va forward :vb end end")
      b.parse("fa fb 50 fc 7 7")
      assert_in_delta(199.0, a.posy, DELTA)
      b.parse("to inner lt 10 end to outer repeat 9 inner end end outer")
      assert_in_delta(270.0, a.direction, DELTA)
    end

Test initialization of LogoParser instances

[Source]

# File LogoParser.rb, line 407
    def testinit
      a = LogoTurtle.new
      b = LogoParser.new(a)
      b.parse("forward 100")
      assert_in_delta(100.0, a.posy, DELTA)
      b.parse(":x = 90")
      b.parse("def test :var rt :var end")
      b.parse("test :x")
      assert_in_delta(90.0, a.direction, DELTA)
      c = LogoParser.new(a, b.functions, b.f_vars, b.variables)
      c.parse("test :x")
      assert_in_delta(180.0, a.direction, DELTA)
    end

Test passive parsing inside blocks

[Source]

# File LogoParser.rb, line 495
    def testpassive
      a = LogoTurtle.new
      b = LogoParser.new(a)
      b.parse("def x repeat 10 forward 10 end end")
      assert_in_delta(0.0, a.posy, DELTA)
      b.parse("x")
      assert_in_delta(100.0, a.posy, DELTA)
      b.parse("repeat 2.9999999999999999 forward 10 end")
      assert_in_delta(130.0, a.posy, DELTA)
      b.parse("repeat -2.5 forward 10 end")
      assert_in_delta(130.0, a.posy, DELTA)
    end

Test method to convert instances to an array

[Source]

# File LogoParser.rb, line 423
    def testto
      a = LogoTurtle.new
      assert_equal([[0.0, 0.0, 0.0, "black", "white", true, true, [], []], {},
        {}, {}], LogoParser.new(a).to_a)
    end

Test variable assignments and mathematical expressions

[Source]

# File LogoParser.rb, line 538
    def testvariables
      a = LogoTurtle.new
      b = LogoParser.new(a)
      b.parse(":a = 5 \n:b = 10 \n:c =:a *:b forward:c")
      assert_in_delta(50.0, a.posy, DELTA)
      b.parse(":d = 5/0")
      assert_match(/DIVIDE BY ZERO/, a.messages[-1])
      b.parse(":e = ( + + 10 + ) ) )")
      assert_match(/BAD EXPRESSION/, a.messages[-1])
      b.parse("forward :d +:e")
      assert_in_delta(50.0, a.posy, DELTA)
      b.parse(":f = ((7+3)*(5+5)/10**2+(90-1))rt:f")
      assert_in_delta(90.0, a.direction, DELTA)
      b.parse(":g = (104 % 5) * (11 % 2) * 20 + 10 rt :g")
      assert_in_delta(180.0, a.direction, DELTA)
      b.parse(":h = coS(0) * sqRt  (    2500  ) fd :h")
      assert_in_delta(0.0, a.posy, DELTA)
      b.parse(":i = eXp( log   ( 100 )) * log10(100) bk :i")
      assert_in_delta(200.0, a.posy, DELTA)
      b.parse(":j = SIN(1.5707963267948966192313216916398) * tan(1.56) fd :j")
      assert_in_delta(107.3795, a.posy, 0.0001)
    end

[Validate]