Files
scc/libs/lex_parser/tests/test_identifier.c
zzy d88fa3b8d3 feat: rename core types to scc prefix for consistency
Updated type names from `core_*` to `scc_*` across lex_parser and stream modules to maintain naming consistency within the SCC codebase. This includes changes to function signatures and internal usage of types like `core_probe_stream_t`, `core_pos_t`, and `cstring_t` to their `scc_*` counterparts.
2025-12-11 13:00:29 +08:00

56 lines
2.2 KiB
C

// test_identifier.c
#include <lex_parser.h>
#include <utest/acutest.h>
cbool check_identifier(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 = lex_parse_identifier(stream, &pos, output);
if (ret && expect) {
return strcmp(output->data, expect) == 0;
}
return ret;
}
#define CHECK_IDENTIFIER_VALID(str, expect) \
do { \
scc_cstring_t _output = scc_cstring_new(); \
cbool ret = check_identifier(str, expect, &_output); \
TEST_CHECK(ret == true); \
TEST_CHECK(strcmp(_output.data, expect) == 0); \
scc_cstring_free(&_output); \
} while (0)
#define CHECK_IDENTIFIER_INVALID(str) \
do { \
scc_cstring_t _output = scc_cstring_new(); \
cbool ret = check_identifier(str, NULL, &_output); \
TEST_CHECK(ret == false); \
scc_cstring_free(&_output); \
} while (0)
void test_valid_identifier(void) {
TEST_CASE("valid identifiers");
CHECK_IDENTIFIER_VALID("variable", "variable");
CHECK_IDENTIFIER_VALID("my_var", "my_var");
CHECK_IDENTIFIER_VALID("_private", "_private");
CHECK_IDENTIFIER_VALID("Var123", "Var123");
CHECK_IDENTIFIER_VALID("a", "a");
}
void test_invalid_identifier(void) {
TEST_CASE("invalid identifiers");
CHECK_IDENTIFIER_INVALID("");
CHECK_IDENTIFIER_INVALID("123var");
}
TEST_LIST = {
{"test_valid_identifier", test_valid_identifier},
{"test_invalid_identifier", test_invalid_identifier},
{NULL, NULL},
};