revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Assertion utilities for testing

use crate::render::Buffer;

/// Result of an assertion
#[derive(Debug, Clone)]
pub enum AssertionResult {
    /// Assertion passed
    Pass,
    /// Assertion failed with message
    Fail(String),
}

impl AssertionResult {
    /// Check if passed
    pub fn is_pass(&self) -> bool {
        matches!(self, AssertionResult::Pass)
    }

    /// Check if failed
    pub fn is_fail(&self) -> bool {
        matches!(self, AssertionResult::Fail(_))
    }

    /// Unwrap or panic with message
    pub fn unwrap(self) {
        if let AssertionResult::Fail(msg) = self {
            panic!("Assertion failed: {}", msg);
        }
    }
}

/// An assertion that can be run against a buffer
pub trait Assertion {
    /// Run the assertion
    fn check(&self, buffer: &Buffer) -> AssertionResult;

    /// Get assertion description
    fn description(&self) -> String;
}

/// Assert that screen contains text
#[cfg(test)]
pub struct ContainsText {
    text: String,
}

#[cfg(test)]
impl ContainsText {
    /// Create new assertion
    pub fn new(text: impl Into<String>) -> Self {
        Self { text: text.into() }
    }
}

#[cfg(test)]
impl Assertion for ContainsText {
    fn check(&self, buffer: &Buffer) -> AssertionResult {
        let screen = buffer_to_string(buffer);
        if screen.contains(&self.text) {
            AssertionResult::Pass
        } else {
            AssertionResult::Fail(format!(
                "Expected screen to contain '{}', but it didn't.\nScreen:\n{}",
                self.text, screen
            ))
        }
    }

    fn description(&self) -> String {
        format!("Screen contains '{}'", self.text)
    }
}

/// Assert that screen does not contain text
#[cfg(test)]
pub struct NotContainsText {
    text: String,
}

#[cfg(test)]
impl NotContainsText {
    /// Create new assertion
    pub fn new(text: impl Into<String>) -> Self {
        Self { text: text.into() }
    }
}

#[cfg(test)]
impl Assertion for NotContainsText {
    fn check(&self, buffer: &Buffer) -> AssertionResult {
        let screen = buffer_to_string(buffer);
        if !screen.contains(&self.text) {
            AssertionResult::Pass
        } else {
            AssertionResult::Fail(format!(
                "Expected screen NOT to contain '{}', but it did.\nScreen:\n{}",
                self.text, screen
            ))
        }
    }

    fn description(&self) -> String {
        format!("Screen does not contain '{}'", self.text)
    }
}

/// Assert that a specific line contains text
#[cfg(test)]
pub struct LineContains {
    line: u16,
    text: String,
}

#[cfg(test)]
impl LineContains {
    /// Create new assertion
    pub fn new(line: u16, text: impl Into<String>) -> Self {
        Self {
            line,
            text: text.into(),
        }
    }
}

#[cfg(test)]
impl Assertion for LineContains {
    fn check(&self, buffer: &Buffer) -> AssertionResult {
        let line_text = get_line(buffer, self.line);
        if line_text.contains(&self.text) {
            AssertionResult::Pass
        } else {
            AssertionResult::Fail(format!(
                "Expected line {} to contain '{}', but got: '{}'",
                self.line, self.text, line_text
            ))
        }
    }

    fn description(&self) -> String {
        format!("Line {} contains '{}'", self.line, self.text)
    }
}

/// Assert cell has specific character
#[cfg(test)]
pub struct CellEquals {
    x: u16,
    y: u16,
    expected: char,
}

#[cfg(test)]
impl CellEquals {
    /// Create new assertion
    pub fn new(x: u16, y: u16, expected: char) -> Self {
        Self { x, y, expected }
    }
}

#[cfg(test)]
impl Assertion for CellEquals {
    fn check(&self, buffer: &Buffer) -> AssertionResult {
        if let Some(cell) = buffer.get(self.x, self.y) {
            if cell.symbol == self.expected {
                AssertionResult::Pass
            } else {
                AssertionResult::Fail(format!(
                    "Expected cell ({}, {}) to be '{}', but got '{}'",
                    self.x, self.y, self.expected, cell.symbol
                ))
            }
        } else {
            AssertionResult::Fail(format!("Cell ({}, {}) is out of bounds", self.x, self.y))
        }
    }

