File: var_usage.cpp

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 (97 lines) | stat: -rw-r--r-- 3,199 bytes parent folder | download | duplicates (2)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*************************************************************************
 * 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.
 *************************************************************************/

#include <catch2/catch_test_macros.hpp>

#include "ast/program.hpp"
#include "parser/nmodl_driver.hpp"
#include "test/unit/utils/test_utils.hpp"
#include "visitors/var_usage_visitor.hpp"

using namespace nmodl;
using namespace visitor;

//=============================================================================
// Variable usage visitor tests
//=============================================================================

static bool run_var_usage_visitor(const std::shared_ptr<ast::Node>& node,
                                  const std::string& variable) {
    return VarUsageVisitor().variable_used(*node, variable);
}

SCENARIO("Searching for variable name using VarUsageVisitor", "[visitor][var_usage]") {
    auto to_ast = [](const std::string& text) {
        parser::NmodlDriver driver;
        return driver.parse_string(text);
    };

    GIVEN("A simple NMODL block") {
        std::string nmodl_text = R"(
            DERIVATIVE states {
                tau = 11.1
                exp(tau)
            {
                h' = h + 2 + n
            }
            }
        )";

        auto ast = to_ast(nmodl_text);
        auto node = ast->get_blocks().front();

        WHEN("Looking for existing variable") {
            THEN("Can find variables") {
                REQUIRE(run_var_usage_visitor(node, "tau"));
                REQUIRE(run_var_usage_visitor(node, "h"));
                REQUIRE(run_var_usage_visitor(node, "n"));
            }
        }

        WHEN("Looking for missing variable") {
            THEN("Can not find variable") {
                REQUIRE_FALSE(run_var_usage_visitor(node, "tauu"));
                REQUIRE_FALSE(run_var_usage_visitor(node, "my_var"));
            }
        }
    }

    GIVEN("A nested NMODL block") {
        std::string nmodl_text = R"(
            NET_RECEIVE (weight,weight_AMPA, weight_NMDA, R){
                LOCAL result
                weight_AMPA = weight
                weight_NMDA = weight * NMDA_ratio
                INITIAL {
                    R = 1
                    u = u0
                    {
                        tsyn = t
                    }
                }
            }
        )";

        auto ast = to_ast(nmodl_text);
        auto node = ast->get_blocks().front();

        WHEN("Looking for existing variable in outer block") {
            THEN("Can find variables") {
                REQUIRE(run_var_usage_visitor(node, "weight"));
                REQUIRE(run_var_usage_visitor(node, "NMDA_ratio"));
            }
        }

        WHEN("Looking for existing variable in inner block") {
            THEN("Can find variables") {
                REQUIRE(run_var_usage_visitor(node, "R"));
                REQUIRE(run_var_usage_visitor(node, "u0"));
                REQUIRE(run_var_usage_visitor(node, "tsyn"));
            }
        }
    }
}