File: JSBeautify.pm

package info (click to toggle)
libcode-tidyall-perl 0.78~ds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,048 kB
  • sloc: perl: 5,061; lisp: 47; sh: 4; makefile: 2
file content (88 lines) | stat: -rw-r--r-- 2,481 bytes parent folder | download | duplicates (2)
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
package TestFor::Code::TidyAll::Plugin::JSBeautify;

use Encode qw(encode);
use Test::Class::Most parent => 'TestFor::Code::TidyAll::Plugin';

BEGIN {
    unless ( $ENV{TRAVIS} ) {
        require Test::Warnings;
        Test::Warnings->import('warnings');
    }
}

sub SKIP_CLASS {

    # For some reason running js-beautify fails under Travis for no reason I
    # can understand
    # (https://travis-ci.org/houseabsolute/perl-code-tidyall/jobs/276610909). I've
    # updating all the NPM modules. I've tried changing how the command is
    # called. I've tried a lot of things! But the test still passes locally,
    # so I'm giving up for now.
    return $ENV{TRAVIS} ? 'Running js-beautify fails under travis for some reason' : 0;
}

sub _extra_path {
    'node_modules/.bin';
}

sub test_main : Tests {
    my $self = shift;

    return unless $self->require_executable('node');
    return unless $self->require_executable('js-beautify');

    my $source = 'sp.toggleResult=function(id){foo(id)}';
    $self->tidyall(
        source      => $source,
        expect_tidy => 'sp.toggleResult = function(id) {\n    foo(id)\n}',
    );
    $self->tidyall(
        source      => $source,
        conf        => { argv => '--indent-size 3 --brace-style expand' },
        expect_tidy => 'sp.toggleResult = function(id)\n{\n   foo(id)\n}',
    );
}

sub test_utf8 : Tests {
    my $self = shift;

    return unless $self->require_executable('node');
    return unless $self->require_executable('js-beautify');

    my $contents = encode( 'UTF-8', qq{var unicode  =  "Unicode - \x{263a}";} );
    my $expect   = encode( 'UTF-8', qq{var unicode = "Unicode - \x{263a}";} );

    local $SIG{__WARN__} = sub { Carp::cluck(@_) };
    is_deeply(
        [
            warnings {
                $self->tidyall(
                    source      => $contents,
                    expect_tidy => $expect,
                    desc        => 'tidy UTF-8 from string',
                );
            }
        ],
        [],
        'no warnings tidying UTF-8 source'
    );

    my $file = $self->{root_dir}->child('test.js');
    $file->spew($contents);

    is_deeply(
        [
            warnings {
                $self->tidyall(
                    source_file => $file,
                    expect_tidy => $expect,
                    desc        => 'tidy UTF-8 from file',
                );
            }
        ],
        [],
        'no warnings tidying UTF-8 source'
    );
}

1;