xref: /aosp_15_r20/external/autotest/server/cros/dynamic_suite/comparators.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Comparators for use in dynamic_suite module unit tests."""
6
7from unittest.mock import ANY
8
9
10class StatusContains(object):
11    @staticmethod
12    def CreateFromStrings(status=None, test_name=None, reason=None):
13        status_comp = AnyStringWith(status) if status else ANY
14        name_comp = AnyStringWith(test_name) if test_name else ANY
15        reason_comp = AnyStringWith(reason) if reason else ANY
16        return StatusContains(status_comp, name_comp, reason_comp)
17
18
19    def __init__(self, status=ANY, test_name=ANY, reason=ANY):
20        """Initialize.
21
22        Takes mox.Comparator objects to apply to job_status.Status
23        member variables.
24
25        @param status: status code, e.g. 'INFO', 'START', etc.
26        @param test_name: expected test name.
27        @param reason: expected reason
28        """
29        self._status = status
30        self._test_name = test_name
31        self._reason = reason
32
33
34    def equals(self, rhs):
35        """Check to see if fields match base_job.status_log_entry obj in rhs.
36
37        @param rhs: base_job.status_log_entry object to match.
38        @return boolean
39        """
40        return (self._status.equals(rhs.status_code) and
41                self._test_name.equals(rhs.operation) and
42                self._reason.equals(rhs.message))
43
44
45    def __repr__(self):
46        return '<Status containing \'%s\t%s\t%s\'>' % (self._status,
47                                                       self._test_name,
48                                                       self._reason)
49
50
51class AnyStringWith(str):
52    def __eq__(self, other):
53        return self in str(other)
54