Refactored the HorizontallyScrollableText struct to handle Unicode strings more uniformly and to not rely on byte boundaries but instead to rely on Unicode char boundaries

This commit is contained in:
2023-08-08 14:15:23 -06:00
parent 218d442694
commit ce12ebd301
7 changed files with 164 additions and 31 deletions
+87 -14
View File
@@ -340,10 +340,11 @@ mod tests {
#[test]
fn test_horizontally_scrollable_text_len() {
let test_text = "우리 생애 최고의 해 The.Best";
let test_text = "우리 생애 최고의 해Test.Text";
let horizontally_scrollable_text = HorizontallyScrollableText::new(test_text.to_owned());
assert_eq!(horizontally_scrollable_text.len(), 20);
assert_eq!(horizontally_scrollable_text.text.len(), 36);
assert_str_eq!(horizontally_scrollable_text.text, test_text);
}
@@ -367,6 +368,26 @@ mod tests {
);
}
#[test]
fn test_horizontally_scrollable_text_scroll_text_left_uses_len_method() {
let horizontally_scrollable_text = HorizontallyScrollableText::from("우리");
horizontally_scrollable_text.scroll_left();
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 1);
assert_str_eq!(horizontally_scrollable_text.to_string(), "");
horizontally_scrollable_text.scroll_left();
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 2);
assert_str_eq!(horizontally_scrollable_text.to_string(), "");
horizontally_scrollable_text.scroll_left();
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 2);
assert!(horizontally_scrollable_text.to_string().is_empty());
}
#[test]
fn test_horizontally_scrollable_text_scroll_text_right() {
let horizontally_scrollable_text = HorizontallyScrollableText::from("Test string");
@@ -398,6 +419,15 @@ mod tests {
);
}
#[test]
fn test_horizontally_scrollable_text_scroll_home_uses_len_method() {
let horizontally_scrollable_text = HorizontallyScrollableText::from("우리");
horizontally_scrollable_text.scroll_home();
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 2);
}
#[test]
fn test_horizontally_scrollable_text_reset_offset() {
let horizontally_scrollable_text = HorizontallyScrollableText {
@@ -411,7 +441,7 @@ mod tests {
}
#[test]
fn test_horizontally_scrollable_text_scroll_or_reset() {
fn test_horizontally_scrollable_text_scroll_left_or_reset() {
let width = 3;
let test_text = "Test string";
let horizontally_scrollable_text = HorizontallyScrollableText::from(test_text);
@@ -438,7 +468,7 @@ mod tests {
}
#[test]
fn test_horizontally_scrollable_test_scroll_or_reset_resets_when_text_unselected() {
fn test_horizontally_scrollable_text_scroll_left_or_reset_resets_when_text_unselected() {
let horizontally_scrollable_test = HorizontallyScrollableText::from("Test string");
horizontally_scrollable_test.scroll_left();
@@ -449,55 +479,98 @@ mod tests {
assert_eq!(*horizontally_scrollable_test.offset.borrow(), 0);
}
#[test]
fn test_horizontally_scrollable_text_scroll_left_or_reset_uses_len_method() {
let horizontally_scrollable_text = HorizontallyScrollableText::from("우리");
let width = 1;
horizontally_scrollable_text.scroll_left_or_reset(width, true, true);
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 1);
horizontally_scrollable_text.scroll_left_or_reset(width, true, true);
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 2);
horizontally_scrollable_text.scroll_left_or_reset(width, true, true);
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 0);
}
#[test]
fn test_horizontally_scrollable_text_pop() {
let test_text = "Test string";
let test_text = "Test sTrin우gs";
let mut horizontally_scrollable_text = HorizontallyScrollableText::from(test_text);
horizontally_scrollable_text.pop();
assert_str_eq!(horizontally_scrollable_text.text, "Test strin");
assert_str_eq!(horizontally_scrollable_text.text, "Test sTrin우g");
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 0);
horizontally_scrollable_text.scroll_left();
horizontally_scrollable_text.pop();
assert_str_eq!(horizontally_scrollable_text.text, "Test strn");
assert_str_eq!(horizontally_scrollable_text.text, "Test sTring");
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 1);
horizontally_scrollable_text.scroll_right();
horizontally_scrollable_text.scroll_right();
horizontally_scrollable_text.pop();
assert_str_eq!(horizontally_scrollable_text.text, "Test str");
assert_str_eq!(horizontally_scrollable_text.text, "Test sTrin");
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 0);
horizontally_scrollable_text.scroll_home();
horizontally_scrollable_text.pop();
assert_str_eq!(horizontally_scrollable_text.text, "Test str");
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 8);
assert_str_eq!(horizontally_scrollable_text.text, "Test sTrin");
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 10);
horizontally_scrollable_text.scroll_right();
horizontally_scrollable_text.pop();
assert_str_eq!(horizontally_scrollable_text.text, "est sTrin");
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 9);
}
#[test]
fn test_horizontally_scrollable_text_pop_uses_len_method() {
let mut horizontally_scrollable_text = HorizontallyScrollableText::from("우리");
horizontally_scrollable_text.pop();
assert_str_eq!(horizontally_scrollable_text.text, "");
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 0);
horizontally_scrollable_text.pop();
assert!(horizontally_scrollable_text.text.is_empty());
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 0);
horizontally_scrollable_text.pop();
assert!(horizontally_scrollable_text.text.is_empty());
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 0);
}
#[test]
fn test_horizontally_scrollable_text_push() {
let test_text = "Test string";
let test_text = "Test string";
let mut horizontally_scrollable_text = HorizontallyScrollableText::from(test_text);
horizontally_scrollable_text.push('h');
assert_str_eq!(horizontally_scrollable_text.text, "Test stringh");
assert_str_eq!(horizontally_scrollable_text.text, "Test stringh");
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 0);
horizontally_scrollable_text.scroll_left();
horizontally_scrollable_text.push('l');
assert_str_eq!(horizontally_scrollable_text.text, "Test stringlh");
assert_str_eq!(horizontally_scrollable_text.text, "Test stringlh");
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 1);
horizontally_scrollable_text.scroll_right();
horizontally_scrollable_text.scroll_right();
horizontally_scrollable_text.push('0');
horizontally_scrollable_text.push('');
assert_str_eq!(horizontally_scrollable_text.text, "Test stringlh0");
assert_str_eq!(horizontally_scrollable_text.text, "Test stringlh");
assert_eq!(*horizontally_scrollable_text.offset.borrow(), 0);
}