Vendor the C99 bb sources, share directory, tests and man page under third_party/bb (MIT, upstream f5373fb) and add a make bb target. A checkout can now build project sites without an installed bb. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
151 lines
6.8 KiB
C
151 lines
6.8 KiB
C
/* theme_test.c — contrast maths, token derivation and the shipped themes.
|
|
* Compile: cc -std=c99 -Wall -Wextra -pedantic -Isrc tests/theme_test.c \
|
|
* src/theme.c src/config.c src/strbuf.c -o tests/theme_test
|
|
* Run from the repo root: ./tests/theme_test */
|
|
#include "bb.h"
|
|
#include <dirent.h>
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static int failures = 0, total = 0;
|
|
|
|
#define CHECK(cond, ...) do { total++; if (!(cond)) { failures++; \
|
|
printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
|
|
|
|
static double ratio(const config *t, const char *fg, const char *bg) {
|
|
return theme_contrast(config_get(t, fg), config_get(t, bg));
|
|
}
|
|
|
|
static void test_contrast_maths(void) {
|
|
CHECK(fabs(theme_contrast("#000000", "#ffffff") - 21.0) < 0.01, "black/white is 21:1");
|
|
CHECK(fabs(theme_contrast("#ffffff", "#000000") - 21.0) < 0.01, "contrast is symmetric");
|
|
CHECK(fabs(theme_contrast("#777", "#fff") - 4.48) < 0.02, "#777 on white is about 4.48 (got %.3f)",
|
|
theme_contrast("#777", "#fff"));
|
|
CHECK(fabs(theme_contrast("#808080", "#808080") - 1.0) < 1e-9, "same colour is 1:1");
|
|
CHECK(theme_contrast("red", "#fff") < 0, "named colours are rejected");
|
|
CHECK(theme_contrast("#12345", "#fff") < 0, "5-digit hex is rejected");
|
|
CHECK(theme_contrast("#gggggg", "#fff") < 0, "non-hex digits are rejected");
|
|
}
|
|
|
|
static void test_derivation(void) {
|
|
config t; config_init(&t);
|
|
config_add(&t, "bg", "#202020");
|
|
theme_normalize(&t, NULL);
|
|
const char *keys[] = { "bg", "text", "link", "link.visited", "heading", "muted",
|
|
"border", "codebg", "light", "dark", "title.bg", "title.fg", "select.bg",
|
|
"select.fg", "frame", "frame.width", "hdr.bg", "hdr.bw", "hdr.bc", "hdr.pad",
|
|
"pre.bw", "pre.bc", NULL };
|
|
for (int i = 0; keys[i]; i++)
|
|
CHECK(config_get(&t, keys[i]) != NULL, "bg-only theme gets token %s", keys[i]);
|
|
CHECK(ratio(&t, "text", "bg") >= 4.5, "derived text readable on dark bg");
|
|
CHECK(ratio(&t, "muted", "bg") >= 4.5, "derived muted readable");
|
|
config_free(&t);
|
|
|
|
config_init(&t);
|
|
theme_normalize(&t, NULL);
|
|
CHECK(strcmp(config_get(&t, "bg"), "#ffffff") == 0, "empty theme falls back to white bg");
|
|
CHECK(ratio(&t, "text", "bg") >= 4.5, "empty theme still readable");
|
|
config_free(&t);
|
|
}
|
|
|
|
static void test_fixes_unreadable(void) {
|
|
config t; config_init(&t);
|
|
config_add(&t, "bg", "#8e8e77");
|
|
config_add(&t, "text", "#000000");
|
|
config_add(&t, "link", "#7f6c6c"); /* 1.47:1 as imported once */
|
|
config_add(&t, "heading", "#82829b"); /* 1.12:1 */
|
|
strbuf rep; sb_init(&rep);
|
|
theme_normalize(&t, &rep);
|
|
CHECK(ratio(&t, "link", "bg") >= 4.5, "link fixed (%.2f)", ratio(&t, "link", "bg"));
|
|
CHECK(ratio(&t, "heading", "bg") >= 3.0, "heading fixed (%.2f)", ratio(&t, "heading", "bg"));
|
|
CHECK(rep.data && strstr(rep.data, "link") && strstr(rep.data, "heading"),
|
|
"report names the fixed tokens");
|
|
sb_free(&rep);
|
|
config_free(&t);
|
|
}
|
|
|
|
static void test_good_theme_untouched(void) {
|
|
config t; config_init(&t);
|
|
config_add(&t, "bg", "#ffffff");
|
|
config_add(&t, "text", "#111111");
|
|
config_add(&t, "link", "#0033aa");
|
|
config_add(&t, "heading", "#222222");
|
|
strbuf rep; sb_init(&rep);
|
|
theme_normalize(&t, &rep);
|
|
CHECK(strcmp(config_get(&t, "link"), "#0033aa") == 0, "readable link is left alone");
|
|
CHECK(strcmp(config_get(&t, "text"), "#111111") == 0, "readable text is left alone");
|
|
CHECK(rep.len == 0, "no report for a readable theme");
|
|
sb_free(&rep);
|
|
config_free(&t);
|
|
}
|
|
|
|
static void test_idempotent(void) {
|
|
config t; config_init(&t);
|
|
config_add(&t, "bg", "#c0c0c0"); config_add(&t, "link", "#c8c8d0");
|
|
config_add(&t, "frame", "bevel");
|
|
theme_normalize(&t, NULL);
|
|
char *first = strdup(config_get(&t, "link"));
|
|
strbuf rep; sb_init(&rep);
|
|
theme_normalize(&t, &rep);
|
|
CHECK(strcmp(first, config_get(&t, "link")) == 0, "second normalize changes nothing");
|
|
CHECK(rep.len == 0, "second normalize reports nothing");
|
|
CHECK(strcmp(config_get(&t, "frame"), "bevel") == 0, "frame stays bevel");
|
|
CHECK(strcmp(config_get(&t, "frame.width"), "2px") == 0, "bevel default width");
|
|
free(first); sb_free(&rep); config_free(&t);
|
|
}
|
|
|
|
static void test_render(void) {
|
|
config t; config_init(&t);
|
|
config_add(&t, "a", "1"); config_add(&t, "b.c", "two");
|
|
strbuf out; sb_init(&out);
|
|
theme_render(&out, "x{{a}}y{{b.c}}z{{missing}}!{{broken", &t);
|
|
CHECK(out.data && strcmp(out.data, "x1ytwoz!{{broken") == 0, "render: got '%s'", out.data ? out.data : "");
|
|
sb_free(&out); config_free(&t);
|
|
}
|
|
|
|
/* Every theme we ship must come out readable, distinguishable and complete. */
|
|
static void test_shipped_themes(void) {
|
|
DIR *d = opendir("share/themes");
|
|
if (!d) { printf("FAIL: cannot open share/themes (run from repo root)\n"); failures++; total++; return; }
|
|
struct dirent *e;
|
|
int n = 0;
|
|
while ((e = readdir(d))) {
|
|
size_t l = strlen(e->d_name);
|
|
if (l < 6 || strcmp(e->d_name + l - 5, ".conf") != 0) continue;
|
|
char path[512];
|
|
snprintf(path, sizeof path, "share/themes/%s", e->d_name);
|
|
config t; config_init(&t);
|
|
CHECK(config_load(&t, path) == 0, "%s loads", e->d_name);
|
|
CHECK(config_get(&t, "bg") != NULL, "%s defines bg", e->d_name);
|
|
theme_normalize(&t, NULL);
|
|
CHECK(ratio(&t, "text", "bg") >= 4.5, "%s text %.2f", e->d_name, ratio(&t, "text", "bg"));
|
|
CHECK(ratio(&t, "link", "bg") >= 4.5, "%s link %.2f", e->d_name, ratio(&t, "link", "bg"));
|
|
CHECK(ratio(&t, "link.visited", "bg") >= 4.5, "%s visited %.2f", e->d_name, ratio(&t, "link.visited", "bg"));
|
|
CHECK(ratio(&t, "heading", "bg") >= 3.0, "%s heading %.2f", e->d_name, ratio(&t, "heading", "bg"));
|
|
CHECK(ratio(&t, "muted", "bg") >= 4.5, "%s muted %.2f", e->d_name, ratio(&t, "muted", "bg"));
|
|
CHECK(ratio(&t, "text", "codebg") >= 4.5, "%s text on codebg %.2f", e->d_name, ratio(&t, "text", "codebg"));
|
|
CHECK(ratio(&t, "title.fg", "title.bg") >= 4.5, "%s title %.2f", e->d_name, ratio(&t, "title.fg", "title.bg"));
|
|
CHECK(ratio(&t, "select.fg", "select.bg") >= 4.5, "%s selection %.2f", e->d_name, ratio(&t, "select.fg", "select.bg"));
|
|
CHECK(ratio(&t, "text", "link") >= 1.25, "%s text/link distinct %.2f", e->d_name, ratio(&t, "text", "link"));
|
|
CHECK(ratio(&t, "bg", "codebg") >= 1.05, "%s bg/codebg distinct %.2f", e->d_name, ratio(&t, "bg", "codebg"));
|
|
config_free(&t);
|
|
n++;
|
|
}
|
|
closedir(d);
|
|
CHECK(n >= 70, "found %d shipped themes", n);
|
|
}
|
|
|
|
int main(void) {
|
|
test_contrast_maths();
|
|
test_derivation();
|
|
test_fixes_unreadable();
|
|
test_good_theme_untouched();
|
|
test_idempotent();
|
|
test_render();
|
|
test_shipped_themes();
|
|
printf("%d/%d passed\n", total - failures, total);
|
|
return failures ? 1 : 0;
|
|
}
|