forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

text_richtext_handler.rs266 lines, 8.5 KiBHistoryRawDownload
1use loro_internal::{
2 cursor::{PosType, Side},
3 handler::Handler,
4 handler::TextDelta,
5 ContainerType, HandlerTrait, LoroDoc, LoroResult, TextHandler, ToJson,
6};
7use pretty_assertions::assert_eq;
8use serde_json::json;
9
10fn richtext_json(text: &TextHandler) -> serde_json::Value {
11 text.get_richtext_value().to_json_value()
12}
13
14#[test]
15fn detached_text_handler_contract_covers_local_branch() -> LoroResult<()> {
16 let text = TextHandler::new_detached();
17
18 assert!(!text.is_attached());
19 assert!(text.doc().is_none());
20 assert!(text.version_id().is_none());
21 assert!(text.get_attached().is_none());
22 assert!(text.is_empty());
23 assert_eq!(format!("{:?}", text), "TextHandler(Unattached)");
24
25 text.insert_utf8(0, "a😀文b")?;
26 text.mark(1, 3, "bold", true.into(), PosType::Unicode)?;
27
28 assert_eq!(text.len_utf8(), 9);
29 assert_eq!(text.len_unicode(), 4);
30 assert_eq!(text.len_utf16(), 5);
31 let event_len = if cfg!(feature = "wasm") { 5 } else { 4 };
32 assert_eq!(text.len_event(), event_len);
33 assert_eq!(text.char_at(1, PosType::Unicode)?, '😀');
34 assert_eq!(text.slice(1, 3, PosType::Unicode)?, "😀文");
35 assert_eq!(text.slice_utf16(1, 4)?, "😀文");
36 assert_eq!(
37 text.convert_pos(2, PosType::Unicode, PosType::Bytes),
38 Some(5)
39 );
40 assert_eq!(
41 text.convert_pos(5, PosType::Bytes, PosType::Unicode),
42 Some(2)
43 );
44 assert_eq!(
45 text.convert_pos(1, PosType::Unicode, PosType::Utf16),
46 Some(1)
47 );
48 assert!(text.char_at(99, PosType::Unicode).is_err());
49 assert!(text.slice(3, 2, PosType::Unicode).is_err());
50
51 let mut seen = Vec::new();
52 text.iter(|chunk| {
53 seen.push(chunk.to_owned());
54 false
55 });
56 assert_eq!(seen, vec!["a".to_string()]);
57
58 assert_eq!(
59 richtext_json(&text),
60 json!([
61 {"insert": "a"},
62 {"insert": "😀文", "attributes": {"bold": true}},
63 {"insert": "b"}
64 ])
65 );
66 assert_eq!(
67 text.slice_delta(0, text.len_unicode(), PosType::Unicode)?,
68 vec![
69 TextDelta::Insert {
70 insert: "a".to_string(),
71 attributes: None,
72 },
73 TextDelta::Insert {
74 insert: "😀文".to_string(),
75 attributes: Some([("bold".to_string(), true.into())].into_iter().collect()),
76 },
77 TextDelta::Insert {
78 insert: "b".to_string(),
79 attributes: None,
80 },
81 ]
82 );
83
84 let handler = text.to_handler();
85 assert_eq!(handler.c_type(), ContainerType::Text);
86 assert!(!handler.is_attached());
87 assert!(handler.attached_handler().is_none());
88 assert!(handler.doc().is_none());
89 assert_eq!(handler.get_value().to_json_value(), json!("a😀文b"));
90 assert_eq!(handler.get_deep_value().to_json_value(), json!("a😀文b"));
91 assert!(<TextHandler as HandlerTrait>::from_handler(handler.clone()).is_some());
92 assert!(<Handler as HandlerTrait>::from_handler(handler).is_some());
93
94 text.diagnose();
95
96 let plain = TextHandler::new_detached();
97 plain.insert_utf8(0, "clear me")?;
98 plain.clear()?;
99 assert_eq!(plain.len_unicode(), 0);
100 assert_eq!(plain.len_utf8(), 0);
101 assert_eq!(richtext_json(&plain), json!([]));
102
103 Ok(())
104}
105
106#[test]
107fn attached_text_handler_contract_covers_document_branch_and_handler_enum() -> LoroResult<()> {
108 let doc = LoroDoc::new_auto_commit();
109 let root = doc.get_map("root");
110
111 let detached = TextHandler::new_detached();
112 detached.insert_utf8(0, "abcd")?;
113 detached.mark(1, 3, "bold", true.into(), PosType::Unicode)?;
114
115 let attached = root.insert_container("body", detached.clone())?;
116
117 assert!(!detached.is_attached());
118 assert!(detached.doc().is_none());
119 assert!(detached.get_attached().is_some());
120 assert!(attached.is_attached());
121 assert!(attached.doc().is_some());
122 assert!(attached.version_id().is_some());
123 assert_eq!(attached.len_utf8(), 4);
124 assert_eq!(attached.len_unicode(), 4);
125 assert_eq!(attached.len_utf16(), 4);
126 assert_eq!(attached.char_at(1, PosType::Unicode)?, 'b');
127 assert_eq!(attached.slice(1, 3, PosType::Unicode)?, "bc");
128 assert_eq!(attached.slice_utf16(1, 3)?, "bc");
129 assert_eq!(
130 attached.convert_pos(2, PosType::Unicode, PosType::Bytes),
131 Some(2)
132 );
133 assert_eq!(
134 attached.convert_pos(2, PosType::Bytes, PosType::Unicode),
135 Some(2)
136 );
137
138 let mut seen = Vec::new();
139 attached.iter(|chunk| {
140 seen.push(chunk.to_owned());
141 false
142 });
143 assert_eq!(seen, vec!["a".to_string()]);
144
145 assert_eq!(
146 attached.slice_delta(0, attached.len_unicode(), PosType::Unicode)?,
147 vec![
148 TextDelta::Insert {
149 insert: "a".to_string(),
150 attributes: None,
151 },
152 TextDelta::Insert {
153 insert: "bc".to_string(),
154 attributes: Some([("bold".to_string(), true.into())].into_iter().collect()),
155 },
156 TextDelta::Insert {
157 insert: "d".to_string(),
158 attributes: None,
159 },
160 ]
161 );
162 assert_eq!(
163 attached.get_richtext_value().to_json_value(),
164 json!([
165 {"insert": "a"},
166 {"insert": "bc", "attributes": {"bold": true}},
167 {"insert": "d"}
168 ])
169 );
170 assert_eq!(
171 root.get_deep_value().to_json_value(),
172 json!({"body": "abcd"})
173 );
174
175 let handler = attached.to_handler();
176 assert_eq!(handler.kind(), ContainerType::Text);
177 assert!(handler.is_attached());
178 assert!(handler.attached_handler().is_some());
179 assert!(handler.doc().is_some());
180 assert_eq!(handler.get_value().to_json_value(), json!("abcd"));
181 assert_eq!(handler.get_deep_value().to_json_value(), json!("abcd"));
182 assert!(<TextHandler as HandlerTrait>::from_handler(handler.clone()).is_some());
183 assert!(<Handler as HandlerTrait>::from_handler(handler.clone()).is_some());
184
185 let before = attached.version_id();
186 attached.insert_utf8(attached.len_utf8(), "!")?;
187 attached.delete_utf16(1, 2)?;
188 assert_ne!(attached.version_id(), before);
189 assert_eq!(
190 attached.slice(0, attached.len_unicode(), PosType::Unicode)?,
191 "ad!"
192 );
193 assert_eq!(
194 root.get_deep_value().to_json_value(),
195 json!({"body": "ad!"})
196 );
197
198 handler.clear()?;
199 assert_eq!(attached.len_unicode(), 0);
200 assert_eq!(attached.len_utf8(), 0);
201 assert!(attached.version_id().is_some());
202 let cursor = attached
203 .get_cursor(0, Side::Middle)
204 .expect("empty attached text");
205 assert_eq!(cursor.id, None);
206 assert_eq!(cursor.side, Side::Left);
207 assert_eq!(attached.char_at(0, PosType::Unicode).is_err(), true);
208 assert_eq!(attached.get_richtext_value().to_json_value(), json!([]));
209
210 attached.diagnose();
211 assert!(attached.char_at(0, PosType::Unicode).is_err());
212 assert!(attached.slice(1, 0, PosType::Unicode).is_err());
213
214 Ok(())
215}
216
217#[test]
218fn text_handler_position_edge_cases_cover_unsupported_and_empty_ranges() -> LoroResult<()> {
219 let text = TextHandler::new_detached();
220 text.insert_utf8(0, "ab")?;
221
222 assert_eq!(
223 text.get_delta(),
224 vec![TextDelta::Insert {
225 insert: "ab".to_string(),
226 attributes: None,
227 }]
228 );
229 assert_eq!(
230 text.convert_pos(0, PosType::Unicode, PosType::Unicode),
231 Some(0)
232 );
233 assert_eq!(text.convert_pos(0, PosType::Entity, PosType::Unicode), None);
234 assert_eq!(text.convert_pos(0, PosType::Unicode, PosType::Entity), None);
235 assert_eq!(text.convert_pos(10, PosType::Unicode, PosType::Bytes), None);
236 assert_eq!(text.slice(1, 1, PosType::Unicode)?, "");
237 assert_eq!(text.slice_utf16(1, 1)?, "");
238
239 let doc = LoroDoc::new_auto_commit();
240 let root = doc.get_map("root");
241 let attached = root.insert_container("body", text.clone())?;
242
243 assert!(attached.is_attached());
244 assert_eq!(
245 attached.get_delta(),
246 vec![TextDelta::Insert {
247 insert: "ab".to_string(),
248 attributes: None,
249 }]
250 );
251 assert_eq!(
252 attached.convert_pos(0, PosType::Entity, PosType::Unicode),
253 None
254 );
255 assert_eq!(
256 attached.convert_pos(attached.len_unicode(), PosType::Unicode, PosType::Unicode),
257 Some(attached.len_unicode())
258 );
259 assert_eq!(
260 attached.convert_pos(attached.len_unicode() + 1, PosType::Unicode, PosType::Bytes),
261 None
262 );
263 assert_eq!(attached.slice(0, 0, PosType::Unicode)?, "");
264
265 Ok(())
266}