Skip to content

Commit 4814f53

Browse files
committed
test: Verify negative indices
1 parent 92c44a3 commit 4814f53

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

tests/testsuite/errors.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,53 @@ use snapbox::{assert_data_eq, str};
33

44
use config::{Config, ConfigError, File, FileFormat, Map, Value};
55

6+
#[test]
7+
#[cfg(feature = "json")]
8+
fn test_error_path_index_bounds() {
9+
let c = Config::builder()
10+
.add_source(File::from_str(
11+
r#"
12+
{
13+
"arr": [1]
14+
}
15+
"#,
16+
FileFormat::Json,
17+
))
18+
.build()
19+
.unwrap();
20+
21+
let res = c.get::<usize>("arr[2]");
22+
assert!(res.is_err());
23+
assert_data_eq!(
24+
res.unwrap_err().to_string(),
25+
str![[r#"configuration property "arr[2]" not found"#]]
26+
);
27+
}
28+
29+
#[test]
30+
#[cfg(feature = "json")]
31+
#[should_panic]
32+
fn test_error_path_index_negative_bounds() {
33+
let c = Config::builder()
34+
.add_source(File::from_str(
35+
r#"
36+
{
37+
"arr": []
38+
}
39+
"#,
40+
FileFormat::Json,
41+
))
42+
.build()
43+
.unwrap();
44+
45+
let res = c.get::<usize>("arr[-1]");
46+
assert!(res.is_err());
47+
assert_data_eq!(
48+
res.unwrap_err().to_string(),
49+
str![[r#"configuration property "arr[2]" not found"#]]
50+
);
51+
}
52+
653
#[test]
754
#[cfg(feature = "json")]
855
fn test_error_parse() {

tests/testsuite/set.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ fn test_set_scalar_path() {
6363

6464
#[test]
6565
#[cfg(feature = "json")]
66+
#[should_panic]
6667
fn test_set_arr_path() {
6768
let config = Config::builder()
6869
.set_override("present[0].name", "Ivan")
@@ -79,6 +80,10 @@ fn test_set_arr_path() {
7980
.unwrap()
8081
.set_override("reverse[-2]", "Alice")
8182
.unwrap()
83+
.set_override("empty[-1]", "Bob")
84+
.unwrap()
85+
.set_override("empty[-2]", "Alice")
86+
.unwrap()
8287
.add_source(File::from_str(
8388
r#"
8489
{
@@ -97,7 +102,8 @@ fn test_set_arr_path() {
97102
{
98103
"name": "l2"
99104
}
100-
]
105+
],
106+
"empty": []
101107
}
102108
"#,
103109
FileFormat::Json,
@@ -115,6 +121,8 @@ fn test_set_arr_path() {
115121
assert_eq!(config.get("present[2]").ok(), Some("George".to_owned()));
116122
assert_eq!(config.get("reverse[1]").ok(), Some("Bob".to_owned()));
117123
assert_eq!(config.get("reverse[0]").ok(), Some("Alice".to_owned()));
124+
assert_eq!(config.get("empty[1]").ok(), Some("Bob".to_owned()));
125+
assert_eq!(config.get("empty[0]").ok(), Some("Alice".to_owned()));
118126
}
119127

120128
#[test]

0 commit comments

Comments
 (0)