| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "gate/tests.h" | ||
| 2 | #include "test_apps.h" | ||
| 3 | #include "gate/applications.h" | ||
| 4 | |||
| 5 | 1 | GATE_TEST_FUNCTION(test_apps_parse_args) | |
| 6 | { | ||
| 7 | static gate_string_t const argset1 = GATE_STRING_INIT_STATIC("arg1 arg2 arg3 arg4 "); | ||
| 8 | static gate_string_t const argset2 = GATE_STRING_INIT_STATIC("arg1 \"arg2 arg3\" arg4"); | ||
| 9 | static gate_string_t const argset3 = GATE_STRING_INIT_STATIC("arg1 \"arg2\\\"arg3\" arg4"); | ||
| 10 | static gate_string_t const argset4 = GATE_STRING_INIT_STATIC("arg1 \"arg2\\\\arg3\" arg4"); | ||
| 11 | static gate_string_t const argset5 = GATE_STRING_INIT_STATIC("arg1 arg2\\arg3 arg4"); | ||
| 12 | |||
| 13 | char buffer[GATE_MAX_COPYBUFFER_LENGTH]; | ||
| 14 | char const* parsed_args[256]; | ||
| 15 | gate_string_t parsed_arg_str[10]; | ||
| 16 | 1 | gate_size_t parsed_args_capacity = sizeof(parsed_args) / sizeof(parsed_args[0]); | |
| 17 | gate_size_t parsed_args_count; | ||
| 18 | |||
| 19 | |||
| 20 | 1 | GATE_TEST_UNIT_BEGIN(test_apps_parse_args); | |
| 21 | |||
| 22 | 1 | parsed_args_count = gate_app_parse_args_buffer(&argset1, buffer, sizeof(buffer), parsed_args, parsed_args_capacity); | |
| 23 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE_EQUAL(parsed_args_count, 4); |
| 24 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[0], "arg1"), 0); |
| 25 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[1], "arg2"), 0); |
| 26 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[2], "arg3"), 0); |
| 27 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[3], "arg4"), 0); |
| 28 | |||
| 29 | 1 | parsed_args_count = gate_app_parse_args(&argset1, &parsed_arg_str[0], sizeof(parsed_arg_str) / sizeof(parsed_arg_str[0])); | |
| 30 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE_EQUAL(parsed_args_count, 4); |
| 31 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_string_equals_str(&parsed_arg_str[0], "arg1"), true); |
| 32 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_string_equals_str(&parsed_arg_str[1], "arg2"), true); |
| 33 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_string_equals_str(&parsed_arg_str[2], "arg3"), true); |
| 34 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_string_equals_str(&parsed_arg_str[3], "arg4"), true); |
| 35 | 1 | gate_string_release(&parsed_arg_str[0]); | |
| 36 | 1 | gate_string_release(&parsed_arg_str[1]); | |
| 37 | 1 | gate_string_release(&parsed_arg_str[2]); | |
| 38 | 1 | gate_string_release(&parsed_arg_str[3]); | |
| 39 | |||
| 40 | 1 | parsed_args_count = gate_app_parse_args_buffer(&argset2, buffer, sizeof(buffer), parsed_args, parsed_args_capacity); | |
| 41 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE_EQUAL(parsed_args_count, 3); |
| 42 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[0], "arg1"), 0); |
| 43 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[1], "arg2 arg3"), 0); |
| 44 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[2], "arg4"), 0); |
| 45 | |||
| 46 | 1 | parsed_args_count = gate_app_parse_args_buffer(&argset3, buffer, sizeof(buffer), parsed_args, parsed_args_capacity); | |
| 47 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE_EQUAL(parsed_args_count, 3); |
| 48 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[0], "arg1"), 0); |
| 49 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[1], "arg2\"arg3"), 0); |
| 50 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[2], "arg4"), 0); |
| 51 | |||
| 52 | 1 | parsed_args_count = gate_app_parse_args_buffer(&argset4, buffer, sizeof(buffer), parsed_args, parsed_args_capacity); | |
| 53 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE_EQUAL(parsed_args_count, 3); |
| 54 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[0], "arg1"), 0); |
| 55 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[1], "arg2\\arg3"), 0); |
| 56 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[2], "arg4"), 0); |
| 57 | |||
| 58 | 1 | parsed_args_count = gate_app_parse_args_buffer(&argset5, buffer, sizeof(buffer), parsed_args, parsed_args_capacity); | |
| 59 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_REQUIRE_EQUAL(parsed_args_count, 3); |
| 60 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[0], "arg1"), 0); |
| 61 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[1], "arg2\\arg3"), 0); |
| 62 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK_EQUAL(gate_str_comp(parsed_args[2], "arg4"), 0); |
| 63 | |||
| 64 | 1 | GATE_TEST_UNIT_END; | |
| 65 | } | ||
| 66 | |||
| 67 | 1 | GATE_TEST_FUNCTION(test_app_options) | |
| 68 | { | ||
| 69 | 1 | GATE_TEST_UNIT_BEGIN(test_app_options); | |
| 70 | |||
| 71 | do | ||
| 72 | { | ||
| 73 | static gate_bool_t var_switch = false; | ||
| 74 | static gate_string_t var_string = GATE_STRING_INIT_EMPTY; | ||
| 75 | |||
| 76 | static gate_app_option_t opts[] = { | ||
| 77 | GATE_APP_OPTION_INIT_STATIC("switch", GATE_APP_OPTION_TYPE_SWITCH, &var_switch, "SW", "switch", ""), | ||
| 78 | GATE_APP_OPTION_INIT_STATIC("string", GATE_APP_OPTION_TYPE_STRING, &var_string, "ST", "string", "") | ||
| 79 | }; | ||
| 80 | 1 | gate_size_t opts_count = sizeof(opts) / sizeof(opts[0]); | |
| 81 | |||
| 82 | 1 | char const* args[] = { | |
| 83 | "--switch", | ||
| 84 | "-ST", "content" | ||
| 85 | }; | ||
| 86 | 1 | gate_size_t args_count = sizeof(args) / sizeof(args[0]); | |
| 87 | gate_app_option_t opt_copy; | ||
| 88 | |||
| 89 | 1 | gate_size_t parsed = gate_app_options_parse_strs(args, args_count, opts, opts_count); | |
| 90 | |||
| 91 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | GATE_TEST_CHECK(gate_string_equals_str(&var_string, "content")); |
| 92 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GATE_TEST_CHECK(var_switch == true); |
| 93 | |||
| 94 | 1 | gate_app_option_copy(&opt_copy, &opts[0]); | |
| 95 | |||
| 96 | 1 | gate_string_release(&var_string); | |
| 97 | } while (0); | ||
| 98 | |||
| 99 | |||
| 100 | 1 | GATE_TEST_UNIT_END; | |
| 101 | } | ||
| 102 | |||
| 103 | 1 | GATE_TEST_FUNCTION(test_apps) | |
| 104 | { | ||
| 105 | 1 | gate_bool_t result = true; | |
| 106 | |||
| 107 | 1 | result |= test_apps_parse_args(); | |
| 108 | 1 | result |= test_app_options(); | |
| 109 | |||
| 110 | |||
| 111 | 1 | return result; | |
| 112 | } | ||
| 113 |