User Guide
nmodl.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"
11 #include "parser/nmodl_driver.hpp"
14 #include "utils/common_utils.hpp"
17 
18 using namespace nmodl;
19 using namespace visitor;
20 using namespace test;
21 using namespace test_utils;
22 
24 
25 
26 //=============================================================================
27 // AST to NMODL printer tests
28 //=============================================================================
29 
30 std::string run_nmodl_visitor(const std::string& text) {
32  const auto& ast = driver.parse_string(text);
33 
34  std::stringstream stream;
35  NmodlPrintVisitor(stream).visit_program(*ast);
36 
37  // check that, after visitor rearrangement, parents are still up-to-date
38  CheckParentVisitor().check_ast(*ast);
39 
40  return stream.str();
41 }
42 
43 SCENARIO("Convert AST back to NMODL form", "[visitor][nmodl]") {
44  for (const auto& construct: nmodl_valid_constructs) {
45  auto test_case = construct.second;
46  const std::string& input_nmodl_text = reindent_text(test_case.input);
47  const std::string& output_nmodl_text = reindent_text(test_case.output);
48  GIVEN(test_case.name) {
49  THEN("Visitor successfully returns : " + input_nmodl_text) {
50  auto result = run_nmodl_visitor(input_nmodl_text);
51  REQUIRE(result == output_nmodl_text);
52  }
53  }
54  }
55 }
test_utils.hpp
nmodl::parser::NmodlDriver
Class that binds all pieces together for parsing nmodl file.
Definition: nmodl_driver.hpp:67
nmodl::test_utils::nmodl_valid_constructs
const std::map< std::string, NmodlTestCase > nmodl_valid_constructs
Definition: nmodl_constructs.cpp:181
SCENARIO
SCENARIO("Convert AST back to NMODL form", "[visitor][nmodl]")
Definition: nmodl.cpp:43
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
program.hpp
Auto generated AST classes declaration.
driver
nmodl::parser::UnitDriver driver
Definition: parser.cpp:28
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
checkparent_visitor.hpp
Visitor for checking parents of ast nodes
nmodl_driver.hpp
nmodl_constructs.hpp
run_nmodl_visitor
std::string run_nmodl_visitor(const std::string &text)
Definition: nmodl.cpp:30
common_utils.hpp
Common utility functions for file/dir manipulation.
nmodl_visitor.hpp
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.