StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.
| 1 | use super::*; |
| 2 | |
| 3 | #[test] |
| 4 | fn test_word_boundaries() { |
| 5 | let buffer = "test/c/ab/word_with_underscores {восибing}"; |
| 6 | |
| 7 | let starts: Vec<_> = buffer |
| 8 | .word_starts_from_offset(Point::zero()) |
| 9 | .unwrap() |
| 10 | .collect(); |
| 11 | assert_eq!( |
| 12 | starts, |
| 13 | [ |
| 14 | Point::new(0, 5), |
| 15 | Point::new(0, 7), |
| 16 | Point::new(0, 10), |
| 17 | Point::new(0, 33), |
| 18 | Point::new(0, 42), |
| 19 | ] |
| 20 | ); |
| 21 | |
| 22 | let ends: Vec<_> = buffer |
| 23 | .word_ends_from_offset_exclusive(Point::zero()) |
| 24 | .unwrap() |
| 25 | .collect(); |
| 26 | assert_eq!( |
| 27 | ends, |
| 28 | [ |
| 29 | Point::new(0, 4), |
| 30 | Point::new(0, 6), |
| 31 | Point::new(0, 9), |
| 32 | Point::new(0, 31), |
| 33 | Point::new(0, 41), |
| 34 | Point::new(0, 42), |
| 35 | ] |
| 36 | ); |
| 37 | |
| 38 | let starts_only_space: Vec<_> = buffer |
| 39 | .word_starts_from_offset(Point::zero()) |
| 40 | .unwrap() |
| 41 | .with_policy(WordBoundariesPolicy::OnlyWhitespace) |
| 42 | .collect(); |
| 43 | assert_eq!(starts_only_space, [Point::new(0, 32), Point::new(0, 42)]); |
| 44 | |
| 45 | let ends_only_space: Vec<_> = buffer |
| 46 | .word_ends_from_offset_exclusive(Point::zero()) |
| 47 | .unwrap() |
| 48 | .with_policy(WordBoundariesPolicy::OnlyWhitespace) |
| 49 | .collect(); |
| 50 | assert_eq!(ends_only_space, [Point::new(0, 31), Point::new(0, 42)]); |
| 51 | |
| 52 | let starts_custom: Vec<_> = buffer |
| 53 | .word_starts_from_offset(Point::zero()) |
| 54 | .unwrap() |
| 55 | .with_policy(WordBoundariesPolicy::Custom(HashSet::from(['{', '}']))) |
| 56 | .collect(); |
| 57 | assert_eq!(starts_custom, [Point::new(0, 33), Point::new(0, 42)]); |
| 58 | |
| 59 | let ends_custom: Vec<_> = buffer |
| 60 | .word_ends_from_offset_exclusive(Point::zero()) |
| 61 | .unwrap() |
| 62 | .with_policy(WordBoundariesPolicy::Custom(HashSet::from(['{', '}']))) |
| 63 | .collect(); |
| 64 | assert_eq!( |
| 65 | ends_custom, |
| 66 | [Point::new(0, 31), Point::new(0, 41), Point::new(0, 42)] |
| 67 | ); |
| 68 | |
| 69 | let starts_reversed: Vec<_> = buffer |
| 70 | .word_starts_backward_from_offset_exclusive(Point::new(0, 42)) |
| 71 | .unwrap() |
| 72 | .collect(); |
| 73 | assert_eq!( |
| 74 | starts_reversed, |
| 75 | [ |
| 76 | Point::new(0, 33), |
| 77 | Point::new(0, 10), |
| 78 | Point::new(0, 7), |
| 79 | Point::new(0, 5), |
| 80 | Point::new(0, 0), |
| 81 | ] |
| 82 | ); |
| 83 | |
| 84 | let starts_mid: Vec<_> = buffer |
| 85 | .word_starts_from_offset(Point::new(0, 7)) |
| 86 | .unwrap() |
| 87 | .collect(); |
| 88 | assert_eq!( |
| 89 | starts_mid, |
| 90 | [Point::new(0, 10), Point::new(0, 33), Point::new(0, 42),] |
| 91 | ); |
| 92 | |
| 93 | let ends_mid: Vec<_> = buffer |
| 94 | .word_ends_from_offset_exclusive(Point::new(0, 6)) |
| 95 | .unwrap() |
| 96 | .collect(); |
| 97 | assert_eq!( |
| 98 | ends_mid, |
| 99 | [ |
| 100 | Point::new(0, 9), |
| 101 | Point::new(0, 31), |
| 102 | Point::new(0, 41), |
| 103 | Point::new(0, 42), |
| 104 | ] |
| 105 | ); |
| 106 | |
| 107 | let starts_reversed_mid: Vec<_> = buffer |
| 108 | .word_starts_backward_from_offset_exclusive(Point::new(0, 8)) |
| 109 | .unwrap() |
| 110 | .collect(); |
| 111 | assert_eq!( |
| 112 | starts_reversed_mid, |
| 113 | [Point::new(0, 7), Point::new(0, 5), Point::new(0, 0),] |
| 114 | ); |
| 115 | |
| 116 | let ends_inclusive: Vec<_> = buffer |
| 117 | .word_ends_from_offset_inclusive(Point::new(0, 6)) |
| 118 | .unwrap() |
| 119 | .collect(); |
| 120 | assert_eq!( |
| 121 | ends_inclusive, |
| 122 | [ |
| 123 | Point::new(0, 6), |
| 124 | Point::new(0, 9), |
| 125 | Point::new(0, 31), |
| 126 | Point::new(0, 41), |
| 127 | Point::new(0, 42), |
| 128 | ] |
| 129 | ); |
| 130 | |
| 131 | let starts_reversed_inclusive: Vec<_> = buffer |
| 132 | .word_starts_backward_from_offset_inclusive(Point::new(0, 10)) |
| 133 | .unwrap() |
| 134 | .collect(); |
| 135 | assert_eq!( |
| 136 | starts_reversed_inclusive, |
| 137 | [ |
| 138 | Point::new(0, 10), |
| 139 | Point::new(0, 7), |
| 140 | Point::new(0, 5), |
| 141 | Point::new(0, 0), |
| 142 | ] |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn test_unicode_whitespace() { |
| 148 | // See https://en.wikipedia.org/wiki/Whitespace_character |
| 149 | let text = "first\tsecond\u{A0}third\u{2003}fourth"; |
| 150 | let starts: Vec<_> = text |
| 151 | .word_starts_from_offset(Point::zero()) |
| 152 | .unwrap() |
| 153 | .collect(); |
| 154 | assert_eq!( |
| 155 | starts, |
| 156 | [ |
| 157 | Point::new(0, 6), |
| 158 | Point::new(0, 13), |
| 159 | Point::new(0, 19), |
| 160 | Point::new(0, 25) |
| 161 | ] |
| 162 | ); |
| 163 | } |
| 164 |