def test_json_visitor(ch_ast): lookup_visitor = visitor.AstLookupVisitor(ast.AstNodeType.PRIME_NAME) ch_ast.accept(lookup_visitor) primes = lookup_visitor.get_nodes() # test compact json prime_str = nmodl.dsl.to_nmodl(primes[0]) prime_json = nmodl.dsl.to_json(primes[0], True) assert prime_json == '{"PrimeName":[{"String":[{"name":"m"}]},{"Integer":[{"name":"1"}]}]}' # test json with expanded keys result_json = nmodl.dsl.to_json(primes[0], compact=True, expand=True) expected_json = ('{"children":[{"children":[{"name":"m"}],' '"name":"String"},{"children":[{"name":"1"}],' '"name":"Integer"}],"name":"PrimeName"}') assert result_json == expected_json # test json with nmodl embedded result_json = nmodl.dsl.to_json(primes[0], compact=True, expand=True, add_nmodl=True) expected_json = ( '{"children":[{"children":[{"name":"m"}],"name":"String","nmodl":"m"},' '{"children":[{"name":"1"}],"name":"Integer","nmodl":"1"}],' '"name":"PrimeName","nmodl":"m\'"}') assert result_json == expected_json
def test_visitor_python_io(ch_ast): lookup_visitor = visitor.AstLookupVisitor() eqs = lookup_visitor.lookup(ch_ast, ast.AstNodeType.DIFF_EQ_EXPRESSION) stream = io.StringIO() pv = visitor.NmodlPrintVisitor(stream) eqs[0].accept(pv) # `del pv` is not required to have the ostream flushed assert stream.getvalue() == "m' = mInf-m"
def test_lookup_visitor_any_node(): """Ensure the AstLookupVisitor.lookup methods accept any node""" lookup_visitor = visitor.AstLookupVisitor(ast.AstNodeType.INTEGER) int42 = ast.Integer(42, None) eqs = lookup_visitor.lookup(int42) assert len(eqs) == 1 eqs = lookup_visitor.lookup(int42, ast.AstNodeType.DOUBLE) assert len(eqs) == 0
def test_lookup_visitor_constructor(ch_ast): lookup_visitor = visitor.AstLookupVisitor(ast.AstNodeType.DIFF_EQ_EXPRESSION) eqs = lookup_visitor.lookup(ch_ast) eq_str = nmodl.dsl.to_nmodl(eqs[0])
def test_lookup_visitor(ch_ast): lookup_visitor = visitor.AstLookupVisitor() eqs = lookup_visitor.lookup(ch_ast, ast.AstNodeType.DIFF_EQ_EXPRESSION) eq_str = nmodl.dsl.to_nmodl(eqs[0]) assert eq_str == "m' = mInf-m"
def test_lookup_visitor_constructor(ch_ast): lookup_visitor = visitor.AstLookupVisitor( ast.AstNodeType.DIFF_EQ_EXPRESSION) ch_ast.accept(lookup_visitor) eqs = lookup_visitor.get_nodes() eq_str = nmodl.dsl.to_nmodl(eqs[0])