Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/tests/framework/state_machine.py
1956 views
1
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
"""Defines a stream based string matcher and a generic state object."""
4
5
6
# Too few public methods (1/2) (too-few-public-methods)
7
# pylint: disable=R0903
8
class MatchStaticString:
9
"""Match a static string versus input."""
10
11
# Prevent state objects from being collected by pytest.
12
__test__ = False
13
14
def __init__(self, match_string):
15
"""Initialize using specified match string."""
16
self._string = match_string
17
self._input = ""
18
19
def match(self, input_char) -> bool:
20
"""
21
Check if `_input` matches the match `_string`.
22
23
Process one char at a time and build `_input` string.
24
Preserve built `_input` if partially matches `_string`.
25
Return True when `_input` is the same as `_string`.
26
"""
27
if input_char == '':
28
return False
29
self._input += str(input_char)
30
if self._input == self._string[:len(self._input)]:
31
if len(self._input) == len(self._string):
32
self._input = ""
33
return True
34
return False
35
36
self._input = self._input[1:]
37
return False
38
39
40
class TestState(MatchStaticString):
41
"""Generic test state object."""
42
43
# Prevent state objects from being collected by pytest.
44
__test__ = False
45
46
def __init__(self, match_string=''):
47
"""Initialize state fields."""
48
MatchStaticString.__init__(self, match_string)
49
print('\n*** Current test state: ', str(self), end='')
50
51
def handle_input(self, serial, input_char):
52
"""Handle input event and return next state."""
53
54
def __repr__(self):
55
"""Leverages the __str__ method to describe the TestState."""
56
return self.__str__()
57
58
def __str__(self):
59
"""Return state name."""
60
return self.__class__.__name__
61
62