NEURON
state_discontinuity.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 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 #include <catch2/matchers/catch_matchers_string.hpp>
10 
11 #include "ast/program.hpp"
12 #include "parser/nmodl_driver.hpp"
13 #include "utils/test_utils.hpp"
18 
19 
20 using namespace nmodl;
21 using namespace visitor;
22 using namespace test_utils;
23 
25 
26 //=============================================================================
27 // State discontinuity tests
28 //=============================================================================
29 
30 auto run_state_discontinuity_visitor(const std::string& text) {
32  const auto& ast = driver.parse_string(text);
34  if (SemanticAnalysisVisitor{}.check(*ast)) {
35  throw std::runtime_error("Semantic analysis failed");
36  }
38 
39  return to_nmodl(*ast);
40 }
41 
42 SCENARIO("State discontinuity in a NET_RECEIVE block", "[visitor][state_discontinuity]") {
43  GIVEN("A call to state_discontinuity") {
44  std::string nmodl_text = R"(
45  NET_RECEIVE (w) {
46  state_discontinuity(A, B)
47  }
48  )";
49  std::string expected = R"(
50  NET_RECEIVE (w) {
51  A = B
52  }
53  )";
54 
55  THEN("Convert it to A = B") {
56  const auto& actual = run_state_discontinuity_visitor(nmodl_text);
57  REQUIRE(reindent_text(expected) == reindent_text(actual));
58  }
59  }
60 }
61 
62 SCENARIO("State discontinuity outside of a NET_RECEIVE block", "[visitor][state_discontinuity]") {
63  GIVEN("A call to state_discontinuity") {
64  const std::string nmodl_text = R"(
65  PROCEDURE fn(w) {
66  state_discontinuity(A, B)
67  }
68  )";
69  THEN("Leave it as-is") {
70  const auto& actual = run_state_discontinuity_visitor(nmodl_text);
71  REQUIRE(reindent_text(nmodl_text) == reindent_text(actual));
72  }
73  }
74 }
75 
76 SCENARIO("State discontinuity call contains invalid arguments", "[visitor][state_discontinuity]") {
77  GIVEN("A call to state_discontinuity with no args") {
78  const std::string nmodl_text = R"(
79  NET_RECEIVE (w) {
80  state_discontinuity()
81  }
82  )";
83  THEN("Raise an error") {
85  }
86  }
87  GIVEN("A call to state_discontinuity with 1 arg") {
88  const std::string nmodl_text = R"(
89  NET_RECEIVE (w) {
90  state_discontinuity(A)
91  }
92  )";
93  THEN("Raise an error") {
95  }
96  }
97  GIVEN("A call to state_discontinuity with 3 args") {
98  const std::string nmodl_text = R"(
99  NET_RECEIVE (w) {
100  state_discontinuity(A, B, C)
101  }
102  )";
103  THEN("Raise an error") {
105  }
106  }
107  GIVEN("A call to state_discontinuity with first arg a compound expression") {
108  const std::string nmodl_text = R"(
109  NET_RECEIVE (w) {
110  state_discontinuity(A + 1, B)
111  }
112  )";
113  THEN("Raise an error") {
115  }
116  }
117 }
Class that binds all pieces together for parsing nmodl file.
void visit_program(ast::Program &node) override
visit node of type ast::Program
Visitor to check some semantic rules on the AST
Visitor used for replacing literal calls to state_discontinuity in a NET_RECEIVE block.
Concrete visitor for constructing symbol table from AST.
void visit_program(ast::Program &node) override
visit node of type ast::Program
int nmodl_text
Definition: modl.cpp:58
bool parse_string(const std::string &input)
parser Units provided as string (used for testing)
Definition: unit_driver.cpp:40
std::string reindent_text(const std::string &text, int indent_level)
Reindent nmodl text for text-to-text comparison.
Definition: test_utils.cpp:55
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
std::string to_nmodl(const ast::Ast &node, const std::set< ast::AstNodeType > &exclude_types)
Given AST node, return the NMODL string representation.
#define text
Definition: plot.cpp:60
Auto generated AST classes declaration.
Visitor to check some semantic rules on the AST
auto run_state_discontinuity_visitor(const std::string &text)
SCENARIO("State discontinuity in a NET_RECEIVE block", "[visitor][state_discontinuity]")
Visitor used for replacing literal calls to state_discontinuity in a NET_RECEIVE block.
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
nmodl::parser::UnitDriver driver
Definition: parser.cpp:28
Utility functions for visitors implementation.