Skip to content

Commit

Permalink
c2: add some unittests
Browse files Browse the repository at this point in the history
Signed-off-by: Yuxuan Shui <[email protected]>
  • Loading branch information
yshui committed Feb 14, 2024
1 parent 83f9b2e commit 7d848b7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/c2.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "list.h"
#include "log.h"
#include "string_utils.h"
#include "test.h"
#include "utils.h"
#include "win.h"
#include "x.h"
Expand Down Expand Up @@ -328,6 +329,8 @@ static int c2_parse_legacy(const char *pattern, int offset, c2_ptr_t *presult);

static void c2_free(c2_ptr_t p);

static size_t c2_condition_to_str(c2_ptr_t p, char *output, size_t len);

/**
* Wrapper of c2_free().
*/
Expand Down Expand Up @@ -391,6 +394,35 @@ c2_lptr_t *c2_parse(c2_lptr_t **pcondlst, const char *pattern, void *data) {
}
}

TEST_CASE(c2_parse) {
c2_lptr_t *cond = c2_parse(NULL, "name = \"xterm\"", NULL);
char str[1024];
TEST_TRUE(cond != NULL);
TEST_TRUE(cond->ptr.l != NULL);
TEST_EQUAL(cond->ptr.l->op, C2_L_OEQ);
TEST_EQUAL(cond->ptr.l->ptntype, C2_L_PTSTRING);
TEST_STREQUAL(cond->ptr.l->ptnstr, "xterm");

size_t len = c2_condition_to_str(cond->ptr, str, sizeof(str));
TEST_STREQUAL3(str, "name:0s = \"xterm\"", len);
c2_list_free(&cond, NULL);

cond = c2_parse(NULL, "_GTK_FRAME_EXTENTS@:c", NULL);
TEST_TRUE(cond != NULL);
TEST_TRUE(cond->ptr.l != NULL);
TEST_EQUAL(cond->ptr.l->op, C2_L_OEXISTS);
TEST_TRUE(cond->ptr.l->tgt_onframe);
TEST_TRUE(cond->ptr.l->tgt != NULL);
TEST_STREQUAL(cond->ptr.l->tgt, "_GTK_FRAME_EXTENTS");

#if 0
// This currently fails
len = c2_condition_to_str(cond->ptr, str, sizeof(str));
TEST_STREQUAL3(str, "_GTK_FRAME_EXTENTS@:c", len);
#endif
c2_list_free(&cond, NULL);
}

#define c2_error(format, ...) \
do { \
log_error("Pattern \"%s\" pos %d: " format, pattern, offset, ##__VA_ARGS__); \
Expand Down

0 comments on commit 7d848b7

Please sign in to comment.