| 1 |
use helix_tui::text::{Span, Spans, StyledGrapheme, Text}; |
| 2 |
use helix_view::graphics::{Color, Modifier, Style}; |
| 3 |
|
| 4 |
|
| 5 |
#[test] |
| 6 |
fn text_width() { |
| 7 |
let text = Text::from("The first line\nThe second line"); |
| 8 |
assert_eq!(15, text.width()); |
| 9 |
} |
| 10 |
|
| 11 |
#[test] |
| 12 |
fn text_height() { |
| 13 |
let text = Text::from("The first line\nThe second line"); |
| 14 |
assert_eq!(2, text.height()); |
| 15 |
} |
| 16 |
|
| 17 |
#[test] |
| 18 |
fn patch_style() { |
| 19 |
let style1 = Style::default().fg(Color::Yellow); |
| 20 |
let style2 = Style::default().fg(Color::Yellow).bg(Color::Black); |
| 21 |
let mut half_styled_text = |
| 22 |
Text::styled(String::from("The first line\nThe second line"), style1); |
| 23 |
let full_styled_text = Text::styled(String::from("The first line\nThe second line"), style2); |
| 24 |
assert_ne!(half_styled_text, full_styled_text); |
| 25 |
|
| 26 |
half_styled_text.patch_style(Style::default().bg(Color::Black)); |
| 27 |
assert_eq!(half_styled_text, full_styled_text); |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn set_style() { |
| 32 |
let style = Style::default() |
| 33 |
.fg(Color::Yellow) |
| 34 |
.add_modifier(Modifier::ITALIC); |
| 35 |
let mut raw_text = Text::raw("The first line\nThe second line"); |
| 36 |
let styled_text = Text::styled(String::from("The first line\nThe second line"), style); |
| 37 |
assert_ne!(raw_text, styled_text); |
| 38 |
|
| 39 |
raw_text.set_style(style); |
| 40 |
assert_eq!(raw_text, styled_text); |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn text_extend() { |
| 45 |
let style = Style::default() |
| 46 |
.fg(Color::Yellow) |
| 47 |
.add_modifier(Modifier::ITALIC); |
| 48 |
let mut text = Text::from("The first line\nThe second line"); |
| 49 |
assert_eq!(2, text.height()); |
| 50 |
|
| 51 |
|
| 52 |
text.extend(Text::raw("These are two\nmore lines!")); |
| 53 |
assert_eq!(4, text.height()); |
| 54 |
|
| 55 |
|
| 56 |
text.extend(Text::styled("Some more lines\nnow with more style!", style)); |
| 57 |
assert_eq!(6, text.height()); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
#[test] |
| 63 |
fn styled_graphemes() { |
| 64 |
let style = Style::default().fg(Color::Yellow); |
| 65 |
let span = Span::styled("Text", style); |
| 66 |
let style = Style::default().fg(Color::Green).bg(Color::Black); |
| 67 |
let styled_graphemes = span.styled_graphemes(style); |
| 68 |
assert_eq!( |
| 69 |
vec![ |
| 70 |
StyledGrapheme { |
| 71 |
symbol: "T", |
| 72 |
style: Style { |
| 73 |
fg: Some(Color::Yellow), |
| 74 |
bg: Some(Color::Black), |
| 75 |
underline_color: None, |
| 76 |
underline_style: None, |
| 77 |
add_modifier: Modifier::empty(), |
| 78 |
sub_modifier: Modifier::empty(), |
| 79 |
}, |
| 80 |
}, |
| 81 |
StyledGrapheme { |
| 82 |
symbol: "e", |
| 83 |
style: Style { |
| 84 |
fg: Some(Color::Yellow), |
| 85 |
bg: Some(Color::Black), |
| 86 |
underline_color: None, |
| 87 |
underline_style: None, |
| 88 |
add_modifier: Modifier::empty(), |
| 89 |
sub_modifier: Modifier::empty(), |
| 90 |
}, |
| 91 |
}, |
| 92 |
StyledGrapheme { |
| 93 |
symbol: "x", |
| 94 |
style: Style { |
| 95 |
fg: Some(Color::Yellow), |
| 96 |
bg: Some(Color::Black), |
| 97 |
underline_color: None, |
| 98 |
underline_style: None, |
| 99 |
add_modifier: Modifier::empty(), |
| 100 |
sub_modifier: Modifier::empty(), |
| 101 |
}, |
| 102 |
}, |
| 103 |
StyledGrapheme { |
| 104 |
symbol: "t", |
| 105 |
style: Style { |
| 106 |
fg: Some(Color::Yellow), |
| 107 |
bg: Some(Color::Black), |
| 108 |
underline_color: None, |
| 109 |
underline_style: None, |
| 110 |
add_modifier: Modifier::empty(), |
| 111 |
sub_modifier: Modifier::empty(), |
| 112 |
}, |
| 113 |
}, |
| 114 |
], |
| 115 |
styled_graphemes.collect::<Vec<StyledGrapheme>>() |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
#[test] |
| 122 |
fn spans_width() { |
| 123 |
let spans = Spans::from(vec![ |
| 124 |
Span::styled("My", Style::default().fg(Color::Yellow)), |
| 125 |
Span::raw(" text"), |
| 126 |
]); |
| 127 |
assert_eq!(7, spans.width()); |
| 128 |
} |