1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
/*************************************************************************
* Copyright (C) 2018-2022 Blue Brain Project
*
* This file is part of NMODL distributed under the terms of the GNU
* Lesser General Public License. See top-level LICENSE file for details.
*************************************************************************/
#include <catch2/catch_test_macros.hpp>
#include "ast/program.hpp"
#include "parser/nmodl_driver.hpp"
#include "visitors/json_visitor.hpp"
#include "visitors/visitor_utils.hpp"
using json = nlohmann::json;
using namespace nmodl;
using namespace visitor;
using nmodl::parser::NmodlDriver;
//=============================================================================
// JSON visitor tests
//=============================================================================
std::string run_json_visitor(const std::string& text, bool compact = false) {
NmodlDriver driver;
auto ast = driver.parse_string(text);
return to_json(*ast, compact);
}
TEST_CASE("Convert NMODL to AST to JSON form using JSONVisitor", "[visitor][json]") {
SECTION("JSON object test") {
std::string nmodl_text = "NEURON {}";
json expected = R"(
{
"Program": [
{
"NeuronBlock": [
{
"StatementBlock": []
}
]
}
]
}
)"_json;
auto json_text = run_json_visitor(nmodl_text);
json result = json::parse(json_text);
REQUIRE(expected == result);
}
SECTION("JSON text test (compact format)") {
std::string nmodl_text = "NEURON {}";
std::string expected = R"({"Program":[{"NeuronBlock":[{"StatementBlock":[]}]}]})";
auto result = run_json_visitor(nmodl_text, true);
REQUIRE(result == expected);
}
}
|