gout

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

format_tests.c (10392B)


      1 #include "format.h"
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <time.h>
      6 
      7 #include "utest.h"
      8 
      9 UTEST(print_time, PositiveOffset) {
     10   char* buf = NULL;
     11   size_t size = 0;
     12   FILE* out = open_memstream(&buf, &size);
     13   ASSERT_NE(NULL, out);
     14 
     15   /* Test time: 2023-12-08 10:30:00 UTC */
     16   time_t test_time = 1702031400;
     17   int timezone_offset = 540; /* +09:00 */
     18 
     19   print_time(out, test_time, timezone_offset);
     20   fclose(out);
     21 
     22   EXPECT_STREQ("Fri,  8 Dec 2023 19:30:00 +0900", buf);
     23 
     24   free(buf);
     25 }
     26 
     27 UTEST(print_time, NegativeOffset) {
     28   char* buf = NULL;
     29   size_t size = 0;
     30   FILE* out = open_memstream(&buf, &size);
     31   ASSERT_NE(NULL, out);
     32 
     33   /* Test time: 2023-12-08 10:30:00 UTC */
     34   time_t test_time = 1702031400;
     35   int timezone_offset = -300; /* -05:00 */
     36 
     37   print_time(out, test_time, timezone_offset);
     38   fclose(out);
     39 
     40   EXPECT_STREQ("Fri,  8 Dec 2023 05:30:00 -0500", buf);
     41 
     42   free(buf);
     43 }
     44 
     45 UTEST(print_time, ZeroOffset) {
     46   char* buf = NULL;
     47   size_t size = 0;
     48   FILE* out = open_memstream(&buf, &size);
     49   ASSERT_NE(NULL, out);
     50 
     51   /* Test time: 2023-12-08 10:30:00 UTC */
     52   time_t test_time = 1702031400;
     53   int timezone_offset = 0; /* UTC */
     54 
     55   print_time(out, test_time, timezone_offset);
     56   fclose(out);
     57 
     58   EXPECT_STREQ("Fri,  8 Dec 2023 10:30:00 +0000", buf);
     59 
     60   free(buf);
     61 }
     62 
     63 UTEST(print_time_z, Basic) {
     64   char* buf = NULL;
     65   size_t size = 0;
     66   FILE* out = open_memstream(&buf, &size);
     67   ASSERT_NE(NULL, out);
     68 
     69   /* Test time: 2023-12-08 10:30:00 UTC */
     70   time_t test_time = 1702031400;
     71 
     72   print_time_z(out, test_time);
     73   fclose(out);
     74 
     75   EXPECT_STREQ("2023-12-08T10:30:00Z", buf);
     76 
     77   free(buf);
     78 }
     79 
     80 UTEST(print_time_short, Basic) {
     81   char* buf = NULL;
     82   size_t size = 0;
     83   FILE* out = open_memstream(&buf, &size);
     84   ASSERT_NE(NULL, out);
     85 
     86   /* Test time: 2023-12-08 10:30:00 UTC */
     87   time_t test_time = 1702031400;
     88 
     89   print_time_short(out, test_time);
     90   fclose(out);
     91 
     92   EXPECT_STREQ("2023-12-08 10:30", buf);
     93 
     94   free(buf);
     95 }
     96 
     97 UTEST(print_percent_encoded, NoEncoding) {
     98   char* buf = NULL;
     99   size_t size = 0;
    100   FILE* out = open_memstream(&buf, &size);
    101   ASSERT_NE(NULL, out);
    102 
    103   print_percent_encoded(out, "abcdef-1234/.,");
    104   fclose(out);
    105 
    106   EXPECT_STREQ("abcdef-1234/.,", buf);
    107 
    108   free(buf);
    109 }
    110 
    111 UTEST(print_percent_encoded, SimpleEncoding) {
    112   char* buf = NULL;
    113   size_t size = 0;
    114   FILE* out = open_memstream(&buf, &size);
    115   ASSERT_NE(NULL, out);
    116 
    117   print_percent_encoded(out, "hello world");
    118   fclose(out);
    119 
    120   EXPECT_STREQ("hello%20world", buf);
    121 
    122   free(buf);
    123 }
    124 
    125 UTEST(print_percent_encoded, ReservedChars) {
    126   char* buf = NULL;
    127   size_t size = 0;
    128   FILE* out = open_memstream(&buf, &size);
    129   ASSERT_NE(NULL, out);
    130 
    131   print_percent_encoded(out, " \"%[]:?@");
    132   fclose(out);
    133 
    134   EXPECT_STREQ("%20%22%25%5B%5D%3A%3F%40", buf);
    135 
    136   free(buf);
    137 }
    138 
    139 UTEST(print_percent_encoded, EmptyString) {
    140   char* buf = NULL;
    141   size_t size = 0;
    142   FILE* out = open_memstream(&buf, &size);
    143   ASSERT_NE(NULL, out);
    144 
    145   print_percent_encoded(out, "");
    146   fclose(out);
    147 
    148   EXPECT_STREQ("", buf);
    149 
    150   free(buf);
    151 }
    152 
    153 UTEST(print_xml_encoded_len, FullEncoding) {
    154   char* buf = NULL;
    155   size_t size = 0;
    156   FILE* out = open_memstream(&buf, &size);
    157   ASSERT_NE(NULL, out);
    158 
    159   const char* test_str = "<tag attr='value'> & \"text\"";
    160   print_xml_encoded_len(out, test_str, -1, true);
    161   fclose(out);
    162 
    163   EXPECT_STREQ("&lt;tag attr=&#39;value&#39;&gt; &amp; &quot;text&quot;", buf);
    164 
    165   free(buf);
    166 }
    167 
    168 UTEST(print_xml_encoded_len, PartialEncoding) {
    169   char* buf = NULL;
    170   size_t size = 0;
    171   FILE* out = open_memstream(&buf, &size);
    172   ASSERT_NE(NULL, out);
    173 
    174   const char* test_str = "<tag>hello</tag>";
    175   print_xml_encoded_len(out, test_str, 5, true);
    176   fclose(out);
    177 
    178   EXPECT_STREQ("&lt;tag&gt;", buf);
    179 
    180   free(buf);
    181 }
    182 
    183 UTEST(print_xml_encoded_len, WithCrlf) {
    184   char* buf = NULL;
    185   size_t size = 0;
    186   FILE* out = open_memstream(&buf, &size);
    187   ASSERT_NE(NULL, out);
    188 
    189   const char* test_str = "line1\r\nline2";
    190   print_xml_encoded_len(out, test_str, -1, true);
    191   fclose(out);
    192 
    193   EXPECT_STREQ("line1\r\nline2", buf);
    194 
    195   free(buf);
    196 }
    197 
    198 UTEST(print_xml_encoded_len, WithoutCrlf) {
    199   char* buf = NULL;
    200   size_t size = 0;
    201   FILE* out = open_memstream(&buf, &size);
    202   ASSERT_NE(NULL, out);
    203 
    204   const char* test_str = "line1\r\nline2";
    205   print_xml_encoded_len(out, test_str, -1, false);
    206   fclose(out);
    207 
    208   EXPECT_STREQ("line1line2", buf);
    209 
    210   free(buf);
    211 }
    212 
    213 UTEST(print_xml_encoded_len, EmptyString) {
    214   char* buf = NULL;
    215   size_t size = 0;
    216   FILE* out = open_memstream(&buf, &size);
    217   ASSERT_NE(NULL, out);
    218 
    219   print_xml_encoded_len(out, "", -1, true);
    220   fclose(out);
    221 
    222   EXPECT_STREQ("", buf);
    223 
    224   free(buf);
    225 }
    226 
    227 UTEST(print_xml_encoded_len, ZeroLength) {
    228   char* buf = NULL;
    229   size_t size = 0;
    230   FILE* out = open_memstream(&buf, &size);
    231   ASSERT_NE(NULL, out);
    232 
    233   print_xml_encoded_len(out, "some text", 0, true);
    234   fclose(out);
    235 
    236   EXPECT_STREQ("", buf);
    237 
    238   free(buf);
    239 }
    240 
    241 UTEST(print_gopher_text_len, BasicWithLf) {
    242   char* buf = NULL;
    243   size_t size = 0;
    244   FILE* out = open_memstream(&buf, &size);
    245   ASSERT_NE(NULL, out);
    246 
    247   print_gopher_text_len(out, "hello\nworld", -1, true);
    248   fclose(out);
    249 
    250   EXPECT_STREQ("hello\nworld", buf);
    251 
    252   free(buf);
    253 }
    254 
    255 UTEST(print_gopher_text_len, BasicNoLf) {
    256   char* buf = NULL;
    257   size_t size = 0;
    258   FILE* out = open_memstream(&buf, &size);
    259   ASSERT_NE(NULL, out);
    260 
    261   print_gopher_text_len(out, "hello\nworld\r", -1, false);
    262   fclose(out);
    263 
    264   EXPECT_STREQ("helloworld", buf);
    265 
    266   free(buf);
    267 }
    268 
    269 UTEST(print_gopher_text_len, EscapesWithLf) {
    270   char* buf = NULL;
    271   size_t size = 0;
    272   FILE* out = open_memstream(&buf, &size);
    273   ASSERT_NE(NULL, out);
    274 
    275   print_gopher_text_len(out, "[hello]\n[world]", -1, true);
    276   fclose(out);
    277 
    278   EXPECT_STREQ("[|hello]\n[|world]", buf);
    279 
    280   free(buf);
    281 }
    282 
    283 UTEST(print_gopher_text_len, DoesNotEscapeIfBracketNotImmediatelyAfterLf) {
    284   char* buf = NULL;
    285   size_t size = 0;
    286   FILE* out = open_memstream(&buf, &size);
    287   ASSERT_NE(NULL, out);
    288 
    289   print_gopher_text_len(out, "[hello]\n\t[world]", -1, true);
    290   fclose(out);
    291 
    292   EXPECT_STREQ("[|hello]\n        [world]", buf);
    293 
    294   free(buf);
    295 }
    296 
    297 UTEST(print_gopher_text_len, NoEscapesWithoutLf) {
    298   char* buf = NULL;
    299   size_t size = 0;
    300   FILE* out = open_memstream(&buf, &size);
    301   ASSERT_NE(NULL, out);
    302 
    303   print_gopher_text_len(out, "[hello]\n\t[world]", -1, false);
    304   fclose(out);
    305 
    306   EXPECT_STREQ("[hello]        [world]", buf);
    307 
    308   free(buf);
    309 }
    310 
    311 UTEST(print_gopher_text_len, PartialLen) {
    312   char* buf = NULL;
    313   size_t size = 0;
    314   FILE* out = open_memstream(&buf, &size);
    315   ASSERT_NE(NULL, out);
    316 
    317   print_gopher_text_len(out, "[hello]\n\t[world]", 8, true);
    318   fclose(out);
    319 
    320   EXPECT_STREQ("[|hello]\n", buf);
    321 
    322   free(buf);
    323 }
    324 
    325 UTEST(print_gopher_text_len, EmptyString) {
    326   char* buf = NULL;
    327   size_t size = 0;
    328   FILE* out = open_memstream(&buf, &size);
    329   ASSERT_NE(NULL, out);
    330 
    331   print_gopher_text_len(out, "", -1, true);
    332   fclose(out);
    333 
    334   EXPECT_STREQ("", buf);
    335 
    336   free(buf);
    337 }
    338 
    339 UTEST(print_gopher_link, SpecialChars) {
    340   char* buf = NULL;
    341   size_t size = 0;
    342   FILE* out = open_memstream(&buf, &size);
    343   ASSERT_NE(NULL, out);
    344 
    345   print_gopher_link(out, "|\t\r\n");
    346   fclose(out);
    347 
    348   EXPECT_STREQ("\\|        ", buf);
    349 
    350   free(buf);
    351 }
    352 
    353 UTEST(print_gopher_link, MixedContent) {
    354   char* buf = NULL;
    355   size_t size = 0;
    356   FILE* out = open_memstream(&buf, &size);
    357   ASSERT_NE(NULL, out);
    358 
    359   print_gopher_link(out, "hello|world\t");
    360   fclose(out);
    361 
    362   EXPECT_STREQ("hello\\|world        ", buf);
    363 
    364   free(buf);
    365 }
    366 
    367 UTEST(print_gopher_link, EmptyString) {
    368   char* buf = NULL;
    369   size_t size = 0;
    370   FILE* out = open_memstream(&buf, &size);
    371   ASSERT_NE(NULL, out);
    372 
    373   print_gopher_link(out, "");
    374   fclose(out);
    375 
    376   EXPECT_STREQ("", buf);
    377 
    378   free(buf);
    379 }
    380 
    381 UTEST(print_gopher_link_padded, BasicPadding) {
    382   char* buf = NULL;
    383   size_t size = 0;
    384   FILE* out = open_memstream(&buf, &size);
    385   ASSERT_NE(NULL, out);
    386 
    387   print_gopher_link_padded(out, "hello", 10, ' ');
    388   fclose(out);
    389 
    390   EXPECT_STREQ("hello     ", buf);
    391 
    392   free(buf);
    393 }
    394 
    395 UTEST(print_gopher_link_padded, NoPadding) {
    396   char* buf = NULL;
    397   size_t size = 0;
    398   FILE* out = open_memstream(&buf, &size);
    399   ASSERT_NE(NULL, out);
    400 
    401   print_gopher_link_padded(out, "hello", 5, ' ');
    402   fclose(out);
    403 
    404   EXPECT_STREQ("hello", buf);
    405 
    406   free(buf);
    407 }
    408 
    409 UTEST(print_gopher_link_padded, Truncation) {
    410   char* buf = NULL;
    411   size_t size = 0;
    412   FILE* out = open_memstream(&buf, &size);
    413   ASSERT_NE(NULL, out);
    414 
    415   print_gopher_link_padded(out, "hello world", 10, ' ');
    416   fclose(out);
    417 
    418   EXPECT_STREQ("hello wor…", buf);
    419 
    420   free(buf);
    421 }
    422 
    423 UTEST(print_gopher_link_padded, SpecialChars) {
    424   char* buf = NULL;
    425   size_t size = 0;
    426   FILE* out = open_memstream(&buf, &size);
    427   ASSERT_NE(NULL, out);
    428 
    429   print_gopher_link_padded(out, "a|\t\r\nb", 20, '.');
    430   fclose(out);
    431 
    432   EXPECT_STREQ("a\\|        b.........", buf);
    433 
    434   free(buf);
    435 }
    436 
    437 UTEST(print_gopher_link_padded, MultiByte) {
    438   char* buf = NULL;
    439   size_t size = 0;
    440   FILE* out = open_memstream(&buf, &size);
    441   ASSERT_NE(NULL, out);
    442 
    443   print_gopher_link_padded(out, "こんにちは", 10, ' ');
    444   fclose(out);
    445 
    446   EXPECT_STREQ("こんにちは     ", buf);
    447 
    448   free(buf);
    449 }
    450 
    451 UTEST(print_gopher_link_padded, ZeroWidth) {
    452   char* buf = NULL;
    453   size_t size = 0;
    454   FILE* out = open_memstream(&buf, &size);
    455   ASSERT_NE(NULL, out);
    456 
    457   print_gopher_link_padded(out, "hello", 0, ' ');
    458   fclose(out);
    459 
    460   EXPECT_STREQ("", buf);
    461 
    462   free(buf);
    463 }
    464 
    465 UTEST(print_gopher_link_padded, NoPadChar) {
    466   char* buf = NULL;
    467   size_t size = 0;
    468   FILE* out = open_memstream(&buf, &size);
    469   ASSERT_NE(NULL, out);
    470 
    471   print_gopher_link_padded(out, "hello", 10, '\0');
    472   fclose(out);
    473 
    474   EXPECT_STREQ("hello", buf);
    475 
    476   free(buf);
    477 }
    478 
    479 UTEST(print_gopher_link_padded, ComplexEmoji) {
    480   char* buf = NULL;
    481   size_t size = 0;
    482   FILE* out = open_memstream(&buf, &size);
    483   ASSERT_NE(NULL, out);
    484 
    485   /* UTF-8 encoding of Unicode extended grapheme cluster for woman health worker
    486      with medium-dark skin tone. This emoji is a Unicode emoji ZWJ sequence
    487      composed of 5 code points. */
    488   const char* emoji =     // 👩🏾‍⚕️
    489       "\xF0\x9F\x91\xA9"  // U+1F469: Woman 👩
    490       "\xF0\x9F\x8F\xBE"  // U+1F3FE: Emoji modifier Fitzpatrick type 5 🏾
    491       "\xE2\x80\x8D"      // U+200D: Zero-width joiner
    492       "\xE2\x9A\x95"      // U+2695: Staff of Aesculapius ⚕
    493       "\xEF\xB8\x8F";     // U+FE0F: Variation selector 16 (colour emoji)
    494   print_gopher_link_padded(out, emoji, 10, ' ');
    495   fclose(out);
    496 
    497   /* Expect 1 emoji, 9 spaces. */
    498   EXPECT_STREQ("👩🏾‍⚕️         ", buf);
    499 
    500   free(buf);
    501 }