From 7757056c7a43d4fbd85e3e817237d178bf6b1761 Mon Sep 17 00:00:00 2001 From: Brad Clawsie Date: Wed, 25 Nov 2026 14:18:46 -0600 Subject: fix: learn_21 --- learn_21/tests/load_config.rs | 8 ++++++-- learn_21/tests/parse_config.rs | 25 ++++++++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) (limited to 'learn_21/tests') diff --git a/learn_21/tests/load_config.rs b/learn_21/tests/load_config.rs index 827ed02..5fc7866 100644 --- a/learn_21/tests/load_config.rs +++ b/learn_21/tests/load_config.rs @@ -16,8 +16,12 @@ fn test_load_config() -> io::Result<()> { file.write_all(b"host=myhost\nport=1\nworkers=1\n")?; } - assert!(load_config(&file_path).is_ok()); - assert!(load_config(std::path::Path::new("/bad/path")).is_err()); + assert!(load_config(&file_path).is_ok_and(|x| x == + Config{host:"myhost".to_string(), port:1, workers:1})); + assert!(matches!( + load_config(std::path::Path::new("/bad/path")), + Err(ConfigError::Io(Error)), + )); fs::remove_file(&file_path)?; Ok(()) diff --git a/learn_21/tests/parse_config.rs b/learn_21/tests/parse_config.rs index 8ca6626..6b963bc 100644 --- a/learn_21/tests/parse_config.rs +++ b/learn_21/tests/parse_config.rs @@ -11,20 +11,23 @@ fn test_ok() { assert!(parse_config(&ok1).is_ok_and(|x| x == Config{host:"myhost-012-".to_string(), port:65535, workers:usize::MAX})); - let ok2 = "port=1\n# comment\n host=my-host \n\n workers=1 \n"; + let ok2 = "port=1\n# comment\n host=my host \n\n workers=1 \n"; assert!(parse_config(ok2).is_ok_and(|x| x == - Config{host:"my-host".to_string(), port:1, workers:1})); + Config{host:"my host".to_string(), port:1, workers:1})); let ok3 = "port=1\n# comment\n host=my=host\n\n workers=1 \n"; assert!(parse_config(ok3).is_ok_and(|x| x == Config{host:"my=host".to_string(), port:1, workers:1})); + let ok4 = "\n# comment\n host= my-host \nport=1 \n workers=1 \n"; + assert!(parse_config(ok4).is_ok_and(|x| x == + Config{host:"my-host".to_string(), port:1, workers:1})); + } #[test] fn test_invalid_line() { let invalid0 = "host=hello\nport\nworkers=1\n"; - dbg!(parse_config(invalid0)); assert!(matches!( parse_config(invalid0), Err(ConfigError::InvalidLine(2)) @@ -69,10 +72,10 @@ fn test_duplicate_field() { #[test] fn test_invalid_value() { - let invalid0 = "host=hello...&&&\nport=1\nworkers=1\n"; + let invalid0 = "host= \nport=1\nworkers=1\n"; assert!(matches!( parse_config(invalid0), - Err(ConfigError::InvalidValue(s)) if s == "hello...&&&" + Err(ConfigError::InvalidValue(s)) if s == "" )); let invalid1 = "host=hello\nport=0\nworkers=1\n"; @@ -110,6 +113,18 @@ fn test_invalid_value() { parse_config(invalid6), Err(ConfigError::InvalidValue(s)) if s == "def" )); + + let invalid7 = "host=hello\nport=+1\nworkers=1\n"; + assert!(matches!( + parse_config(invalid7), + Err(ConfigError::InvalidValue(s)) if s == "+1" + )); + + let invalid8 = "host=hello\nport=1\nworkers=+1\n"; + assert!(matches!( + parse_config(invalid8), + Err(ConfigError::InvalidValue(s)) if s == "+1" + )); } #[test] -- cgit v1.2.3