From 3aab69110e5864518f51c97a6453d15551115a64 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Thu, 1 Jun 2023 15:24:36 -0500 Subject: [PATCH] Some tests added with github copilot preview (#9332) # Description This PR adds some tests that were generated in GitHub Copilot Chat Preview. I was just playing around and figured that more tests couldn't hurt. # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-color-config/src/color_config.rs | 89 ++++++++++ crates/nu-color-config/src/text_style.rs | 185 +++++++++++++++++++++ 2 files changed, 274 insertions(+) diff --git a/crates/nu-color-config/src/color_config.rs b/crates/nu-color-config/src/color_config.rs index e8045386c1..d5dfb6a5a6 100644 --- a/crates/nu-color-config/src/color_config.rs +++ b/crates/nu-color-config/src/color_config.rs @@ -87,3 +87,92 @@ fn color_string_to_nustyle(color_string: String) -> Style { parse_nustyle(nu_style) } + +#[cfg(test)] +mod tests { + use super::*; + use nu_ansi_term::{Color, Style}; + use nu_protocol::{Span, Value}; + + #[test] + fn test_color_string_to_nustyle_empty_string() { + let color_string = String::new(); + let style = color_string_to_nustyle(color_string); + assert_eq!(style, Style::default()); + } + + #[test] + fn test_color_string_to_nustyle_valid_string() { + let color_string = r#"{"fg": "black", "bg": "white", "attr": "b"}"#.to_string(); + let style = color_string_to_nustyle(color_string); + assert_eq!(style.foreground, Some(Color::Black)); + assert_eq!(style.background, Some(Color::White)); + assert_eq!(style.is_bold, true); + } + + #[test] + fn test_color_string_to_nustyle_invalid_string() { + let color_string = "invalid string".to_string(); + let style = color_string_to_nustyle(color_string); + assert_eq!(style, Style::default()); + } + + #[test] + fn test_get_style_from_value() { + // Test case 1: all values are valid + let cols = vec!["bg".to_string(), "fg".to_string(), "attr".to_string()]; + let vals = vec![ + Value::String { + val: "red".to_string(), + span: Span::unknown(), + }, + Value::String { + val: "blue".to_string(), + span: Span::unknown(), + }, + Value::String { + val: "bold".to_string(), + span: Span::unknown(), + }, + ]; + let expected_style = NuStyle { + bg: Some("red".to_string()), + fg: Some("blue".to_string()), + attr: Some("bold".to_string()), + }; + assert_eq!(get_style_from_value(&cols, &vals), Some(expected_style)); + + // Test case 2: no values are valid + let cols = vec!["invalid".to_string()]; + let vals = vec![Value::nothing(Span::unknown())]; + assert_eq!(get_style_from_value(&cols, &vals), None); + + // Test case 3: some values are valid + let cols = vec!["bg".to_string(), "invalid".to_string()]; + let vals = vec![ + Value::String { + val: "green".to_string(), + span: Span::unknown(), + }, + Value::nothing(Span::unknown()), + ]; + let expected_style = NuStyle { + bg: Some("green".to_string()), + fg: None, + attr: None, + }; + assert_eq!(get_style_from_value(&cols, &vals), Some(expected_style)); + } + + #[test] + fn test_parse_map_entry() { + let mut hm = HashMap::new(); + let key = "test_key".to_owned(); + let value = Value::String { + val: "red".to_owned(), + span: Span::unknown(), + }; + parse_map_entry(&mut hm, &key, &value); + assert_eq!(hm.get(&key), Some(&lookup_ansi_color_style("red"))); + } +} diff --git a/crates/nu-color-config/src/text_style.rs b/crates/nu-color-config/src/text_style.rs index d0c3b98ae2..d9753c453f 100644 --- a/crates/nu-color-config/src/text_style.rs +++ b/crates/nu-color-config/src/text_style.rs @@ -244,3 +244,188 @@ impl Default for TextStyle { Self::new() } } +#[cfg(test)] +mod tests { + use super::*; + use nu_ansi_term::Style; + + #[test] + fn test_is_bold() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + is_bold: true, + ..Default::default() + }), + }; + assert_eq!(text_style.is_bold(), true); + } + + #[test] + fn test_dimmed() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + ..Default::default() + }), + }; + let dimmed_style = text_style.dimmed(); + assert_eq!(dimmed_style.is_dimmed(), true); + } + + #[test] + fn test_is_dimmed() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + is_dimmed: true, + ..Default::default() + }), + }; + assert_eq!(text_style.is_dimmed(), true); + } + + #[test] + fn test_italic() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + ..Default::default() + }), + }; + let italic_style = text_style.italic(); + assert_eq!(italic_style.is_italic(), true); + } + + #[test] + fn test_is_italic() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + is_italic: true, + ..Default::default() + }), + }; + assert_eq!(text_style.is_italic(), true); + } + + #[test] + fn test_underline() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + ..Default::default() + }), + }; + let underline_style = text_style.underline(); + assert_eq!(underline_style.is_underline(), true); + } + + #[test] + fn test_is_underline() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + is_underline: true, + ..Default::default() + }), + }; + assert_eq!(text_style.is_underline(), true); + } + + #[test] + fn test_blink() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + ..Default::default() + }), + }; + let blink_style = text_style.blink(); + assert_eq!(blink_style.is_blink(), true); + } + + #[test] + fn test_is_blink() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + is_blink: true, + ..Default::default() + }), + }; + assert_eq!(text_style.is_blink(), true); + } + + #[test] + fn test_reverse() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + ..Default::default() + }), + }; + let reverse_style = text_style.reverse(); + assert_eq!(reverse_style.is_reverse(), true); + } + + #[test] + fn test_is_reverse() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + is_reverse: true, + ..Default::default() + }), + }; + assert_eq!(text_style.is_reverse(), true); + } + + #[test] + fn test_hidden() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + ..Default::default() + }), + }; + let hidden_style = text_style.hidden(); + assert_eq!(hidden_style.is_hidden(), true); + } + + #[test] + fn test_is_hidden() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + is_hidden: true, + ..Default::default() + }), + }; + assert_eq!(text_style.is_hidden(), true); + } + + #[test] + fn test_strikethrough() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + ..Default::default() + }), + }; + let strikethrough_style = text_style.strikethrough(); + assert_eq!(strikethrough_style.is_strikethrough(), true); + } + + #[test] + fn test_is_strikethrough() { + let text_style = TextStyle { + alignment: Alignment::Left, + color_style: Some(Style { + is_strikethrough: true, + ..Default::default() + }), + }; + assert_eq!(text_style.is_strikethrough(), true); + } +}