User Guide
json.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 
15 #include <nlohmann/json.hpp>
16 
18 
19 using namespace nmodl;
20 using namespace visitor;
21 
23 
24 //=============================================================================
25 // JSON visitor tests
26 //=============================================================================
27 
28 std::string run_json_visitor(const std::string& text, bool compact = false) {
30  auto ast = driver.parse_string(text);
31 
32  return to_json(*ast, compact);
33 }
34 
35 TEST_CASE("Convert NMODL to AST to JSON form using JSONVisitor", "[visitor][json]") {
36  SECTION("JSON object test") {
37  std::string nmodl_text = "NEURON {}";
38  json expected = R"(
39  {
40  "Program": [
41  {
42  "NeuronBlock": [
43  {
44  "StatementBlock": []
45  }
46  ]
47  }
48  ]
49  }
50  )"_json;
51 
52  auto json_text = run_json_visitor(nmodl_text);
53  json result = json::parse(json_text);
54 
55  REQUIRE(expected == result);
56  }
57 
58  SECTION("JSON text test (compact format)") {
59  std::string nmodl_text = "NEURON {}";
60  std::string expected = R"({"Program":[{"NeuronBlock":[{"StatementBlock":[]}]}]})";
61 
62  auto result = run_json_visitor(nmodl_text, true);
63  REQUIRE(result == expected);
64  }
65 }
nmodl::parser::NmodlDriver
Class that binds all pieces together for parsing nmodl file.
Definition: nmodl_driver.hpp:67
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::to_json
std::string to_json(const ast::Ast &node, bool compact, bool expand, bool add_nmodl)
Given AST node, return the JSON string representation.
Definition: visitor_utils.cpp:242
TEST_CASE
TEST_CASE("Convert NMODL to AST to JSON form using JSONVisitor", "[visitor][json]")
Definition: json.cpp:35
visitor_utils.hpp
Utility functions for visitors implementation.
program.hpp
Auto generated AST classes declaration.
driver
nmodl::parser::UnitDriver driver
Definition: parser.cpp:28
run_json_visitor
std::string run_json_visitor(const std::string &text, bool compact=false)
Definition: json.cpp:28
json
nlohmann::json json
Definition: json.cpp:17
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
nmodl_driver.hpp
json_visitor.hpp
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.