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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Edge case tests for complex widgets
//!
//! Tests edge cases for more complex interactive widgets:
//! - QrCode: empty data, very long data, unicode, special characters
//! - Image: invalid paths, unsupported formats, zero dimensions
//! - Link: empty URLs, very long URLs, unicode
//! - Pagination: edge cases for page navigation

use revue::layout::Rect;
use revue::render::Buffer;
use revue::widget::traits::{RenderContext, View};
use revue::widget::Link;
use revue::widget::Pagination;

#[cfg(feature = "qrcode")]
use revue::widget::{ErrorCorrection, QrCodeWidget, QrStyle};

#[cfg(feature = "image")]
use revue::widget::{Image, ImageError, ScaleMode};

/// Test QrCode widget edge cases
#[cfg(feature = "qrcode")]
mod qrcode_edge_cases {
    use super::*;

    #[test]
    fn test_qrcode_with_empty_data() {
        let qr = QrCodeWidget::new("");
        let mut buffer = Buffer::new(50, 50);
        let area = Rect::new(0, 0, 50, 50);
        let mut ctx = RenderContext::new(&mut buffer, area);

        // Should not panic with empty data
        qr.render(&mut ctx);
    }

    #[test]
    fn test_qrcode_with_very_long_data() {
        let long_data = "A".repeat(10000);
        let qr = QrCodeWidget::new(&long_data);
        let mut buffer = Buffer::new(100, 100);
        let area = Rect::new(0, 0, 100, 100);
        let mut ctx = RenderContext::new(&mut buffer, area);

        // Should handle long data (may truncate or fail gracefully)
        qr.render(&mut ctx);
    }

    #[test]
    fn test_qrcode_with_unicode_data() {
        let unicode_data = "안녕하세요 🎉";
        let qr = QrCodeWidget::new(unicode_data);
        let mut buffer = Buffer::new(50, 50);
        let area = Rect::new(0, 0, 50, 50);
        let mut ctx = RenderContext::new(&mut buffer, area);

        qr.render(&mut ctx);
    }

    #[test]
    fn test_qrcode_with_special_characters() {
        let special_data = "\t\r\n\x00\x01";
        let qr = QrCodeWidget::new(special_data);
        let mut buffer = Buffer::new(50, 50);
        let area = Rect::new(0, 0, 50, 50);
        let mut ctx = RenderContext::new(&mut buffer, area);

        qr.render(&mut ctx);
    }

    #[test]
    fn test_qrcode_with_zero_quiet_zone() {
        let qr = QrCodeWidget::new("Test").quiet_zone(0);
        let mut buffer = Buffer::new(50, 50);
        let area = Rect::new(0, 0, 50, 50);
        let mut ctx = RenderContext::new(&mut buffer, area);

        qr.render(&mut ctx);
    }

    #[test]
    fn test_qrcode_with_large_quiet_zone() {
        let qr = QrCodeWidget::new("Test").quiet_zone(10);
        let mut buffer = Buffer::new(100, 100);
        let area = Rect::new(0, 0, 100, 100);
        let mut ctx = RenderContext::new(&mut buffer, area);

        qr.render(&mut ctx);
    }

    #[test]
    fn test_qrcode_all_styles() {
        let styles = [
            QrStyle::HalfBlock,
            QrStyle::FullBlock,
            QrStyle::Ascii,
            QrStyle::Braille,
        ];

        for style in styles {
            let qr = QrCodeWidget::new("Test").style(style);
            let mut buffer = Buffer::new(50, 50);
            let area = Rect::new(0, 0, 50, 50);
            let mut ctx = RenderContext::new(&mut buffer, area);

            qr.render(&mut ctx);
        }
    }

