def test_either() -> None: ast = Either([ [ Stmt(Print(Expr("foo"))), ], [ Stmt(Print(Expr("bar"))), ], [ Stmt(Print(Expr("baz"))), ], ]) ast.validate() assert_that( str(ast), is_( equal_to( dedent("""\ either print foo; or print bar; or print baz; end either;"""))), )
def test_with() -> None: ast = With( declarations=[ VarDecl( Variable("foo"), ( DeclType.EQUALS, Expr("1"), ), ), VarDecl( Variable("bar"), ( DeclType.IN, Expr("1..10"), ), ), ], statements=[ Stmt(Print(Expr("foo + bar"))), ], ) ast.validate() assert_that( str(ast), is_(equal_to(dedent("""\ with foo = 1, bar \\in 1..10 do print foo + bar; end with;"""))), )
def test_if_elsif() -> None: ast = If( IfClause( Expr("foo"), [ Stmt(Print(Expr("bar"))), ], ), [ ElsifClause( Expr("baz"), [ Stmt(Print(Expr("baz"))), ], ), ], ) ast.validate() assert_that( str(ast), is_( equal_to( dedent("""\ if foo then print bar; elsif baz then print baz; end if;"""))), )
def test_assign_qualifier() -> None: ast = Assign([ Assignment( left=LHS( name=Variable("foo"), items=[ Field("a"), [ Expr("b"), Expr("c"), Expr("d"), ], Field("e"), ], ), right=Expr("bar"), ), ]) ast.validate() assert_that( str(ast), is_(equal_to("foo.a[b, c, d].e := bar;")), )
def test_expr(): ast = Expr("foo") ast.validate() assert_that( str(ast), is_(equal_to("foo")), )
def test_call() -> None: ast = Call( Name("foo"), [ Expr("bar"), Expr("baz"), ], ) ast.validate() assert_that( str(ast), is_(equal_to("call foo(bar, baz);")), )
def test_assert() -> None: ast = Assert(Expr("foo")) ast.validate() assert_that( str(ast), is_(equal_to("assert foo;")), )
def test_print() -> None: ast = Print(Expr('"foo"')) ast.validate() assert_that( str(ast), is_(equal_to("print \"foo\";")), )
def test_assign_two() -> None: ast = Assign([ Assignment( left=LHS(name=Variable("foo"), ), right=Expr("bar"), ), Assignment( left=LHS(name=Variable("this"), ), right=Expr("that"), ), ]) ast.validate() assert_that( str(ast), is_(equal_to("foo := bar || this := that;")), )
def test_process() -> None: ast = Process( name=Name("foo"), type=DeclType.EQUALS, value=Expr("\"bar\""), body=AlgorithmBody(items=[ Stmt( value=While( condition=Expr("TRUE"), statements=[ Stmt(Skip()), ], ), label=Label("Foo"), ), ], ), variables=VarDecls(items=[ VarDecl( Variable("baz"), ( DeclType.EQUALS, Expr("1"), ), ), ], ), ) ast.validate() assert_that( str(ast), is_( equal_to( dedent("""\ process foo = "bar" variable baz = 1; begin Foo: while TRUE do skip; end while; end process;"""))), )
def test_stmt_without_label() -> None: ast = Stmt(value=Print(value=Expr("1"), ), ) ast.validate() assert_that( str(ast), is_(equal_to(dedent("""\ print 1;"""))), )
def test_procedure() -> None: ast = Procedure( name=Name("foo"), args=[ Variable("bar"), ], variables=PVarDecls( items=[ PVarDecl( name=Variable("baz"), value=Expr("1"), ), PVarDecl( name=Variable("qux"), ), ], ), body=AlgorithmBody( items=[ Stmt(Assert(Expr("bar \\= baz")), label=Label("Foo")), Stmt(Return()), ], ), ) ast.validate() assert_that( str(ast), is_(equal_to(dedent("""\ procedure foo(bar) variables baz = 1, qux; begin Foo: assert bar \\= baz; return; end procedure;"""))), )
def test_macro() -> None: ast = Macro( name=Name("foo"), args=[ Variable("bar"), Variable("baz"), ], body=AlgorithmBody(items=[ Stmt(Assert(Expr("bar \\= baz"))), ], ), ) ast.validate() assert_that( str(ast), is_( equal_to( dedent("""\ macro foo(bar, baz) begin assert bar \\= baz; end macro;"""))), )