aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/json_test_suite.rs
blob: 7bb67f42e0b2d0e7471d2b8393f6d95e5c655848 (plain)
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
#[test]
fn y_json_files_should_be_valid() {
    let test_suite_dir: std::path::PathBuf = [
        env!("CARGO_MANIFEST_DIR"),
        "tests",
        "JSONTestSuite",
        "test_parsing",
    ]
    .iter()
    .collect();

    for entry in std::fs::read_dir(&test_suite_dir).expect("failed to read test suite directory") {
        let entry = entry.expect("failed to read directory entry");
        let path = entry.path();
        let name = path.file_name().unwrap().to_string_lossy();
        if !name.starts_with("y_") || !name.ends_with(".json") {
            continue;
        }

        let input = std::fs::read(&path).expect("failed to read file");
        let mut output = Vec::new();
        let result = reparojson::repair(&input[..], &mut output);

        assert!(
            matches!(result, Ok(reparojson::RepairOk::Valid)),
            "{}: expected valid JSON, but {:?}",
            name,
            result
        );
        assert_eq!(input, output, "{name}: output differs from input");
    }
}

#[test]
fn n_json_files_should_be_invalid() {
    let test_suite_dir: std::path::PathBuf = [
        env!("CARGO_MANIFEST_DIR"),
        "tests",
        "JSONTestSuite",
        "test_parsing",
    ]
    .iter()
    .collect();

    for entry in std::fs::read_dir(&test_suite_dir).expect("failed to read test suite directory") {
        let entry = entry.expect("failed to read directory entry");
        let path = entry.path();
        let name = path.file_name().unwrap().to_string_lossy();
        if !name.starts_with("n_") || !name.ends_with(".json") {
            continue;
        }

        // Skip test cases that cause stack overflow.
        if *name == *"n_structure_100000_opening_arrays.json"
            || *name == *"n_structure_open_array_object.json"
        {
            continue;
        }

        let input = std::fs::read(&path).expect("failed to read file");
        let mut output = Vec::new();
        let result = reparojson::repair(&input[..], &mut output);

        assert!(
            !matches!(result, Ok(reparojson::RepairOk::Valid)),
            "{}: expected invalid JSON, but {:?}",
            name,
            result
        );
    }
}