|
| 1 | +// Regression test for issue #23: https://github.com/0xvasanth/gonfig/issues/23 |
| 2 | +// Comprehensive test with both core and std aliased and all features enabled |
| 3 | + |
| 4 | +#![allow(unused_imports)] |
| 5 | + |
| 6 | +use core as my_core; |
| 7 | +use gonfig::Gonfig; |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | +use std as my_std; |
| 10 | + |
| 11 | +#[derive(Debug, Clone, Serialize, Deserialize, Gonfig)] |
| 12 | +#[gonfig(env_prefix = "BOTH_ALIAS", allow_config, allow_cli)] |
| 13 | +pub struct FullFeaturedConfig { |
| 14 | + #[gonfig(default = "localhost")] |
| 15 | + pub hostname: String, |
| 16 | + |
| 17 | + #[gonfig(default = "8080")] |
| 18 | + pub port: u16, |
| 19 | + |
| 20 | + #[gonfig(default = "info")] |
| 21 | + #[gonfig(env_name = "LOG_LEVEL")] |
| 22 | + pub log_level: String, |
| 23 | +} |
| 24 | + |
| 25 | +// Test nested configuration |
| 26 | +#[derive(Debug, Clone, Serialize, Deserialize, Gonfig)] |
| 27 | +#[gonfig(env_prefix = "DATABASE", allow_config)] |
| 28 | +pub struct DatabaseConfig { |
| 29 | + #[gonfig(default = "postgres://localhost/mydb")] |
| 30 | + pub url: String, |
| 31 | + |
| 32 | + #[gonfig(default = "10")] |
| 33 | + pub max_connections: u32, |
| 34 | + |
| 35 | + #[gonfig(default = "30")] |
| 36 | + pub timeout_seconds: u64, |
| 37 | +} |
| 38 | + |
| 39 | +// Test with skip attribute |
| 40 | +#[derive(Debug, Clone, Serialize, Deserialize, Gonfig)] |
| 41 | +#[gonfig(env_prefix = "APP", allow_config, allow_cli)] |
| 42 | +pub struct AppConfigWithSkip { |
| 43 | + #[gonfig(default = "production")] |
| 44 | + pub environment: String, |
| 45 | + |
| 46 | + #[skip] |
| 47 | + #[serde(skip)] |
| 48 | + pub runtime_data: Option<String>, |
| 49 | +} |
| 50 | + |
| 51 | +#[cfg(test)] |
| 52 | +mod tests { |
| 53 | + use super::*; |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn test_both_aliases_all_features() { |
| 57 | + // Most comprehensive test: both std and core aliased, all features enabled |
| 58 | + let config = FullFeaturedConfig::from_gonfig(); |
| 59 | + assert!( |
| 60 | + config.is_ok(), |
| 61 | + "Should compile and run with both std and core aliased" |
| 62 | + ); |
| 63 | + |
| 64 | + let config = config.unwrap(); |
| 65 | + assert_eq!(config.hostname, "localhost"); |
| 66 | + assert_eq!(config.port, 8080); |
| 67 | + assert_eq!(config.log_level, "info"); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn test_both_aliases_nested_config() { |
| 72 | + // Test with database configuration |
| 73 | + let config = DatabaseConfig::from_gonfig(); |
| 74 | + assert!( |
| 75 | + config.is_ok(), |
| 76 | + "Nested config should work with both aliases" |
| 77 | + ); |
| 78 | + |
| 79 | + let config = config.unwrap(); |
| 80 | + assert_eq!(config.url, "postgres://localhost/mydb"); |
| 81 | + assert_eq!(config.max_connections, 10); |
| 82 | + assert_eq!(config.timeout_seconds, 30); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_both_aliases_with_skip() { |
| 87 | + // Test with skip attribute |
| 88 | + let config = AppConfigWithSkip::from_gonfig(); |
| 89 | + assert!(config.is_ok(), "Config with skip should work with aliases"); |
| 90 | + |
| 91 | + let config = config.unwrap(); |
| 92 | + assert_eq!(config.environment, "production"); |
| 93 | + assert_eq!(config.runtime_data, None); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_builder_pattern_with_aliases() { |
| 98 | + // Test the builder pattern also works with aliases |
| 99 | + let builder = FullFeaturedConfig::gonfig_builder(); |
| 100 | + let config = FullFeaturedConfig::from_gonfig_with_builder(builder); |
| 101 | + assert!(config.is_ok(), "Builder pattern should work with aliases"); |
| 102 | + } |
| 103 | +} |
0 commit comments