Skip to content

Commit

Permalink
Change Yomichan parser tests to mock functions
Browse files Browse the repository at this point in the history
  • Loading branch information
btrkeks committed May 31, 2024
1 parent b4eb8b9 commit 1bd3741
Showing 1 changed file with 27 additions and 55 deletions.
82 changes: 27 additions & 55 deletions tests/yomichan_parser_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "yomichan_parser.c"

#include <cgreen/mocks.h>

TestSuite *yomichan_parser_tests(void);

Describe(Parser);
Expand All @@ -11,34 +13,12 @@ BeforeEach(Parser) {
AfterEach(Parser) {
}

static bool frequency_entries_equal(freqentry a, freqentry b) {
return s8equals(a.word, b.word) && s8equals(a.reading, b.reading) && a.frequency == b.frequency;
}

static bool dict_entries_equal(dictentry a, dictentry b) {
return s8equals(a.kanji, b.kanji) && s8equals(a.reading, b.reading) &&
s8equals(a.definition, b.definition) && s8equals(a.dictname, b.dictname) &&
a.frequency == b.frequency;
}

static _nonnull_ void store_freqentry(freqentry *dest, freqentry fe) {
static bool called = false;
if (called) {
puts("ERROR: store_freqentry was called twice");
abort();
}
*dest = freqentry_dup(fe);
called = true;
static void foreach_freqentry(void *userdata, freqentry fe) {
mock(fe.frequency, fe.reading.s, fe.word.s);
}

static _nonnull_ void store_dictentry(dictentry *dest, dictentry fe) {
static bool called = false;
if (called) {
puts("ERROR: store_dictentry was called twice");
abort();
}
*dest = dictentry_dup(fe);
called = true;
static _nonnull_ void foreach_dictentry(void *userdata, dictentry de) {
mock(de.definition.s, de.dictname.s, de.kanji.s, de.reading.s);
}

#define CHECK_NR(n) \
Expand All @@ -47,19 +27,18 @@ static _nonnull_ void store_dictentry(dictentry *dest, dictentry fe) {
g_file_get_contents("../tests/files/testentries/" #n "_entry", (char **)&toparse.s, \
(gsize *)&toparse.len, NULL); \
\
dictentry parsed = {0}; \
parse_yomichan_dictentries_from_buffer( \
toparse, S("Test"), (void (*)(void *, dictentry))store_dictentry, &parsed); \
\
s8 expected = {0}; \
g_file_get_contents("../tests/files/testentries/" #n "_expected", (char **)&expected.s, \
(gsize *)&expected.len, NULL); \
g_strchomp((char *)expected.s); \
assert_that(parsed.definition.s, is_equal_to_string((char *)expected.s)); \
\
expect(foreach_dictentry, \
when(de.definition.s, is_equal_to_string((char *)expected.s)), \
when(de.dictname.s, is_equal_to_string("Test"))); \
parse_yomichan_dictentries_from_buffer(toparse, S("Test"), &foreach_dictentry, NULL); \
\
g_free(toparse.s); \
g_free(expected.s); \
dictentry_free(&parsed); \
} while (0)

Ensure(Parser, correctly_parses_structured_content1) {
Expand Down Expand Up @@ -93,41 +72,34 @@ Ensure(Parser, correctly_parses_nested_lists) {
Ensure(Parser, correctly_parses_frequency_entry_with_reading) {
const s8 toparse = S("[[\"糞\", \"freq\", {\"reading\": \"くそ\", \"frequency\": 9788}]]");

freqentry parsed = {0};
parse_yomichan_freqentries_from_buffer(toparse, (void (*)(void *, freqentry))store_freqentry,
&parsed);

freqentry expected = {.word = S("糞"), .reading = S("くそ"), .frequency = 9788};
assert_that(parsed.word.s, is_equal_to_string((char *)expected.word.s));
assert_that(parsed.reading.s, is_equal_to_string((char *)expected.reading.s));
assert_that(parsed.frequency, is_equal_to(expected.frequency));

freqentry_free(&parsed);
expect(foreach_freqentry,
when(fe.word.s, is_equal_to_string((char *)expected.word.s)),
when(fe.reading.s, is_equal_to_string((char *)expected.reading.s)),
when(fe.frequency, is_equal_to(expected.frequency)));
parse_yomichan_freqentries_from_buffer(toparse, &foreach_freqentry, NULL);
}

Ensure(Parser, correctly_parses_frequency_entry_without_reading) {
const s8 toparse = S("[[\"私たち\", \"freq\", 581]]");

freqentry parsed = {0};
parse_yomichan_freqentries_from_buffer(toparse, (void (*)(void *, freqentry))store_freqentry,
&parsed);

freqentry expected = {.word = S("私たち"), .reading = (s8){0}, .frequency = 581};
assert_that(parsed.word.s, is_equal_to_string((char *)expected.word.s));
assert_that(parsed.reading.s, is_equal_to_string((char *)expected.reading.s));
assert_that(parsed.frequency, is_equal_to(expected.frequency));

freqentry_free(&parsed);
expect(foreach_freqentry,
when(fe.word.s, is_equal_to_string((char *)expected.word.s)),
when(fe.reading.s, is_equal_to_string((char *)expected.reading.s)),
when(fe.frequency, is_equal_to(expected.frequency)));
parse_yomichan_freqentries_from_buffer(toparse, &foreach_freqentry, NULL);
}

Ensure(Parser, correctly_extracts_dictionary_name) {
const s8 buffer = S("{"
"\"title\": \"Test dictionary\","
"\"sequenced\": true,"
"\"format\": 3,"
"\"revision\": \"2024-05-04\","
"\"attribution\": \"\""
"}");
"\"title\": \"Test dictionary\","
"\"sequenced\": true,"
"\"format\": 3,"
"\"revision\": \"2024-05-04\","
"\"attribution\": \"\""
"}");

_drop_(frees8) s8 dictname = extract_dictname_from_buffer(buffer);
assert_that(dictname.s, is_equal_to_string("Test dictionary"));
Expand Down

0 comments on commit 1bd3741

Please sign in to comment.