    fn description(&self) -> String {
        format!("Cell ({}, {}) equals '{}'", self.x, self.y, self.expected)
    }
}

/// Assert screen matches exact text
#[cfg(test)]
pub struct ScreenEquals {
    expected: String,
}

#[cfg(test)]
impl ScreenEquals {
    /// Create new assertion
    pub fn new(expected: impl Into<String>) -> Self {
        Self {
            expected: expected.into(),
        }
    }
}

#[cfg(test)]
impl Assertion for ScreenEquals {
    fn check(&self, buffer: &Buffer) -> AssertionResult {
        let actual = buffer_to_string(buffer);
        let expected_trimmed = self.expected.trim();
        let actual_trimmed = actual.trim();

        if actual_trimmed == expected_trimmed {
            AssertionResult::Pass
        } else {
            AssertionResult::Fail(format!(
                "Screen does not match expected.\nExpected:\n{}\n\nActual:\n{}",
                expected_trimmed, actual_trimmed
            ))
        }
    }

    fn description(&self) -> String {
        "Screen matches expected text".to_string()
    }
}

// Helper functions

#[cfg(test)]
fn buffer_to_string(buffer: &Buffer) -> String {
    let mut lines = Vec::new();
    for y in 0..buffer.height() {
        let mut line = String::new();
        for x in 0..buffer.width() {
            if let Some(cell) = buffer.get(x, y) {
                line.push(cell.symbol);
            } else {
                line.push(' ');
            }
        }
        lines.push(line.trim_end().to_string());
    }

    // Remove trailing empty lines
    while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
        lines.pop();
    }

    lines.join("\n")
}

