LCOV - code coverage report
Current view: top level - src/plugins/date - testmod_date.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 58 58 100.0 %
Date: 2019-09-12 12:28:41 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for date plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #ifdef __GNU_LIBRARY__
      11             : #include <features.h>
      12             : #endif
      13             : 
      14             : #include <locale.h>
      15             : #include <stdlib.h>
      16             : #include <string.h>
      17             : 
      18             : #include <kdbconfig.h>
      19             : 
      20             : #include <tests_plugin.h>
      21             : 
      22             : 
      23           6 : static void testFmt (const char * date, const char * fmt, const short res)
      24             : {
      25           6 :         Key * parentKey = keyNew ("user/tests/date", KEY_VALUE, "", KEY_END);
      26           6 :         KeySet * ks = ksNew (5,
      27             :                              keyNew ("user/tests/date/test", KEY_VALUE, date, KEY_META, "check/date", "POSIX", KEY_META,
      28             :                                      "check/date/format", fmt, KEY_END),
      29             :                              KS_END);
      30           6 :         KeySet * conf = ksNew (0, KS_END);
      31           6 :         PLUGIN_OPEN ("date");
      32           6 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) == res, "validation failed");
      33           6 :         ksDel (ks);
      34           6 :         keyDel (parentKey);
      35           6 :         PLUGIN_CLOSE ();
      36           6 : }
      37             : 
      38          18 : static void testIso (const char * date, const char * isoString, const short res)
      39             : {
      40          18 :         Key * parentKey = keyNew ("user/tests/date", KEY_VALUE, "", KEY_END);
      41          18 :         KeySet * ks = ksNew (5,
      42             :                              keyNew ("user/tests/date/test", KEY_VALUE, date, KEY_META, "check/date", "ISO8601", KEY_META,
      43             :                                      "check/date/format", isoString, KEY_END),
      44             :                              KS_END);
      45          18 :         KeySet * conf = ksNew (0, KS_END);
      46          18 :         PLUGIN_OPEN ("date");
      47          18 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) == res, "validation failed");
      48          18 :         ksDel (ks);
      49          18 :         keyDel (parentKey);
      50          18 :         PLUGIN_CLOSE ();
      51          18 : }
      52             : 
      53          10 : static void testRfc2822 (const char * date, const short res)
      54             : {
      55          10 :         Key * parentKey = keyNew ("user/tests/date", KEY_VALUE, "", KEY_END);
      56          10 :         KeySet * ks = ksNew (5,
      57             :                              keyNew ("user/tests/date/test", KEY_VALUE, date, KEY_META, "check/date", "RFC2822", KEY_META,
      58             :                                      "check/date/format", "", KEY_END),
      59             :                              KS_END);
      60          10 :         KeySet * conf = ksNew (0, KS_END);
      61          10 :         PLUGIN_OPEN ("date");
      62          10 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) == res, "validation failed");
      63          10 :         ksDel (ks);
      64          10 :         keyDel (parentKey);
      65          10 :         PLUGIN_CLOSE ();
      66          10 : }
      67             : 
      68           2 : int main (int argc, char ** argv)
      69             : {
      70           2 :         printf ("DATE     TESTS\n");
      71           2 :         printf ("==================\n\n");
      72           2 :         const char * old_locale = setlocale (LC_ALL, NULL);
      73             : 
      74           2 :         init (argc, argv);
      75             : 
      76           2 :         testFmt ("20:15:00", "%H:%M:%S", 1);
      77           2 :         testFmt ("20:15:00", "%I:%M:%S", -1);
      78             : #ifdef __GNU_LIBRARY__
      79           2 :         setlocale (LC_ALL, "C");
      80           2 :         testFmt ("Sat 17 Dec 2016 08:07:43 PM CET", "%a %d %b %Y %r %Z", 1);
      81           2 :         setlocale (LC_ALL, old_locale);
      82             : #else
      83             :         testFmt ("Sat 17 Dec 2016 08:07:43 PM", "%a %d %b %Y %r", 1);
      84             : #endif
      85             : 
      86             : #ifdef __GNU_LIBRARY__
      87           2 :         testIso ("2016-12-12T23:59:01Z", "datetime complete", 1);
      88           2 :         testIso ("2016-12-12 23:59:01Z", "datetime complete noT", 1);
      89           2 :         testIso ("2016-12-12T23:59:01Z", "datetime truncated", -1);
      90           2 :         testIso ("-12-12T23:59:01Z", "datetime truncated", 1);
      91           2 :         testIso ("22:30+04", "utc extended", 1);
      92           2 :         testIso ("22:30-04", "utc extended", 1);
      93           2 :         testIso ("2016-W23", "weekdate", 1);
      94             : #else
      95             :         testIso ("2016-12-12T23:59:01", "datetime complete", 1);
      96             :         testIso ("2016-12-12T23:59:01", "datetime truncated", -1);
      97             :         testIso ("-12-12T23:59:01", "datetime truncated", 1);
      98             : #endif
      99           2 :         testIso ("2230", "timeofday extended", -1);
     100           2 :         testIso ("2230", "timeofday basic", 1);
     101             : 
     102           2 :         setlocale (LC_ALL, "C");
     103           2 :         testRfc2822 ("Sat, 01 Mar 2016 23:59:01 +0400", 1);
     104           2 :         testRfc2822 ("01 Mar 2016 23:59:01 -0400", 1);
     105           2 :         testRfc2822 ("Sat, Mar 01 2016 23:59:01 +0400", -1);
     106           2 :         testRfc2822 ("01 Mar 2016 23:59 +0400", 1);
     107           2 :         testRfc2822 ("01 Mar 2016 01:00:59", -1);
     108           2 :         setlocale (LC_ALL, old_locale);
     109             : 
     110           2 :         print_result ("testmod_date");
     111             : 
     112           2 :         return nbError;
     113             : }

Generated by: LCOV version 1.13