gout

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

cache_tests.c (2632B)


      1 #include "writer/cache/cache.h"
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include "git/commit.h"
      8 #include "utest.h"
      9 
     10 static void mock_write_commit(FILE* out, const GitCommit* commit) {
     11   fprintf(out, "row:%s\n", commit->oid);
     12 }
     13 
     14 UTEST(cache, new_cache) {
     15   char* out_buf = NULL;
     16   size_t out_size = 0;
     17   FILE* out = open_memstream(&out_buf, &out_size);
     18 
     19   Cache* cache = cache_create(NULL, out, mock_write_commit);
     20   GitCommit c1 = {.oid = "sha1"};
     21   GitCommit c2 = {.oid = "sha2"};
     22 
     23   EXPECT_TRUE(cache_can_add_commits(cache));
     24   cache_add_commit_row(cache, &c1);
     25   EXPECT_TRUE(cache_can_add_commits(cache));
     26   cache_add_commit_row(cache, &c2);
     27   EXPECT_TRUE(cache_can_add_commits(cache));
     28 
     29   cache_finish(cache);
     30   cache_free(cache);
     31   fflush(out);
     32   fclose(out);
     33 
     34   const char* expected = "sha1\nrow:sha1\nrow:sha2\n";
     35   EXPECT_STREQ(expected, out_buf);
     36 
     37   free(out_buf);
     38 }
     39 
     40 UTEST(cache, incremental_update) {
     41   char cache_data[] = "sha2\nrow:sha2\nrow:sha3\n";
     42   FILE* in = fmemopen(cache_data, sizeof(cache_data), "r");
     43 
     44   char* out_buf = NULL;
     45   size_t out_size = 0;
     46   FILE* out = open_memstream(&out_buf, &out_size);
     47 
     48   Cache* cache = cache_create(in, out, mock_write_commit);
     49 
     50   GitCommit c1 = {.oid = "sha1"};
     51   GitCommit c2 = {.oid = "sha2"};
     52   GitCommit c3 = {.oid = "sha3"};
     53 
     54   EXPECT_TRUE(cache_can_add_commits(cache));
     55   cache_add_commit_row(cache, &c1);
     56 
     57   EXPECT_TRUE(cache_can_add_commits(cache));
     58   cache_add_commit_row(cache, &c2);
     59   EXPECT_FALSE(cache_can_add_commits(cache));
     60 
     61   cache_add_commit_row(cache, &c3);
     62 
     63   cache_finish(cache);
     64   cache_free(cache);
     65   fclose(in);
     66   fflush(out);
     67   fclose(out);
     68 
     69   const char* expected = "sha1\nrow:sha1\nrow:sha2\nrow:sha3\n";
     70   EXPECT_STREQ(expected, out_buf);
     71 
     72   free(out_buf);
     73 }
     74 
     75 UTEST(cache, corrupt_input) {
     76   char cache_data[] = "";
     77   FILE* in = fmemopen(cache_data, sizeof(cache_data), "r");
     78 
     79   char* out_buf = NULL;
     80   size_t out_size = 0;
     81   FILE* out = open_memstream(&out_buf, &out_size);
     82 
     83   Cache* cache = cache_create(in, out, mock_write_commit);
     84   GitCommit c1 = {.oid = "sha1"};
     85   cache_add_commit_row(cache, &c1);
     86   cache_finish(cache);
     87   cache_free(cache);
     88   fclose(in);
     89   fflush(out);
     90   fclose(out);
     91 
     92   EXPECT_STREQ("sha1\nrow:sha1\n", out_buf);
     93   free(out_buf);
     94 }
     95 
     96 UTEST(cache, copy_log) {
     97   char cache_data[] = "lastsha\nline1\nline2\n";
     98   FILE* in = fmemopen(cache_data, sizeof(cache_data), "r");
     99 
    100   char* out_buf = NULL;
    101   size_t out_size = 0;
    102   FILE* out = open_memstream(&out_buf, &out_size);
    103 
    104   cache_copy_log(in, out);
    105   fflush(out);
    106   fclose(in);
    107   fclose(out);
    108 
    109   EXPECT_STREQ("line1\nline2\n", out_buf);
    110   free(out_buf);
    111 }