2021-06-20 00:41:28 +02:00
|
|
|
/*
|
|
|
|
* frrscript unit tests
|
|
|
|
* Copyright (C) 2021 Donald Lee
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; see the file COPYING; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <zebra.h>
|
|
|
|
|
|
|
|
#include "lib/frrscript.h"
|
2021-07-07 15:54:00 +02:00
|
|
|
#include "lib/frrlua.h"
|
2021-06-20 00:41:28 +02:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
frrscript_init("./lib");
|
2021-07-04 17:13:32 +02:00
|
|
|
struct frrscript *fs = frrscript_new("script1");
|
|
|
|
int result;
|
2021-06-20 00:41:28 +02:00
|
|
|
|
|
|
|
long long a = 100, b = 200;
|
|
|
|
|
2021-07-04 17:13:32 +02:00
|
|
|
result = frrscript_load(fs, "foo", NULL);
|
|
|
|
assert(result == 0);
|
|
|
|
result = frrscript_call(fs, "foo", ("a", &a), ("b", &b));
|
2021-06-20 00:41:28 +02:00
|
|
|
assert(result == 0);
|
|
|
|
assert(a == 300);
|
|
|
|
assert(b == 200);
|
|
|
|
|
2021-07-04 23:25:49 +02:00
|
|
|
long long n = 5;
|
|
|
|
|
2021-07-07 15:54:00 +02:00
|
|
|
result = frrscript_load(fs, "fact", NULL);
|
|
|
|
assert(result == 0);
|
2021-07-04 23:25:49 +02:00
|
|
|
result = frrscript_call(fs, "fact", ("n", &n));
|
|
|
|
assert(result == 0);
|
2021-07-07 15:54:00 +02:00
|
|
|
long long *ansptr =
|
|
|
|
frrscript_get_result(fs, "fact", "ans", lua_tointegerp);
|
|
|
|
assert(*ansptr == 120);
|
|
|
|
XFREE(MTYPE_TMP, ansptr);
|
2021-07-04 23:25:49 +02:00
|
|
|
|
|
|
|
/* Function does not exist in script file*/
|
|
|
|
result = frrscript_load(fs, "does_not_exist", NULL);
|
|
|
|
assert(result == 1);
|
|
|
|
|
|
|
|
/* Function does not exist in script file*/
|
2021-07-04 17:13:32 +02:00
|
|
|
result = frrscript_load(fs, "does_not_exist", NULL);
|
|
|
|
assert(result == 1);
|
|
|
|
|
2021-07-04 23:25:49 +02:00
|
|
|
/* Function was not (successfully) loaded */
|
2021-07-04 17:13:32 +02:00
|
|
|
result = frrscript_call(fs, "does_not_exist", ("a", &a), ("b", &b));
|
|
|
|
assert(result == 1);
|
|
|
|
|
2021-07-04 23:25:49 +02:00
|
|
|
/* Function returns void */
|
|
|
|
result = frrscript_call(fs, "bad_return1");
|
|
|
|
assert(result == 1);
|
2021-07-04 17:13:32 +02:00
|
|
|
|
2021-07-04 23:25:49 +02:00
|
|
|
/* Function returns number */
|
|
|
|
result = frrscript_call(fs, "bad_return2");
|
|
|
|
assert(result == 1);
|
|
|
|
|
|
|
|
/* Function throws exception */
|
|
|
|
result = frrscript_call(fs, "bad_return3");
|
|
|
|
assert(result == 1);
|
2021-07-04 17:13:32 +02:00
|
|
|
|
2021-06-20 00:41:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|