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
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
Darren Schroeder 2023-06-01 15:24:36 -05:00 committed by GitHub
parent b9e65e35b8
commit 3aab69110e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 274 additions and 0 deletions

View file

@ -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")));
}
}

View file

@ -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);
}
}