1
use ron::{
2
    error::{Position, Span, SpannedError},
3
    Error, Value,
4
};
5

            
6
#[test]
7
4
fn char_apostrophe_must_be_escaped() {
8
4
    assert_eq!(ron::from_str::<char>(r"'\''"), Ok('\''));
9
4
    assert_eq!(ron::to_string(&'\'').unwrap(), r"'\''");
10

            
11
    // `''` is not a char, so `'''` must not be read as the char `'`
12
12
    for ron in ["''", "'''", "''''"] {
13
12
        assert_eq!(
14
12
            ron::from_str::<char>(ron),
15
            Err(SpannedError {
16
                code: Error::ExpectedChar,
17
                span: Span {
18
                    start: Position { line: 1, col: 1 },
19
                    end: Position { line: 1, col: 3 },
20
                },
21
            }),
22
            "{ron}",
23
        );
24
12
        assert_eq!(
25
12
            ron::from_str::<Value>(ron),
26
            Err(SpannedError {
27
                code: Error::ExpectedChar,
28
                span: Span {
29
                    start: Position { line: 1, col: 1 },
30
                    end: Position { line: 1, col: 3 },
31
                },
32
            }),
33
            "{ron}",
34
        );
35
    }
36

            
37
4
    assert_eq!(
38
4
        ron::from_str::<Vec<char>>("['\\'', 'a']"),
39
4
        Ok(vec!['\'', 'a'])
40
    );
41
4
    assert_eq!(
42
4
        ron::from_str::<Vec<char>>("[''', 'a']"),
43
        Err(SpannedError {
44
            code: Error::ExpectedChar,
45
            span: Span {
46
                start: Position { line: 1, col: 2 },
47
                end: Position { line: 1, col: 4 },
48
            },
49
        }),
50
    );
51
4
}
52

            
53
#[test]
54
4
fn byte_literal_apostrophe_must_be_escaped() {
55
4
    assert_eq!(ron::from_str::<u8>(r"b'\''"), Ok(b'\''));
56
4
    assert_eq!(
57
4
        ron::from_str::<Value>(r"b'\''"),
58
        Ok(Value::Number(ron::value::Number::U8(b'\'')))
59
    );
60

            
61
12
    for ron in ["b''", "b'''", "b''''"] {
62
12
        assert_eq!(
63
12
            ron::from_str::<u8>(ron),
64
            Err(SpannedError {
65
                code: Error::ExpectedByteLiteral,
66
                span: Span {
67
                    start: Position { line: 1, col: 1 },
68
                    end: Position { line: 1, col: 4 },
69
                },
70
            }),
71
            "{ron}",
72
        );
73
12
        assert_eq!(
74
12
            ron::from_str::<Value>(ron),
75
            Err(SpannedError {
76
                code: Error::ExpectedByteLiteral,
77
                span: Span {
78
                    start: Position { line: 1, col: 1 },
79
                    end: Position { line: 1, col: 4 },
80
                },
81
            }),
82
            "{ron}",
83
        );
84
    }
85
4
}