Add tests for formatParse

With one currently failing so you know they're worth it.
master
Curtis McEnroe 2018-09-14 14:57:32 -04:00
parent 326bc5163d
commit 3cf064a531
No known key found for this signature in database
GPG Key ID: CEA2F97ADCFCD77C
3 changed files with 77 additions and 3 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
*.o
*.t
chatte
chroot.tar
root

View File

@ -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

View File

@ -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