Debian Codegraph @

t/cli.cc

raw symbols SCIP
1 #include <opustags.h>
2 #include "tap.h"
3
4 #include <string.h>
5
6 static ot::status read_comments(FILE* input, std::list<std::u8string>& comments, bool raw)
7 {
8 ot::options opt;
9 opt.raw = raw;
10 try {
11 comments = ot::read_comments(input, opt);
12 } catch (const ot::status& rc) {
13 return rc;
14 }
15 return ot::st::ok;
16 }
17
18 void check_read_comments()
19 {
20 std::list<std::u8string> comments;
21 ot::status rc;
22 {
23 std::string txt = "TITLE=a b c\n\nARTIST=X\nArtist=Y\n"s;
24 ot::file input = fmemopen((char*) txt.data(), txt.size(), "r");
25 rc = read_comments(input.get(), comments, false);
26 if (rc != ot::st::ok)
27 throw failure("could not read comments");
28 auto&& expected = {u8"TITLE=a b c", u8"ARTIST=X", u8"Artist=Y"};
29 if (!std::equal(comments.begin(), comments.end(), expected.begin(), expected.end()))
30 throw failure("parsed user comments did not match expectations");
31 }
32 {
33 std::string txt = "CORRUPTED=\xFF\xFF\n"s;
34 ot::file input = fmemopen((char*) txt.data(), txt.size(), "r");
35 rc = read_comments(input.get(), comments, false);
36 if (rc != ot::st::badly_encoded)
37 throw failure("did not get the expected error reading corrupted data");
38 }
39 {
40 std::string txt = "RAW=\xFF\xFF\n"s;
41 ot::file input = fmemopen((char*) txt.data(), txt.size(), "r");
42 rc = read_comments(input.get(), comments, true);
43 if (rc != ot::st::ok)
44 throw failure("could not read comments");
45 if (comments.front() != (char8_t*) "RAW=\xFF\xFF")
46 throw failure("parsed user comments did not match expectations");
47 }
48 {
49 std::string txt = "MULTILINE=First\n\tSecond\n"s;
50 ot::file input = fmemopen((char*) txt.data(), txt.size(), "r");
51 rc = read_comments(input.get(), comments, true);
52 if (rc != ot::st::ok)
53 throw failure("could not read comments");
54 if (comments.front() != u8"MULTILINE=First\nSecond")
55 throw failure("parsed user comments did not match expectations");
56 }
57 {
58 std::string txt = "MALFORMED\n"s;
59 ot::file input = fmemopen((char*) txt.data(), txt.size(), "r");
60 rc = read_comments(input.get(), comments, false);
61 if (rc != ot::st::error)
62 throw failure("did not get the expected error reading malformed comments");
63 }
64 {
65 std::string txt = "\tBad"s;
66 ot::file input = fmemopen((char*) txt.data(), txt.size(), "r");
67 rc = read_comments(input.get(), comments, true);
68 if (rc != ot::st::error)
69 throw failure("did not get the expected error reading bad continuation line");
70 }
71 }
72
73 /**
74 * Wrap #ot::parse_options with a higher-level interface much more convenient for testing.
75 * In practice, the argc/argv combo are enough though for the current state of opustags.
76 */
77 static ot::status parse_options(const std::vector<const char*>& args, ot::options& opt, FILE *comments)
78 {
79 int argc = args.size();
80 char* argv[argc];
81 for (int i = 0; i < argc; ++i)
82 argv[i] = strdup(args[i]);
83 ot::status rc = ot::st::ok;
84 try {
85 opt = ot::parse_options(argc, argv, comments);
86 } catch (const ot::status& e) {
87 rc = e;
88 }
89 for (int i = 0; i < argc; ++i)
90 free(argv[i]);
91 return rc;
92 }
93
94 void check_good_arguments()
95 {
96 auto parse = [](std::vector<const char*> args) {
97 ot::options opt;
98 std::string txt = "N=1\n"s;
99 ot::file input = fmemopen((char*) txt.data(), txt.size(), "r");
100 ot::status rc = parse_options(args, opt, input.get());
101 if (rc.code != ot::st::ok)
102 throw failure("unexpected option parsing error");
103 return opt;
104 };
105
106 ot::options opt;
107 opt = parse({"opustags", "--help", "x", "-o", "y"});
108 if (!opt.print_help)
109 throw failure("did not catch --help");
110
111 opt = parse({"opustags", "x", "--output", "y", "-D", "-s", "X=Y Z", "-d", "a=b"});
112 if (opt.paths_in.size() != 1 || opt.paths_in.front() != "x" || !opt.path_out ||
113 opt.path_out != "y" || !opt.delete_all || opt.overwrite || opt.to_delete.size() != 2 ||
114 opt.to_delete.front() != u8"X" || *std::next(opt.to_delete.begin()) != u8"a=b" ||
115 opt.to_add != std::list<std::u8string>{ u8"X=Y Z" })
116 throw failure("unexpected option parsing result for case #1");
117
118 opt = parse({"opustags", "-S", "x", "-S", "-a", "x=y z", "-i"});
119 if (opt.paths_in.size() != 1 || opt.paths_in.front() != "x" || opt.path_out ||
120 !opt.overwrite || opt.to_delete.size() != 0 ||
121 opt.to_add != std::list<std::u8string>{ u8"N=1", u8"x=y z" })
122 throw failure("unexpected option parsing result for case #2");
123
124 opt = parse({"opustags", "-i", "x", "y", "z"});
125 if (opt.paths_in.size() != 3 || opt.paths_in[0] != "x" || opt.paths_in[1] != "y" ||
126 opt.paths_in[2] != "z" || !opt.overwrite || !opt.in_place)
127 throw failure("unexpected option parsing result for case #3");
128
129 opt = parse({"opustags", "-ie", "x"});
130 if (opt.paths_in.size() != 1 || opt.paths_in[0] != "x" ||
131 !opt.edit_interactively || !opt.overwrite || !opt.in_place)
132 throw failure("unexpected option parsing result for case #4");
133
134 opt = parse({"opustags", "-a", "X=\xFF", "--raw", "x"});
135 if (!opt.raw || opt.to_add.front() != u8"X=\xFF")
136 throw failure("--raw did not disable transcoding");
137 }
138
139 void check_bad_arguments()
140 {
141 auto error_code_case = [](std::vector<const char*> args, const char* message, ot::st error_code, const std::string& name) {
142 ot::options opt;
143 std::string txt = "N=1\nINVALID"s;
144 ot::file input = fmemopen((char*) txt.data(), txt.size(), "r");
145 ot::status rc = parse_options(args, opt, input.get());
146 if (rc.code != error_code)
147 throw failure("bad error code for case " + name);
148 if (!rc.message.starts_with(message))
149 throw failure("bad error message for case " + name + ", got: " + rc.message);
150 };
151 auto error_case = [&error_code_case](std::vector<const char*> args, const char* message, const std::string& name) {
152 error_code_case(args, message, ot::st::bad_arguments, name);
153 };
154 error_case({"opustags"}, "No arguments specified. Use -h for help.", "no arguments");
155 error_case({"opustags", "-a", "X"}, "Comment does not contain an equal sign: X.", "bad comment for -a");
156 error_case({"opustags", "--set", "X"}, "Comment does not contain an equal sign: X.", "bad comment for --set");
157 error_case({"opustags", "-a"}, "Missing value for option '-a'.", "short option with missing value");
158 error_case({"opustags", "-x"}, "Unrecognized option '-x'.", "unrecognized short option");
159 error_case({"opustags", "--derp"}, "Unrecognized option '--derp'.", "unrecognized long option");
160 error_case({"opustags", "-x=y"}, "Unrecognized option '-x'.", "unrecognized short option with value");
161 error_case({"opustags", "--derp=y"}, "Unrecognized option '--derp=y'.", "unrecognized long option with value");
162 error_case({"opustags", "-aX=Y"}, "Exactly one input file must be specified.", "no input file");
163 error_case({"opustags", "-i", "-o", "/dev/null", "-"}, "Cannot combine --in-place and --output.", "in-place + output");
164 error_case({"opustags", "-S", "-"}, "Cannot use standard input more than once.", "set all and read opus from stdin");
165 error_case({"opustags", "-i", "-"}, "Cannot modify standard input in place.", "write stdin in-place");
166 error_case({"opustags", "-o", "x", "--output", "y", "z"},
167 "Cannot specify --output more than once.", "double output");
168 error_code_case({"opustags", "-S", "x"}, "Malformed tag: INVALID", ot::st::error, "attempt to read invalid argument with -S");
169 error_case({"opustags", "-o", "", "--output", "y", "z"},
170 "Cannot specify --output more than once.", "double output with first filename empty");
171 error_case({"opustags", "-e", "-i", "x", "y"},
172 "Exactly one input file must be specified.", "editing interactively two files at once");
173 error_case({"opustags", "--edit", "-", "-o", "x"},
174 "Cannot edit interactively when standard input or standard output are already used.",
175 "editing interactively from stdandard intput");
176 error_case({"opustags", "--edit", "x", "-o", "-"},
177 "Cannot edit interactively when standard input or standard output are already used.",
178 "editing interactively to stdandard output");
179 error_case({"opustags", "--edit", "x"}, "Cannot edit interactively when no output is specified.", "editing without output");
180 error_case({"opustags", "--edit", "x", "-i", "-a", "X=Y"}, "Cannot mix --edit with -adDsS.", "mixing -e and -a");
181 error_case({"opustags", "--edit", "x", "-i", "-d", "X"}, "Cannot mix --edit with -adDsS.", "mixing -e and -d");
182 error_case({"opustags", "--edit", "x", "-i", "-D"}, "Cannot mix --edit with -adDsS.", "mixing -e and -D");
183 error_case({"opustags", "--edit", "x", "-i", "-S"}, "Cannot mix --edit with -adDsS.", "mixing -e and -S");
184 error_case({"opustags", "--output-cover", "x", "--output-cover", "y"},
185 "Cannot specify --output-cover more than once.", "multiple --output-cover");
186 error_case({"opustags", "x", "-o", "-", "--output-cover", "-"},
187 "Cannot specify standard output for both --output and --output-cover.", "-o and --output-cover conflict");
188 error_case({"opustags", "-i", "x", "y", "--output-cover", "z"},
189 "Cannot use --output-cover with multiple input files.", "--output-cover with multiple input");
190 error_case({"opustags", "-i", "--vendor", "x"},
191 "--vendor is only supported in read-only mode.", "--vendor when editing");
192 error_case({"opustags", "-d", "\xFF", "x"},
193 "Could not encode argument into UTF-8:",
194 "-d with binary data");
195 error_case({"opustags", "-a", "X=\xFF", "x"},
196 "Could not encode argument into UTF-8:",
197 "-a with binary data");
198 error_case({"opustags", "-s", "X=\xFF", "x"},
199 "Could not encode argument into UTF-8:",
200 "-s with binary data");
201 }
202
203 static void check_delete_comments()
204 {
205 using C = std::list<std::u8string>;
206 C original = {u8"TITLE=X", u8"Title=Y", u8"Title=Z", u8"ARTIST=A", u8"artIst=B"};
207
208 C edited = original;
209 ot::delete_comments(edited, u8"derp");
210 if (!std::equal(edited.begin(), edited.end(), original.begin(), original.end()))
211 throw failure("should not have deleted anything");
212
213 ot::delete_comments(edited, u8"Title");
214 C expected = {u8"ARTIST=A", u8"artIst=B"};
215 if (!std::equal(edited.begin(), edited.end(), expected.begin(), expected.end()))
216 throw failure("did not delete all titles correctly");
217
218 edited = original;
219 ot::delete_comments(edited, u8"titlE=Y");
220 ot::delete_comments(edited, u8"Title=z");
221 expected = {u8"TITLE=X", u8"Title=Z", u8"ARTIST=A", u8"artIst=B"};
222 if (!std::equal(edited.begin(), edited.end(), expected.begin(), expected.end()))
223 throw failure("did not delete a specific title correctly");
224 }
225
226 int main(int argc, char **argv)
227 {
228 std::cout << "1..4\n";
229 run(check_read_comments, "check tags parsing");
230 run(check_good_arguments, "check options parsing");
231 run(check_bad_arguments, "check options parsing errors");
232 run(check_delete_comments, "delete comments");
233 return 0;
234 }