Skip to content

Commit 5fa2cc3

Browse files
committed
Add gradient parser test
1 parent e18f382 commit 5fa2cc3

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

src/draw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void gradient_pattern(struct gradient *grad)
115115
}
116116
}
117117

118-
char *gradient_to_string(struct gradient *grad)
118+
char *gradient_to_string(const struct gradient *grad)
119119
{
120120
if (!GRADIENT_VALID(grad)) return NULL;
121121

src/draw.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void gradient_free(struct gradient *grad);
6767

6868
void gradient_pattern(struct gradient *grad);
6969

70-
char *gradient_to_string(struct gradient *grad);
70+
char *gradient_to_string(const struct gradient *grad);
7171

7272

7373
void draw_setup(void);

test/option_parser.c

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ TEST test_string_to_color_invalid(void)
696696
struct setting s;
697697
s.type = TYPE_COLOR;
698698
s.value = &val;
699-
s.name = "test_color";
699+
s.name = "test_color_invalid";
700700

701701
const char* inputs[] = {
702702
"",
@@ -727,6 +727,69 @@ TEST test_string_to_color_invalid(void)
727727
PASS();
728728
}
729729

730+
TEST test_string_to_gradient(void)
731+
{
732+
struct gradient *grad = NULL;
733+
struct setting s;
734+
s.type = TYPE_GRADIENT;
735+
s.value = &grad;
736+
s.name = "test_gradient";
737+
738+
const char* inputs[] = {
739+
"#123456",
740+
"#ab123c",
741+
"#abc, #ebf, #aaafff",
742+
"#abc123, #acaf8f",
743+
};
744+
745+
// NOTE: Flexible array shenanigans
746+
struct gradient *results[] = {
747+
gradient_alloc(1),
748+
gradient_alloc(1),
749+
gradient_alloc(3),
750+
gradient_alloc(2),
751+
};
752+
753+
results[0]->colors[0] = (struct color) { (double)0x12 / 0xff, (double)0x34 / 0xff, (double)0x56 / 0xff, 1.0};
754+
755+
results[1]->colors[0] = (struct color) { (double)0xab / 0xff, (double)0x12 / 0xff, (double)0x3c / 0xff, 1.0};
756+
757+
results[2]->colors[0] = (struct color) { (double)0xaa / 0xff, (double)0xbb / 0xff, (double)0xcc / 0xff, 1.0};
758+
results[2]->colors[1] = (struct color) { (double)0xee / 0xff, (double)0xbb / 0xff, (double)0xff / 0xff, 1.0};
759+
results[2]->colors[2] = (struct color) { (double)0xaa / 0xff, (double)0xaf / 0xff, (double)0xff / 0xff, 1.0};
760+
761+
results[3]->colors[0] = (struct color) { (double)0xab / 0xff, (double)0xc1 / 0xff, (double)0x23 / 0xff, 1.0};
762+
results[3]->colors[1] = (struct color) { (double)0xac / 0xff, (double)0xaf / 0xff, (double)0x8f / 0xff, 1.0};
763+
764+
ARRAY_SAME_LENGTH(inputs, results);
765+
766+
static char buf[100];
767+
768+
for (int i = 0; i < G_N_ELEMENTS(inputs); i++) {
769+
sprintf(buf, "Failed in round %i", i);
770+
ASSERTm(buf, set_from_string(&grad, s, inputs[i]));
771+
772+
char *t1 = gradient_to_string(results[i]);
773+
char *t2 = gradient_to_string(grad);
774+
775+
sprintf(buf, "Failed in round %i. Expected %s, got %s", i, t1, t2);
776+
777+
g_free(t1);
778+
g_free(t2);
779+
780+
ASSERTm(buf, grad != NULL);
781+
ASSERTm(buf, grad->length == results[i]->length);
782+
783+
for (int k = 0; k < grad->length; k++)
784+
ASSERTm(buf, COLOR_SAME(grad->colors[k], results[i]->colors[k]));
785+
}
786+
787+
for (int i = 0; i < G_N_ELEMENTS(results); i++)
788+
gradient_free(results[i]);
789+
790+
PASS();
791+
}
792+
730793
TEST test_string_to_length(void)
731794
{
732795
struct length val = {0};
@@ -999,6 +1062,7 @@ SUITE(suite_option_parser)
9991062
RUN_TEST(test_string_to_sepcolor_invalid);
10001063
RUN_TEST(test_string_to_color);
10011064
RUN_TEST(test_string_to_color_invalid);
1065+
RUN_TEST(test_string_to_gradient);
10021066
RUN_TEST(test_enum_size);
10031067
RUN_TEST(test_string_to_length);
10041068
RUN_TEST(test_string_to_length_invalid);

0 commit comments

Comments
 (0)