User Guide
test_utils.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 "test_utils.hpp"
9 #include "utils/logger.hpp"
10 #include "utils/string_utils.hpp"
11 
12 #include <cassert>
13 
14 namespace nmodl {
15 namespace test_utils {
16 
17 int count_leading_spaces(std::string text) {
18  auto const length = text.size();
19  text = nmodl::stringutils::ltrim(text);
20  auto const num_whitespaces = length - text.size();
21  assert(num_whitespaces <= std::numeric_limits<int>::max());
22  return static_cast<int>(num_whitespaces);
23 }
24 
25 /// check if string has only whitespaces
26 bool is_empty(const std::string& text) {
27  return nmodl::stringutils::trim(text).empty();
28 }
29 
30 /** Reindent nmodl text for text-to-text comparison
31  *
32  * Nmodl constructs defined in test database has extra leading whitespace.
33  * This is done for readability reason in nmodl_constructs.cpp. For example,
34  * we have following nmodl text with 8 leading whitespaces:
35 
36 
37  NEURON {
38  RANGE x
39  }
40 
41  * We convert above paragraph to:
42 
43 NEURON {
44  RANGE x
45 }
46 
47  * i.e. we get first non-empty line and count number of leading whitespaces (X).
48  * Then for every sub-sequent line, we remove first X characters (assuming those
49  * all are whitespaces). This is done because when ast is transformed back to
50  * nmodl, the nmodl output is without "extra" whitespaces in the provided input.
51  */
52 
53 std::string reindent_text(const std::string& text, int indent_level) {
54  std::string indented_text;
55  int num_whitespaces = 0;
56  bool flag = false;
57  std::string line;
58  std::stringstream stream(text);
59  std::string indent(4 * indent_level, ' ');
60 
61  while (std::getline(stream, line)) {
62  if (!line.empty()) {
63  /// count whitespaces for first non-empty line only
64  if (!flag) {
65  flag = true;
66  num_whitespaces = count_leading_spaces(line);
67  }
68 
69  /// make sure we don't remove non whitespaces characters
70  if (!is_empty(line.substr(0, num_whitespaces))) {
71  throw std::runtime_error("Test nmodl input not correctly formatted");
72  }
73 
74  line.erase(0, num_whitespaces);
75  indented_text += indent + line;
76  }
77  /// discard empty lines at very beginning
78  if (!stream.eof() && flag) {
79  indented_text += "\n";
80  }
81  }
82  return indented_text;
83 }
84 
85 } // namespace test_utils
86 } // namespace nmodl
test_utils.hpp
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
string_utils.hpp
Implement string manipulation functions.
nmodl::test_utils::is_empty
bool is_empty(const std::string &text)
check if string has only whitespaces
Definition: test_utils.cpp:26
nmodl::stringutils::trim
static std::string trim(std::string text)
Definition: string_utils.hpp:63
nmodl::test_utils::count_leading_spaces
int count_leading_spaces(std::string text)
Definition: test_utils.cpp:17
nmodl::stringutils::ltrim
static std::string ltrim(std::string text)
Definition: string_utils.hpp:41
logger.hpp
Implement logger based on spdlog library.