use std::collections::HashMap; use shiny_configuration::configuration_provider::ConfigurationProvider; use shiny_configuration::configuration_provider::json5_provider::{CreateJson5ProviderFromFileError, Json5Provider}; use shiny_configuration::value::Value; use crate::utils; #[test] fn test_file_is_missing() { let file_path = utils::temp_file_path().join("random"); let error = Json5Provider::from_path(&file_path).unwrap_err(); assert!(matches!( error, CreateJson5ProviderFromFileError::FailedToReadFile { .. } )); assert_eq!( error.to_string(), format!("Failed to read configuration file [file_path = `{}`]", file_path.display()), ) } #[test] fn test_config_file_is_invalid() { let file_path = utils::write_to_temp_file( r#" value: {{123abc "#, ); let error = Json5Provider::from_path(&file_path).unwrap_err(); assert!(matches!( error, CreateJson5ProviderFromFileError::FailedToDeserialize { .. } )); assert_eq!( error.to_string(), format!( "Failed to parse configuration file [file_path = `{}`]", file_path.display() ), ) } #[test] fn test_provide() { let file_path = utils::write_to_temp_file( r#" { value_string: "", value_string2: "Hello World", bool: true, float: 42.1, uint: 184467440737095516, int: -42, none: null, map: { hello: "World" }, array: [1,2,3,4], ref: "{{ provider.key }}", } "#, ); let provider = Json5Provider::from_path(&file_path).unwrap(); let value = provider.provide(); assert_eq!(value.get("value_string").unwrap(), &"".into()); assert_eq!(value.get("value_string2").unwrap(), &"Hello World".into()); assert_eq!(value.get("bool").unwrap(), &true.into()); assert_eq!(value.get("float").unwrap(), &42.1.into()); assert_eq!( value.get("uint").unwrap(), &(184467440737095516i64).into() ); assert_eq!(value.get("int").unwrap(), &(-42).into()); assert_eq!(value.get("None"), None); assert_eq!( value.get("map").unwrap(), &HashMap::from([("hello", "World")]).into() ); assert_eq!(value.get("array").unwrap(), &vec![1, 2, 3, 4].into()); assert_eq!( value.get("ref").unwrap(), &Value::String("{{ provider.key }}".to_string()) ); }