User Guide
transform.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <catch2/catch_test_macros.hpp>
9 
10 #include "ast/program.hpp"
12 #include "parser/nmodl_driver.hpp"
15 
16 using namespace nmodl;
17 using namespace visitor;
18 using namespace test_utils;
19 
21 
22 
23 //=============================================================================
24 // Variable rename tests
25 //=============================================================================
26 
27 static std::string run_transform_visitor(const std::string& text) {
29  const auto& ast = driver.parse_string(text);
31  std::stringstream stream;
32  NmodlPrintVisitor(stream).visit_program(*ast);
33 
34  return stream.str();
35 }
36 
37 SCENARIO("Adding a variable for a table inside a function", "[visitor][transform]") {
38  GIVEN("A mod file with a table inside a function") {
39  // sample nmodl text
40  std::string input_nmodl_text = R"(
41  FUNCTION mAlpha(v) {
42  TABLE FROM 0 TO 150 WITH 1
43  mAlpha = 1
44  }
45  )";
46 
47  /// expected result after adding the variable
48  std::string output_nmodl_text = R"(
49  FUNCTION mAlpha(v) {
50  TABLE mAlpha FROM 0 TO 150 WITH 1
51  mAlpha = 1
52  }
53  )";
54 
55  std::string input = reindent_text(input_nmodl_text);
56  std::string expected_output = reindent_text(output_nmodl_text);
57 
58  THEN("variable has been added") {
59  auto result = run_transform_visitor(input);
60  REQUIRE(result == expected_output);
61  }
62  }
63 }
test_utils.hpp
nmodl::parser::NmodlDriver
Class that binds all pieces together for parsing nmodl file.
Definition: nmodl_driver.hpp:67
SCENARIO
SCENARIO("Adding a variable for a table inside a function", "[visitor][transform]")
Definition: transform.cpp:37
nmodl::test_utils::reindent_text
std::string reindent_text(const std::string &text, int indent_level)
Reindent nmodl text for text-to-text comparison.
Definition: test_utils.cpp:53
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::CodegenTransformVisitor
Visitor to make last transformation to AST before codegen.
Definition: codegen_transform_visitor.hpp:33
nmodl::visitor::AstVisitor::visit_program
void visit_program(ast::Program &node) override
visit node of type ast::Program
Definition: ast_visitor.cpp:364
program.hpp
Auto generated AST classes declaration.
driver
nmodl::parser::UnitDriver driver
Definition: parser.cpp:28
codegen_transform_visitor.hpp
nmodl::parser::UnitDriver::parse_string
bool parse_string(const std::string &input)
parser Units provided as string (used for testing)
Definition: unit_driver.cpp:40
run_transform_visitor
static std::string run_transform_visitor(const std::string &text)
Definition: transform.cpp:27
nmodl_driver.hpp
nmodl_visitor.hpp
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.