aboutsummaryrefslogtreecommitdiffstats
path: root/test/matchers.py
blob: 5696d64e72d626e6b3bf113749fef04a2fe6854f (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
#
# -*- coding: utf-8 -*-
# Wireshark tests
#
# Copyright (c) 2018 Peter Wu <peter@lekensteyn.nl>
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
'''Helpers for matching test results.'''

import re

class MatchAny(object):
    '''Matches any other value.'''

    def __init__(self, type=None):
        self.type = type

    def __eq__(self, other):
        return self.type is None or self.type == type(other)

    def __repr__(self):
        return '<MatchAny type=%s>' % (self.type.__name__,)


class MatchObject(object):
    '''Matches all expected fields of an object, ignoring excess others.'''

    def __init__(self, fields):
        self.fields = fields

    def __eq__(self, other):
        return all(other.get(k) == v for k, v in self.fields.items())

    def __repr__(self):
        return '<MatchObject fields=%r>' % (self.fields,)


class MatchList(object):
    '''Matches elements of a list. Optionally checks list length.'''

    def __init__(self, item, n=None, match_element=all):
        self.item = item
        self.n = n
        self.match_element = match_element

    def __eq__(self, other):
        if self.n is not None and len(other) != self.n:
            return False
        return self.match_element(self.item == elm for elm in other)

    def __repr__(self):
        return '<MatchList item=%r n=%r match_element=%s>' % \
                (self.item, self.n, self.match_element.__name__)


class MatchRegExp(object):
    '''Matches a string against a regular expression.'''

    def __init__(self, pattern):
        self.pattern = pattern

    def __eq__(self, other):
        return type(other) == str and re.match(self.pattern, other)

    def __repr__(self):
        return '<MatchRegExp pattern=%r>' % (self.pattern)