1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
// ok to copy/adapt

int _test_parse_boolean_true_trailing_characters() {
    mu_start();
    //────────────────────
    bool value;
    char const* input = "true123";
    char const* pos = input;
    bool is_success = parse_boolean(&value, &pos);
    mu_check(is_success);
    mu_check(value == true);
    mu_check(pos == input + 4);
    //────────────────────
    mu_end();
}

int _test_parse_boolean_invalid_false() {
    mu_start();
    //────────────────────
    bool value;
    char const* input = "falsy";
    char const* pos = input;
    bool is_success = parse_boolean(&value, &pos);
    mu_check(!is_success);
    mu_check(pos == input + 4);
    //────────────────────
    mu_end();
}

int _test_parse_null_valid_trailing_characters() {
    mu_start();
    //────────────────────
    char const* input = "null123";
    char const* pos = input;
    bool is_success = parse_null(&pos);
    mu_check(is_success);
    mu_check(pos == input + 4);
    //────────────────────
    mu_end();
}

int _test_read_json_valid_integer() {
    mu_start();
    //────────────────────
    // assuming the file already exists
    char const* filename = "valid_integer.json";
    ParseResult result = read_json(filename);
    mu_check(result.is_success);
    mu_check(result.element.type == ELEMENT_INT);
    mu_check(result.element.as_int == 12345);
    free_element(result.element);
    //────────────────────
    mu_end();
}

int _test_read_json_valid_integer() {
    mu_start();
    //────────────────────
    char const* filename = "valid_string.json";
    // can write a helper function to write a string into a file
    _write_string_into_file(filename, "\"Hello\"");
    ParseResult result = read_json(filename);
    mu_check(result.is_success);
    mu_check(result.element.type == ELEMENT_STRING);
    mu_check_stirngs_equal(result.element.as_string, "Hello");
    free_element(result.element);
    //────────────────────
    mu_end();
}

int _test_read_json_invalid_too_long_name() {
    mu_start();
    //────────────────────
    char const* filename = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json";
    ParseResult result = read_json(filename);
    mu_check(!result.is_success);
    mu_check(result.file_error == ENAMETOOLONG);
    printf("Invalid filename too long error: %d: %s\n",
            result.file_error, strerror(result.file_error));
    mu_check(result.error_idx == 0);
    // don't free element, we don't have a valid element
    // when is_success is false
    //────────────────────
    mu_end();
}

int main(int argc, char* argv[]) {
    
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2024 Alexander J. Quinn & David Burnett         This content is protected and may not be shared, uploaded, or distributed.