diff --git a/test/main.cpp b/test/main.cpp index c0c22a0..52a7145 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -11,33 +11,63 @@ static scan_arc_ctx_t arc_list_ctx; static scan_text_ctx_t text_500_ctx; -TEST(TextTest, BookCsvContentLen) { - const char *filepath = "libscan-test-files/test_files/text/books.csv"; - +TEST(Text, BookCsvContentLen) { vfile_t f; document_t doc; - doc.meta_head = nullptr; - doc.meta_tail = nullptr; - load_file(filepath, &f); + load_doc_file("libscan-test-files/test_files/text/books.csv", &f, &doc); + parse_text(&text_500_ctx, &f, &doc); ASSERT_NEAR(strlen(get_meta(&doc, MetaContent)->str_val), 500, 1); - CLOSE_FILE(f) - destroy_doc(&doc); + cleanup(&doc, &f); } -TEST(TextTest, MemUtf8_1) { - const char *content = "a"; //todo - +TEST(Text, MemUtf8_1) { + const char *content = "a"; vfile_t f; document_t doc; - doc.meta_head = nullptr; - doc.meta_tail = nullptr; - load_mem((void *) content, strlen(content), &f); + load_doc_mem((void *) content, strlen(content), &f, &doc); + parse_text(&text_500_ctx, &f, &doc); ASSERT_EQ(strlen(get_meta(&doc, MetaContent)->str_val), 1); - destroy_doc(&doc); + cleanup(&doc, &f); +} + +TEST(Text, MemUtf8_Invalid1) { + const char *content = "12345\xE0"; + vfile_t f; + document_t doc; + load_doc_mem((void *) content, strlen(content), &f, &doc); + + parse_text(&text_500_ctx, &f, &doc); + + ASSERT_STREQ(get_meta(&doc, MetaContent)->str_val, "12345"); + cleanup(&doc, &f); +} + +TEST(Text, MemUtf8_2) { + const char *content = "最後測試"; + vfile_t f; + document_t doc; + load_doc_mem((void *) content, strlen(content), &f, &doc); + + parse_text(&text_500_ctx, &f, &doc); + + ASSERT_STREQ(get_meta(&doc, MetaContent)->str_val, "最後測試"); + cleanup(&doc, &f); +} + +TEST(Text, MemUtf8_Invalid2) { + const char *content = "最後測\xe8\xa9"; + vfile_t f; + document_t doc; + load_doc_mem((void *) content, strlen(content), &f, &doc); + + parse_text(&text_500_ctx, &f, &doc); + + ASSERT_STREQ(get_meta(&doc, MetaContent)->str_val, "最後測"); + cleanup(&doc, &f); } diff --git a/test/test_util.cpp b/test/test_util.cpp index 8aef3c1..60bb979 100644 --- a/test/test_util.cpp +++ b/test/test_util.cpp @@ -32,6 +32,23 @@ void fs_close(vfile_t *f) { } } +void load_doc_file(const char *filepath, vfile_t *f, document_t *doc) { + doc->meta_head = nullptr; + doc->meta_tail = nullptr; + load_file(filepath, f); +} + +void load_doc_mem(void *mem, size_t mem_len, vfile_t *f, document_t *doc) { + doc->meta_head = nullptr; + doc->meta_tail = nullptr; + load_mem(mem, mem_len, f); +} + +void cleanup(document_t *doc, vfile_t *f) { + destroy_doc(doc); + CLOSE_FILE((*f)) +} + void load_file(const char *filepath, vfile_t *f) { stat(filepath, &f->info); f->fd = open(filepath, O_RDONLY); diff --git a/test/test_util.h b/test/test_util.h index 3df9400..abc90b4 100644 --- a/test/test_util.h +++ b/test/test_util.h @@ -5,6 +5,9 @@ void load_file(const char *filepath, vfile_t *f); void load_mem(void *mem, size_t size, vfile_t *f); +void load_doc_mem(void *mem, size_t mem_len, vfile_t *f, document_t *doc); +void load_doc_file(const char *filepath, vfile_t *f, document_t *doc); +void cleanup(document_t *doc, vfile_t *f); static void noop_logf(char *filepath, int level, char *format, ...) { // noop