packages feed

spade-0.1.0.0: test/Interpreter/InterpreterSpec.hs

module Interpreter.InterpreterSpec where

import Test.Hspec
import NeatInterpolation

import Compiler.Parser
import Interpreter
import Interpreter.Interpreter
import Interpreter.Common


spec :: Spec
spec = do
  describe "While loop" $ do
    it "loops as expected" $ do
      let program = [text|
        let a = 100
        while a > 10
          let a = a - 1
        endwhile
        |]
      compile program >>= interpret'' >>= ensureVarIs (SkIdentifier "a") (NumberValue $ NumberInt 10)

    it "breaks as expected" $ do
      let program = [text|
        let a = 100
        while a > 10
          let a = a - 1
          if a == 20 then
            break
          endif
        endwhile
        |]
      compile program >>= interpret'' >>= ensureVarIs (SkIdentifier "a") (NumberValue $ NumberInt 20)

    it "break only exist one nesting" $ do
      let program = [text|
        let a = 100
        while a > 10
          let b = 100
          let a = a - 1
          while true
            let b = b - 1
            if b == 20 then
              break
            endif
          endwhile
        endwhile
        |]
      compile program >>= interpret'' >>= ensureVarIs (SkIdentifier "a") (NumberValue $ NumberInt 10)
  where
    interpret'' = interpret_ (error "Input stream not available")

ensureVarIs :: ScopeKey -> Value -> InterpreterState -> Expectation
ensureVarIs sk v (isGlobalScope -> scope) = (lookupInTopScope sk [scope]) `shouldBe` (Just v)