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
|
#!/usr/bin/env perl
use strict;
use warnings;
use MojoMojo::Formatter::SyntaxHighlight;
use HTTP::Request::Common;
use Test::More;
use Test::Differences;
my ( $content, $got, $expected, $test, $c, $original_formatter );
BEGIN {
plan skip_all => 'Requirements not installed for the Syntax Highlighter formatter'
unless MojoMojo::Formatter::SyntaxHighlight->module_loaded;
plan tests => 15;
use_ok('MojoMojo::Formatter::Textile');
$ENV{CATALYST_CONFIG} = 't/var/mojomojo.yml';
use_ok( 'Catalyst::Test', 'MojoMojo' );
}
END {
ok( $c->pref( main_formatter => $original_formatter ),
'restore original formatter' );
}
( undef, $c ) = ctx_request('/');
ok( $original_formatter = $c->pref('main_formatter'),
'save original formatter' );
ok( $c->pref( main_formatter => 'MojoMojo::Formatter::Textile' ),
'set preferred formatter to Textile' );
$test .= 'single word run through all formatters';
$content = 'palabra';
# We expect to get the word back surrounded with <p> tag and \n added.
$expected = '<p>' . $content . '</p>
';
$got = get( POST '/.jsrpc/render', [ content => $content ] );
is( $got, $expected, $test );
$test = 'single word run through all formatters with textile off';
$content = '==palabra==';
# We expect to get the word back surrounded with only \n added.
$expected = 'palabra
';
$got = get( POST '/.jsrpc/render', [ content => $content ] );
is( $got, $expected, $test );
$test = 'two words run through all formatters';
$content = 'dues palabres';
# We expect to get the two words back surrounded with <p> tag and \n added.
$expected = '<p>' . $content . '</p>
';
$got = get( POST '/.jsrpc/render', [ content => $content ] );
is( $got, $expected, $test );
{
$test = 'Single <code>';
$content = <<HTML;
<pre lang="HTML">
<code>
Ha en god dag
</code>
</pre>
HTML
$got = MojoMojo::Formatter::SyntaxHighlight->format_content( \$content );
$expected = <<'HTML';
<pre>
<b><code></b>
Ha en god dag
<b></code></b>
</pre>
HTML
is( $$got, $expected, $test );
}
#-------------------------------------------------------------------------------
{
$test = 'Single <div> from SyntaxHighlight->format_content';
$content = <<'HTML';
<pre lang="HTML">
<div>
Ha en god dag
</div>
</pre>
HTML
$got = MojoMojo::Formatter::SyntaxHighlight->format_content( \$content );
$expected = '<pre>
<b><div></b>
Ha en god dag
<b></div></b>
</pre>
';
eq_or_diff( $$got, $expected, $test );
$content = <<'HTML';
<pre lang="HTML">
<div>
Ha en god dag
</div>
</pre>
HTML
# Now run through all formatters.
$test = 'The same single <div> from the JSRPC renderer';
$got = get( POST '/.jsrpc/render', [ content => $content ] );
eq_or_diff( $got, $expected, $test );
}
{
$test = 'Simple Perl';
$content = <<Perl;
<pre lang="Perl">
say "Hola Cabrón";
</pre>
Perl
$got = MojoMojo::Formatter::SyntaxHighlight->format_content( \$content );
$expected = <<Perl;
<pre>
say <span class="kateOperator">"</span><span class="kateString">Hola Cabrón</span><span class="kateOperator">"</span>;
</pre>
Perl
is( $$got, $expected, $test );
}
{
$test = 'Simple SQL SELECT';
$content = <<SQL;
<pre lang="SQL">
select * from foo
</pre>
SQL
$got = MojoMojo::Formatter::SyntaxHighlight->format_content( \$content );
$expected = <<SQL;
<pre>
<b>select</b> * <b>from</b> foo
</pre>
SQL
is( $$got, $expected, $test );
}
{
$test = 'Simple HTML Form';
$content = <<'HTML';
<pre lang="HTML">
<form action="[% c.uri_for('/login') %]" method="get">
<input type="text" name="openid_identifier" value="http://" />
<button type="submit">Sign in with OpenID</button>
</form>
</pre>
HTML
$got = MojoMojo::Formatter::SyntaxHighlight->format_content( \$content );
$expected = <<HTML;
<pre>
<b><form</b><span class="kateOthers"> action=</span><span class="kateString">"[% c.uri_for('/login') %]"</span><span class="kateOthers"> method=</span><span class="kateString">"get"</span><b>></b>
<b><input</b><span class="kateOthers"> type=</span><span class="kateString">"text"</span><span class="kateOthers"> name=</span><span class="kateString">"openid_identifier"</span><span class="kateOthers"> value=</span><span class="kateString">"http://"</span> <b>/></b>
<b><button</b><span class="kateOthers"> type=</span><span class="kateString">"submit"</span><b>></b>Sign in with OpenID<b></button></b>
<b></form></b>
</pre>
HTML
is( $$got, $expected, $test );
}
{
$test = 'More Complex Perl';
my $content = <<PERL;
<pre lang="Perl">
sub login : Local {
my ( \$self, \$c ) = @_;
# eval necessary because LWPx::ParanoidAgent
# croaks if invalid URL is specified
eval {
# Authenticate against OpenID to get user URL
if ( \$c->authenticate({}, 'openid' ) ) {
# ...
else {
# ...
}
};
if (\$@) {
\$c->log->error("Failure during login: " . \$@);
\$c->flash->{'error_msg'}='Failure during login: ' . \$@;
\$c->stash->{'template'}='login.tt';
}
}
</pre>
PERL
$got = MojoMojo::Formatter::SyntaxHighlight->format_content( \$content );
my $wanted = <<'PERL';
<pre>
<b>sub </b><span class="kateFunction">login</span> : <b>Local</b> {
<b>my</b> ( <span class="kateDataType">$self</span>, <span class="kateDataType">$c</span> ) = ;
<span class="kateComment"><i># eval necessary because LWPx::ParanoidAgent</i></span><span class="kateComment"><i>
</i></span> <span class="kateComment"><i># croaks if invalid URL is specified</i></span><span class="kateComment"><i>
</i></span> <span class="kateFunction">eval</span> {
<span class="kateComment"><i># Authenticate against OpenID to get user URL</i></span><span class="kateComment"><i>
</i></span> <b>if</b> ( <span class="kateDataType">$c</span>-><span class="kateDataType">authenticate</span>({}, <span class="kateOperator">'</span><span class="kateString">openid</span><span class="kateOperator">'</span> ) ) {
<span class="kateComment"><i># ...</i></span><span class="kateComment"><i>
</i></span> <b>else</b> {
<span class="kateComment"><i># ...</i></span><span class="kateComment"><i>
</i></span> }
};
<b>if</b> (<span class="kateVariable"><b>$@</b></span>) {
<span class="kateDataType">$c</span>-><span class="kateDataType">log</span>-><span class="kateDataType">error</span>(<span class="kateOperator">"</span><span class="kateString">Failure during login: </span><span class="kateOperator">"</span> . <span class="kateVariable"><b>$@</b></span>);
<span class="kateDataType">$c</span>-><span class="kateDataType">flash</span>->{<span class="kateOperator">'</span><span class="kateString">error_msg</span><span class="kateOperator">'</span>}=<span class="kateOperator">'</span><span class="kateString">Failure during login: </span><span class="kateOperator">'</span> . <span class="kateVariable"><b>$@</b></span>;
<span class="kateDataType">$c</span>-><span class="kateDataType">stash</span>->{<span class="kateOperator">'</span><span class="kateString">template</span><span class="kateOperator">'</span>}=<span class="kateOperator">'</span><span class="kateString">login.tt</span><span class="kateOperator">'</span>;
}
}
</pre>
PERL
is( $content, $wanted, $test );
}
|