    #[test]
    fn test_qrcode_with_zero_width_buffer() {
        let qr = QrCodeWidget::new("Test");
        let mut buffer = Buffer::new(0, 10);
        let area = Rect::new(0, 0, 0, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        qr.render(&mut ctx);
    }

    #[test]
    fn test_qrcode_with_zero_height_buffer() {
        let qr = QrCodeWidget::new("Test");
        let mut buffer = Buffer::new(10, 0);
        let area = Rect::new(0, 0, 10, 0);
        let mut ctx = RenderContext::new(&mut buffer, area);

        qr.render(&mut ctx);
    }

    #[test]
    fn test_qrcode_inverted_colors() {
        let qr = QrCodeWidget::new("Test").inverted(true);
        let mut buffer = Buffer::new(50, 50);
        let area = Rect::new(0, 0, 50, 50);
        let mut ctx = RenderContext::new(&mut buffer, area);

        qr.render(&mut ctx);
    }

    #[test]
    fn test_qrcode_data_update() {
        let mut qr = QrCodeWidget::new("Initial");
        qr.set_data("Updated");
        // data field is private, just verify the method doesn't panic
        let _size = qr.required_size();
    }

    #[test]
    fn test_qrcode_all_error_levels() {
        let levels = [
            ErrorCorrection::Low,
            ErrorCorrection::Medium,
            ErrorCorrection::Quartile,
            ErrorCorrection::High,
        ];

        for level in levels {
            let qr = QrCodeWidget::new("Test").error_correction(level);
            let size = qr.required_size();
            assert!(size.is_some());
        }
    }

    #[test]
    fn test_qrcode_with_url() {
        let urls = [
            "https://example.com",
            "http://very-long-domain-name.example.com/path/to/resource?query=value&another=param",
            "ftp://files.example.com/file.txt",
        ];

        for url in urls {
            let qr = QrCodeWidget::new(url);
            let size = qr.required_size();
            assert!(size.is_some());
        }
    }

    #[test]
    fn test_qrcode_required_size_all_styles() {
        let styles = [
            QrStyle::HalfBlock,
            QrStyle::FullBlock,
            QrStyle::Ascii,
            QrStyle::Braille,
        ];

        for style in styles {
            let qr = QrCodeWidget::new("Test").style(style);
            let size = qr.required_size();
            assert!(size.is_some());
        }
    }

    #[test]
    fn test_qrcode_with_newline_data() {
        let qr = QrCodeWidget::new("Line 1\nLine 2");
        let mut buffer = Buffer::new(50, 50);
        let area = Rect::new(0, 0, 50, 50);
        let mut ctx = RenderContext::new(&mut buffer, area);

        qr.render(&mut ctx);
    }
}

/// Test Image widget edge cases
#[cfg(feature = "image")]
mod image_edge_cases {
    use super::*;

    /// Create a minimal test image (1x1 red pixel)
    fn test_image() -> Image {
        Image::from_rgb(vec![255, 0, 0], 1, 1)
    }

    #[test]
    fn test_image_with_nonexistent_path() {
        let result = Image::try_from_file("/nonexistent/path/to/image.png");
        assert!(result.is_none(), "Should return None for nonexistent path");
    }

    #[test]
    fn test_image_from_file_error() {
        let result = Image::from_file("/nonexistent/path/to/image.png");
        assert!(result.is_err());
        if let Err(ImageError::FileRead { path, .. }) = result {
            assert!(path.to_str().unwrap().contains("nonexistent"));
        } else {
            panic!("Expected FileRead error");
        }
    }

    #[test]
    fn test_image_with_empty_path() {
        let result = Image::try_from_file("");
        assert!(result.is_none(), "Should return None for empty path");
    }

