arguments: The first test

Test it reports an error when we provide no arguments.
parent 61f83b56
......@@ -10,6 +10,7 @@
/src/opkg-trans/opkg-trans
# Stuff dump core all over the place.
core
vgcore.*
# Test stuff
tests/check-compiled
tests/arguments
......@@ -16,7 +16,7 @@ BINARIES += $(patsubst %,tests/%,$(TESTS))
define DO_C_TEST
BINARIES_NOTARGET += tests/$(1)
$(1)_MODULES += $(1)
$(1)_MODULES += $(1) ctest
$$(patsubst %,$(O)/.objs/tests/%.o,$$($(1)_MODULES)): check-compile
$(1)_SYSTEM_LIBS += check m rt
$(1)_LOCAL_LIBS += updater
......
#include <check.h>
/*
* Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This file is part of NUCI configuration server.
*
* NUCI 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 3 of the License, or
* (at your option) any later version.
*
* NUCI 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 NUCI. If not, see <http://www.gnu.org/licenses/>.
*/
START_TEST(empty_test) {
#include "ctest.h"
#include "../src/lib/arguments.h"
#include <stdlib.h>
#include <stdbool.h>
// A test case, arguments and the expected operations
struct arg_case {
const char *name;
char **args;
struct cmd_op *expected_ops;
};
// Bad arguments passed, give help and give up
static struct cmd_op bad_args_ops[] = { { .type = COT_HELP }, { .type = COT_CRASH } };
static char *no_args[] = { NULL };
static struct arg_case cases[] = {
{
/*
* No arguments → give help and exit.
*/
.name = "No args",
.args = no_args,
.expected_ops = bad_args_ops
}
};
START_TEST(cmd_args_parse_test) {
struct arg_case *c = &cases[_i];
// Count the arguments and provide the 0th command name in a copy
int count = 1;
for (char **arg = c->args; *arg; arg ++)
count ++;
char **args = malloc((count + 1) * sizeof *args);
*args = "opkg-trans";
for (int i = 1; i < count; i ++)
args[i] = c->args[i - 1];
args[count] = NULL;
// Call the tested function
struct cmd_op *ops = cmd_args_parse(count, args);
// They are already parsed, no longer needed
free(args);
// Check the result is the same as expected
struct cmd_op *op = ops;
struct cmd_op *expected = c->expected_ops;
size_t i = 0;
bool terminated = false;
do {
if (expected->type == COT_EXIT || expected->type == COT_CRASH)
terminated = true;
ck_assert_msg(expected->type == op->type, "Types at position %zu does not match: %d vs %d", i, (int) expected->type, (int) op->type);
if (expected->parameter && !op->parameter)
ck_abort_msg("Missing parameter at position %zu", i);
if (!expected->parameter && op->parameter)
ck_abort_msg("Extra parameter at position %zu", i);
if (expected->parameter)
ck_assert_str_eq(expected->parameter, op->parameter);
} while (!terminated);
free(ops);
}
END_TEST
int main(int argc, char *argv[]) {
Suite *gen_test_suite(void) {
Suite *result = suite_create("Command line arguments");
TCase *arguments = tcase_create("Arguments");
tcase_add_loop_test(arguments, cmd_args_parse_test, 0, sizeof cases / sizeof *cases);
suite_add_tcase(result, arguments);
return result;
}
/*
* Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This file is part of NUCI configuration server.
*
* NUCI 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 3 of the License, or
* (at your option) any later version.
*
* NUCI 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 NUCI. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ctest.h"
int main(void) {
Suite *suite = gen_test_suite();
SRunner *runner = srunner_create(suite);
srunner_run_all(runner, CK_NORMAL);
int failed = srunner_ntests_failed(runner);
srunner_free(runner);
return !!failed;
}
/*
* Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This file is part of NUCI configuration server.
*
* NUCI 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 3 of the License, or
* (at your option) any later version.
*
* NUCI 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 NUCI. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* This module provides generic main for the C based tests.
*/
#ifndef C_TEST_H
#define C_TEST_H
#include <check.h>
/*
* Header of the function a test case should provide. It shall
* create a test suite, fill it with tests and pass as a result.
* It will get called by the provided main() function.
*/
Suite *gen_test_suite(void) __attribute__((returns_nonnull));
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment