summaryrefslogtreecommitdiff
path: root/t/01item-text.t
blob: ca5a5fe3610f432b563d56f51afd1c21b55cd440 (plain)
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

use Tickit::Test 0.12;
use Tickit::RenderBuffer;

use Tickit::Widget::Scroller::Item::Text;

my $term = mk_term;

my $item = Tickit::Widget::Scroller::Item::Text->new( "My message here" );

isa_ok( $item, "Tickit::Widget::Scroller::Item::Text", '$item' );

is_deeply( [ $item->chunks ],
           [ [ "My message here", 15 ] ],
           '$item->chunks' );

is( $item->height_for_width( 80 ), 1, 'height_for_width 80' );

my $rb = Tickit::RenderBuffer->new( lines => $term->lines, cols => $term->cols );

$item->render( $rb, top => 0, firstline => 0, lastline => 0, width => 80, height => 25 );
$rb->flush_to_term( $term );

flush_tickit;

is_termlog( [ GOTO(0,0),
              SETPEN,
              PRINT("My message here"),
              SETBG(undef),
              ERASECH(65) ],
            'Termlog for render fullwidth' );

is_display( [ [TEXT("My message here")] ],
            'Display for render fullwidth' );

$term->clear;
drain_termlog;

{
   {
      $rb->save;

      $rb->clip( Tickit::Rect->new(
         top   => 0,
         left  => 0,
         lines => 10,
         cols  => 12,
      ) );

      is( $item->height_for_width( 12 ), 2, 'height_for_width 12' );

      $item->render( $rb, top => 0, firstline => 0, lastline => 1, width => 12, height => 10 );

      $rb->restore;
   }

   $rb->flush_to_term( $term );

   flush_tickit;

   is_termlog( [ GOTO(0,0),
                 SETPEN,
                 PRINT("My message "),
                 SETBG(undef),
                 ERASECH(1),
                 GOTO(1,0),
                 SETPEN,
                 PRINT("here"),
                 SETBG(undef),
                 ERASECH(8) ],
               'Termlog for render width 12' );

   is_display( [ [TEXT("My message")],
                 [TEXT("here")] ],
               'Display for render width 12' );

   my $indenteditem = Tickit::Widget::Scroller::Item::Text->new( "My message here", indent => 4 );

   is( $indenteditem->height_for_width( 12 ), 2, 'height_for_width 12 with indent' );

   $indenteditem->render( $rb, top => 0, firstline => 0, lastline => 1, width => 12, height => 10 );
   $rb->flush_to_term( $term );

   flush_tickit;

   is_termlog( [ GOTO(0,0),
                 SETPEN,
                 PRINT("My message "),
                 SETBG(undef),
                 ERASECH(1),
                 GOTO(1,0),
                 SETBG(undef),
                 ERASECH(4,1),
                 SETPEN,
                 PRINT("here"),
                 SETBG(undef),
                 ERASECH(4) ],
               'Termlog for render width 12 with indent' );

   is_display( [ [TEXT("My message")],
                 [TEXT("    here")] ],
               'Display for render width 12 with indent' );
}

# Boundary condition in whitespace splitting
{
   $term->clear;
   drain_termlog;

   my $item = Tickit::Widget::Scroller::Item::Text->new( "AAAA BBBB CCCC DDDD" );

   is( $item->height_for_width( 9 ), 2, 'height_for_width 2 for splitting boundary' );

   $item->render( $rb, top => 0, firstline => 0, lastline => 1, width => 9, height => 2 );
   $rb->flush_to_term( $term );

   flush_tickit;

   is_termlog( [ GOTO(0,0),
                 SETPEN,
                 PRINT("AAAA BBBB"),
                 GOTO(1,0),
                 SETPEN,
                 PRINT("CCCC DDDD") ],
               'Termlog for render splitting boundary' );

   is_display( [ [TEXT("AAAA BBBB")],
                 [TEXT("CCCC DDDD")] ],
               'Display for render splitting boundary' );
}

# Linefeeds
{
   $term->clear;
   drain_termlog;

   my $item = Tickit::Widget::Scroller::Item::Text->new( "Some more text\nwith linefeeds" );

   is_deeply( [ $item->chunks ],
              [ [ "Some more text", 14, linebreak => 1 ],
                [ "with linefeeds", 14 ] ],
              '$item->chunks with linefeeds' );

   is( $item->height_for_width( 80 ), 2, 'height_for_width 2 with linefeeds' );

   $item->render( $rb, top => 0, firstline => 0, lastline => 1, width => 80, height => 2 );
   $rb->flush_to_term( $term );

   flush_tickit;

   is_termlog( [ GOTO(0,0),
                 SETPEN,
                 PRINT("Some more text"),
                 SETPEN,
                 ERASECH(66),
                 GOTO(1,0),
                 SETPEN,
                 PRINT("with linefeeds"),
                 SETPEN,
                 ERASECH(66) ],
               'Termlog for render with linefeeds' );

   is_display( [ [TEXT("Some more text")],
                 [TEXT("with linefeeds")] ],
               'Display for render with linefeeds' );
}

# Odd Unicode
{
   use utf8;

   $term->clear;
   drain_termlog;

   my $item = Tickit::Widget::Scroller::Item::Text->new( "(ノಠ益ಠ)ノ彡┻━┻" );

   is_deeply( [ $item->chunks ],
              [ [ "(ノಠ益ಠ)ノ彡┻━┻", 15 ] ],
              '$item->chunks with Unicode' );

   is( $item->height_for_width( 80 ), 1, 'height_for_width 2 with Unicode' );

   $item->render( $rb, top => 0, firstline => 0, lastline => 0, width => 80, height => 1 );
   $rb->flush_to_term( $term );

   flush_tickit;

   is_termlog( [ GOTO(0,0),
                 SETPEN,
                 PRINT("(ノಠ益ಠ)ノ彡┻━┻"),
                 SETPEN,
                 ERASECH(65) ],
               'Termlog for render with Unicode' );

   is_display( [ [TEXT("(ノಠ益ಠ)ノ彡┻━┻")] ],
               'Display for render with Unicode' );
}

done_testing;