File: 05_text2html.t

package info (click to toggle)
noss 1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 704 kB
  • sloc: perl: 6,571; sh: 569; xml: 294; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 1,941 bytes parent folder | download
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
#!/usr/bin/perl
use 5.016;
use strict;

use Test::More;

use WWW::Noss::TextToHtml qw(text2html escape_html unescape_html strip_tags);

my $TEST_TEXT = <<'HERE';
> This is a paragraph.

>< This is another paragraph.

>& This is an additional paragraph.
HERE

my $TEST_ENTITY = '<>< && << >> &&';

my $text2html = text2html($TEST_TEXT);

like(
    $text2html,
    qr/\Q&gt; This is a paragraph.\E/,
    'text2html retained paragraphs'
);
like(
    $text2html,
    qr/\Q&gt;&lt; This is another paragraph.\E/,
    'text2html retained paragraphs'
);
like(
    $text2html,
    qr/\Q&gt;&amp; This is an additional paragraph.\E/,
    'text2html retained paragraphs'
);
like(
    $text2html,
    qr/(<p>.+?<\/p>.*?){3}/s,
    'text2html added paragraph tags'
);

is(
    escape_html($TEST_ENTITY),
    '&lt;&gt;&lt; &amp;&amp; &lt;&lt; &gt;&gt; &amp;&amp;',
    'escape_html performed entity conversions correctly'
);

subtest 'unescape_html ok' => sub {
    is(
        unescape_html('&#72;&#69;&#76;&#76;&#79;'),
        'HELLO',
        'numerical entities ok'
    );
    is(
        unescape_html('&#x0048;&#x0045;&#x004c;&#x004c;&#x004f;'),
        'HELLO',
        'hexadecimal entities ok'
    );
    is(
        unescape_html('&Alpha;&Beta;&Gamma;&Delta;&Epsilon;'),
        join('', map { chr } 913 .. 917),
        'named entities ok'
    );
    is(
        unescape_html('&#x0026;amp&#x003b;'),
        '&amp;',
        'expanding into entities ok'
    );
};

subtest 'strip_tags ok' => sub {
    is(
        strip_tags('<p>Some</p><h1>Test</h1><div class="test">Text</div>'),
        'SomeTestText',
        'strip_tags ok'
    );
    is(
        strip_tags('<p <!-- yadda yadda -->>Test</p><h1 <!-- -->>Text</h1>'),
        'TestText',
        'nested tags ok'
    );
    is(
        strip_tags('<p>Test<![CDATA[ < > & >:-) ]]>Text</p>'),
        'TestText',
        'CDATA ok'
    );
};

done_testing;

# vim: expandtab shiftwidth=4