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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <errno.h>

int _test_write_json_multicharacter_string() {
    mu_start();
    //────────────────────
    char const* filename = "multicharater_string.json";
    Element element = {
        .type = ELEMENT_STRING,
        .as_string = "abc" // this is not on the heap
                           // so not valid for free_element
    };
    int file_error = write_json(filename, element);
    mu_check(file_error == 0);

    // calling some helper for my tests
    char* file_contents = _read_file_into_string(filename);
    // the newline is going to be printed
    // in my print_element
    // any whitespace that isvalid in JSON is valid
    // (will pass our tests), but for your tests may be
    // simplier to assume specific whitespace
    mu_check_strings_equal(file_contents, "\"abc\"\n");
    free(file_contents);

    // intentionally do not call free_element
    // as I did not reserve space on the heap
    //────────────────────
    mu_end();
}

int _test_write_json_invalid_file_no_folder() {
    mu_start();
    //────────────────────
    char const* filename = "does_not_exist/string.json";
    Element element = {
        .type = ELEMENT_STRING,
        // _copy_string creates a copy of the string
        // on the heap
        .as_string = _copy_string("abc")
    };
    int file_error = write_json(filename, element);
    mu_check(file_error == ENOENT);
    // could do above, which is equivelent to below
    // you don't need both generally, they compile the same
    mu_check(file_error == 2);

    free_element(element);
    //────────────────────
    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.