openMSX
AdhocCliCommParser_test.cc
Go to the documentation of this file.
1 #include "catch.hpp"
2 #include "AdhocCliCommParser.hh"
3 #include <string>
4 #include <vector>
5 
6 using namespace std;
7 
8 static vector<string> parse(const string& stream)
9 {
10  vector<string> result;
11  AdhocCliCommParser parser([&](const string& cmd) { result.push_back(cmd); });
12  parser.parse(stream.data(), stream.size());
13  return result;
14 }
15 
16 TEST_CASE("AdhocCliCommParser")
17 {
18  SECTION("whitespace") {
19  CHECK(parse("<command>foo</command>") ==
20  vector<string>{"foo"});
21  CHECK(parse(" \t<command>foo</command>") ==
22  vector<string>{"foo"});
23  CHECK(parse("<command>foo</command>\n ") ==
24  vector<string>{"foo"});
25  CHECK(parse(" \n\n<command>foo</command> \t\n ") ==
26  vector<string>{"foo"});
27  }
28  SECTION("multiple commands") {
29  CHECK(parse("<command>foo</command><command>bar</command>") ==
30  vector<string>{"foo", "bar"});
31  CHECK(parse("<command>foo</command> <command>bar</command>") ==
32  vector<string>{"foo", "bar"});
33  }
34  SECTION("XML-entities") {
35  CHECK(parse("<command>&amp;</command>") ==
36  vector<string>{"&"});
37  CHECK(parse("<command>&apos;</command>") ==
38  vector<string>{"'"});
39  CHECK(parse("<command>&quot;</command>") ==
40  vector<string>{"\""});
41  CHECK(parse("<command>&lt;</command>") ==
42  vector<string>{"<"});
43  CHECK(parse("<command>&gt;</command>") ==
44  vector<string>{">"});
45  CHECK(parse("<command>&#65;</command>") ==
46  vector<string>{"A"});
47  CHECK(parse("<command>&#x41;</command>") ==
48  vector<string>{"A"});
49  CHECK(parse("<command>&lt;command&gt;</command>") ==
50  vector<string>{"<command>"});
51  }
52  SECTION("tags other than <command> are ignored") {
53  CHECK(parse("<openmsx-control> <command>foo</command> </openmsx-control>") ==
54  vector<string>{"foo"});
55  CHECK(parse("<openmsx-control> <unknown>foo</unknown> <command>foo</command>") ==
56  vector<string>{"foo"});
57  }
58  SECTION("errors, but we do recover") {
59  CHECK(parse("<command>&unknown;</command><command>foo</command>") ==
60  vector<string>{"foo"});
61  CHECK(parse("<command>&#fffffff;</command><command>foo</command>") ==
62  vector<string>{"foo"});
63  CHECK(parse("<<<<<command>foo</command>") ==
64  vector<string>{"foo"});
65  CHECK(parse("<foo></bar><command>foo</command>") ==
66  vector<string>{"foo"});
67  CHECK(parse("</command><command>foo</command>") ==
68  vector<string>{"foo"});
69  CHECK(parse("<command>bar</foobar><command>foo</command>") ==
70  vector<string>{"foo"});
71  }
72  SECTION("old (XML) parser didn't accept this, AdhocCliCommParser does") {
73  // new parser ignores the <openmsx-control> tag
74  CHECK(parse("<openmsx-control></openmsx-control><command>foo</command>") ==
75  vector<string>{"foo"});
76  }
77  SECTION("old (XML) parser did accept this, AdhocCliCommParser does not") {
78  // attributes are no longer accepted
79  CHECK(parse("<command value=\"3\">foo</command>") ==
80  vector<string>{});
81  // tags nested in the <command> tag are no longer accepted
82  CHECK(parse("<command><bla></bla>foo</command>") ==
83  vector<string>{});
84  // The shorthand notation for an empty tag is no longer recognized:
85  // sort-of OK, was before an empty command, now no command
86  CHECK(parse("<command/>") ==
87  vector<string>{});
88  }
89 }
TEST_CASE("AdhocCliCommParser")
void parse(const char *buf, size_t n)
CHECK(m3==m3)