Eshu
view release on metacpan or search on metacpan
t/0112-hl-js.t view on Meta::CPAN
{
my $got = hl("switch (x) { case 1: break; }");
like($got, qr{<span class="esh-k">switch</span>}, 'JS keyword: switch');
like($got, qr{<span class="esh-k">case</span>}, 'JS keyword: case');
}
{
my $got = hl("delete obj.key; void 0;");
like($got, qr{<span class="esh-k">delete</span>}, 'JS keyword: delete');
like($got, qr{<span class="esh-k">void</span>}, 'JS keyword: void');
}
{
my $got = hl("this.x = super.x;");
like($got, qr{<span class="esh-k">this</span>}, 'JS keyword: this');
like($got, qr{<span class="esh-k">super</span>}, 'JS keyword: super');
}
{
my $got = hl("for (const x of arr) { }");
like($got, qr{<span class="esh-k">of</span>}, 'JS keyword: of');
}
{
my $got = hl("for (const x in obj) { }");
like($got, qr{<span class="esh-k">in</span>}, 'JS keyword: in');
}
{
my $got = hl("const x = new Map();");
like($got, qr{<span class="esh-k">new</span>}, 'JS keyword: new');
}
{
my $got = hl("function* gen() { yield 1; }");
like($got, qr{<span class="esh-k">function</span>}, 'JS keyword: function (generator)');
like($got, qr{<span class="esh-k">yield</span>}, 'JS keyword: yield');
}
{
my $got = hl("if (x) throw new Error('oops');");
like($got, qr{<span class="esh-k">throw</span>}, 'JS keyword: throw');
}
{
my $got = hl("with (obj) { }");
like($got, qr{<span class="esh-k">with</span>}, 'JS keyword: with');
}
{
my $got = hl("debugger;");
like($got, qr{<span class="esh-k">debugger</span>}, 'JS keyword: debugger');
}
{
my $got = hl("const x = undefined;");
like($got, qr{<span class="esh-k">undefined</span>}, 'JS keyword: undefined');
}
{
my $got = hl("x = Infinity + NaN;");
like($got, qr{<span class="esh-k">Infinity</span>}, 'JS keyword: Infinity');
like($got, qr{<span class="esh-k">NaN</span>}, 'JS keyword: NaN');
}
# ââ static getter/setter (contextual keywords) âââââââââââââââââââââ
{
my $got = hl("class A { static get foo() {} set bar(v) {} }");
like($got, qr{<span class="esh-k">static</span>}, 'JS keyword: static');
like($got, qr{<span class="esh-k">get</span>}, 'JS keyword: get');
like($got, qr{<span class="esh-k">set</span>}, 'JS keyword: set');
}
# ââ more numbers ââââââââââââââââââââââââââââââââââââââââââââââââââ
{
my $got = hl("const n = -42;");
like($got, qr{<span class="esh-n">42</span>}, 'negative integer (the digits)');
}
{
my $got = hl("1.5e+3");
like($got, qr{<span class="esh-n">1\.5e\+3</span>}, 'scientific with + exponent');
}
# ââ HTML safety âââââââââââââââââââââââââââââââââââââââââââââââââââ
{
my $got = hl('"a & b"');
like($got, qr{&}, 'ampersand in string is HTML-escaped');
}
# ââ partial keyword non-match ââââââââââââââââââââââââââââââââââââââ
{
my $got = hl("constant lettuce newInstance");
unlike($got, qr{<span class="esh-k">const</span>}, 'const not matched inside constant');
unlike($got, qr{<span class="esh-k">let</span>}, 'let not matched inside lettuce');
unlike($got, qr{<span class="esh-k">new</span>}, 'new not matched inside newInstance');
}
# ââ highlight_string lang aliases ââââââââââââââââââââââââââââââââââ
{
my $got = Eshu->highlight_string("const x = 1;", lang => 'javascript');
like($got, qr{<span class="esh-k">const</span>}, 'lang=javascript dispatches correctly');
}
{
my $got = Eshu->highlight_string("const x = 1;", lang => 'jsx');
like($got, qr{<span class="esh-k">const</span>}, 'lang=jsx dispatches correctly');
}
done_testing;
( run in 1.730 second using v1.01-cache-2.11-cpan-60884a8df76 )