File: test_ast.py

package info (click to toggle)
nmodl 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,992 kB
  • sloc: cpp: 28,492; javascript: 9,841; yacc: 2,804; python: 1,967; lex: 1,674; xml: 181; sh: 136; ansic: 37; makefile: 18; pascal: 7
file content (51 lines) | stat: -rw-r--r-- 1,774 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# ***********************************************************************
# Copyright (C) 2018-2022 Blue Brain Project
#
# This file is part of NMODL distributed under the terms of the GNU
# Lesser General Public License. See top-level LICENSE file for details.
# ***********************************************************************

from nmodl.dsl import ast
import nmodl.dsl as nmodl
import pytest

class TestAst(object):
    def test_empty_program(self):
        pnode = ast.Program()
        assert str(pnode) == ''

    def test_ast_construction(self):
        string = ast.String("tau")
        name = ast.Name(string)
        assert nmodl.to_nmodl(name) == 'tau'

        int_macro = nmodl.ast.Integer(1, ast.Name(ast.String("x")))
        assert nmodl.to_nmodl(int_macro) == 'x'

        statements = []
        block = ast.StatementBlock(statements)
        neuron_block = ast.NeuronBlock(block)
        assert nmodl.to_nmodl(neuron_block) == 'NEURON {\n}'

    def test_get_parent(self):
        x_name = ast.Name(ast.String("x"))
        int_macro = nmodl.ast.Integer(1, x_name)
        assert x_name.parent == int_macro # getting the parent

    def test_set_parent(self):
        x_name = ast.Name(ast.String("x"))
        y_name = ast.Name(ast.String("y"))
        int_macro = nmodl.ast.Integer(1, x_name)
        y_name.parent = int_macro # setting the parent
        int_macro.macro = y_name
        assert nmodl.to_nmodl(int_macro) == 'y'

    def test_ast_node_repr(self):
        string = ast.String("tau")
        name = ast.Name(string)
        assert repr(name) == nmodl.to_json(name, compact=True)

    def test_ast_node_str(self):
        string = ast.String("tau")
        name = ast.Name(string)
        assert str(name) == nmodl.to_nmodl(name)