Repository navigation
Expand file tree
/
Copy pathcppsh_test.cpp
More file actions
478 lines (411 loc) · 17 KB
/
Copy pathcppsh_test.cpp
File metadata and controls
478 lines (411 loc) · 17 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#include <iostream>
#include <string>
#include "cppsh_core.hpp"
// Minimal zero-dependency testing framework
int tests_run = 0;
int tests_failed = 0;
#define EXPECT_EQ(expected, actual) \
do { \
tests_run++; \
if ((expected) != (actual)) { \
std::cerr << "FAIL: " << __FILE__ << ":" << __LINE__ \
<< "\n Expected: " << (expected) \
<< "\n Actual: " << (actual) << std::endl; \
tests_failed++; \
} \
} while (0)
#define TEST(name) void name()
// ---------------------------------------------------------
// TESTS
// ---------------------------------------------------------
TEST(test_script_wrapping) {
std::string input = "std::println(\"Hello World\");";
// The expected boilerplate injection
std::string expected_body =
"int main(int argc, char* argv[]) {\n"
" std::signal(SIGSEGV, _cppsh_signal_handler);\n"
" std::signal(SIGABRT, _cppsh_signal_handler);\n"
" std::vector<std::string> args(argv, argv + argc);\n"
"std::println(\"Hello World\");\n"
" return 0;\n"
"}\n";
std::string wrapped = cppsh::wrap_script(input);
if (wrapped.find(expected_body) == std::string::npos) {
EXPECT_EQ(expected_body, "Body not found in wrapped script.");
} else {
EXPECT_EQ(true, true);
}
}
TEST(test_file_reading) {
// RED Phase: We expect a cppsh::read_file function to exist
std::string test_filename = "test_dummy.cppsh";
std::string expected_content = "std::println(\"I am a file\");";
// Create dummy file
FILE* f = fopen(test_filename.c_str(), "w");
if (f) {
fputs(expected_content.c_str(), f);
fclose(f);
}
// Attempt to read it
std::string actual_content = cppsh::read_file(test_filename);
EXPECT_EQ(expected_content, actual_content);
// Cleanup
remove(test_filename.c_str());
}
TEST(test_trim) {
EXPECT_EQ("hello", cppsh::trim(" hello "));
EXPECT_EQ("hello", cppsh::trim("hello "));
EXPECT_EQ("hello", cppsh::trim(" hello"));
EXPECT_EQ("hello", cppsh::trim("hello"));
EXPECT_EQ("", cppsh::trim(" "));
EXPECT_EQ("", cppsh::trim(""));
EXPECT_EQ("hello world", cppsh::trim(" \t\n hello world \r\n "));
}
TEST(test_magic_comments) {
std::string script =
"// cppsh_compiler: clang++-22\n"
"// cppsh_std: c++20\n"
"// import ssl\n"
"// import crypto\n"
"// cppsh_debugger: lldb\n"
"int main() { return 0; }\n";
cppsh::ScriptMetadata meta = cppsh::extract_metadata(script);
EXPECT_EQ("clang++-22", meta.compiler);
EXPECT_EQ("c++20", meta.std_version);
EXPECT_EQ("lldb", meta.debugger);
EXPECT_EQ(2, meta.imports.size());
if (meta.imports.size() == 2) {
EXPECT_EQ("ssl", meta.imports[0]);
EXPECT_EQ("crypto", meta.imports[1]);
}
}
TEST(test_include_hoisting) {
std::string input =
"#!/usr/bin/env cppsh\n"
"#include <iostream>\n"
"// some comment\n"
"#include <cmath>\n"
"std::cout << \"Hello\";\n";
std::string expected_body =
"int main(int argc, char* argv[]) {\n"
" std::signal(SIGSEGV, _cppsh_signal_handler);\n"
" std::signal(SIGABRT, _cppsh_signal_handler);\n"
" std::vector<std::string> args(argv, argv + argc);\n"
"// some comment\n"
"std::cout << \"Hello\";\n"
" return 0;\n"
"}\n";
// Use emit_line_directives=false so #line injections don't interfere with the
// body substring check. Line directive correctness is tested in test_native_debugging.
std::string wrapped = cppsh::wrap_script(input, "script.cppsh", false);
bool has_includes = wrapped.find("#include <iostream>") != std::string::npos &&
wrapped.find("#include <cmath>") != std::string::npos;
if (wrapped.find(expected_body) == std::string::npos || !has_includes) {
EXPECT_EQ(expected_body, "Body or hoisted includes not found.");
} else {
EXPECT_EQ(true, true);
}
}
TEST(test_modern_prelude) {
std::string input = "str name = \"cppsh\";\nvec<u64> nums = {1, 2, 3};\n";
// We expect the prelude to be injected right after standard includes
std::string expected_body =
"int main(int argc, char* argv[]) {\n"
" std::signal(SIGSEGV, _cppsh_signal_handler);\n"
" std::signal(SIGABRT, _cppsh_signal_handler);\n"
" std::vector<std::string> args(argv, argv + argc);\n"
"str name = \"cppsh\";\n"
"vec<u64> nums = {1, 2, 3};\n"
" return 0;\n"
"}\n";
std::string wrapped = cppsh::wrap_script(input);
bool has_prelude = wrapped.find("using str = std::string;") != std::string::npos &&
wrapped.find("using u64 = uint64_t;") != std::string::npos;
if (wrapped.find(expected_body) == std::string::npos || !has_prelude) {
EXPECT_EQ(expected_body, "Body or prelude not found.");
} else {
EXPECT_EQ(true, true);
}
}
TEST(test_shell_transpilation) {
// We expect the state-aware scanner to ignore backticks in comments and strings
std::string input =
"// `ignored`\n"
"str a = \"`ignored`\";\n"
"`echo hello`;\n"
"`ls -la {dir} {file}`;\n";
std::string expected_body =
"// `ignored`\n"
"str a = \"`ignored`\";\n"
"_cppsh_sh(std::format(R\"cppsh_delim(echo hello 2>&1)cppsh_delim\"));\n"
"_cppsh_sh(std::format(R\"cppsh_delim(ls -la {} {} 2>&1)cppsh_delim\", _sh_escape(dir), _sh_escape(file)));\n";
std::string wrapped = cppsh::wrap_script(input);
// Check if the wrapped script contains the expected body
if (wrapped.find(expected_body) == std::string::npos) {
EXPECT_EQ(expected_body, "Body not found in wrapped script.");
} else {
EXPECT_EQ(true, true); // Success
}
}
TEST(test_native_debugging) {
std::string input =
"#!/usr/bin/env cppsh\n"
"str x = \"hello\";\n"
"#include <iostream>\n"
"std::cout << x;\n";
std::string wrapped = cppsh::wrap_script(input, "my_script.cppsh");
// Line 1 is skipped.
// Line 2 is normal, so we expect #line 2
// Line 3 is hoisted.
// Line 4 is normal, so we expect #line 4
EXPECT_EQ(true, wrapped.find("#line 2 \"my_script.cppsh\"") != std::string::npos);
EXPECT_EQ(true, wrapped.find("#line 4 \"my_script.cppsh\"") != std::string::npos);
}
TEST(test_crash_dumps) {
std::string wrapped = cppsh::wrap_script("");
EXPECT_EQ(true, wrapped.find("#include <stacktrace>") != std::string::npos);
EXPECT_EQ(true, wrapped.find("#include <csignal>") != std::string::npos);
EXPECT_EQ(true, wrapped.find("std::signal(SIGSEGV, _cppsh_signal_handler)") != std::string::npos);
EXPECT_EQ(true, wrapped.find("void _cppsh_signal_handler") != std::string::npos);
}
TEST(test_optional_headers) {
std::string wrapped = cppsh::wrap_script("");
// <print> and <stacktrace> must be guarded with __has_include so cppsh
// compiles on any C++23 compiler regardless of optional header availability.
EXPECT_EQ(true, wrapped.find("#if __has_include(<stacktrace>)") != std::string::npos);
EXPECT_EQ(true, wrapped.find("#if __has_include(<print>)") != std::string::npos);
// The bare unconditional includes must NOT appear
EXPECT_EQ(true, wrapped.find("\n#include <stacktrace>\n") == std::string::npos ||
wrapped.find("#if __has_include(<stacktrace>)") != std::string::npos);
}
TEST(test_script_with_own_main) {
// A script that already defines int main() must not be double-wrapped.
std::string input =
"#!/usr/bin/env cppsh\n"
"#include <iostream>\n"
"int main() {\n"
" std::cout << \"hello\";\n"
" return 0;\n"
"}\n";
std::string wrapped = cppsh::wrap_script(input);
// Should contain exactly one 'int main(' — the user's own
size_t first = wrapped.find("int main(");
size_t second = (first != std::string::npos) ? wrapped.find("int main(", first + 1) : std::string::npos;
EXPECT_EQ(true, first != std::string::npos); // must have user's main
EXPECT_EQ(true, second == std::string::npos); // must NOT have a second injected main
}
// ---------------------------------------------------------
// EDGE CASES & MISUSES
// ---------------------------------------------------------
TEST(test_edge_script_wrapping) {
// Misuse: Completely empty script
std::string wrapped = cppsh::wrap_script("");
EXPECT_EQ(true, wrapped.find("int main") != std::string::npos);
}
TEST(test_edge_file_reading) {
// Misuse: Reading a file that doesn't exist
bool threw = false;
try {
cppsh::read_file("this_file_definitely_does_not_exist.cppsh");
} catch (const std::runtime_error&) {
threw = true;
}
EXPECT_EQ(true, threw);
}
TEST(test_edge_trim) {
// Edge case: string with only whitespace
EXPECT_EQ("", cppsh::trim(" \t\n \r "));
}
TEST(test_edge_magic_comments) {
// Misuse: Missing values or malformed magic comments
std::string script =
"// cppsh_compiler: \n"
"// cppsh_std:\n"
"// import\n"; // Missing space after import
cppsh::ScriptMetadata meta = cppsh::extract_metadata(script);
EXPECT_EQ("", meta.compiler);
EXPECT_EQ("", meta.std_version);
EXPECT_EQ(0, meta.imports.size());
}
TEST(test_edge_include_hoisting) {
// Edge case: #include with leading whitespace
std::string input = " #include <map>";
std::string wrapped = cppsh::wrap_script(input);
// It should successfully hoist it, preserving the line
EXPECT_EQ(true, wrapped.find(" #include <map>") != std::string::npos);
}
TEST(test_edge_shell_transpilation) {
// Misuse: Unmatched backtick
// Currently, our scanner swallows the unterminated command block.
std::string input = "str a = `echo missing close;\n";
std::string wrapped = cppsh::wrap_script(input);
// It should fail to inject _cppsh_sh because the backtick never closed
EXPECT_EQ(true, wrapped.find("_cppsh_sh(std::format") == std::string::npos);
}
TEST(test_cli_flag_parsing) {
std::vector<std::string> args1 = {"--debug", "script.cppsh", "arg1"};
cppsh::CliOptions opt1 = cppsh::parse_cli_args(args1);
EXPECT_EQ(true, opt1.debug);
EXPECT_EQ(false, opt1.build);
EXPECT_EQ("script.cppsh", opt1.script_path);
EXPECT_EQ(1, opt1.script_args.size());
if (!opt1.script_args.empty()) EXPECT_EQ("arg1", opt1.script_args[0]);
std::vector<std::string> args2 = {"--build", "main.cppsh"};
cppsh::CliOptions opt2 = cppsh::parse_cli_args(args2);
EXPECT_EQ(true, opt2.build);
EXPECT_EQ("main.cppsh", opt2.script_path);
std::vector<std::string> args3 = {"--export", "test.cppsh"};
cppsh::CliOptions opt3 = cppsh::parse_cli_args(args3);
EXPECT_EQ(true, opt3.export_cpp);
std::vector<std::string> args4 = {"--watch", "server.cppsh"};
cppsh::CliOptions opt4 = cppsh::parse_cli_args(args4);
EXPECT_EQ(true, opt4.watch);
std::vector<std::string> args5 = {};
cppsh::CliOptions opt5 = cppsh::parse_cli_args(args5);
EXPECT_EQ(true, opt5.repl);
}
TEST(test_env_config) {
cppsh::EnvConfig cfg = cppsh::get_env_config();
EXPECT_EQ(true, cfg.cxx != "");
EXPECT_EQ(true, cfg.debugger != "");
}
TEST(test_edge_cli_flag_parsing) {
// Misuse: Unknown flag
bool threw1 = false;
try {
cppsh::parse_cli_args({"--unknown", "script.cppsh"});
} catch (const std::runtime_error&) { threw1 = true; }
EXPECT_EQ(true, threw1);
// Misuse: Missing script path after flag
bool threw2 = false;
try {
cppsh::parse_cli_args({"--debug"});
} catch (const std::runtime_error&) { threw2 = true; }
EXPECT_EQ(true, threw2);
// Misuse: Conflicting flags
bool threw3 = false;
try {
cppsh::parse_cli_args({"--build", "--export", "script.cppsh"});
} catch (const std::runtime_error&) { threw3 = true; }
EXPECT_EQ(true, threw3);
}
TEST(test_export_mode_wrapping) {
std::string input =
"#!/usr/bin/env cppsh\n"
"str x = \"hello\";\n"
"#include <iostream>\n"
"std::cout << x;\n";
// wrap_script with emit_line_directives = false
std::string wrapped = cppsh::wrap_script(input, "my_script.cppsh", false);
EXPECT_EQ(true, wrapped.find("#line") == std::string::npos);
EXPECT_EQ(true, wrapped.find("std::cout << x;") != std::string::npos);
}
TEST(test_build_export_paths) {
EXPECT_EQ("./data_pipeline", cppsh::get_build_output_path("data_pipeline.cppsh"));
EXPECT_EQ("./run", cppsh::get_build_output_path("/var/tmp/run.cppsh"));
EXPECT_EQ("./my_script.cpp", cppsh::get_export_output_path("my_script.cppsh"));
EXPECT_EQ("./test.cpp", cppsh::get_export_output_path("/a/b/c/test.cppsh"));
}
TEST(test_edge_build_export_paths) {
// Edge case: script with no extension
EXPECT_EQ("./script_bin", cppsh::get_build_output_path("script"));
EXPECT_EQ("./script.cpp", cppsh::get_export_output_path("script"));
// Edge case: empty path
EXPECT_EQ("./a.out", cppsh::get_build_output_path(""));
EXPECT_EQ("./script.cpp", cppsh::get_export_output_path(""));
}
TEST(test_repl_is_expression) {
EXPECT_EQ(true, cppsh::is_expression("name"));
EXPECT_EQ(true, cppsh::is_expression("1 + 2"));
EXPECT_EQ(false, cppsh::is_expression("auto x = 5;"));
EXPECT_EQ(false, cppsh::is_expression("str s = \"hello\";"));
EXPECT_EQ(false, cppsh::is_expression("if (true) {}"));
}
TEST(test_repl_state_processing) {
cppsh::ReplState state;
std::string code1 = cppsh::process_repl_input(state, "auto x = 10;");
EXPECT_EQ(1, state.history.size());
EXPECT_EQ(false, state.last_was_expr);
EXPECT_EQ(true, code1.find("auto x = 10;") != std::string::npos);
std::string code2 = cppsh::process_repl_input(state, "x");
EXPECT_EQ(1, state.history.size()); // Temporary expression is not stored in history!
EXPECT_EQ(true, state.last_was_expr);
EXPECT_EQ(true, code2.find("std::println(\"{}\", x);") != std::string::npos);
std::string code3 = cppsh::process_repl_input(state, "x + 5");
EXPECT_EQ(1, state.history.size());
EXPECT_EQ(true, state.last_was_expr);
EXPECT_EQ(true, code3.find("std::println(\"{}\", x + 5);") != std::string::npos);
EXPECT_EQ(true, code3.find("std::println(\"{}\", x);") == std::string::npos); // Previous expression omitted!
}
TEST(test_fnv1a_hashing) {
uint64_t h1 = cppsh::compute_fnv1a_hash("hello world");
uint64_t h2 = cppsh::compute_fnv1a_hash("hello world");
uint64_t h3 = cppsh::compute_fnv1a_hash("hello world!");
EXPECT_EQ(h1, h2);
EXPECT_EQ(true, h1 != h3);
}
TEST(test_caching_paths) {
std::string cache_dir = cppsh::get_cache_dir();
EXPECT_EQ(true, !cache_dir.empty());
std::string bin_path = cppsh::get_cached_binary_path("int main() {}", "-O3");
EXPECT_EQ(true, bin_path.find(cache_dir) != std::string::npos);
EXPECT_EQ(true, bin_path.find(".out") != std::string::npos);
}
TEST(test_check_stacktrace_support) {
// Probing a nonexistent compiler should return -1
int mode = cppsh::check_stacktrace_support("nonexistent_compiler_12345");
EXPECT_EQ(-1, mode);
}
TEST(test_repl_readline_simulation) {
// Test the input_history population during REPL processing
cppsh::ReplState state;
cppsh::process_repl_input(state, "auto a = 1;");
cppsh::process_repl_input(state, "a + 2");
EXPECT_EQ(2, state.input_history.size());
if (state.input_history.size() == 2) {
EXPECT_EQ("auto a = 1;", state.input_history[0]);
EXPECT_EQ("a + 2", state.input_history[1]);
}
}
int main() {
std::cout << "Running cppsh tests..." << std::endl;
// Happy Paths
test_script_wrapping();
test_file_reading();
test_trim();
test_magic_comments();
test_include_hoisting();
test_modern_prelude();
test_shell_transpilation();
test_native_debugging();
test_crash_dumps();
test_cli_flag_parsing();
test_env_config();
test_export_mode_wrapping();
test_build_export_paths();
test_repl_is_expression();
test_repl_state_processing();
test_fnv1a_hashing();
test_caching_paths();
test_optional_headers();
test_script_with_own_main();
test_check_stacktrace_support();
test_repl_readline_simulation();
// Edge Cases
test_edge_script_wrapping();
test_edge_file_reading();
test_edge_trim();
test_edge_magic_comments();
test_edge_include_hoisting();
test_edge_shell_transpilation();
test_edge_cli_flag_parsing();
test_edge_build_export_paths();
std::cout << "---------------------------------" << std::endl;
std::cout << "Tests run: " << tests_run << std::endl;
if (tests_failed > 0) {
std::cerr << "Tests failed: " << tests_failed << std::endl;
return 1;
}
std::cout << "All tests passed!" << std::endl;
return 0;
}