gout

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

atom_tests.c (6196B)


      1 #include "writer/atom/atom.h"
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include "git/commit.h"
      8 #include "git/repo.h"
      9 #include "test_utils.h"
     10 #include "utest.h"
     11 
     12 UTEST(atom, begin) {
     13   char* buf = NULL;
     14   size_t size = 0;
     15   FILE* out = open_memstream(&buf, &size);
     16   ASSERT_NE(NULL, out);
     17 
     18   GitRepo repo = {
     19       .short_name = "test-repo",
     20       .description = "A test repository",
     21   };
     22 
     23   Atom* atom = atom_create(&repo, out);
     24   atom_begin(atom);
     25   atom_free(atom);
     26   fflush(out);
     27   fclose(out);
     28 
     29   EXPECT_STR_SEQUENCE(buf,                                             //
     30                       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",    //
     31                       "<feed xmlns=\"http://www.w3.org/2005/Atom\">",  //
     32                       "<title>test-repo, branch HEAD</title>",         //
     33                       "<subtitle>A test repository</subtitle>");
     34 
     35   free(buf);
     36 }
     37 
     38 UTEST(atom, add_commit) {
     39   char* buf = NULL;
     40   size_t size = 0;
     41   FILE* out = open_memstream(&buf, &size);
     42   ASSERT_NE(NULL, out);
     43 
     44   GitRepo repo = {.short_name = "test-repo"};
     45   Atom* atom = atom_create(&repo, out);
     46   atom_set_baseurl(atom, "https://example.com/");
     47 
     48   GitCommit commit = {
     49       .oid = "abc1234567890",
     50       .parentoid = "def0987654321",
     51       .summary = "Fix a bug",
     52       .message = "Detailed description of the fix.",
     53       .author_name = "Test User",
     54       .author_email = "test@example.com",
     55       .author_time = 1702031400,     /* 2023-12-08 10:30:00 UTC */
     56       .author_timezone_offset = 540, /* +09:00 */
     57       .commit_time = 1702035000,     /* 2023-12-08 11:30:00 UTC */
     58       .commit_timezone_offset = 540,
     59   };
     60 
     61   atom_add_commit(atom, &commit, "commit/abc.html", "text/html", "v1.0");
     62   atom_free(atom);
     63   fflush(out);
     64   fclose(out);
     65 
     66   EXPECT_STR_SEQUENCE(buf,                                            //
     67                       "<entry>",                                      //
     68                       "<id>abc1234567890</id>",                       //
     69                       "<published>2023-12-08T10:30:00Z</published>",  //
     70                       "<updated>2023-12-08T11:30:00Z</updated>",      //
     71                       "<title>[v1.0]Fix a bug</title>",               //
     72                       "<link rel=\"alternate\" type=\"text/html\" "
     73                       "href=\"https://example.com/commit/abc.html\" />",  //
     74                       "<author>",                                         //
     75                       "<name>Test User</name>",                           //
     76                       "<email>test@example.com</email>",                  //
     77                       "</author>",                                        //
     78                       "<content>",                                        //
     79                       "commit abc1234567890",                             //
     80                       "parent def0987654321",                             //
     81                       "Author: Test User &lt;test@example.com&gt;",       //
     82                       "Detailed description of the fix.",                 //
     83                       "</content>",                                       //
     84                       "</entry>");
     85 
     86   free(buf);
     87 }
     88 
     89 UTEST(atom, multiple_commits) {
     90   char* buf = NULL;
     91   size_t size = 0;
     92   FILE* out = open_memstream(&buf, &size);
     93   ASSERT_NE(NULL, out);
     94 
     95   GitRepo repo = {.short_name = "test-repo"};
     96   Atom* atom = atom_create(&repo, out);
     97 
     98   GitCommit commit1 = {
     99       .oid = "sha1",
    100       .summary = "Commit 1",
    101       .author_name = "User 1",
    102       .author_email = "user1@mail.com",
    103   };
    104   GitCommit commit2 = {
    105       .oid = "sha2",
    106       .summary = "Commit 2",
    107       .author_name = "User 2",
    108       .author_email = "user2@mail.com",
    109   };
    110 
    111   atom_add_commit(atom, &commit1, "c1", "text/html", "");
    112   atom_add_commit(atom, &commit2, "c2", "text/html", "");
    113   atom_free(atom);
    114   fflush(out);
    115   fclose(out);
    116 
    117   EXPECT_STR_SEQUENCE(buf,                        //
    118                       "<entry>",                  //
    119                       "<id>sha1</id>",            //
    120                       "<title>Commit 1</title>",  //
    121                       "</entry>",                 //
    122                       "<entry>",                  //
    123                       "<id>sha2</id>",            //
    124                       "<title>Commit 2</title>",  //
    125                       "</entry>");
    126 
    127   free(buf);
    128 }
    129 
    130 UTEST(atom, end) {
    131   char* buf = NULL;
    132   size_t size = 0;
    133   FILE* out = open_memstream(&buf, &size);
    134   ASSERT_NE(NULL, out);
    135 
    136   GitRepo repo = {.short_name = "test-repo"};
    137   Atom* atom = atom_create(&repo, out);
    138   atom_end(atom);
    139   atom_free(atom);
    140   fflush(out);
    141   fclose(out);
    142 
    143   EXPECT_STREQ("</feed>\n", buf);
    144 
    145   free(buf);
    146 }
    147 
    148 UTEST(atom, limit_commits) {
    149   char* buf = NULL;
    150   size_t size = 0;
    151   FILE* out = open_memstream(&buf, &size);
    152   ASSERT_NE(NULL, out);
    153 
    154   GitRepo repo = {.short_name = "test-repo"};
    155   Atom* atom = atom_create(&repo, out);
    156 
    157   GitCommit commit = {
    158       .oid = "sha", .author_name = "user", .author_email = "mail"};
    159   for (int i = 0; i < 105; i++) {
    160     atom_add_commit(atom, &commit, "path", "text/html", "");
    161   }
    162 
    163   fflush(out);
    164   EXPECT_STR_COUNT(buf, "<entry>", 100);
    165 
    166   atom_free(atom);
    167   fclose(out);
    168   free(buf);
    169 }
    170 
    171 UTEST(atom, url_concatenation) {
    172   GitRepo repo = {.short_name = "test-repo"};
    173   GitCommit commit = {.oid = "sha"};
    174 
    175   struct {
    176     const char* base;
    177     const char* path;
    178     const char* expected;
    179   } cases[] = {
    180       {"https://ex.com/", "c/1.html", "href=\"https://ex.com/c/1.html\""},
    181       {"https://ex.com", "c/1.html", "href=\"https://ex.com/c/1.html\""},
    182       {"https://ex.com", "/c/1.html", "href=\"https://ex.com/c/1.html\""},
    183       {"https://ex.com/", "/c/1.html", "href=\"https://ex.com/c/1.html\""},
    184       {"", "c/1.html", "href=\"c/1.html\""},
    185   };
    186 
    187   for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
    188     char* buf = NULL;
    189     size_t size = 0;
    190     FILE* out = open_memstream(&buf, &size);
    191     Atom* atom = atom_create(&repo, out);
    192     atom_set_baseurl(atom, cases[i].base);
    193 
    194     atom_add_commit(atom, &commit, cases[i].path, "text/html", "");
    195 
    196     fflush(out);
    197     EXPECT_NE(NULL, strstr(buf, cases[i].expected));
    198 
    199     atom_free(atom);
    200     fclose(out);
    201     free(buf);
    202   }
    203 }