gout

A static git page generator
git clone https://git.bracken.jp/gout.git
Log | Files | Refs | README | LICENSE

refs_tests.c (3722B)


      1 #include "writer/html/refs.h"
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include "fs_inmemory.h"
      8 #include "git/commit.h"
      9 #include "git/reference.h"
     10 #include "git/repo.h"
     11 #include "test_utils.h"
     12 #include "utest.h"
     13 
     14 struct html_refs {
     15   int dummy;
     16 };
     17 
     18 UTEST_F_SETUP(html_refs) {
     19   inmemory_fs_clear();
     20 }
     21 
     22 UTEST_F_TEARDOWN(html_refs) {}
     23 
     24 UTEST_F(html_refs, branches) {
     25   GitRepo repo = {.short_name = "test-repo"};
     26   HtmlRefs* refs = html_refs_create(&repo, g_fs_inmemory);
     27   ASSERT_NE(NULL, refs);
     28 
     29   GitCommit commit = {
     30       .author_name = "User A",
     31       .author_time = 1702031400,
     32   };
     33   GitReference ref = {
     34       .type = kReftypeBranch,
     35       .shorthand = "main",
     36       .commit = &commit,
     37   };
     38 
     39   html_refs_begin(refs);
     40   html_refs_add_ref(refs, &ref);
     41   html_refs_end(refs);
     42   html_refs_free(refs);
     43 
     44   const char* buf = inmemory_fs_get_buffer("refs.html");
     45   ASSERT_NE(NULL, buf);
     46   EXPECT_STR_SEQUENCE(buf, "<h2>Branches</h2>", "<table id=\"branches\">",
     47                       "main", "2023-12-08 10:30", "User A");
     48 }
     49 
     50 UTEST_F(html_refs, tags) {
     51   GitRepo repo = {.short_name = "test-repo"};
     52   HtmlRefs* refs = html_refs_create(&repo, g_fs_inmemory);
     53   ASSERT_NE(NULL, refs);
     54 
     55   GitCommit commit = {
     56       .author_name = "User B",
     57       .author_time = 1702035000,
     58   };
     59   GitReference ref = {
     60       .type = kReftypeTag,
     61       .shorthand = "v1.0",
     62       .commit = &commit,
     63   };
     64 
     65   html_refs_begin(refs);
     66   html_refs_add_ref(refs, &ref);
     67   html_refs_end(refs);
     68   html_refs_free(refs);
     69 
     70   const char* buf = inmemory_fs_get_buffer("refs.html");
     71   ASSERT_NE(NULL, buf);
     72   EXPECT_STR_SEQUENCE(buf, "<h2>Tags</h2>", "<table id=\"tags\">", "v1.0",
     73                       "2023-12-08 11:30", "User B");
     74 }
     75 
     76 UTEST_F(html_refs, both) {
     77   GitRepo repo = {.short_name = "test-repo"};
     78   HtmlRefs* refs = html_refs_create(&repo, g_fs_inmemory);
     79   ASSERT_NE(NULL, refs);
     80 
     81   GitCommit commit1 = {.author_name = "User 1", .author_time = 1000};
     82   GitReference branch = {
     83       .type = kReftypeBranch, .shorthand = "main", .commit = &commit1};
     84 
     85   GitCommit commit2 = {.author_name = "User 2", .author_time = 2000};
     86   GitReference tag = {
     87       .type = kReftypeTag, .shorthand = "v1", .commit = &commit2};
     88 
     89   html_refs_begin(refs);
     90   html_refs_add_ref(refs, &branch);
     91   html_refs_add_ref(refs, &tag);
     92   html_refs_end(refs);
     93   html_refs_free(refs);
     94 
     95   const char* buf = inmemory_fs_get_buffer("refs.html");
     96   ASSERT_NE(NULL, buf);
     97   EXPECT_STR_SEQUENCE(buf, "<h2>Branches</h2>", "main", "<h2>Tags</h2>", "v1");
     98 }
     99 
    100 UTEST_F(html_refs, interleaved) {
    101   GitRepo repo = {.short_name = "test-repo"};
    102   HtmlRefs* refs = html_refs_create(&repo, g_fs_inmemory);
    103   ASSERT_NE(NULL, refs);
    104 
    105   GitCommit commit1 = {.author_name = "User 1", .author_time = 1000};
    106   GitReference branch1 = {
    107       .type = kReftypeBranch, .shorthand = "main", .commit = &commit1};
    108 
    109   GitCommit commit2 = {.author_name = "User 2", .author_time = 2000};
    110   GitReference tag = {
    111       .type = kReftypeTag, .shorthand = "v1", .commit = &commit2};
    112 
    113   GitCommit commit3 = {.author_name = "User 3", .author_time = 3000};
    114   GitReference branch2 = {
    115       .type = kReftypeBranch, .shorthand = "dev", .commit = &commit3};
    116 
    117   html_refs_begin(refs);
    118   html_refs_add_ref(refs, &branch1);
    119   html_refs_add_ref(refs, &tag);
    120   html_refs_add_ref(refs, &branch2);
    121   html_refs_end(refs);
    122   html_refs_free(refs);
    123 
    124   const char* buf = inmemory_fs_get_buffer("refs.html");
    125   ASSERT_NE(NULL, buf);
    126 
    127   // Expect all branches to be grouped under "Branches", and all tags under
    128   // "Tags", without duplicate "Branches" headers or formatting/structural
    129   // duplication.
    130   EXPECT_STR_SEQUENCE(buf, "<h2>Branches</h2>", "main", "dev", "<h2>Tags</h2>",
    131                       "v1");
    132 }