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

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <stdlib.h>
      10             : 
      11             : #ifdef HAVE_KDBCONFIG_H
      12             : #include "kdbconfig.h"
      13             : #endif
      14             : 
      15             : #include <tests_plugin.h>
      16             : 
      17             : // test simple variable passing
      18           1 : static void test_variable_passing (void)
      19             : {
      20           1 :         printf ("Testing simple variable passing...\n");
      21             : 
      22           1 :         KeySet * conf = ksNew (1, keyNew ("user/script", KEY_VALUE, srcdir_file ("lua/lua_plugin.lua"), KEY_END),
      23             :                                keyNew ("user/print", KEY_END), KS_END);
      24           1 :         PLUGIN_OPEN ("lua");
      25             : 
      26           1 :         Key * parentKey = keyNew ("user/from_c", KEY_END);
      27           1 :         KeySet * ks = ksNew (0, KS_END);
      28           1 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
      29           1 :         succeed_if (ksGetSize (ks) == 1, "keyset size is still 0");
      30           1 :         succeed_if (ksGetSize (ks) == 1 && !strcmp (keyName (ksHead (ks)), "user/from_lua"), "key in keyset has wrong name");
      31           1 :         succeed_if (output_warnings (parentKey), "warnings in kdbOpen");
      32           1 :         succeed_if (output_error (parentKey), "errors in kdbOpen");
      33             : 
      34           1 :         ksDel (ks);
      35           1 :         keyDel (parentKey);
      36             : 
      37           1 :         PLUGIN_CLOSE ();
      38           1 : }
      39             : 
      40             : // test loading lua twice
      41           1 : static void test_two_scripts (void)
      42             : {
      43           1 :         printf ("Testing loading of two active lua plugins...\n");
      44             : 
      45           1 :         KeySet * modules = ksNew (0, KS_END);
      46           1 :         elektraModulesInit (modules, 0);
      47             : 
      48           1 :         KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, srcdir_file ("lua/lua_plugin.lua"), KEY_END),
      49             :                                keyNew ("user/print", KEY_END), KS_END);
      50             : 
      51           1 :         KeySet * conf2 = ksNew (2, keyNew ("user/script", KEY_VALUE, srcdir_file ("lua/lua_plugin2.lua"), KEY_END),
      52             :                                 keyNew ("user/print", KEY_END), KS_END);
      53             : 
      54           1 :         Key * errorKey = keyNew ("", KEY_END);
      55           1 :         Plugin * plugin = elektraPluginOpen ("lua", modules, conf, errorKey);
      56           1 :         succeed_if (output_warnings (errorKey), "warnings in kdbOpen");
      57           1 :         succeed_if (output_error (errorKey), "errors in kdbOpen");
      58           1 :         exit_if_fail (plugin != NULL, "unable to load lua plugin");
      59           1 :         keyDel (errorKey);
      60             : 
      61           1 :         Key * errorKey2 = keyNew ("", KEY_END);
      62           1 :         Plugin * plugin2 = elektraPluginOpen ("lua", modules, conf2, errorKey2);
      63           1 :         succeed_if (output_warnings (errorKey2), "warnings in kdbOpen");
      64           1 :         succeed_if (output_error (errorKey2), "errors in kdbOpen");
      65           1 :         exit_if_fail (plugin2 != NULL, "unable to load lua plugin again");
      66           1 :         keyDel (errorKey2);
      67             : 
      68           1 :         elektraPluginClose (plugin2, 0);
      69           1 :         elektraPluginClose (plugin, 0);
      70           1 :         elektraModulesClose (modules, 0);
      71           1 :         ksDel (modules);
      72           1 : }
      73             : 
      74             : // simple return value test
      75           1 : static void test_fail (void)
      76             : {
      77           1 :         printf ("Testing return values from lua functions...\n");
      78             : 
      79           1 :         KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, srcdir_file ("lua/lua_plugin_fail.lua"), KEY_END),
      80             :                                keyNew ("user/print", KEY_END), KS_END);
      81           1 :         PLUGIN_OPEN ("lua");
      82             : 
      83           1 :         Key * parentKey = keyNew ("user/tests/from_c", KEY_END);
      84           1 :         KeySet * ks = ksNew (0, KS_END);
      85             : 
      86           1 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) == -1, "call to kdbGet didn't fail");
      87           1 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == -1, "call to kdbSet didn't fail");
      88           1 :         succeed_if (plugin->kdbError (plugin, ks, parentKey) == -1, "call to kdbError didn't fail");
      89           1 :         succeed_if (output_warnings (parentKey), "warnings in kdbOpen");
      90           1 :         succeed_if (output_error (parentKey), "errors in kdbOpen");
      91             : 
      92           1 :         ksDel (ks);
      93           1 :         keyDel (parentKey);
      94             : 
      95           1 :         PLUGIN_CLOSE ();
      96           1 : }
      97             : 
      98             : // test script with syntax error
      99           1 : static void test_wrong (void)
     100             : {
     101           1 :         printf ("Testing lua script with syntax error...\n");
     102             : 
     103           1 :         KeySet * modules = ksNew (0, KS_END);
     104           1 :         elektraModulesInit (modules, 0);
     105             : 
     106           1 :         KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, srcdir_file ("lua/lua_plugin_wrong.lua"), KEY_END),
     107             :                                keyNew ("user/print", KEY_END), KS_END);
     108             : 
     109           1 :         Key * errorKey = keyNew ("", KEY_END);
     110           1 :         Plugin * plugin = elektraPluginOpen ("lua", modules, conf, errorKey);
     111           1 :         succeed_if (!output_warnings (errorKey), "we expect some warnings");
     112           1 :         succeed_if (!output_error (errorKey), "we expect some errors");
     113           1 :         succeed_if (plugin == NULL, "lua plugin shouldn't be loadable");
     114           1 :         keyDel (errorKey);
     115             : 
     116           1 :         elektraModulesClose (modules, 0);
     117           1 :         ksDel (modules);
     118           1 : }
     119             : 
     120           1 : int main (int argc, char ** argv)
     121             : {
     122           1 :         printf ("LUA         TESTS\n");
     123           1 :         printf ("==================\n\n");
     124             : 
     125           1 :         init (argc, argv);
     126             : 
     127           1 :         test_variable_passing ();
     128           1 :         test_two_scripts ();
     129             : 
     130           1 :         printf ("\n");
     131           1 :         printf ("========================================================================\n");
     132           1 :         printf ("NOTE: The following errors are intended. We're testing error conditions!\n");
     133           1 :         printf ("========================================================================\n");
     134           1 :         test_fail ();
     135           1 :         test_wrong ();
     136             : 
     137           1 :         print_result ("test_lua");
     138             : 
     139           1 :         return nbError;
     140             : }

Generated by: LCOV version 1.13