    #[test]
    fn test_image_with_zero_width_buffer() {
        let image = test_image();
        let mut buffer = Buffer::new(0, 10);
        let area = Rect::new(0, 0, 0, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        // Should handle gracefully
        image.render(&mut ctx);
    }

    #[test]
    fn test_image_with_zero_height_buffer() {
        let image = test_image();
        let mut buffer = Buffer::new(10, 0);
        let area = Rect::new(0, 0, 10, 0);
        let mut ctx = RenderContext::new(&mut buffer, area);

        image.render(&mut ctx);
    }

    #[test]
    fn test_image_with_both_zero() {
        let image = test_image();
        let mut buffer = Buffer::new(0, 0);
        let area = Rect::new(0, 0, 0, 0);
        let mut ctx = RenderContext::new(&mut buffer, area);

        image.render(&mut ctx);
    }

    #[test]
    fn test_image_width_height_getters() {
        let image = Image::from_rgb(vec![0; 12], 4, 3);
        assert_eq!(image.width(), 4);
        assert_eq!(image.height(), 3);
    }

    #[test]
    fn test_image_with_large_dimensions() {
        // Create a large image (10000x10000 would be too much memory, use smaller)
        let large_data = vec![0u8; 100 * 100 * 3]; // 100x100 RGB
        let image = Image::from_rgb(large_data, 100, 100);

        assert_eq!(image.width(), 100);
        assert_eq!(image.height(), 100);

        // Rendering with small buffer should clip
        let mut buffer = Buffer::new(10, 10);
        let area = Rect::new(0, 0, 10, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        image.render(&mut ctx);
    }

    #[test]
    fn test_image_scale_modes() {
        // Test all scale modes (create separate images since Image doesn't implement Clone)
        let _ = test_image().scale(ScaleMode::Fit);
        let _ = test_image().scale(ScaleMode::Fill);
        let _ = test_image().scale(ScaleMode::Stretch);
        let _ = test_image().scale(ScaleMode::None);
    }

    #[test]
    fn test_image_placeholder() {
        let image = Image::from_rgb(vec![255, 0, 0], 2, 2).placeholder('#');
        let mut buffer = Buffer::new(5, 5);
        let area = Rect::new(0, 0, 5, 5);
        let mut ctx = RenderContext::new(&mut buffer, area);

        image.render(&mut ctx);

        // Check placeholder was rendered
        assert_eq!(buffer.get(0, 0).unwrap().symbol, '#');
    }

    #[test]
    fn test_image_scaled_dimensions_fit() {
        let image = Image::from_rgb(vec![0; 600], 200, 100); // 2:1 aspect
        let (w, h) = image.scaled_dimensions(80, 40);
        assert_eq!(w, 80);
        assert_eq!(h, 40);
    }

    #[test]
    fn test_image_scaled_dimensions_stretch() {
        let image = Image::from_rgb(vec![0; 300], 10, 10).scale(ScaleMode::Stretch);
        let (w, h) = image.scaled_dimensions(80, 24);
        assert_eq!(w, 80);
        assert_eq!(h, 24);
    }

    #[test]
    fn test_image_rgba() {
        let data = vec![255, 0, 0, 255, 0, 255, 0, 255]; // 2 pixels
        let image = Image::from_rgba(data, 2, 1);
        assert_eq!(image.width(), 2);
        assert_eq!(image.height(), 1);
    }
}

/// Test Link widget edge cases
mod link_edge_cases {
    use super::*;

    #[test]
    fn test_link_with_empty_url() {
        let link = Link::new("");
        let mut buffer = Buffer::new(20, 10);
        let area = Rect::new(0, 0, 20, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        link.render(&mut ctx);
    }

    #[test]
    fn test_link_with_empty_label() {
        let link = Link::new("https://example.com").text("");
        let mut buffer = Buffer::new(20, 10);
        let area = Rect::new(0, 0, 20, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        link.render(&mut ctx);
    }

    #[test]
    fn test_link_with_both_empty() {
        let link = Link::new("").text("");
        let mut buffer = Buffer::new(20, 10);
        let area = Rect::new(0, 0, 20, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        link.render(&mut ctx);
    }

    #[test]
    fn test_link_with_very_long_url() {
        let long_url = "https://example.com/".repeat(100);
        let link = Link::new(&long_url);
        let mut buffer = Buffer::new(20, 10);
        let area = Rect::new(0, 0, 20, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        link.render(&mut ctx);
    }

    #[test]
    fn test_link_with_very_long_label() {
        let long_label = "A".repeat(1000);
        let link = Link::new("https://example.com").text(&long_label);
        let mut buffer = Buffer::new(20, 10);
        let area = Rect::new(0, 0, 20, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        // Should clip to buffer width
        link.render(&mut ctx);
    }

    #[test]
    fn test_link_with_unicode_label() {
        let link = Link::new("https://example.com").text("클릭하세요 🖱️");
        let mut buffer = Buffer::new(20, 10);
        let area = Rect::new(0, 0, 20, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        link.render(&mut ctx);
    }

    #[test]
    fn test_link_with_special_chars_in_url() {
        let urls = [
            "https://example.com/path?query=value&foo=bar",
            "https://example.com/path#fragment",
            "https://user:pass@example.com/",
            "https://example.com/path/%20%space%",
        ];

        for url in urls {
            let link = Link::new(url);
            let mut buffer = Buffer::new(20, 10);
            let area = Rect::new(0, 0, 20, 10);
            let mut ctx = RenderContext::new(&mut buffer, area);

            link.render(&mut ctx);
        }
    }

    #[test]
    fn test_link_with_zero_width_buffer() {
        let link = Link::new("https://example.com");
        let mut buffer = Buffer::new(0, 10);
        let area = Rect::new(0, 0, 0, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        link.render(&mut ctx);
    }
}

/// Test Pagination widget edge cases
mod pagination_edge_cases {
    use super::*;

    #[test]
    fn test_pagination_with_zero_items() {
        let pagination = Pagination::new(0);
        let mut buffer = Buffer::new(20, 10);
        let area = Rect::new(0, 0, 20, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        pagination.render(&mut ctx);
    }

    #[test]
    fn test_pagination_with_single_item() {
        let pagination = Pagination::new(1);
        let mut buffer = Buffer::new(20, 10);
        let area = Rect::new(0, 0, 20, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        pagination.render(&mut ctx);
    }

    #[test]
    fn test_pagination_with_very_large_total() {
        let pagination = Pagination::new(1000);
        let mut buffer = Buffer::new(20, 10);
        let area = Rect::new(0, 0, 20, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        pagination.render(&mut ctx);
    }

    #[test]
    fn test_pagination_first_page() {
        let pagination = Pagination::new(10).current(1);
        let mut buffer = Buffer::new(30, 10);
        let area = Rect::new(0, 0, 30, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        pagination.render(&mut ctx);
    }

    #[test]
    fn test_pagination_last_page() {
        let pagination = Pagination::new(10).current(10);
        let mut buffer = Buffer::new(30, 10);
        let area = Rect::new(0, 0, 30, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        pagination.render(&mut ctx);
    }

    #[test]
    fn test_pagination_out_of_bounds_page() {
        let pagination = Pagination::new(10).current(100);
        let mut buffer = Buffer::new(30, 10);
        let area = Rect::new(0, 0, 30, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        pagination.render(&mut ctx);
    }

    #[test]
    fn test_pagination_with_zero_width_buffer() {
        let pagination = Pagination::new(10);
        let mut buffer = Buffer::new(0, 10);
        let area = Rect::new(0, 0, 0, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        pagination.render(&mut ctx);
    }

    #[test]
    fn test_pagination_negative_page() {
        // Using current(0) should clamp to 1
        let pagination = Pagination::new(10).current(0);
        let mut buffer = Buffer::new(30, 10);
        let area = Rect::new(0, 0, 30, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        pagination.render(&mut ctx);
    }

    #[test]
    fn test_pagination_exact_multiple() {
        // Items divide evenly
        let pagination = Pagination::new(100);
        let mut buffer = Buffer::new(30, 10);
        let area = Rect::new(0, 0, 30, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        pagination.render(&mut ctx);
    }

    #[test]
    fn test_pagination_with_very_small_buffer() {
        let pagination = Pagination::new(10);
        let mut buffer = Buffer::new(5, 5);
        let area = Rect::new(0, 0, 5, 5);
        let mut ctx = RenderContext::new(&mut buffer, area);

        // Should clip to buffer
        pagination.render(&mut ctx);
    }

    #[test]
    fn test_pagination_all_styles() {
        let styles = [
            revue::widget::PaginationStyle::Full,
            revue::widget::PaginationStyle::Simple,
            revue::widget::PaginationStyle::Compact,
            revue::widget::PaginationStyle::Dots,
        ];

        for style in styles {
            let pagination = Pagination::new(10).style(style);
            let mut buffer = Buffer::new(20, 10);
            let area = Rect::new(0, 0, 20, 10);
            let mut ctx = RenderContext::new(&mut buffer, area);

            pagination.render(&mut ctx);
        }
    }
}