File: 04-random-order.t

package info (click to toggle)
libhttp-browserdetect-perl 3.14-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,012 kB
  • ctags: 56
  • sloc: perl: 2,549; makefile: 2
file content (103 lines) | stat: -rw-r--r-- 2,285 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
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::Most;
use Test::FailWarnings;

use FindBin;
use JSON::PP;
use Path::Tiny qw( path );

# test that the module loads without errors
my $w;
{
    local $SIG{__WARN__} = sub { $w = shift };
    require HTTP::BrowserDetect;
}
ok( !$w, 'no warnings on require' );

my $json  = path("$FindBin::Bin/useragents.json")->slurp;
my $tests = JSON::PP->new->ascii->decode($json);

$json = path("$FindBin::Bin/more-useragents.json")->slurp;
my $more_tests = JSON::PP->new->ascii->decode($json);
$tests = { %$tests, %$more_tests };

srand(88);    # Consistent seed for consistent results

my $N_DETECTS = 5;
my $N_TESTS   = 30;

my @detect;

my @methods = (
    qw(
        beta
        browser
        browser_properties
        browser_string
        country
        device
        device
        device_name
        device_string
        engine
        engine_beta
        engine_major
        engine_minor
        engine_string
        engine_version
        engine_major
        engine_minor
        gecko_version
        language
        major
        minor
        os
        os_beta
        os_major
        os_minor
        os_string
        os_version
        public_major
        public_minor
        public_version
        realplayer_browser
        robot_string
        version
        ), &HTTP::BrowserDetect::_all_tests()
);

foreach my $ua ( sort ( keys %{$tests} ) ) {

    my %test_results;

    diag($ua);

    for my $i ( 0 .. $N_DETECTS - 1 ) {
        $detect[$i] = HTTP::BrowserDetect->new($ua);

        for my $j ( 1 .. $N_TESTS ) {
            my $method = $methods[ int( rand(@methods) ) ];
            my $result = $detect[$i]->$method;
            if ( exists( $test_results{$method} ) ) {
                if ( !defined($result) ) {
                    ok( !defined( $test_results{$method} ), $method );
                }
                elsif ( ref($result) eq 'ARRAY' ) {
                    is_deeply( $result, $test_results{$method}, $method );
                }
                else {
                    cmp_ok( $result, 'eq', $test_results{$method}, $method );
                }
            }
            else {
                $test_results{$method} = $result;
            }
        }
    }
}

done_testing();