QMCPACK
test_AttributeSet.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2022 QMCPACK developers.
6 //
7 // File developed by: Mark Dewing, markdewing@gmail.com, University of Illinois at Urbana-Champaign
8 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
9 //
10 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 #include "catch.hpp"
15 
16 #include "OhmmsData/Libxml2Doc.h"
17 #include "OhmmsData/AttributeSet.h"
18 #include <string>
19 
20 using std::string;
21 
22 TEST_CASE("AttributeSet", "[xml]")
23 {
24  const char* content = R"(
25  <simulation name="here" deprecated_tag="lmn">
26  </simulation>)";
28  bool okay = doc.parseFromString(content);
29  REQUIRE(okay == true);
30 
31  xmlNodePtr root = doc.getRoot();
32  OhmmsAttributeSet pattrib;
33  string name = "default_name";
34  string other = "default";
35  string deprecated_tag;
36  pattrib.add(name, "name");
37  pattrib.add(other, "other");
38  pattrib.add(deprecated_tag, "deprecated_tag", {"abc", "def"}, TagStatus::DEPRECATED);
39  CHECK_THROWS_WITH(pattrib.put(doc.getRoot()), Catch::Matchers::Contains("is not valid"));
40 
41  REQUIRE(name == "here");
42  REQUIRE(other == "default");
43  REQUIRE(deprecated_tag == "lmn");
44 }
45 
46 TEST_CASE("AttributeSet_bool", "[xml]")
47 {
48  {
49  const char* content = R"(<simulation/>)";
51  bool okay = doc.parseFromString(content);
52  REQUIRE(okay == true);
53 
54  OhmmsAttributeSet pattrib;
55  bool use_feature = true;
56  pattrib.add(use_feature, "use_feature", {false, true});
57  pattrib.put(doc.getRoot());
58  CHECK(use_feature == false);
59  }
60 
61  {
62  const char* content = R"(<simulation use_feature=" yes "/>)";
64  bool okay = doc.parseFromString(content);
65  REQUIRE(okay == true);
66 
67  OhmmsAttributeSet pattrib;
68  bool use_feature = true;
69  pattrib.add(use_feature, "use_feature", {false, true});
70  pattrib.put(doc.getRoot());
71  CHECK(use_feature == true);
72  }
73 
74  {
75  const char* content = R"(<simulation use_feature=" no "/>)";
77  bool okay = doc.parseFromString(content);
78  REQUIRE(okay == true);
79 
80  OhmmsAttributeSet pattrib;
81  bool use_feature = true;
82  pattrib.add(use_feature, "use_feature", {false, true});
83  pattrib.put(doc.getRoot());
84  CHECK(use_feature == false);
85  }
86 
87  {
88  const char* content = R"(<simulation use_feature=" YES "/>)";
90  bool okay = doc.parseFromString(content);
91  REQUIRE(okay == true);
92 
93  OhmmsAttributeSet pattrib;
94  bool use_feature = true;
95  pattrib.add(use_feature, "use_feature", {false, true});
96  pattrib.put(doc.getRoot());
97  CHECK(use_feature == true);
98  }
99 
100  {
101  const char* content = R"(<simulation use_feature=" No"/>)";
103  bool okay = doc.parseFromString(content);
104  REQUIRE(okay == true);
105 
106  OhmmsAttributeSet pattrib;
107  bool use_feature = true;
108  pattrib.add(use_feature, "use_feature", {false, true});
109  pattrib.put(doc.getRoot());
110  CHECK(use_feature == false);
111  }
112 
113  {
114  const char* content = R"(<simulation use_feature=" true "/>)";
116  bool okay = doc.parseFromString(content);
117  REQUIRE(okay == true);
118 
119  OhmmsAttributeSet pattrib;
120  bool use_feature = true;
121  pattrib.add(use_feature, "use_feature", {false, true});
122  pattrib.put(doc.getRoot());
123  CHECK(use_feature == true);
124  }
125 
126  {
127  const char* content = R"(<simulation use_feature="false"/>)";
129  bool okay = doc.parseFromString(content);
130  REQUIRE(okay == true);
131 
132  OhmmsAttributeSet pattrib;
133  bool use_feature = true;
134  pattrib.add(use_feature, "use_feature", {false, true});
135  pattrib.put(doc.getRoot());
136  CHECK(use_feature == false);
137  }
138 
139  {
140  const char* content = R"(<simulation use_feature=" "/>)";
142  bool okay = doc.parseFromString(content);
143  REQUIRE(okay == true);
144 
145  OhmmsAttributeSet pattrib;
146  bool use_feature = true;
147  pattrib.add(use_feature, "use_feature", {false, true});
148  CHECK_THROWS_WITH(pattrib.put(doc.getRoot()), "use_feature requires a single value input.");
149  }
150 
151  {
152  const char* content = R"(<simulation use_feature=" no a "/>)";
154  bool okay = doc.parseFromString(content);
155  REQUIRE(okay == true);
156 
157  OhmmsAttributeSet pattrib;
158  bool use_feature = true;
159  pattrib.add(use_feature, "use_feature", {false, true});
160  CHECK_THROWS_WITH(pattrib.put(doc.getRoot()), "use_feature only accepts a single value input.");
161  }
162 
163  {
164  const char* content = R"(<simulation use_feature=" here "/>)";
166  bool okay = doc.parseFromString(content);
167  REQUIRE(okay == true);
168 
169  OhmmsAttributeSet pattrib;
170  bool use_feature = true;
171  pattrib.add(use_feature, "use_feature", {false, true});
172  CHECK_THROWS_WITH(pattrib.put(doc.getRoot()),
173  "use_feature only accepts 'yes'/'no'/'true'/'false' but the input value is 'here'.");
174  }
175 }
class that handles xmlDoc
Definition: Libxml2Doc.h:76
bool put(xmlNodePtr cur)
assign attributes to the set
Definition: AttributeSet.h:55
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
REQUIRE(std::filesystem::exists(filename))
class to handle a set of attributes of an xmlNode
Definition: AttributeSet.h:24
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
void add(PDT &aparam, const std::string &aname, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new attribute
Definition: AttributeSet.h:42
TEST_CASE("AttributeSet", "[xml]")