-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathnumbers.rs
More file actions
146 lines (132 loc) · 3.87 KB
/
Copy pathnumbers.rs
File metadata and controls
146 lines (132 loc) · 3.87 KB
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
use ron::{
de::from_str,
error::{Error, Position, SpannedError},
ser::{to_string_pretty, PrettyConfig},
};
#[test]
fn test_hex() {
assert_eq!(from_str("0x507"), Ok(0x507));
assert_eq!(from_str("0x1A5"), Ok(0x1A5));
assert_eq!(from_str("0x53C537"), Ok(0x53C537));
assert_eq!(
from_str::<u8>("0x"),
Err(SpannedError {
code: Error::ExpectedInteger,
position: Position { line: 1, col: 3 },
})
);
assert_eq!(
from_str::<u8>("0x_1"),
Err(SpannedError {
code: Error::UnderscoreAtBeginning,
position: Position { line: 1, col: 3 },
})
);
assert_eq!(
from_str::<u8>("0xFFF"),
Err(SpannedError {
code: Error::IntegerOutOfBounds,
position: Position { line: 1, col: 6 },
})
);
}
#[test]
fn test_bin() {
assert_eq!(from_str("0b101"), Ok(0b101));
assert_eq!(from_str("0b001"), Ok(0b001));
assert_eq!(from_str("0b100100"), Ok(0b100100));
assert_eq!(
from_str::<u8>("0b"),
Err(SpannedError {
code: Error::ExpectedInteger,
position: Position { line: 1, col: 3 },
})
);
assert_eq!(
from_str::<u8>("0b_1"),
Err(SpannedError {
code: Error::UnderscoreAtBeginning,
position: Position { line: 1, col: 3 },
})
);
assert_eq!(
from_str::<u8>("0b111111111"),
Err(SpannedError {
code: Error::IntegerOutOfBounds,
position: Position { line: 1, col: 12 },
})
);
}
#[test]
fn test_oct() {
assert_eq!(from_str("0o1461"), Ok(0o1461));
assert_eq!(from_str("0o051"), Ok(0o051));
assert_eq!(from_str("0o150700"), Ok(0o150700));
assert_eq!(
from_str::<u8>("0o"),
Err(SpannedError {
code: Error::ExpectedInteger,
position: Position { line: 1, col: 3 },
})
);
assert_eq!(
from_str::<u8>("0o_1"),
Err(SpannedError {
code: Error::UnderscoreAtBeginning,
position: Position { line: 1, col: 3 },
})
);
assert_eq!(
from_str::<u8>("0o77777"),
Err(SpannedError {
code: Error::IntegerOutOfBounds,
position: Position { line: 1, col: 8 },
})
);
}
#[test]
fn test_dec() {
assert_eq!(from_str("1461"), Ok(1461));
assert_eq!(from_str("51"), Ok(51));
assert_eq!(from_str("150700"), Ok(150700));
assert_eq!(
from_str::<i8>("-_1"),
Err(SpannedError {
code: Error::UnderscoreAtBeginning,
position: Position { line: 1, col: 2 },
})
);
assert_eq!(
from_str::<u8>("256"),
Err(SpannedError {
code: Error::IntegerOutOfBounds,
position: Position { line: 1, col: 4 },
})
);
}
#[test]
fn test_hex_serialization() {
let config = PrettyConfig::new()
.hex_as_raw(true);
// Test valid hex without quotes
let val = "0x1234";
let serialized = to_string_pretty(&val, config.clone()).unwrap();
assert_eq!(serialized, "0x1234");
// Test uppercase hex
let val = "0X1A5B";
let serialized = to_string_pretty(&val, config.clone()).unwrap();
assert_eq!(serialized, "0X1A5B");
// Test that normal strings are still escaped
let val = "normal string";
let serialized = to_string_pretty(&val, config.clone()).unwrap();
assert_eq!(serialized, "\"normal string\"");
// Test that invalid hex is treated as a normal string
let val = "0xGGG";
let serialized = to_string_pretty(&val, config.clone()).unwrap();
assert_eq!(serialized, "\"0xGGG\"");
// Test with hex_as_raw disabled
let config = PrettyConfig::new().hex_as_raw(false);
let val = "0x1234";
let serialized = to_string_pretty(&val, config).unwrap();
assert_eq!(serialized, "\"0x1234\"");
}