139 lines
3.7 KiB
C
139 lines
3.7 KiB
C
#include "../src/argumentc/argumentc.h"
|
|
#include <assert.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
void test_long()
|
|
{
|
|
int argc = 3;
|
|
const char* argv[] = {"--hello", "--world", "--!"};
|
|
Argumentc argumentc;
|
|
assert(Argumentc_Create(&argumentc, argc, argv) == EXIT_SUCCESS);
|
|
|
|
assert(Argumentc_PopLongOption(&argumentc, StringView_FromString("hello")).type != OPTIONTYPE_NONE);
|
|
assert(Argumentc_PopLongOption(&argumentc, StringView_FromString("!")).type != OPTIONTYPE_NONE);
|
|
assert(Argumentc_PopLongOption(&argumentc, StringView_FromString("world")).type != OPTIONTYPE_NONE);
|
|
|
|
assert(Argumentc_HaveNextOption(&argumentc) == false);
|
|
|
|
Argumentc_Destroy(&argumentc);
|
|
|
|
return;
|
|
}
|
|
|
|
void test_short()
|
|
{
|
|
int argc = 2;
|
|
const char* argv[] = {"-abv", "-vv"};
|
|
Argumentc argumentc;
|
|
assert(Argumentc_Create(&argumentc, argc, argv) == EXIT_SUCCESS);
|
|
|
|
assert(Argumentc_PopShortOption(&argumentc, StringView_FromString("a")).type != OPTIONTYPE_NONE);
|
|
assert(Argumentc_PopShortOption(&argumentc, StringView_FromString("b")).type != OPTIONTYPE_NONE);
|
|
assert(Argumentc_PopShortOption(&argumentc, StringView_FromString("v")).type != OPTIONTYPE_NONE);
|
|
assert(Argumentc_PopShortOption(&argumentc, StringView_FromString("v")).type != OPTIONTYPE_NONE);
|
|
assert(Argumentc_PopShortOption(&argumentc, StringView_FromString("v")).type != OPTIONTYPE_NONE);
|
|
|
|
assert(Argumentc_HaveNextOption(&argumentc) == false);
|
|
|
|
Argumentc_Destroy(&argumentc);
|
|
|
|
return;
|
|
}
|
|
|
|
void test_long_arg()
|
|
{
|
|
int argc = 4;
|
|
const char* argv[] = {"--long", "argument", "--longer", "argumenter"};
|
|
Argumentc argumentc;
|
|
assert(Argumentc_Create(&argumentc, argc, argv) == EXIT_SUCCESS);
|
|
|
|
assert(
|
|
Argumentc_PopLongArgument(
|
|
&argumentc,
|
|
StringView_FromString("longer")
|
|
).argument.type
|
|
!=
|
|
OPTIONTYPE_NONE
|
|
);
|
|
assert(
|
|
Argumentc_PopLongArgument(
|
|
&argumentc,
|
|
StringView_FromString("long")
|
|
).argument.type
|
|
!=
|
|
OPTIONTYPE_NONE
|
|
);
|
|
|
|
assert(Argumentc_HaveNextOption(&argumentc) == false);
|
|
|
|
Argumentc_Destroy(&argumentc);
|
|
|
|
return;
|
|
}
|
|
|
|
void test_short_arg()
|
|
{
|
|
int argc = 4;
|
|
const char* argv[] = {"-ab", "argument", "-s", "15"};
|
|
Argumentc argumentc;
|
|
assert(Argumentc_Create(&argumentc, argc, argv) == EXIT_SUCCESS);
|
|
|
|
assert(
|
|
Argumentc_PopShortArgument(
|
|
&argumentc,
|
|
StringView_FromString("b")
|
|
).argument.type
|
|
!=
|
|
OPTIONTYPE_NONE
|
|
);
|
|
assert(
|
|
Argumentc_PopShortArgument(
|
|
&argumentc,
|
|
StringView_FromString("s")
|
|
).argument.type
|
|
!=
|
|
OPTIONTYPE_NONE
|
|
);
|
|
assert(Argumentc_PopShortOption(&argumentc, StringView_FromString("a")).type != OPTIONTYPE_NONE);
|
|
|
|
assert(Argumentc_HaveNextOption(&argumentc) == false);
|
|
|
|
Argumentc_Destroy(&argumentc);
|
|
|
|
return;
|
|
}
|
|
|
|
int main(int argc, char const *argv[]) {
|
|
Argumentc argumentc;
|
|
if (Argumentc_Create(&argumentc, argc, argv)) {
|
|
fprintf(stderr, "Failed to parse options for memory reasons");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
for (size_t i = 0; i < DynamicArray_GetLength(&argumentc.array); i++) {
|
|
Option* option = DynamicArray_GetPointer(&argumentc.array, i);
|
|
char c[option->content.length + 1];
|
|
memset(c, 0, option->content.length + 1);
|
|
memcpy(c, option->content.source, option->content.length);
|
|
printf("%-20s: %s\n",
|
|
OptionType_ToString(option->type),
|
|
c);
|
|
}
|
|
|
|
if (Argumentc_PopLongOption(&argumentc, StringView_FromString("long")).type != OPTIONTYPE_NONE) {
|
|
test_long();
|
|
}
|
|
if (Argumentc_PopLongOption(&argumentc, StringView_FromString("short")).type != OPTIONTYPE_NONE) {
|
|
test_short();
|
|
}
|
|
if (Argumentc_PopLongOption(&argumentc, StringView_FromString("long-arg")).type != OPTIONTYPE_NONE) {
|
|
test_long_arg();
|
|
}
|
|
if (Argumentc_PopLongOption(&argumentc, StringView_FromString("short-arg")).type != OPTIONTYPE_NONE) {
|
|
test_short_arg();
|
|
}
|
|
|
|
Argumentc_Destroy(&argumentc);
|
|
}
|