Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-core/src/text/mod_tests.rs
StratoSDK / crates / strato-ui-core / src / text / mod_tests.rs
1use super::{
2 count_chars_up_to_byte,
3 point::Point,
4 {char_slice, BufferIndex, TextBuffer},
5};
6 
7use super::str_to_byte_vec;
8 
9#[test]
10fn test_str_to_byte_vec() {
11 assert_eq!(
12 str_to_byte_vec("foo bar"),
13 vec![0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61, 0x72]
14 );
15}
16 
17/// Test the [`str`] implementation of [`TextBuffer`], which we rely on in other unit tests.
18#[test]
19fn test_str_buffer() -> anyhow::Result<()> {
20 let buf = "Hello\nWorld!";
21 
22 assert_eq!(buf.chars_at(2.into())?.collect::<String>(), "llo\nWorld!");
23 assert_eq!(buf.chars_rev_at(3.into())?.collect::<String>(), "leH");
24 
25 // For simplicity, we do not wrap newlines into new rows.
26 assert_eq!(buf.to_point(7.into())?, Point::new(0, 7));
27 assert!(Point::new(1, 1).to_char_offset(buf).is_err());
28 assert_eq!(Point::new(0, 7).to_char_offset(buf)?, 7.into());
29 
30 Ok(())
31}
32 
33#[test]
34fn test_char_slice() {
35 let has_nonbreaking_space = "A\u{a0}non-breaking space occupies 2 bytes in UTF-8";
36 assert_eq!(char_slice(has_nonbreaking_space, 0, 3), Some("A\u{a0}n"));
37 
38 // This string has characters ['A', '❤', '\u{fe0f}', '\u{200d}', '🔥', 'b']
39 assert_eq!(char_slice("A❤️‍🔥b", 4, 5), Some("🔥"));
40 
41 assert_eq!(char_slice("abc", 5, 10), None);
42 assert_eq!(char_slice("abc", 2, 0), None);
43 assert_eq!(char_slice("abc", 1, 4), None);
44 
45 assert_eq!(char_slice("A string", 2, 4), Some("st"));
46 
47 assert_eq!(char_slice("The end: 🫥??", 10, 12), Some("??"));
48 
49 assert_eq!(char_slice("🫥", 0, 0), Some(""));
50}
51 
52#[test]
53fn test_char_counts_up_to_byte() {
54 let text = "abc🔥abc☄️abc😬";
55 assert_eq!(count_chars_up_to_byte(text, 0.into()), Some(0.into()));
56 assert_eq!(
57 count_chars_up_to_byte(text, "abc🔥".len().into()),
58 Some(4.into())
59 );
60 assert_eq!(
61 count_chars_up_to_byte(text, text.len().into()),
62 Some(text.chars().count().into())
63 );
64}
65