- Change header guard from `__SMCC_LEX_PARSER_H__` to `__SCC_LEX_PARSER_H__` - Prefix all lexer functions with `scc_` (e.g., `lex_parse_char` → `scc_lex_parse_char`) - Add new helper function `scc_lex_parse_is_identifier_header` - Update references in source and test files to use new function names - Replace `core_stream_eof` with `scc_stream_eof` for consistency
62 lines
2.4 KiB
C
62 lines
2.4 KiB
C
// test_string.c
|
|
#include <lex_parser.h>
|
|
#include <utest/acutest.h>
|
|
|
|
cbool check_string(const char *str, const char *expect, scc_cstring_t *output) {
|
|
log_set_level(&__default_logger_root, 0);
|
|
scc_pos_t pos = scc_pos_init();
|
|
scc_mem_probe_stream_t mem_stream;
|
|
scc_probe_stream_t *stream =
|
|
scc_mem_probe_stream_init(&mem_stream, str, scc_strlen(str), false);
|
|
|
|
cbool ret = scc_lex_parse_string(stream, &pos, output);
|
|
if (ret && expect) {
|
|
return strcmp(output->data, expect) == 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
#define CHECK_STRING_VALID(str, expect) \
|
|
do { \
|
|
scc_cstring_t _output = scc_cstring_new(); \
|
|
cbool ret = check_string(str, expect, &_output); \
|
|
TEST_CHECK(ret == true); \
|
|
TEST_CHECK(strcmp(_output.data, expect) == 0); \
|
|
scc_cstring_free(&_output); \
|
|
} while (0)
|
|
|
|
#define CHECK_STRING_INVALID(str) \
|
|
do { \
|
|
scc_cstring_t _output = scc_cstring_new(); \
|
|
cbool ret = check_string(str, NULL, &_output); \
|
|
TEST_CHECK(ret == false); \
|
|
scc_cstring_free(&_output); \
|
|
} while (0)
|
|
|
|
void test_simple_string(void) {
|
|
TEST_CASE("simple strings");
|
|
CHECK_STRING_VALID("\"\"", "");
|
|
CHECK_STRING_VALID("\"hello\"", "hello");
|
|
CHECK_STRING_VALID("\"hello world\"", "hello world");
|
|
}
|
|
|
|
void test_escape_string(void) {
|
|
TEST_CASE("escape strings");
|
|
CHECK_STRING_VALID("\"\\n\"", "\n");
|
|
CHECK_STRING_VALID("\"\\t\"", "\t");
|
|
CHECK_STRING_VALID("\"\\\"\"", "\"");
|
|
CHECK_STRING_VALID("\"Hello\\nWorld\"", "Hello\nWorld");
|
|
}
|
|
|
|
void test_invalid_string(void) {
|
|
TEST_CASE("invalid strings");
|
|
CHECK_STRING_INVALID("\"unterminated");
|
|
CHECK_STRING_INVALID("\"newline\n\"");
|
|
}
|
|
|
|
TEST_LIST = {
|
|
{"test_simple_string", test_simple_string},
|
|
{"test_escape_string", test_escape_string},
|
|
{"test_invalid_string", test_invalid_string},
|
|
{NULL, NULL},
|
|
}; |