#[cfg(test)]
fn get_line(buffer: &Buffer, row: u16) -> String {
    if row >= buffer.height() {
        return String::new();
    }

    let mut line = String::new();
    for x in 0..buffer.width() {
        if let Some(cell) = buffer.get(x, row) {
            line.push(cell.symbol);
        } else {
            line.push(' ');
        }
    }
    line.trim_end().to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::render::Cell;

    fn make_buffer(text: &str) -> Buffer {
        let lines: Vec<&str> = text.lines().collect();
        let height = lines.len() as u16;
        let width = lines.iter().map(|l| l.len()).max().unwrap_or(0) as u16;

        let mut buffer = Buffer::new(width.max(1), height.max(1));
        for (y, line) in lines.iter().enumerate() {
            for (x, ch) in line.chars().enumerate() {
                buffer.set(x as u16, y as u16, Cell::new(ch));
            }
        }
        buffer
    }

    #[test]
    fn test_contains_text_pass() {
        let buffer = make_buffer("Hello, World!");
        let assertion = ContainsText::new("World");
        assert!(assertion.check(&buffer).is_pass());
    }

    #[test]
    fn test_contains_text_fail() {
        let buffer = make_buffer("Hello, World!");
        let assertion = ContainsText::new("Goodbye");
        assert!(assertion.check(&buffer).is_fail());
    }

    #[test]
    fn test_not_contains_text() {
        let buffer = make_buffer("Hello, World!");
        let assertion = NotContainsText::new("Goodbye");
        assert!(assertion.check(&buffer).is_pass());
    }

    #[test]
    fn test_line_contains() {
        let buffer = make_buffer("Line 1\nLine 2\nLine 3");
        let assertion = LineContains::new(1, "Line 2");
        assert!(assertion.check(&buffer).is_pass());
    }

    #[test]
    fn test_cell_equals() {
        let buffer = make_buffer("ABC");
        let assertion = CellEquals::new(1, 0, 'B');
        assert!(assertion.check(&buffer).is_pass());
    }

    #[test]
    fn test_assertion_result_clone() {
        let result = AssertionResult::Pass;
        let cloned = result.clone();
        assert!(cloned.is_pass());

        let fail = AssertionResult::Fail("error".to_string());
        let fail_cloned = fail.clone();
        assert!(fail_cloned.is_fail());
    }

    #[test]
    fn test_assertion_result_debug() {
        let pass = AssertionResult::Pass;
        let debug = format!("{:?}", pass);
        assert!(debug.contains("Pass"));

        let fail = AssertionResult::Fail("test error".to_string());
        let debug = format!("{:?}", fail);
        assert!(debug.contains("Fail"));
        assert!(debug.contains("test error"));
    }

    #[test]
    #[should_panic(expected = "Assertion failed")]
    fn test_assertion_result_unwrap_fail() {
        let result = AssertionResult::Fail("test failure".to_string());
        result.unwrap();
    }

    #[test]
    fn test_assertion_result_unwrap_pass() {
        let result = AssertionResult::Pass;
        result.unwrap(); // Should not panic
    }

    #[test]
    fn test_contains_text_description() {
        let assertion = ContainsText::new("test");
        assert_eq!(assertion.description(), "Screen contains 'test'");
    }

    #[test]
    fn test_not_contains_text_description() {
        let assertion = NotContainsText::new("test");
        assert_eq!(assertion.description(), "Screen does not contain 'test'");
    }

    #[test]
    fn test_not_contains_text_fail() {
        let buffer = make_buffer("Hello, World!");
        let assertion = NotContainsText::new("Hello");
        assert!(assertion.check(&buffer).is_fail());
    }

    #[test]
    fn test_line_contains_description() {
        let assertion = LineContains::new(5, "text");
        assert_eq!(assertion.description(), "Line 5 contains 'text'");
    }

    #[test]
    fn test_line_contains_fail() {
        let buffer = make_buffer("Line 1\nLine 2\nLine 3");
        let assertion = LineContains::new(1, "foo");
        assert!(assertion.check(&buffer).is_fail());
    }

    #[test]
    fn test_line_contains_out_of_bounds() {
        let buffer = make_buffer("Line 1\nLine 2");
        let assertion = LineContains::new(10, "text"); // Out of bounds
                                                       // Out of bounds line should fail (empty line)
        assert!(assertion.check(&buffer).is_fail());
    }

    #[test]
    fn test_cell_equals_description() {
        let assertion = CellEquals::new(5, 10, 'X');
        assert_eq!(assertion.description(), "Cell (5, 10) equals 'X'");
    }

    #[test]
    fn test_cell_equals_fail() {
        let buffer = make_buffer("ABC");
        let assertion = CellEquals::new(0, 0, 'Z');
        assert!(assertion.check(&buffer).is_fail());
    }

    #[test]
    fn test_cell_equals_out_of_bounds() {
        let buffer = make_buffer("ABC");
        let assertion = CellEquals::new(100, 100, 'X');
        let result = assertion.check(&buffer);
        assert!(result.is_fail());
    }

    #[test]
    fn test_screen_equals_pass() {
        let buffer = make_buffer("Hello\nWorld");
        let assertion = ScreenEquals::new("Hello\nWorld");
        assert!(assertion.check(&buffer).is_pass());
    }

    #[test]
    fn test_screen_equals_fail() {
        let buffer = make_buffer("Hello\nWorld");
        let assertion = ScreenEquals::new("Goodbye\nWorld");
        assert!(assertion.check(&buffer).is_fail());
    }

    #[test]
    fn test_screen_equals_description() {
        let assertion = ScreenEquals::new("test");
        assert_eq!(assertion.description(), "Screen matches expected text");
    }

    #[test]
    fn test_screen_equals_trims_whitespace() {
        let buffer = make_buffer("Hello");
        let assertion = ScreenEquals::new("  Hello  ");
        assert!(assertion.check(&buffer).is_pass());
    }

    #[test]
    fn test_buffer_to_string_empty() {
        let buffer = Buffer::new(5, 5);
        let s = buffer_to_string(&buffer);
        assert!(s.is_empty() || s.chars().all(|c| c.is_whitespace() || c == '\n'));
    }

    #[test]
    fn test_get_line_out_of_bounds() {
        let buffer = Buffer::new(10, 5);
        let line = get_line(&buffer, 100);
        assert!(line.is_empty());
    }

    #[test]
    fn test_multiline_buffer() {
        let buffer = make_buffer("Line A\nLine B\nLine C");

        // Test contains across lines
        let assertion = ContainsText::new("Line B");
        assert!(assertion.check(&buffer).is_pass());

        // Test specific lines
        let assertion = LineContains::new(0, "Line A");
        assert!(assertion.check(&buffer).is_pass());

        let assertion = LineContains::new(2, "Line C");
        assert!(assertion.check(&buffer).is_pass());
    }
}