File: 203_error_positions.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 (153 lines) | stat: -rw-r--r-- 5,063 bytes parent folder | download | duplicates (5)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::num::NonZeroU32;

use ron::error::{Error, Position, Span, SpannedError};
use serde::{
    de::{Deserialize, Error as DeError, Unexpected},
    Deserializer,
};

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

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

#[derive(Debug, serde::Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
enum Test {
    TupleVariant(i32, String),
    StructVariant { a: bool, b: NonZeroU32, c: i32 },
}

#[derive(Debug, PartialEq)] // GRCOV_EXCL_LINE
struct TypeError;

impl<'de> Deserialize<'de> for TypeError {
    fn deserialize<D: Deserializer<'de>>(_deserializer: D) -> Result<Self, D::Error> {
        Err(D::Error::invalid_type(Unexpected::Unit, &"impossible"))
    }
}

#[test]
fn test_error_positions() {
    let bogus_struct = "  ()";
    let expected_err = Err(SpannedError {
        code: Error::InvalidValueForType {
            expected: String::from("impossible"),
            found: String::from("a unit value"),
        },
        span: Span {
            start: Position { line: 1, col: 1 },
            end: Position { line: 1, col: 3 },
        },
    });

    assert_eq!(ron::from_str::<TypeError>(bogus_struct), expected_err,);

    #[cfg(feature = "internal-span-substring-test")]
    check_error_span_inclusive::<TypeError>(bogus_struct, expected_err, "  (");

    let bogus_struct = "StructVariant(a: true, b: 0, c: -42)";
    let expected_err = Err(SpannedError {
        code: Error::InvalidValueForType {
            expected: String::from("a nonzero u32"),
            found: String::from("the unsigned integer `0`"),
        },
        span: Span {
            start: Position { line: 1, col: 27 },
            end: Position { line: 1, col: 28 },
        },
    });

    assert_eq!(ron::from_str::<Test>(bogus_struct), expected_err);

    #[cfg(feature = "internal-span-substring-test")]
    check_error_span_inclusive::<Test>(bogus_struct, expected_err, "0,");

    let bogus_struct = "TupleVariant(42)";
    let expected_err = Err(SpannedError {
        code: Error::ExpectedDifferentLength {
            expected: String::from("tuple variant Test::TupleVariant with 2 elements"),
            found: 1,
        },
        span: Span {
            start: Position { line: 1, col: 16 },
            end: Position { line: 1, col: 16 },
        },
    });

    assert_eq!(ron::from_str::<Test>(bogus_struct), expected_err);

    #[cfg(feature = "internal-span-substring-test")]
    check_error_span_inclusive::<Test>(bogus_struct, expected_err, ")");

    let bogus_struct = "NotAVariant";
    let expected_err = Err(SpannedError {
        code: Error::NoSuchEnumVariant {
            expected: &["TupleVariant", "StructVariant"],
            found: String::from("NotAVariant"),
            outer: Some(String::from("Test")),
        },
        span: Span {
            start: Position { line: 1, col: 1 },
            end: Position { line: 1, col: 12 },
        },
    });

    assert_eq!(ron::from_str::<Test>(bogus_struct), expected_err);

    #[cfg(feature = "internal-span-substring-test")]
    check_error_span_exclusive::<Test>(bogus_struct, expected_err, "NotAVariant");

    let bogus_struct = "StructVariant(a: true, b: 1, c: -42, d: \"gotcha\")";
    let expected_err = Err(SpannedError {
        code: Error::NoSuchStructField {
            expected: &["a", "b", "c"],
            found: String::from("d"),
            outer: Some(String::from("StructVariant")),
        },
        span: Span {
            start: Position { line: 1, col: 38 },
            end: Position { line: 1, col: 39 },
        },
    });

    assert_eq!(ron::from_str::<Test>(bogus_struct), expected_err);

    #[cfg(feature = "internal-span-substring-test")]
    check_error_span_inclusive::<Test>(bogus_struct, expected_err, "d:");

    let bogus_struct = "StructVariant(a: true, c: -42)";
    let expected_err = Err(SpannedError {
        code: Error::MissingStructField {
            field: "b",
            outer: Some(String::from("StructVariant")),
        },
        span: Span {
            start: Position { line: 1, col: 30 },
            end: Position { line: 1, col: 30 },
        },
    });

    assert_eq!(ron::from_str::<Test>(bogus_struct), expected_err);

    #[cfg(feature = "internal-span-substring-test")]
    check_error_span_inclusive::<Test>(bogus_struct, expected_err, ")");

    let bogus_struct = "StructVariant(a: true, b: 1, a: false, c: -42)";
    let expected_err = Err(SpannedError {
        code: Error::DuplicateStructField {
            field: "a",
            outer: Some(String::from("StructVariant")),
        },
        span: Span {
            start: Position { line: 1, col: 30 },
            end: Position { line: 1, col: 31 },
        },
    });

    assert_eq!(ron::from_str::<Test>(bogus_struct), expected_err);

    #[cfg(feature = "internal-span-substring-test")]
    check_error_span_inclusive::<Test>(bogus_struct, expected_err, "a:");
}