feat: v0.2.0
This commit is contained in:
34
tests/CMakeLists.txt
Normal file
34
tests/CMakeLists.txt
Normal file
@@ -0,0 +1,34 @@
|
||||
project(CArgParseTest_)
|
||||
|
||||
#单命令的单一选项多值测试
|
||||
add_executable(${PROJECT_NAME}single_arg test_single_arg.c)
|
||||
target_link_libraries(${PROJECT_NAME}single_arg CArgParse)
|
||||
add_test(${PROJECT_NAME}single_arg ${PROJECT_NAME}single_arg install -p testpackge1 testpackge2 testpackge3)
|
||||
|
||||
# 单命令的多选项测试
|
||||
add_executable(${PROJECT_NAME}mult_arg test_mult_arg.c)
|
||||
target_link_libraries(${PROJECT_NAME}mult_arg CArgParse)
|
||||
add_test(${PROJECT_NAME}mult_arg ${PROJECT_NAME}mult_arg install -p testpackge1 testpackge2 testpackge3 -i www.test.com -f file1.txt file2.txt)
|
||||
|
||||
# 全局选项测试
|
||||
add_executable(${PROJECT_NAME}global_arg test_global_arg.c)
|
||||
target_link_libraries(${PROJECT_NAME}global_arg CArgParse)
|
||||
add_test(${PROJECT_NAME}global_arg ${PROJECT_NAME}global_arg -v install -p testpackge1 testpackge2 testpackge3 -q)
|
||||
|
||||
# 子命令测试
|
||||
add_executable(${PROJECT_NAME}subcommand test_subcommand.c)
|
||||
target_link_libraries(${PROJECT_NAME}subcommand CArgParse)
|
||||
add_test(${PROJECT_NAME}subcommand ${PROJECT_NAME}subcommand -v install tools -t tool1 tool2)
|
||||
|
||||
# 未知命令测试
|
||||
add_executable(${PROJECT_NAME}unknow_command test_unknow_command.c)
|
||||
target_link_libraries(${PROJECT_NAME}unknow_command CArgParse)
|
||||
add_test(${PROJECT_NAME}unknow_command ${PROJECT_NAME}unknow_command unknow)
|
||||
set_tests_properties(${PROJECT_NAME}unknow_command PROPERTIES WILL_FAIL TRUE)
|
||||
set_tests_properties(${PROJECT_NAME}unknow_command PROPERTIES FAIL_REGULAR_EXPRESSION "ERROR: Last command is not found")
|
||||
|
||||
# 未知命令选项测试,预期打印该命令的帮助信息
|
||||
add_executable(${PROJECT_NAME}unknow_command_arg test_unknow_command.c)
|
||||
target_link_libraries(${PROJECT_NAME}unknow_command_arg CArgParse)
|
||||
add_test(${PROJECT_NAME}unknow_command_arg ${PROJECT_NAME}unknow_command_arg install --unknow)
|
||||
set_tests_properties(${PROJECT_NAME}unknow_command_arg PROPERTIES WILL_FAIL TRUE)
|
||||
91
tests/initArgParse.h
Normal file
91
tests/initArgParse.h
Normal file
@@ -0,0 +1,91 @@
|
||||
#include "ArgParse.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
ArgParse *initArgParse() {
|
||||
ArgParse *argparse = argParseInit("测试程序");
|
||||
Command *command = NULL;
|
||||
Command *sub_command = NULL;
|
||||
|
||||
// add global arguments
|
||||
argParseAddGlobalArg(argparse,
|
||||
"-v",
|
||||
"--version",
|
||||
"Show version",
|
||||
NULL,
|
||||
NULL,
|
||||
false,
|
||||
NOVALUE);
|
||||
argParseAddGlobalArg(
|
||||
argparse, "-q", "--quiet", "Quiet mode", NULL, NULL, false, NOVALUE);
|
||||
|
||||
// add arguments
|
||||
command = argParseAddCommand(
|
||||
argparse, "install", "Install the package", NULL, NULL, NULL, NOVALUE);
|
||||
argParseAddArg(command,
|
||||
"-i",
|
||||
"--index",
|
||||
"Index URL",
|
||||
"https://example.com",
|
||||
NULL,
|
||||
false,
|
||||
SINGLEVALUE);
|
||||
argParseAddArg(command,
|
||||
"-f",
|
||||
"--file",
|
||||
"Package file",
|
||||
"package.json",
|
||||
NULL,
|
||||
false,
|
||||
MULTIVALUE);
|
||||
argParseAddArg(command,
|
||||
"-p",
|
||||
"--package",
|
||||
"Package file",
|
||||
"package.json",
|
||||
NULL,
|
||||
false,
|
||||
MULTIVALUE);
|
||||
|
||||
sub_command = argParseAddSubCommand(
|
||||
command, "tools", "Install tools", NULL, NULL, NULL, MULTIVALUE);
|
||||
|
||||
argParseAddArg(sub_command,
|
||||
"-t",
|
||||
"--tool",
|
||||
"Tool name",
|
||||
"Tool name",
|
||||
NULL,
|
||||
true,
|
||||
MULTIVALUE);
|
||||
sub_command = argParseAddSubCommand(
|
||||
command, "tools_sub", "Install tools", NULL, NULL, NULL, MULTIVALUE);
|
||||
|
||||
argParseAddArg(sub_command,
|
||||
"-s",
|
||||
"--source",
|
||||
"test_source",
|
||||
"tools subcommand test",
|
||||
NULL,
|
||||
true,
|
||||
MULTIVALUE);
|
||||
|
||||
command = argParseAddCommand(argparse,
|
||||
"uninstall",
|
||||
"Uninstall the package",
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
SINGLEVALUE);
|
||||
argParseAddArg(command,
|
||||
"-p",
|
||||
"--package",
|
||||
"Package name",
|
||||
"Package name",
|
||||
NULL,
|
||||
true,
|
||||
MULTIVALUE);
|
||||
|
||||
return argparse;
|
||||
}
|
||||
39
tests/test_global_arg.c
Normal file
39
tests/test_global_arg.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "ArgParse.h"
|
||||
#include "initArgParse.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ArgParse *argparse = initArgParse();
|
||||
|
||||
argParseParse(argparse, argc, argv);
|
||||
|
||||
char *val = argParseGetGlobalArg(argparse, "-v");
|
||||
|
||||
if (argParseCheckGlobalTriggered(argparse, "-v")) {
|
||||
printf("Global arg -v triggered\n");
|
||||
}
|
||||
|
||||
if (val != NULL) {
|
||||
printf("Global arg -v: %s\n", val);
|
||||
} else {
|
||||
printf("Global arg -v is NULL\n");
|
||||
}
|
||||
|
||||
char *val_q = argParseGetGlobalArg(argparse, "-q");
|
||||
|
||||
if (argParseCheckGlobalTriggered(argparse, "-q")) {
|
||||
printf("Global arg -q triggered\n");
|
||||
}
|
||||
|
||||
if (val != NULL) {
|
||||
printf("Global arg -q: %s\n", val);
|
||||
} else {
|
||||
printf("Global arg -q is NULL\n");
|
||||
}
|
||||
|
||||
argParseFree(argparse);
|
||||
|
||||
return 0;
|
||||
}
|
||||
45
tests/test_mult_arg.c
Normal file
45
tests/test_mult_arg.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "ArgParse.h"
|
||||
#include "initArgParse.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ArgParse *argparse = initArgParse();
|
||||
|
||||
argParseParse(argparse, argc, argv);
|
||||
|
||||
const char *testv[3] = {"testpackge1", "testpackge2", "testpackge3"};
|
||||
|
||||
|
||||
// Test -p
|
||||
char *val = argParseGetCurArg(argparse, "-p");
|
||||
|
||||
int len = 0;
|
||||
char **vals = argParseGetCurArgList(argparse, "-p", &len);
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
printf("-p Value: %s\n", vals[i]);
|
||||
assert(strcmp(vals[i], testv[i]) == 0);
|
||||
}
|
||||
|
||||
// Test -i
|
||||
char *val_i = argParseGetCurArg(argparse, "-i");
|
||||
printf("-i Value: %s\n", val_i);
|
||||
assert(strcmp(val_i, "www.test.com") == 0);
|
||||
|
||||
|
||||
|
||||
// Test -f
|
||||
len = 0;
|
||||
const char *testf[2] = {"file1.txt", "file2.txt"};
|
||||
char **val_f = argParseGetCurArgList(argparse, "-f", &len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
printf("-f Value: %s\n", val_f[i]);
|
||||
assert(strcmp(val_f[i], testf[i]) == 0);
|
||||
}
|
||||
|
||||
|
||||
argParseFree(argparse);
|
||||
|
||||
return 0;
|
||||
}
|
||||
26
tests/test_single_arg.c
Normal file
26
tests/test_single_arg.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "ArgParse.h"
|
||||
#include "initArgParse.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ArgParse *argparse = initArgParse();
|
||||
|
||||
argParseParse(argparse, argc, argv);
|
||||
|
||||
const char *testv[3] = {"testpackge1", "testpackge2", "testpackge3"};
|
||||
|
||||
char *val = argParseGetCurArg(argparse, "-p");
|
||||
|
||||
int len = 0;
|
||||
char **vals = argParseGetCurArgList(argparse, "-p", &len);
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
printf("-p Value: %s\n", vals[i]);
|
||||
assert(strcmp(vals[i], testv[i]) == 0);
|
||||
}
|
||||
|
||||
argParseFree(argparse);
|
||||
|
||||
return 0;
|
||||
}
|
||||
22
tests/test_subcommand.c
Normal file
22
tests/test_subcommand.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "ArgParse.h"
|
||||
#include "initArgParse.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ArgParse *argparse = initArgParse();
|
||||
|
||||
argParseParse(argparse, argc, argv);
|
||||
|
||||
|
||||
char *command_name = argParseGetCurCommandName(argparse);
|
||||
printf("command name: %s\n", command_name);
|
||||
assert(strcmp(command_name, "tools") == 0);
|
||||
|
||||
char * val = argParseGetCurArg(argparse, "-t");
|
||||
printf("tools -t: %s\n", val);
|
||||
|
||||
argParseFree(argparse);
|
||||
|
||||
return 0;
|
||||
}
|
||||
14
tests/test_unknow_command.c
Normal file
14
tests/test_unknow_command.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "ArgParse.h"
|
||||
#include "initArgParse.h"
|
||||
#include <assert.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ArgParse *argparse = initArgParse();
|
||||
|
||||
argParseParse(argparse, argc, argv);
|
||||
|
||||
|
||||
argParseFree(argparse);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user