|
1 | 1 | #![cfg(feature = "toml")]
|
2 | 2 |
|
| 3 | +use std::collections::BTreeMap; |
| 4 | + |
3 | 5 | use chrono::{DateTime, TimeZone, Utc};
|
4 | 6 | use float_cmp::ApproxEqUlps;
|
5 | 7 | use serde_derive::Deserialize;
|
@@ -422,6 +424,60 @@ bar = "bar is a lowercase param"
|
422 | 424 | );
|
423 | 425 | }
|
424 | 426 |
|
| 427 | +#[test] |
| 428 | +fn test_override_empty_tables() { |
| 429 | + #[derive(Debug, Deserialize)] |
| 430 | + struct Settings { |
| 431 | + profile: BTreeMap<String, Profile>, |
| 432 | + } |
| 433 | + |
| 434 | + #[derive(Debug, Default, Deserialize)] |
| 435 | + struct Profile { |
| 436 | + name: Option<String>, |
| 437 | + } |
| 438 | + |
| 439 | + // Test a few scenarios with empty tables: |
| 440 | + // * foo: empty table -> table with k/v |
| 441 | + // * bar: table with k/v -> empty table |
| 442 | + // * baz: empty table relying on Default impl |
| 443 | + let cfg = Config::builder() |
| 444 | + .add_source(File::from_str( |
| 445 | + r#" |
| 446 | + [profile.foo] |
| 447 | +"#, |
| 448 | + FileFormat::Toml, |
| 449 | + )) |
| 450 | + .add_source(File::from_str( |
| 451 | + r#" |
| 452 | + [profile.foo] |
| 453 | + name = "foo" |
| 454 | +"#, |
| 455 | + FileFormat::Toml, |
| 456 | + )) |
| 457 | + .add_source(File::from_str( |
| 458 | + r#" |
| 459 | + [profile.bar] |
| 460 | + name = "bar" |
| 461 | +"#, |
| 462 | + FileFormat::Toml, |
| 463 | + )) |
| 464 | + .add_source(File::from_str( |
| 465 | + r#" |
| 466 | + [profile.bar] |
| 467 | + [profile.baz] |
| 468 | +"#, |
| 469 | + FileFormat::Toml, |
| 470 | + )) |
| 471 | + .build() |
| 472 | + .unwrap(); |
| 473 | + |
| 474 | + let settings: Settings = cfg.try_deserialize().unwrap(); |
| 475 | + assert_eq!(settings.profile.len(), 3); |
| 476 | + assert_eq!(settings.profile["foo"].name.as_deref(), Some("foo")); |
| 477 | + assert_eq!(settings.profile["bar"].name.as_deref(), Some("bar")); |
| 478 | + assert_eq!(settings.profile["baz"].name.as_deref(), None); |
| 479 | +} |
| 480 | + |
425 | 481 | #[test]
|
426 | 482 | fn toml() {
|
427 | 483 | let s = Config::builder()
|
|
0 commit comments