-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathstring_quote.cpp
69 lines (65 loc) · 3.93 KB
/
string_quote.cpp
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
64
65
66
67
68
69
//
// string_quote.cpp
// boe_test
//
// Created by Celtic Minstrel on 2023-01-21.
//
#include "catch.hpp"
#include "fileio/fileio.hpp"
TEST_CASE("Quoting Strings") {
CHECK(maybe_quote_string("") == std::string("''"));
CHECK(maybe_quote_string(" ") == std::string("' '"));
CHECK(maybe_quote_string("Don't!") == std::string("Don't!"));
CHECK(maybe_quote_string("\"") == std::string("'\"'"));
CHECK(maybe_quote_string("'") == std::string("\"'\""));
CHECK(maybe_quote_string("-\"-") == std::string("-\"-"));
CHECK(maybe_quote_string("-'-") == std::string("-'-"));
CHECK(maybe_quote_string("Hello World") == std::string("'Hello World'"));
CHECK(maybe_quote_string("It's great!") == std::string("\"It's great!\""));
CHECK(maybe_quote_string("That is a \"silly\" idea.") == std::string("'That is a \"silly\" idea.'"));
CHECK(maybe_quote_string("1\n2") == std::string("'1\\n2'"));
CHECK(maybe_quote_string("1\t2") == std::string("'1\t2'"));
CHECK(maybe_quote_string("1\f2") == std::string("'1\\f2'"));
CHECK(maybe_quote_string("foo\"") == std::string("foo\""));
CHECK(maybe_quote_string("foo'") == std::string("foo'"));
CHECK(maybe_quote_string("That|is|great") == std::string("That|is|great"));
CHECK(maybe_quote_string("==!==") == std::string("==!=="));
CHECK(maybe_quote_string("Hello") == std::string("Hello"));
CHECK(maybe_quote_string("123") == std::string("123"));
CHECK(maybe_quote_string(".") == std::string("."));
CHECK(maybe_quote_string("path\\to\\file") == std::string("path\\to\\file"));
CHECK(maybe_quote_string("'path\\to\\file'") == std::string("\"'path\\\\to\\\\file'\""));
CHECK(maybe_quote_string("Can't stumble with \"quotes\" of both types!") == std::string("'Can\\'t stumble with \"quotes\" of both types!'"));
CHECK(maybe_quote_string("This is a \"complicated\" string\nwith 'many' different things to \\escape\\ in it! Shouldn't be too hard...?") == std::string("\"This is a \\\"complicated\\\" string\\nwith 'many' different things to \\\\escape\\\\ in it! Shouldn't be too hard...?\""));
}
static std::string unquote_string(std::string str) {
std::istringstream is(str);
return read_maybe_quoted_string(is);
}
TEST_CASE("Unquoting Strings") {
CHECK(unquote_string("''") == std::string(""));
CHECK(unquote_string("' '" ) == std::string(" "));
CHECK(unquote_string("Don't!") == std::string("Don't!"));
CHECK(unquote_string("'\"'") == std::string("\""));
CHECK(unquote_string("\"'\"" ) == std::string("'"));
CHECK(unquote_string("-\"-") == std::string("-\"-"));
CHECK(unquote_string("-'-") == std::string("-'-"));
CHECK(unquote_string("'Hello World'") == std::string("Hello World"));
CHECK(unquote_string("\"It's great!\"") == std::string("It's great!"));
CHECK(unquote_string("'That is a \"silly\" idea.'") == std::string("That is a \"silly\" idea."));
CHECK(unquote_string("'1\\n2'") == std::string("1\n2"));
CHECK(unquote_string("'1\t2'") == std::string("1\t2"));
CHECK(unquote_string("'1\\t2'") == std::string("1\t2"));
CHECK(unquote_string("'1\\f2'") == std::string("1\f2"));
CHECK(unquote_string("foo\"") == std::string("foo\""));
CHECK(unquote_string("foo'") == std::string("foo'"));
CHECK(unquote_string("That|is|great") == std::string("That|is|great"));
CHECK(unquote_string("==!==") == std::string("==!=="));
CHECK(unquote_string("Hello") == std::string("Hello"));
CHECK(unquote_string("123") == std::string("123"));
CHECK(unquote_string(".") == std::string("."));
CHECK(unquote_string("path\\to\\file") == std::string("path\\to\\file"));
CHECK(unquote_string("\"'path\\\\to\\\\file'\"") == std::string("'path\\to\\file'"));
CHECK(unquote_string("'Can\\'t stumble with \"quotes\" of both types!'") == std::string("Can't stumble with \"quotes\" of both types!"));
CHECK(unquote_string("\"This is a \\\"complicated\\\" string\\nwith 'many' different things to \\\\escape\\\\ in it! Shouldn't be too hard...?\"") == std::string("This is a \"complicated\" string\nwith 'many' different things to \\escape\\ in it! Shouldn't be too hard...?"));
}