lattest-lib-0.2: test/Test/Lattest/Util/STSJSONParserTest.hs
{-# LANGUAGE QuasiQuotes #-}
module Test.Lattest.Util.STSJSONParserTest
( testSTSJSONParserNominal
, testSTSJSONParserNominalFloat
, testSTSJSONParserUnknownType
, testSTSJSONParserAssignmentTypeMismatch
, testSTSJSONParserGuardTypeMismatch
, testSTSJSONParserGateIdDup
, testSTSJSONParserUnsupportedGuardOperand
, testSTSJSONParserUnsupportedAssignmentOperand
, testSTSJSONParserMissingSwitches
, testSTSJSONParserMissingGates
, stsJSONParserTests
) where
import Data.List (isInfixOf)
import qualified Data.Set as Set
import qualified Text.RawString.QQ as QQ
import Test.HUnit
import Lattest.Model.Alphabet(IOAct(..), SymInteract(..))
import Lattest.Model.Automaton(alphabet, prettyPrintIntrp)
import Lattest.Model.StandardAutomata(interpretSTS)
import Lattest.Model.Symbolic.Expr(Type(..), Variable(..), Valuation(..), Constant(..))
import Lattest.Util.STSJSONParser(stsFromJSONFile)
import qualified Data.Dependent.Map as DMap
import Data.Some (Some(..))
testDir :: FilePath
testDir = "./test/Test/Lattest/Util/STSJSONExamples/"
-- | Check that the error string contains the expected substring.
assertErrorContains :: String -> String -> Either String a -> Assertion
assertErrorContains label errsubstr (Right _) =
assertFailure (label ++ ": expected an error containing '" ++ errsubstr ++ "', but parsing succeeded")
assertErrorContains label errsubstr (Left err) =
assertBool
(label ++ ": expected error to contain '" ++ errsubstr ++ "', got: " ++ err)
(errsubstr `isInfixOf` err)
{-|
Evaluate nominal case with:
- a variety of variable types (Int, String, Bool)
- (some) shortnames for gates
- multiple assignments in a single switch
- multiple switches with the same gate but different guards and assignments
-}
testSTSJSONParserNominal :: Test
testSTSJSONParserNominal = TestCase $ do
result <- stsFromJSONFile (testDir ++ "nominal_all_types.json")
case result of
Left err -> assertFailure ("expected successful parse, got: " ++ err)
Right (id, sts, gs, as, valuation, enums, _) -> do
assertEqual "STS id" "STS1" id
assertEqual "initial valuation"
(Valuation $
DMap.insert (Variable "counter" IntType) (Constant 5) $
DMap.insert (Variable "label" (ListType CharType)) (Constant "") $
DMap.insert (Variable "active" BoolType) (Constant False) $
mempty)
valuation
assertEqual "alphabet"
(Set.fromList
[ SymInteract (In "register") [Some $ Variable "label_p" (ListType CharType)]
, SymInteract (In "update") [Some $ Variable "counter_p" IntType ]
, SymInteract (Out "O1") []
, SymInteract (Out "confirm") [Some $ Variable "counter_p" IntType ]
])
(alphabet sts)
assertBool failureMessage (expected == actual)
where
intrpsts = interpretSTS sts valuation
actual = "\n" ++ prettyPrintIntrp intrpsts ++ "\n"
failureMessage = "print of STS does not match, expected:" ++ expected ++ "but received:" ++ actual
expected = [QQ.r|
current state configuration: ("0",{active:=False,counter:=5,label:=""})
initial location configuration: "0"
locations: "0", "1", "2"
transitions:
"0" ――?"register" [label_p:[Char]]⟶ ⊤
"0" ――?"update" [counter_p:Int]⟶ (counter+-4 ≥ 0, {active:=False},"2") ∧ (¬(counter+-5 ≥ 0), {counter:=counter+counter_p},"1")
"0" ――!"O1" []⟶ ⊥
"0" ――!"confirm" [counter_p:Int]⟶ ⊥
"1" ――?"register" [label_p:[Char]]⟶ (active = True, {label:=label_p},"0") ∧ (label = label_p, {active:=True, counter:=counter+1},"0")
"1" ――?"update" [counter_p:Int]⟶ ⊤
"1" ――!"O1" []⟶ ⊥
"1" ――!"confirm" [counter_p:Int]⟶ ⊥
"2" ――?"register" [label_p:[Char]]⟶ ⊤
"2" ――?"update" [counter_p:Int]⟶ ⊤
"2" ――!"O1" []⟶ (True, {},"0")
"2" ――!"confirm" [counter_p:Int]⟶ ⊥
|]
{-|
Evaluate nominal case with float variables
-}
testSTSJSONParserNominalFloat :: Test
testSTSJSONParserNominalFloat = TestCase $ do
result <- stsFromJSONFile (testDir ++ "nominal_float_types.json")
case result of
Left err -> assertFailure ("expected successful parse, got: " ++ err)
Right (id, sts, gs, as, valuation, enums, _) -> do
assertEqual "STS id" "" id -- no ID defined
assertEqual "initial valuation"
(Valuation $
DMap.insert (Variable "counter" FloatType) (Constant 0.0) $
DMap.insert (Variable "label" (ListType CharType)) (Constant "") $
DMap.insert (Variable "active" BoolType) (Constant False)
mempty)
valuation
assertEqual "alphabet"
(Set.fromList
[ SymInteract (In "register") [Some $ Variable "label_p" (ListType CharType)]
, SymInteract (In "update") [Some $ Variable "counter_p" FloatType ]
, SymInteract (Out "O1") []
, SymInteract (Out "confirm") [Some $ Variable "counter_p" FloatType ]
])
(alphabet sts)
assertBool failureMessage (expected == actual)
where
intrpsts = interpretSTS sts valuation
actual = "\n" ++ prettyPrintIntrp intrpsts ++ "\n"
failureMessage = "print of STS does not match, expected:" ++ expected ++ "but received:" ++ actual
expected = [QQ.r|
current state configuration: ("0",{active:=False,counter:=0.0,label:=""})
initial location configuration: "0"
locations: "0", "1", "2"
transitions:
"0" ――?"register" [label_p:[Char]]⟶ ⊤
"0" ――?"update" [counter_p:Float]⟶ (counter+-5.5 ≥ 0, {active:=False},"2") ∧ (¬(counter+-5.5 ≥ 0), {counter:=counter+counter_p},"1")
"0" ――!"O1" []⟶ ⊥
"0" ――!"confirm" [counter_p:Float]⟶ ⊥
"1" ――?"register" [label_p:[Char]]⟶ (active = True, {label:=label_p},"0") ∧ (label = label_p, {active:=True, counter:=counter+1.0},"0")
"1" ――?"update" [counter_p:Float]⟶ ⊤
"1" ――!"O1" []⟶ ⊥
"1" ――!"confirm" [counter_p:Float]⟶ ⊥
"2" ――?"register" [label_p:[Char]]⟶ ⊤
"2" ――?"update" [counter_p:Float]⟶ ⊤
"2" ――!"O1" []⟶ (True, {},"0")
"2" ――!"confirm" [counter_p:Float]⟶ ⊥
|]
----- Non-nominal cases -----
-- | Variable type "dummyType" is not recognised as a type.
testSTSJSONParserUnknownType :: Test
testSTSJSONParserUnknownType = TestCase $ do
result <- stsFromJSONFile (testDir ++ "unknown_type.json")
assertErrorContains "unknown type" "unknown variable type: dummyType" result
-- | Variable "label" has type String but the assignment expression is an integer literal.
testSTSJSONParserAssignmentTypeMismatch :: Test
testSTSJSONParserAssignmentTypeMismatch = TestCase $ do
result <- stsFromJSONFile (testDir ++ "assignment_type_mismatch.json")
assertErrorContains "Test assignment type mismatch" "assigment to variable of wrong type" result
-- | Guard compares integer with string using ==
testSTSJSONParserGuardTypeMismatch :: Test
testSTSJSONParserGuardTypeMismatch = TestCase $ do
result <- stsFromJSONFile (testDir ++ "guard_type_mismatch.json")
assertErrorContains "Test guard type mismatch" "unknown or badly typed op2: ==" result
-- | Switch refers to an undefined gate
testSTSJSONParserGateIdDup :: Test
testSTSJSONParserGateIdDup = TestCase $ do
result <- stsFromJSONFile (testDir ++ "gate_id_dup.json")
assertErrorContains "Test gate id inconsistency" "unknown gate" result
-- | Guard expression uses operator "?!?" which is not a recognised op2.
testSTSJSONParserUnsupportedGuardOperand :: Test
testSTSJSONParserUnsupportedGuardOperand = TestCase $ do
result <- stsFromJSONFile (testDir ++ "unsupported_guard_operand.json")
assertErrorContains "Test unsupported guard operand" "unknown or badly typed op2: ?!?" result
-- | Assignment expression uses an unsupported operator
testSTSJSONParserUnsupportedAssignmentOperand :: Test
testSTSJSONParserUnsupportedAssignmentOperand = TestCase $ do
result <- stsFromJSONFile (testDir ++ "unsupported_assignment_operand.json")
assertErrorContains "Test unsupported assignment operand" "unknown or badly typed op2: ???" result
-- | The mandatory "switches" field is absent from the JSON.
testSTSJSONParserMissingSwitches :: Test
testSTSJSONParserMissingSwitches = TestCase $ do
result <- stsFromJSONFile (testDir ++ "missing_switches.json")
assertErrorContains "Test missing switches" "switches" result
assertErrorContains "Test missing switches" "not found" result
-- | A used input gate is absent from the JSON.
testSTSJSONParserMissingGates :: Test
testSTSJSONParserMissingGates = TestCase $ do
result <- stsFromJSONFile (testDir ++ "missing_gates.json")
assertErrorContains "Test missing inputGates" "unknown gate 'I1' in switch" result
stsJSONParserTests :: [Test]
stsJSONParserTests =
[ testSTSJSONParserNominal
, testSTSJSONParserNominalFloat
, testSTSJSONParserUnknownType
, testSTSJSONParserAssignmentTypeMismatch
, testSTSJSONParserGuardTypeMismatch
, testSTSJSONParserGateIdDup
, testSTSJSONParserUnsupportedGuardOperand
, testSTSJSONParserUnsupportedAssignmentOperand
, testSTSJSONParserMissingSwitches
, testSTSJSONParserMissingGates
]