fileblob_tests.c (3218B)
1 #include "writer/html/fileblob.h" 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 #include "fs_inmemory.h" 8 #include "git/file.h" 9 #include "git/repo.h" 10 #include "test_utils.h" 11 #include "utest.h" 12 13 struct html_fileblob { 14 int dummy; 15 }; 16 17 UTEST_F_SETUP(html_fileblob) { 18 inmemory_fs_clear(); 19 } 20 21 UTEST_F_TEARDOWN(html_fileblob) {} 22 23 UTEST_F(html_fileblob, basic) { 24 GitRepo repo = {.short_name = "test-repo"}; 25 HtmlFileBlob* blob_writer = 26 html_fileblob_create(&repo, g_fs_inmemory, "src/main.c"); 27 ASSERT_NE(NULL, blob_writer); 28 29 const char* content = "int main() {\n return 0;\n}\n"; 30 GitFile file = { 31 .repo_path = "src/main.c", 32 .content = (char*)content, 33 .size_bytes = strlen(content), 34 .size_lines = 3, 35 }; 36 37 html_fileblob_begin(blob_writer); 38 html_fileblob_add_file(blob_writer, &file); 39 html_fileblob_end(blob_writer); 40 html_fileblob_free(blob_writer); 41 42 const char* buf = inmemory_fs_get_buffer("file/src/main.c.html"); 43 ASSERT_NE(NULL, buf); 44 45 /* Verify Header/Filename */ 46 EXPECT_STR_SEQUENCE(buf, "main.c", "(27B)"); 47 48 /* Verify Content and Line Numbers */ 49 EXPECT_STR_SEQUENCE(buf, "id=\"l1\"", " 1", "int main()", // 50 "id=\"l2\"", " 2", "return 0;", // 51 "id=\"l3\"", " 3", "}"); 52 } 53 54 UTEST_F(html_fileblob, binary) { 55 GitRepo repo = {.short_name = "test-repo"}; 56 HtmlFileBlob* blob_writer = 57 html_fileblob_create(&repo, g_fs_inmemory, "logo.png"); 58 59 GitFile file = { 60 .repo_path = "logo.png", 61 .size_bytes = 100, 62 .size_lines = -1, /* Binary */ 63 }; 64 65 html_fileblob_begin(blob_writer); 66 html_fileblob_add_file(blob_writer, &file); 67 html_fileblob_end(blob_writer); 68 html_fileblob_free(blob_writer); 69 70 const char* buf = inmemory_fs_get_buffer("file/logo.png.html"); 71 ASSERT_NE(NULL, buf); 72 EXPECT_STR_SEQUENCE(buf, "logo.png", "Binary file."); 73 } 74 75 UTEST_F(html_fileblob, too_large) { 76 GitRepo repo = {.short_name = "test-repo"}; 77 HtmlFileBlob* blob_writer = 78 html_fileblob_create(&repo, g_fs_inmemory, "big.txt"); 79 80 GitFile file = { 81 .repo_path = "big.txt", 82 .size_bytes = 1000000, 83 .size_lines = -2, /* Too large */ 84 }; 85 86 html_fileblob_begin(blob_writer); 87 html_fileblob_add_file(blob_writer, &file); 88 html_fileblob_end(blob_writer); 89 html_fileblob_free(blob_writer); 90 91 const char* buf = inmemory_fs_get_buffer("file/big.txt.html"); 92 ASSERT_NE(NULL, buf); 93 EXPECT_STR_SEQUENCE(buf, "big.txt", "File too large to display."); 94 } 95 96 UTEST_F(html_fileblob, escaping) { 97 GitRepo repo = {.short_name = "test-repo"}; 98 HtmlFileBlob* blob_writer = 99 html_fileblob_create(&repo, g_fs_inmemory, "tags.html"); 100 101 const char* content = "<html>\n"; 102 GitFile file = { 103 .repo_path = "tags.html", 104 .content = (char*)content, 105 .size_bytes = strlen(content), 106 .size_lines = 1, 107 }; 108 109 html_fileblob_begin(blob_writer); 110 html_fileblob_add_file(blob_writer, &file); 111 html_fileblob_end(blob_writer); 112 html_fileblob_free(blob_writer); 113 114 const char* buf = inmemory_fs_get_buffer("file/tags.html.html"); 115 ASSERT_NE(NULL, buf); 116 /* Verify that <html> is escaped to <html> */ 117 EXPECT_STR_SEQUENCE(buf, "<html>"); 118 }