From f23467407ca6272901ed39325798b8f4ca179295 Mon Sep 17 00:00:00 2001 From: Boris Faure Date: Thu, 28 May 2020 22:44:45 +0200 Subject: [PATCH] termiolink: add unit test on parsing edc color --- src/bin/sb.h | 1 - src/bin/termiolink.c | 60 +++++++++++++++++++++++++++++++++++++++++++- src/bin/tytest.c | 1 + src/bin/unit_tests.h | 1 + src/bin/utils.h | 2 ++ 5 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/bin/sb.h b/src/bin/sb.h index f1851044..1df238b5 100644 --- a/src/bin/sb.h +++ b/src/bin/sb.h @@ -24,5 +24,4 @@ void ty_sb_free(struct ty_sb *sb); #define sbstartswith(SB, ConstRef) \ (((SB)->len >= sizeof(ConstRef) -1) \ && (!strncmp((SB)->buf, ConstRef, sizeof(ConstRef) - 1))) - #endif diff --git a/src/bin/termiolink.c b/src/bin/termiolink.c index 12004e01..0531f3b9 100644 --- a/src/bin/termiolink.c +++ b/src/bin/termiolink.c @@ -811,10 +811,18 @@ _parse_edc_color(struct ty_sb *sb, { uint8_t r = 0, g = 0, b = 0, a = 255; + ty_sb_spaces_ltrim(sb); /* skip color */ + if (sb->len <= 6) + return EINA_FALSE; + if (strncmp(sb->buf, "color", 5) != 0) + return EINA_FALSE; ty_sb_lskip(sb, 5); - if (sb->buf[0] != ':') + if (sb->buf[0] == '2' || sb->buf[0] == '3') ty_sb_lskip(sb, 1); + ty_sb_spaces_ltrim(sb); + if (sb->buf[0] != ':') + return EINA_FALSE; ty_sb_lskip(sb, 1); /* skip ':' */ ty_sb_spaces_ltrim(sb); @@ -1268,6 +1276,12 @@ tytest_color_parse_uint8(void) assert(v == 234); ty_sb_free(&sb); + /* ending with semicolon */ + assert(TY_SB_ADD(&sb, "42;") == 0); + assert(_parse_uint8(&sb, &v) == EINA_TRUE); + assert(v == 42); + ty_sb_free(&sb); + /* 0 padded */ assert(TY_SB_ADD(&sb, "012") == 0); assert(_parse_uint8(&sb, &v) == EINA_TRUE); @@ -1296,4 +1310,48 @@ tytest_color_parse_uint8(void) return 0; } + +int +tytest_color_parse_edc(void) +{ + struct ty_sb sb = {}; + uint8_t r = 0, g = 0, b = 0, a = 0; + + /* color */ + assert(TY_SB_ADD(&sb, " color: 51 153 255 32;") == 0); + assert(_parse_edc_color(&sb, &r, &g, &b, &a) == EINA_TRUE); + assert(r == 51 && g == 153 && b == 255 && a == 32); + ty_sb_free(&sb); + + /* color2 */ + assert(TY_SB_ADD(&sb, " color2: 244 99 93 127;") == 0); + assert(_parse_edc_color(&sb, &r, &g, &b, &a) == EINA_TRUE); + assert(r == 244 && g == 99 && b == 93 && a == 127); + ty_sb_free(&sb); + + /* color3 */ + assert(TY_SB_ADD(&sb, " color3: 149 181 16 234;") == 0); + assert(_parse_edc_color(&sb, &r, &g, &b, &a) == EINA_TRUE); + assert(r == 149 && g == 181 && b == 16 && a == 234); + ty_sb_free(&sb); + + /* color: #color */ + assert(TY_SB_ADD(&sb, " color: #3399ff20") == 0); + assert(_parse_edc_color(&sb, &r, &g, &b, &a) == EINA_TRUE); + assert(r == 51 && g == 153 && b == 255 && a == 32); + ty_sb_free(&sb); + + /* with tabs */ + assert(TY_SB_ADD(&sb, " color\t:\t244\t99\t93\t127\t;") == 0); + assert(_parse_edc_color(&sb, &r, &g, &b, &a) == EINA_TRUE); + assert(r == 244 && g == 99 && b == 93 && a == 127); + ty_sb_free(&sb); + + /* invalid */ + assert(TY_SB_ADD(&sb, " color: COL;") == 0); + assert(_parse_edc_color(&sb, &r, &g, &b, &a) == EINA_FALSE); + ty_sb_free(&sb); + + return 0; +} #endif diff --git a/src/bin/tytest.c b/src/bin/tytest.c index 946b1234..ebe02b89 100644 --- a/src/bin/tytest.c +++ b/src/bin/tytest.c @@ -37,6 +37,7 @@ static struct { { "color_parse_2hex", tytest_color_parse_2hex}, { "color_parse_sharp", tytest_color_parse_sharp}, { "color_parse_uint8", tytest_color_parse_uint8}, + { "color_parse_edc", tytest_color_parse_edc}, { NULL, NULL}, }; diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h index a9cc4ba4..f48e5d50 100644 --- a/src/bin/unit_tests.h +++ b/src/bin/unit_tests.h @@ -14,5 +14,6 @@ int tytest_color_parse_hex(void); int tytest_color_parse_2hex(void); int tytest_color_parse_sharp(void); int tytest_color_parse_uint8(void); +int tytest_color_parse_edc(void); #endif diff --git a/src/bin/utils.h b/src/bin/utils.h index 864db508..cbad8ece 100644 --- a/src/bin/utils.h +++ b/src/bin/utils.h @@ -16,6 +16,8 @@ Eina_Bool utils_need_scale_wizard(void); #define casestartswith(str, constref) \ (!strncasecmp(str, constref, sizeof(constref) - 1)) +#define startswith(str, constref) \ + (!strncmp(str, constref, sizeof(constref) - 1)) #if !defined(HAVE_STRCHRNUL) static inline char *