Add colour tests

And fix bugs found by these tests...
This commit is contained in:
Ben S 2014-06-27 22:31:23 +01:00
parent 03bf728a1e
commit a8d73d1cd9

View File

@ -93,7 +93,7 @@ impl Style {
match *self {
Plain => Style(StyleStruct { foreground: White, background: None, bold: true, underline: false }),
Foreground(c) => Style(StyleStruct { foreground: c, background: None, bold: true, underline: false }),
Style(st) => Style(StyleStruct { foreground: st.foreground, background: st.background, bold: true, underline: false }),
Style(st) => Style(StyleStruct { foreground: st.foreground, background: st.background, bold: true, underline: st.underline }),
}
}
@ -101,7 +101,7 @@ impl Style {
match *self {
Plain => Style(StyleStruct { foreground: White, background: None, bold: false, underline: true }),
Foreground(c) => Style(StyleStruct { foreground: c, background: None, bold: false, underline: true }),
Style(st) => Style(StyleStruct { foreground: st.foreground, background: st.background, bold: false, underline: true }),
Style(st) => Style(StyleStruct { foreground: st.foreground, background: st.background, bold: st.bold, underline: true }),
}
}
@ -109,7 +109,7 @@ impl Style {
match *self {
Plain => Style(StyleStruct { foreground: White, background: Some(background), bold: false, underline: false }),
Foreground(c) => Style(StyleStruct { foreground: c, background: Some(background), bold: false, underline: false }),
Style(st) => Style(StyleStruct { foreground: st.foreground, background: Some(background), bold: false, underline: false }),
Style(st) => Style(StyleStruct { foreground: st.foreground, background: Some(background), bold: st.bold, underline: st.underline }),
}
}
}
@ -146,3 +146,104 @@ pub fn strip_formatting(input: &String) -> String {
let re = regex!("\x1B\\[.+?m");
re.replace_all(input.as_slice(), "").to_string()
}
#[test]
fn test_red() {
let hi = Red.paint("hi");
assert!(hi == "\x1B[31mhi\x1B[0m".to_string());
}
#[test]
fn test_black() {
let hi = Black.normal().paint("hi");
assert!(hi == "\x1B[30mhi\x1B[0m".to_string());
}
#[test]
fn test_yellow_bold() {
let hi = Yellow.bold().paint("hi");
assert!(hi == "\x1B[1;33mhi\x1B[0m".to_string());
}
#[test]
fn test_yellow_bold_2() {
let hi = Yellow.normal().bold().paint("hi");
assert!(hi == "\x1B[1;33mhi\x1B[0m".to_string());
}
#[test]
fn test_blue_underline() {
let hi = Blue.underline().paint("hi");
assert!(hi == "\x1B[4;34mhi\x1B[0m".to_string());
}
#[test]
fn test_green_bold_underline() {
let hi = Green.bold().underline().paint("hi");
assert!(hi == "\x1B[1;4;32mhi\x1B[0m".to_string());
}
#[test]
fn test_green_bold_underline_2() {
let hi = Green.underline().bold().paint("hi");
assert!(hi == "\x1B[1;4;32mhi\x1B[0m".to_string());
}
#[test]
fn test_purple_on_white() {
let hi = Purple.on(White).paint("hi");
assert!(hi == "\x1B[47;35mhi\x1B[0m".to_string());
}
#[test]
fn test_purple_on_white_2() {
let hi = Purple.normal().on(White).paint("hi");
assert!(hi == "\x1B[47;35mhi\x1B[0m".to_string());
}
#[test]
fn test_cyan_bold_on_white() {
let hi = Cyan.bold().on(White).paint("hi");
assert!(hi == "\x1B[1;47;36mhi\x1B[0m".to_string());
}
#[test]
fn test_cyan_underline_on_white() {
let hi = Cyan.underline().on(White).paint("hi");
assert!(hi == "\x1B[4;47;36mhi\x1B[0m".to_string());
}
#[test]
fn test_cyan_bold_underline_on_white() {
let hi = Cyan.bold().underline().on(White).paint("hi");
assert!(hi == "\x1B[1;4;47;36mhi\x1B[0m".to_string());
}
#[test]
fn test_cyan_underline_bold_on_white() {
let hi = Cyan.underline().bold().on(White).paint("hi");
assert!(hi == "\x1B[1;4;47;36mhi\x1B[0m".to_string());
}
#[test]
fn test_fixed() {
let hi = Fixed(100).paint("hi");
assert!(hi == "\x1B[38;5;100mhi\x1B[0m".to_string());
}
#[test]
fn test_fixed_on_purple() {
let hi = Fixed(100).on(Purple).paint("hi");
assert!(hi == "\x1B[45;38;5;100mhi\x1B[0m".to_string());
}
#[test]
fn test_fixed_on_fixed() {
let hi = Fixed(100).on(Fixed(200)).paint("hi");
assert!(hi == "\x1B[48;5;200;38;5;100mhi\x1B[0m".to_string());
}