芝麻web文件管理V1.00
编辑当前文件:/home/mgatv524/public_html/mctv/vendor/emojione/emojione/lib/js/js-test
#!/usr/bin/env perl =head1 SYNOPSIS js-test [options] Options: --output=/some/path writes to given file (default: ./tests.html) --run opens test file in the default browser --help shows this help This code generates a file (defaults to 'tests.html') to test the JavaScript implementation of Emojione. Please run this program if you update either validate.yml or emojione.js to make sure everything is ok. To update and run the tests, just type "./js-test" on the console. =cut use strict; use warnings; use Getopt::Long; use Pod::Usage; my $test_file = 'tests.html'; my $run_in_browser = 0; my $show_help = 0; GetOptions( 'output=s' => \$test_file, 'run' => \$run_in_browser, 'help|?' => \$show_help, ) or pod2usage(); pod2usage() if $show_help; run_app(); exit; sub run_app { my $test_src = parse_validation_file(); my $qunit = generate_qunit_code( $test_src ); write_js_test_file( $qunit, $test_file ); if ($run_in_browser) { run_test_file( $test_file ); } } sub generate_qunit_code { my ($test_src) = @_; my $js = ''; foreach my $function_name ( sort keys %$test_src ) { $js .= generate_qunit_module( $function_name ); my $test_cases = $test_src->{$function_name}; foreach my $test ( @$test_cases ) { $js .= generate_qunit_test( $function_name, $test->{description}, $test->{text}, $test->{expected}, ); } } return $js; } sub write_js_test_file { my ($qunit_string, $filename) = @_; my $code = join '' =>
; $code =~ s/<% tests %>/$qunit_string/; open my $fh, '>:encoding(UTF-8)', $filename or die "error writing $filename: $!"; print $fh $code; close $fh; } sub run_test_file { my ($filename) = @_; # $^O contains the name of the operating system if ($^O eq 'darwin') { system "open $filename"; } else { system "xdg-open $filename &> /dev/null"; } } sub generate_qunit_module { my ($function_name) = @_; return qq( QUnit.module("$function_name"); ); } sub generate_qunit_test { my ($function_name, $description, $text, $expected) = escape( @_ ); return qq( QUnit.test( "$description", function( assert ) { assert.equal( emojione.$function_name("$text"), "$expected" ); }); ); } sub escape { return map { s/"/\\"/g; s/\n/\\n/g; $_ } @_ } sub parse_validation_file { eval 'use YAML'; if ($@) { die <<'ERROR'; We need the YAML library installed to parse the validation test file. Please install it by typing on the command line: sudo cpan YAML and try again. ERROR } my $validate = YAML::LoadFile( '../validate.yml' ); return $validate->{tests}; } __DATA__