Branchements conditionnels
This commit is contained in:
parent
45aaef98cf
commit
b7672f1218
|
@ -104,11 +104,10 @@ class ExpectationCollection:
|
||||||
expectations = [exp for exps in self.expectations_lookup.values() for exp in exps]
|
expectations = [exp for exps in self.expectations_lookup.values() for exp in exps]
|
||||||
if recursive:
|
if recursive:
|
||||||
expectations.extend([exp for exps in expectations for exp in exps.get_following_expectations()])
|
expectations.extend([exp for exps in expectations for exp in exps.get_following_expectations()])
|
||||||
pass
|
return list(set(expectations))
|
||||||
return expectations
|
|
||||||
|
|
||||||
def count_expectations(self):
|
def count_expectations(self):
|
||||||
return sum([exp.count_expectations() for exp in self.get_expectations()])
|
return len(self.get_expectations(recursive=True))
|
||||||
|
|
||||||
def merge(self, collection):
|
def merge(self, collection):
|
||||||
for expectation in collection.get_expectations():
|
for expectation in collection.get_expectations():
|
||||||
|
@ -121,21 +120,24 @@ class Expectation:
|
||||||
self.context = [p.strip() for p in multiline_pattern.strip().split('\n') if p.strip()]
|
self.context = [p.strip() for p in multiline_pattern.strip().split('\n') if p.strip()]
|
||||||
self.pattern = self.context[-1]
|
self.pattern = self.context[-1]
|
||||||
self.response = response
|
self.response = response
|
||||||
self.next = None
|
self.next = {}
|
||||||
|
|
||||||
def get_pattern(self):
|
def get_pattern(self, previous_answer=None):
|
||||||
return re.compile(f'(.*){re.escape(self.pattern)}(.*)')
|
return re.compile(f'(.*){re.escape(self.pattern.format(variable=previous_answer))}(.*)')
|
||||||
|
|
||||||
def set_next_expectation(self, expectation):
|
def set_next_expectation(self, expectation, triggers=None):
|
||||||
self.next = expectation
|
if not isinstance(triggers, list):
|
||||||
|
triggers = [triggers]
|
||||||
|
for trigger in triggers:
|
||||||
|
self.next[trigger] = expectation
|
||||||
|
|
||||||
def set_response(self, response):
|
def set_response(self, response):
|
||||||
print(f'Setting {response} for {self.pattern}')
|
print(f'Setting {response} for {self.pattern}')
|
||||||
self.response = response
|
self.response = response
|
||||||
|
|
||||||
def expect(self, spawned):
|
def expect(self, spawned, previous_answer=None):
|
||||||
print(f'-> expecting next "{self.pattern}"')
|
print(f'-> expecting next "{self.pattern.format(variable=previous_answer)}"')
|
||||||
p = spawned.expect([pexpect.EOF, pexpect.TIMEOUT, self.get_pattern()])
|
p = spawned.expect([pexpect.EOF, pexpect.TIMEOUT, self.get_pattern(previous_answer=previous_answer)])
|
||||||
if p not in [0, 1]:
|
if p not in [0, 1]:
|
||||||
self.answer(spawned)
|
self.answer(spawned)
|
||||||
else:
|
else:
|
||||||
|
@ -145,8 +147,10 @@ class Expectation:
|
||||||
print(f'-> answering "{self.response}" to "{spawned.after}"')
|
print(f'-> answering "{self.response}" to "{spawned.after}"')
|
||||||
if self.response is not None:
|
if self.response is not None:
|
||||||
spawned.sendline(self.response)
|
spawned.sendline(self.response)
|
||||||
if self.next:
|
if self.response in self.next:
|
||||||
self.next.expect(spawned)
|
self.next[self.response].expect(spawned, previous_answer=self.response)
|
||||||
|
elif None in self.next:
|
||||||
|
self.next[None].expect(spawned, previous_answer=self.response)
|
||||||
|
|
||||||
def is_the_one(self, context):
|
def is_the_one(self, context):
|
||||||
#print(f'testing if it is the one {self.get_pattern()} for {context}')
|
#print(f'testing if it is the one {self.get_pattern()} for {context}')
|
||||||
|
@ -160,18 +164,12 @@ class Expectation:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def count_expectations(self):
|
|
||||||
count = 1
|
|
||||||
if self.next:
|
|
||||||
count += self.next.count_expectations()
|
|
||||||
return count
|
|
||||||
|
|
||||||
def get_following_expectations(self):
|
def get_following_expectations(self):
|
||||||
if self.next:
|
following = []
|
||||||
return [self.next] + self.next.get_following_expectations()
|
for next_expectation in set(self.next.values()):
|
||||||
return []
|
following.append(next_expectation)
|
||||||
|
following.extend(next_expectation.get_following_expectations())
|
||||||
|
return following
|
||||||
|
|
||||||
|
|
||||||
expectations = ExpectationCollection()
|
expectations = ExpectationCollection()
|
||||||
|
|
Loading…
Reference in New Issue