Debian Codegraph @

t/101-inline-tree.t

raw
1 use strict;
2 use warnings;
3 use utf8;
4
5 use Markdown::Perl::InlineTree ':all';
6 use Test2::V0;
7
8 sub text_tree {
9 my $t = Markdown::Perl::InlineTree->new();
10 for (@_) {
11 $t->push(new_text($_)) if $_;
12 $t->push(new_code('ignored)')) unless $_;
13 }
14 return $t;
15 }
16
17 my $t = text_tree('a(bc', '123d(e)f', '', 'gh)i3');
18
19 is([$t->find_in_text(qr/\(/, 0, 0)], [0, 1, 2], 'find_in_text_from_start');
20 is([$t->find_in_text(qr/\(/, 1, 0)], [1, 4, 5], 'find_in_text_from_second_child');
21 is([$t->find_in_text(qr/\(/, 0, 1)], [0, 1, 2], 'find_in_text_from_early_in_first_child');
22 is([$t->find_in_text(qr/\(/, 0, 2)], [1, 4, 5], 'find_in_text_from_far_in_first_child');
23 is([$t->find_in_text(qr/\(/, 0, 2, 2, 0)], [1, 4, 5], 'find_in_text_with_bound');
24 is($t->find_in_text(qr/\(/, 0, 2, 1, 1), U(), 'find_in_text_with_too_small_bound');
25
26 is([$t->find_balanced_in_text(qr/\(/, qr/\)/, 0, 2)], [3, 2, 3], 'find_balanced_in_text');
27
28 is([$t->find_in_text_with_balanced_content(qr/\(/, qr/\)/, qr/./, 0, 0)], [0, 0, 1], 'find_with_balance_anything');
29 is([$t->find_in_text_with_balanced_content(qr/\(/, qr/\)/, qr/\d/, 0, 0)], [3, 4, 5], 'find_with_balance_digit');
30 is([$t->find_in_text_with_balanced_content(qr/\(/, qr/\)/, qr/\d/, 0, 2)], [1, 0, 1], 'find_with_balance_digit_easy');
31
32 is($t->span_to_source_text(1,2,3,4), '3d(e)f<code>ignored)</code>gh)i', 'span_to_source_text');
33
34 my $nt = $t->extract(1, 3, 3, 2);
35 is($nt->fold(sub { $_[1].$_[0]->{content} }, ''), 'd(e)fignored)gh', 'extract_extracted');
36 is($t->fold(sub { $_[1].$_[0]->{content} }, ''), 'a(bc123)i3', 'extract_rest');
37
38 $nt = $t->extract(0, 0, 2, 3);
39 is($nt->fold(sub { $_[1].$_[0]->{content} }, ''), 'a(bc123)i3', 'extract_all');
40 is($t->fold(sub { $_[1].$_[0]->{content} }, ''), '', 'extract_rest_nothing');
41
42 {
43 my $t = text_tree('*foo');
44 my $nt = $t->extract(0, 0, 0, 1);
45 is(scalar(@{$nt->{children}}), 1, 'extract_first_char1');
46 is($nt->{children}[0]{content}, '*', 'extract_first_char2');
47 is(scalar(@{$t->{children}}), 1, 'extract_first_char3');
48 is($t->{children}[0]{content}, 'foo', 'extract_first_char4');
49 }
50
51 {
52 my $t = text_tree('*foo');
53 $t->insert(0, new_text('bar'));
54 my $nt = $t->extract(1, 0, 1, 1);
55 is(scalar(@{$nt->{children}}), 1, 'extract_from_second_child1');
56 is($nt->{children}[0]{content}, '*', 'extract_from_second_child2');
57 is(scalar(@{$t->{children}}), 2, 'extract_from_second_child3');
58 is($t->{children}[1]{content}, 'foo', 'extract_from_second_child4');
59 }
60
61 done_testing;