summaryrefslogtreecommitdiff
path: root/tools/util_test.py
blob: b5d5dcc985da7960c8db4a5c130a1a17a6955681 (plain)
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
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
from util import pattern_match, parse_exit_code, shell_quote_win
import util
import os
import sys


def pattern_match_test():
    print "Testing util.pattern_match()..."
    # yapf: disable
    fixtures = [("foobarbaz", "foobarbaz", True),
                ("[WILDCARD]", "foobarbaz", True),
                ("foobar", "foobarbaz", False),
                ("foo[WILDCARD]baz", "foobarbaz", True),
                ("foo[WILDCARD]baz", "foobazbar", False),
                ("foo[WILDCARD]baz[WILDCARD]qux", "foobarbazqatqux", True),
                ("foo[WILDCARD]", "foobar", True),
                ("foo[WILDCARD]baz[WILDCARD]", "foobarbazqat", True)]
    # yapf: enable

    # Iterate through the fixture lists, testing each one
    for (pattern, string, expected) in fixtures:
        actual = pattern_match(pattern, string)
        assert expected == actual, \
            "expected %s for\nExpected:\n%s\nTo equal actual:\n%s" % (
            expected, pattern, string)

    assert pattern_match("foo[BAR]baz", "foobarbaz",
                         "[BAR]") == True, "expected wildcard to be set"
    assert pattern_match("foo[BAR]baz", "foobazbar",
                         "[BAR]") == False, "expected wildcard to be set"


def parse_exit_code_test():
    print "Testing util.parse_exit_code()..."
    assert 54 == parse_exit_code('hello_error54_world')
    assert 1 == parse_exit_code('hello_error_world')
    assert 0 == parse_exit_code('hello_world')


def shell_quote_win_test():
    print "Testing util.shell_quote_win()..."
    assert 'simple' == shell_quote_win('simple')
    assert 'roof/\\isoprojection' == shell_quote_win('roof/\\isoprojection')
    assert '"with space"' == shell_quote_win('with space')
    assert '"embedded""quote"' == shell_quote_win('embedded"quote')
    assert '"a""b""""c\\d\\\\""e\\\\\\\\"' == shell_quote_win(
        'a"b""c\\d\\"e\\\\')


def parse_wrk_output_test():
    print "Testing util.parse_wrk_output_test()..."
    f = open(os.path.join(util.root_path, "tools/testdata/wrk1.txt"))
    stats = util.parse_wrk_output(f.read())
    assert stats['req_per_sec'] == 1837
    assert stats['max_latency'] == 34.96

    f2 = open(os.path.join(util.root_path, "tools/testdata/wrk2.txt"))
    stats2 = util.parse_wrk_output(f2.read())
    assert stats2['req_per_sec'] == 53435
    assert stats2['max_latency'] == 0.00125

    f3 = open(os.path.join(util.root_path, "tools/testdata/wrk3.txt"))
    stats3 = util.parse_wrk_output(f3.read())
    assert stats3['req_per_sec'] == 96037
    assert stats3['max_latency'] == 1630.0


def util_test():
    pattern_match_test()
    parse_exit_code_test()
    shell_quote_win_test()
    parse_wrk_output_test()


if __name__ == '__main__':
    util_test()