File: 256_comma_error.rs

package info (click to toggle)
rust-ron 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: makefile: 2
file content (116 lines) | stat: -rw-r--r-- 2,942 bytes parent folder | download | duplicates (3)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#![allow(dead_code)]

use ron::error::{Error, Position, Span, SpannedError};

#[cfg(feature = "internal-span-substring-test")]
use ron::util::span_substring::check_error_span_inclusive;

#[derive(Debug, serde::Deserialize)]
struct Test {
    a: i32,
    b: i32,
}

#[test]
fn test_missing_comma_error() {
    let tuple_string = r#"(
        1 // <-- forgotten comma here
        2
    )"#;

    assert_eq!(
        ron::from_str::<(i32, i32)>(tuple_string).unwrap_err(),
        SpannedError {
            code: Error::ExpectedComma,
            span: Span {
                start: Position { line: 3, col: 9 },
                end: Position { line: 3, col: 9 }
            }
        }
    );

    let list_string = r#"[
        0,
        1 // <-- forgotten comma here
        2
    ]"#;

    assert_eq!(
        ron::from_str::<Vec<i32>>(list_string).unwrap_err(),
        SpannedError {
            code: Error::ExpectedComma,
            span: Span {
                start: Position { line: 4, col: 9 },
                end: Position { line: 4, col: 9 }
            }
        }
    );

    let struct_string = r#"Test(
        a: 1 // <-- forgotten comma here
        b: 2
    )"#;

    assert_eq!(
        ron::from_str::<Test>(struct_string).unwrap_err(),
        SpannedError {
            code: Error::ExpectedComma,
            span: Span {
                start: Position { line: 3, col: 9 },
                end: Position { line: 3, col: 9 }
            }
        }
    );

    let map_string = r#"{
        "a": 1 // <-- forgotten comma here
        "b": 2
    }"#;

    assert_eq!(
        ron::from_str::<std::collections::HashMap<String, i32>>(map_string).unwrap_err(),
        SpannedError {
            code: Error::ExpectedComma,
            span: Span {
                start: Position { line: 3, col: 9 },
                end: Position { line: 3, col: 9 }
            }
        }
    );

    let extensions_string = r#"#![enable(
        implicit_some // <-- forgotten comma here
        unwrap_newtypes
    ]) 42"#;

    assert_eq!(
        ron::from_str::<u8>(extensions_string).unwrap_err(),
        SpannedError {
            code: Error::ExpectedComma,
            span: Span {
                start: Position { line: 2, col: 50 },
                end: Position { line: 3, col: 9 }
            }
        }
    );

    #[cfg(feature = "internal-span-substring-test")]
    check_error_span_inclusive::<u8>(
        extensions_string,
        Err(SpannedError {
            code: Error::ExpectedComma,
            span: Span {
                start: Position { line: 2, col: 50 },
                end: Position { line: 3, col: 9 },
            },
        }),
        "\n        u",
    );
}

#[test]
fn test_comma_end() {
    assert_eq!(ron::from_str::<(i32, i32)>("(0, 1)").unwrap(), (0, 1));
    assert_eq!(ron::from_str::<(i32, i32)>("(0, 1,)").unwrap(), (0, 1));
    assert_eq!(ron::from_str::<()>("()"), Ok(()));
}