Skip to content

Commit 77590f1

Browse files
committed
add test cases for empty file.
1 parent 0a770e9 commit 77590f1

File tree

9 files changed

+93
-7
lines changed

9 files changed

+93
-7
lines changed

Cargo.lock

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/file/format/jsonc.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
use std::error::Error;
22

3-
use crate::error::{ConfigError, Unexpected};
3+
use crate::format;
44
use crate::map::Map;
55
use crate::value::{Value, ValueKind};
6-
76
use jsonc_parser::JsonValue;
87

98
pub(crate) fn parse(
109
uri: Option<&String>,
1110
text: &str,
1211
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
13-
let parsed = jsonc_parser::parse_to_value(text, &Default::default())?;
14-
let value = from_jsonc_value(uri, parsed.unwrap_or(ValueKind::Nil));
12+
let value = match jsonc_parser::parse_to_value(text, &Default::default())? {
13+
Some(json_value) => from_jsonc_value(uri, json_value),
14+
None => Value::new(uri, ValueKind::Nil),
15+
};
1516
format::extract_root_table(uri, value)
1617
}
1718

tests/testsuite/file_ini.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,14 @@ fn ini() {
252252
let date: DateTime<Utc> = s.get("ini_datetime").unwrap();
253253
assert_eq!(date, Utc.with_ymd_and_hms(2017, 5, 10, 2, 14, 53).unwrap());
254254
}
255+
256+
#[test]
257+
fn test_nothing() {
258+
let res = Config::builder()
259+
.add_source(File::from_str("", FileFormat::Ini))
260+
.build();
261+
assert!(res.is_ok());
262+
let c = res.unwrap();
263+
let cc = format!("{:?}", c);
264+
assert_data_eq!(cc, str!("Config { defaults: {}, overrides: {}, sources: [], cache: Value { origin: None, kind: Table({}) } }"));
265+
}

tests/testsuite/file_json.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,15 @@ fn json() {
316316
let date: DateTime<Utc> = s.get("json_datetime").unwrap();
317317
assert_eq!(date, Utc.with_ymd_and_hms(2017, 5, 10, 2, 14, 53).unwrap());
318318
}
319+
320+
#[test]
321+
fn test_nothing() {
322+
let res = Config::builder()
323+
.add_source(File::from_str("", FileFormat::Json))
324+
.build();
325+
assert!(res.is_err());
326+
assert_data_eq!(
327+
res.unwrap_err().to_string(),
328+
format!("EOF while parsing a value at line 1 column 0")
329+
);
330+
}

tests/testsuite/file_json5.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,15 @@ fn json() {
325325
let date: DateTime<Utc> = s.get("json_datetime").unwrap();
326326
assert_eq!(date, Utc.with_ymd_and_hms(2017, 5, 10, 2, 14, 53).unwrap());
327327
}
328+
329+
#[test]
330+
fn test_nothing() {
331+
let res = Config::builder()
332+
.add_source(File::from_str("", FileFormat::Json5))
333+
.build();
334+
assert!(res.is_err());
335+
assert_data_eq!(
336+
res.unwrap_err().to_string(),
337+
format!(" --> 1:1\n |\n1 | \n | ^---\n |\n = expected array, boolean, null, number, object, or string")
338+
);
339+
}

tests/testsuite/file_jsonc.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn test_error_parse() {
101101
assert_eq!(
102102
res.unwrap_err().to_string(),
103103
format!(
104-
"Expected a colon after the string or word in an object property on line 4 column 1. in {}",
104+
"Expected colon after the string or word in object property on line 4 column 1 in {}",
105105
std::path::Path::new(&f)
106106
.strip_prefix(std::env::current_dir().unwrap())
107107
.unwrap_or(std::path::Path::new(&f))
@@ -187,3 +187,15 @@ fn test_override_lowercase_value_for_enums() {
187187
str!["enum EnumSettings does not have variant constructor bar"]
188188
);
189189
}
190+
191+
#[test]
192+
fn test_nothing() {
193+
let res = Config::builder()
194+
.add_source(File::from_str("", FileFormat::Jsonc))
195+
.build();
196+
assert!(res.is_err());
197+
assert_data_eq!(
198+
res.unwrap_err().to_string(),
199+
format!("invalid type: unit value, expected a map")
200+
);
201+
}

tests/testsuite/file_ron.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,15 @@ fn ron() {
317317
let date: DateTime<Utc> = s.get("ron_datetime").unwrap();
318318
assert_eq!(date, Utc.with_ymd_and_hms(2021, 4, 19, 11, 33, 2).unwrap());
319319
}
320+
321+
#[test]
322+
fn test_nothing() {
323+
let res = Config::builder()
324+
.add_source(File::from_str("", FileFormat::Ron))
325+
.build();
326+
assert!(res.is_err());
327+
assert_data_eq!(
328+
res.unwrap_err().to_string(),
329+
format!("1:1: Unexpected end of RON")
330+
);
331+
}

tests/testsuite/file_toml.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,14 @@ fn toml() {
438438
let date: DateTime<Utc> = s.get("toml_datetime").unwrap();
439439
assert_eq!(date, Utc.with_ymd_and_hms(2017, 5, 11, 14, 55, 15).unwrap());
440440
}
441+
442+
#[test]
443+
fn test_nothing() {
444+
let res = Config::builder()
445+
.add_source(File::from_str("", FileFormat::Toml))
446+
.build();
447+
assert!(res.is_ok());
448+
let c = res.unwrap();
449+
let cc = format!("{:?}", c);
450+
assert_data_eq!(cc, str!("Config { defaults: {}, overrides: {}, sources: [], cache: Value { origin: None, kind: Table({}) } }"));
451+
}

tests/testsuite/file_yaml.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,14 @@ inner_float:
406406
assert_eq!(config.inner_float.get("0.1").unwrap(), "float 0.1");
407407
assert_eq!(config.inner_float.get("0.2").unwrap(), "float 0.2");
408408
}
409+
410+
#[test]
411+
fn test_nothing() {
412+
let res = Config::builder()
413+
.add_source(File::from_str("", FileFormat::Yaml))
414+
.build();
415+
assert!(res.is_ok());
416+
let c = res.unwrap();
417+
let cc = format!("{:?}", c);
418+
assert_data_eq!(cc, str!("Config { defaults: {}, overrides: {}, sources: [], cache: Value { origin: None, kind: Table({}) } }"));
419+
}

0 commit comments

Comments
 (0)