about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-09-14 14:57:32 -0400
committerJune McEnroe <june@causal.agency>2018-09-14 14:57:32 -0400
commit3cf064a5311bc5901d9ad5e11126444adf29ced0 (patch)
tree21da0d251c4548d355246c20f29edaf91b08e247
parentPreview with nick in input window (diff)
downloadcatgirl-3cf064a5311bc5901d9ad5e11126444adf29ced0.tar.gz
catgirl-3cf064a5311bc5901d9ad5e11126444adf29ced0.zip
Add tests for formatParse
With one currently failing so you know they're worth it.
Diffstat (limited to '')
-rw-r--r--.gitignore1
-rw-r--r--Makefile16
-rw-r--r--format.c63
3 files changed, 77 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore
index cdb8b82..d3a0af8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 *.o
+*.t
 chatte
 chroot.tar
 root
diff --git a/Makefile b/Makefile
index 3ca0008..ac361d1 100644
--- a/Makefile
+++ b/Makefile
@@ -24,15 +24,25 @@ OBJS += term.o
 OBJS += ui.o
 OBJS += url.o
 
+TESTS += format.t
+
 all: tags chatte
 
+tags: *.h *.c
+	ctags -w *.h *.c
+
 chatte: $(OBJS)
 	$(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $@
 
 $(OBJS): chat.h
 
-tags: *.h *.c
-	ctags -w *.h *.c
+test: $(TESTS)
+	$(TESTS:%=./%;)
+
+.SUFFIXES: .t
+
+.c.t:
+	$(CC) $(CFLAGS) -DTEST $(LDFLAGS) $< $(LDLIBS) -o $@
 
 install: chatte chatte.1
 	install -d $(PREFIX)/bin $(MANPATH)/man1
@@ -77,4 +87,4 @@ chroot.tar: chatte chatte.1 man.sh
 	tar -c -f chroot.tar -C root bin etc home lib libexec usr
 
 clean:
-	rm -rf tags chatte $(OBJS) root chroot.tar
+	rm -rf tags chatte $(OBJS) $(TESTS) root chroot.tar
diff --git a/format.c b/format.c
index 58735c5..8120165 100644
--- a/format.c
+++ b/format.c
@@ -117,3 +117,66 @@ bool formatParse(struct Format *format, const wchar_t *split) {
 	}
 	return true;
 }
+
+#ifdef TEST
+#include <assert.h>
+
+static bool testColor(
+	const wchar_t *str, enum IRCColor fg, enum IRCColor bg, size_t index
+) {
+	struct Format format = { .str = str };
+	formatReset(&format);
+	if (!formatParse(&format, NULL)) return false;
+	if (format.fg != fg) return false;
+	if (format.bg != bg) return false;
+	return (format.str == &str[index]);
+}
+
+static bool testSplit(const wchar_t *str, size_t index) {
+	struct Format format = { .str = str };
+	formatReset(&format);
+	bool split = false;
+	while (formatParse(&format, &str[index])) {
+		if (format.split && split) return false;
+		if (format.split) split = true;
+	}
+	return split;
+}
+
+static bool testSplits(const wchar_t *str) {
+	for (size_t i = 0; i <= wcslen(str); ++i) {
+		if (!testSplit(str, i)) return false;
+	}
+	return true;
+}
+
+int main() {
+	assert(testColor(L"\003a",      IRCDefault,   IRCDefault,   1));
+	assert(testColor(L"\003,a",     IRCDefault,   IRCDefault,   1));
+	assert(testColor(L"\003,1",     IRCDefault,   IRCDefault,   1));
+	assert(testColor(L"\0031a",     IRCBlack,     IRCDefault,   2));
+	assert(testColor(L"\0031,a",    IRCBlack,     IRCDefault,   2));
+	assert(testColor(L"\00312a",    IRCLightBlue, IRCDefault,   3));
+	assert(testColor(L"\00312,a",   IRCLightBlue, IRCDefault,   3));
+	assert(testColor(L"\003123",    IRCLightBlue, IRCDefault,   3));
+	assert(testColor(L"\0031,1a",   IRCBlack,     IRCBlack,     4));
+	assert(testColor(L"\0031,12a",  IRCBlack,     IRCLightBlue, 5));
+	assert(testColor(L"\0031,123",  IRCBlack,     IRCLightBlue, 5));
+	assert(testColor(L"\00312,1a",  IRCLightBlue, IRCBlack,     5));
+	assert(testColor(L"\00312,12a", IRCLightBlue, IRCLightBlue, 6));
+	assert(testColor(L"\00312,123", IRCLightBlue, IRCLightBlue, 6));
+
+	assert(testColor(L"\00316,16a", IRCDefault, IRCDefault, 6));
+	assert(testColor(L"\00399,99a", IRCDefault, IRCDefault, 6));
+
+	assert(testSplits(L"ab"));
+	assert(testSplits(L"\002ab"));
+	assert(testSplits(L"a\002b"));
+	assert(testSplits(L"a\002\003b"));
+	assert(testSplits(L"a\0031b"));
+	assert(testSplits(L"a\00312b"));
+	assert(testSplits(L"a\00312,1b"));
+	assert(testSplits(L"a\00312,12b"));
+}
+
+#endif