8 #include <catch2/catch_test_macros.hpp>
20 using namespace nmodl;
21 using namespace visitor;
23 using namespace test_utils;
33 const std::string&
text,
34 const std::vector<std::pair<std::string, std::string>>& variables) {
37 for (
const auto& variable: variables) {
40 std::stringstream stream;
49 SCENARIO(
"Renaming any variable in mod file with RenameVisitor",
"[visitor][rename]") {
52 std::string input_nmodl_text = R
"(
55 USEION na READ ena WRITE ina
60 gNaTs2_tbar = 0.1 (S/cm2)
69 m and gNaTs2_tbar remain same here
74 gNaTs2_t = gNaTs2_tbar*m*m*m*h
75 ina = gNaTs2_t*(v-ena)
83 std::string output_nmodl_text = R
"(
86 USEION na READ ena WRITE ina
91 new_gNaTs2_tbar = 0.1 (S/cm2)
100 m and gNaTs2_tbar remain same here
105 gNaTs2_t = new_gNaTs2_tbar*mm*mm*mm*h
106 ina = gNaTs2_t*(v-ena)
114 std::string expected_output = reindent_text(output_nmodl_text);
116 THEN("existing variables could be renamed") {
117 std::vector<std::pair<std::string, std::string>> variables = {
119 {
"gNaTs2_tbar",
"new_gNaTs2_tbar"},
123 REQUIRE(
result == expected_output);
126 THEN(
"non-existing variables will be ignored") {
127 std::vector<std::pair<std::string, std::string>> variables = {
128 {
"unknown_variable",
"doesnot_matter"}};
147 std::stringstream stream;
152 SCENARIO(
"Renaming with presence of local and global variables in same block",
153 "[visitor][rename]") {
154 GIVEN(
"A neuron block and procedure with same variable name") {
158 USEION na READ ena WRITE ina
164 gNaTs2_tbar = 2.1 + ena
168 std::string expected_nmodl_text = R"(
171 USEION na READ ena WRITE ina
176 LOCAL gNaTs2_tbar_r_0
177 gNaTs2_tbar_r_0 = 2.1+ena
181 THEN("var renaming pass changes only local variables in procedure") {
185 REQUIRE(
result == expected_result);
190 SCENARIO(
"Renaming in the absence of global blocks",
"[visitor][rename]") {
191 GIVEN(
"Procedures containing same variables") {
193 PROCEDURE rates_1() {
195 gNaTs2_tbar = 2.1+ena
198 PROCEDURE rates_2() {
200 gNaTs2_tbar = 2.1+ena
204 THEN("nothing gets renamed") {
212 SCENARIO(
"Variable renaming in nested blocks",
"[visitor][rename]") {
213 GIVEN(
"Mod file containing procedures with nested blocks") {
214 std::string input_nmodl_text = R
"(
217 USEION na READ ena WRITE ina
222 gNaTs2_tbar = 0.1 (S/cm2)
233 gNaTs2_t = gNaTs2_tbar*m*m*m*h
234 ina = gNaTs2_t*(v-ena)
256 LOCAL h, x, gNaTs2_tbar
257 m = h * x * gNaTs2_tbar + tau
263 std::string expected_nmodl_text = R"(
266 USEION na READ ena WRITE ina
271 gNaTs2_tbar = 0.1 (S/cm2)
282 gNaTs2_t = gNaTs2_tbar*m*m*m*h
283 ina = gNaTs2_t*(v-ena)
285 LOCAL gNaTs2_t_r_0, h_r_1
286 gNaTs2_t_r_0 = m+h_r_1
289 m_r_1 = gNaTs2_t_r_0+h_r_1
302 m_r_2 = x+gNaTs2_tbar
305 LOCAL h_r_2, x_r_0, gNaTs2_tbar_r_0
306 m_r_2 = h_r_2*x_r_0*gNaTs2_tbar_r_0+tau
312 THEN("variables conflicting with global variables get renamed starting from inner block") {
316 REQUIRE(
result == expected_result);
322 SCENARIO(
"Renaming in presence of local variable in verbatim block",
"[visitor][rename]") {
323 GIVEN(
"A neuron block and procedure with same variable name") {
333 #define my_macro_var _lgNaTs2_tbar*2
335 gNaTs2_tbar = my_macro_var + 1
345 std::string expected_nmodl_text = R"(
351 LOCAL gNaTs2_tbar_r_0, x
354 #define my_macro_var gNaTs2_tbar_r_0*2
356 gNaTs2_tbar_r_0 = my_macro_var+1
366 THEN("var renaming pass changes local & global variable in verbatim block") {
370 REQUIRE(
result == expected_result);
Visitor for checking parents of ast nodes
Class that binds all pieces together for parsing nmodl file.
void visit_program(ast::Program &node) override
visit node of type ast::Program
void visit_program(const ast::Program &node) override
visit node of type ast::Program
Visitor to rename local variables conflicting with global scope
Visitor for printing AST back to NMODL
void visit_program(const ast::Program &node) override
visit node of type ast::Program
Blindly rename given variable to new name
Concrete visitor for constructing symbol table from AST.
void visit_program(ast::Program &node) override
visit node of type ast::Program
Rename variable in verbatim block.
Visitor for checking parents of ast nodes
int check_ast(const ast::Ast &node)
A small wrapper to have a nicer call in parser.cpp.
bool parse_string(const std::string &input)
parser Units provided as string (used for testing)
Visitor to rename local variables conflicting with global scope
std::string reindent_text(const std::string &text, int indent_level)
Reindent nmodl text for text-to-text comparison.
encapsulates code generation backend implementations
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
Auto generated AST classes declaration.
static std::string run_var_rename_visitor(const std::string &text, const std::vector< std::pair< std::string, std::string >> &variables)
std::string run_local_var_rename_visitor(const std::string &text)
SCENARIO("Renaming any variable in mod file with RenameVisitor", "[visitor][rename]")
Blindly rename given variable to new name
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
nmodl::parser::UnitDriver driver
Rename variable in verbatim block.