|
19 | 19 | #include "private/test-utils.h" |
20 | 20 | #include <yaml.h> |
21 | 21 |
|
| 22 | +static void |
| 23 | +test (const char *input, gint64 expected_value, gboolean expected_error) |
| 24 | +{ |
| 25 | + gint64 parsed; |
| 26 | + g_autoptr (GError) error = NULL; |
| 27 | + MMD_INIT_YAML_EVENT (event); |
| 28 | + MMD_INIT_YAML_PARSER (parser); |
| 29 | + |
| 30 | + yaml_parser_set_input_string ( |
| 31 | + &parser, (const unsigned char *)input, strlen (input)); |
| 32 | + parser_skip_document_start (&parser); |
| 33 | + |
| 34 | + parsed = modulemd_yaml_parse_int64 (&parser, &error); |
| 35 | + if (expected_error) |
| 36 | + g_assert_nonnull (error); |
| 37 | + else |
| 38 | + g_assert_null (error); |
| 39 | + g_assert_cmpuint (parsed, ==, expected_value); |
| 40 | +} |
| 41 | + |
| 42 | +static void |
| 43 | +test_int64_valid (void) |
| 44 | +{ |
| 45 | + test ("42", 42, FALSE); |
| 46 | +} |
| 47 | + |
| 48 | +static void |
| 49 | +test_int64_invalid_no_digit (void) |
| 50 | +{ |
| 51 | + test ("foo", 0, TRUE); |
| 52 | +} |
| 53 | + |
| 54 | +static void |
| 55 | +test_int64_invalid_incomplete (void) |
| 56 | +{ |
| 57 | + test ("42foo", 0, TRUE); |
| 58 | +} |
| 59 | + |
| 60 | +static void |
| 61 | +test_int64_valid_negative (void) |
| 62 | +{ |
| 63 | + test ("-42", -42, FALSE); |
| 64 | +} |
| 65 | + |
| 66 | +static void |
| 67 | +test_int64_invalid_too_big (void) |
| 68 | +{ |
| 69 | + test ("9223372036854775808", 0, TRUE); |
| 70 | +} |
| 71 | + |
| 72 | +static void |
| 73 | +test_int64_invalid_too_small (void) |
| 74 | +{ |
| 75 | + test ("-9223372036854775809", 0, TRUE); |
| 76 | +} |
| 77 | + |
22 | 78 | static void |
23 | 79 | utest (const char *input, guint64 expected_value, gboolean expected_error) |
24 | 80 | { |
@@ -76,6 +132,18 @@ main (int argc, char *argv[]) |
76 | 132 | setlocale (LC_ALL, ""); |
77 | 133 | g_test_init (&argc, &argv, NULL); |
78 | 134 |
|
| 135 | + g_test_add_func ("/modulemd/v2/int64/yaml/parse/valid", test_int64_valid); |
| 136 | + g_test_add_func ("/modulemd/v2/int64/yaml/parse/invalid_no_digit", |
| 137 | + test_int64_invalid_no_digit); |
| 138 | + g_test_add_func ("/modulemd/v2/int64/yaml/parse/invalid_incomplete", |
| 139 | + test_int64_invalid_incomplete); |
| 140 | + g_test_add_func ("/modulemd/v2/int64/yaml/parse/valid_negative", |
| 141 | + test_int64_valid_negative); |
| 142 | + g_test_add_func ("/modulemd/v2/int64/yaml/parse/invalid_too_big", |
| 143 | + test_int64_invalid_too_big); |
| 144 | + g_test_add_func ("/modulemd/v2/int64/yaml/parse/invalid_too_small", |
| 145 | + test_int64_invalid_too_small); |
| 146 | + |
79 | 147 | g_test_add_func ("/modulemd/v2/uint64/yaml/parse/valid", test_uint64_valid); |
80 | 148 | g_test_add_func ("/modulemd/v2/uint64/yaml/parse/invalid_no_digit", |
81 | 149 | test_uint64_invalid_no_digit); |
|
0 commit comments