File: regexp.t

package info (click to toggle)
libjavascript-quickjs-perl 0.21-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,180 kB
  • sloc: ansic: 72,822; javascript: 7,743; perl: 1,065; makefile: 353; sh: 108
file content (75 lines) | stat: -rw-r--r-- 1,487 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
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Test::Deep;
use Test::FailWarnings;

use JavaScript::QuickJS;
use Scalar::Util;

my $regexp = JavaScript::QuickJS->new()->eval('/foo/g');

my @props = (
    [ flags => 'g' ],
    [ global => bool(1) ],
    [ hasIndices => bool(0) ],
    [ multiline => bool(0) ],
    [ source => 'foo' ],
    [ sticky => bool(0) ],
    [ unicode => bool(0) ],
    [ lastIndex => 0 ],
);

for my $p_ar (@props) {
    my ($propname, $value) = @$p_ar;

    cmp_deeply( $regexp->$propname(), $value, "$propname()" );
}

cmp_deeply( $regexp->test('fo'), bool(0), 'test() w/ non-match' );
cmp_deeply( $regexp->test('fooo'), bool(1), 'test() w/ match' );

is(
    $regexp->exec('fo'),
    undef,
    'exec() w/ non-match',
);

is_deeply(
    $regexp->exec('foooo'),
    ['foo'],
    'exec() w/ match',
);

#----------------------------------------------------------------------

{
    my $js = JavaScript::QuickJS->new();
    my $regexp = $js->eval('/foo/g');

    $js->set_globals(
        re_from_perl => $regexp,
    );

    my $match = $js->eval('re_from_perl.exec("foooo")');

    cmp_deeply($match, ['foo'], 'RegExp to Perl, back to JS');

    eval { JavaScript::QuickJS->new()->set_globals( bad => $regexp ) };
    my $err = $@;

    my $re_class = ref $regexp;

    cmp_deeply(
        $err,
        all(
            re( qr<\Q$re_class\E> ),
        ),
        'error when mismatched Perl RegExp object given',
    );
}

done_testing;