Skip to content

Commit

Permalink
Improve & move test_connection method to baseclass
Browse files Browse the repository at this point in the history
  • Loading branch information
pushfoo committed Jun 21, 2020
1 parent 742f4d9 commit cc08cc6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
29 changes: 27 additions & 2 deletions pyc2e/interfaces/interface.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""
Baseclass and common functionality for c2e interfaces.
"""

from random import choice
from string import ascii_letters
from abc import ABC, abstractmethod
from typing import Union, ByteString

Expand All @@ -16,6 +17,15 @@
StrOrByteString = Union[str, ByteString]


def random_string(length: int = 5) -> str:
"""
Generate a randomized string of ascii-convertible characters
:param length: how long the string should be
:return:
"""
return ''.join(choice(ascii_letters) for i in range(0, length))

def coerce_to_bytearray(source: StrOrByteString):
"""
Coerce the source to a bytearray or return it if it already is one.
Expand Down Expand Up @@ -205,8 +215,23 @@ def add_script(
"""
pass

def test_connection(self) -> bool:
"""
Tests connection to engine.
Verifies that the target is a valid c2e engine by requesting it
concatenate two halves of a random string. This behavior should
be supported across all c2e engines, including Sea Monkeys.
:return: True if we can talk to an engine, false otherwise.
"""
expected_echo = random_string(10)

response = self.execute_caos(
'sets va00 "{0}" sets va01 "{1}" adds va00 va01 outs va00'.format(
expected_echo[0:6],
expected_echo[6:]
)
)


return response.text == expected_echo
9 changes: 0 additions & 9 deletions pyc2e/interfaces/win32/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,6 @@ def execute_caos(self, request_body: StrOrByteString) -> Response:

return self.raw_request(b"execute\n" + request_body)

def test_connection(self) -> bool:
"""Tests connection to engine.
:return: True if we can talk to an engine, false otherwise.
"""
this_works = "this works"
response = self.execute_caos('outs "{0}"'.format(this_works)).text
return response.text == this_works

def add_script(
self,
script_body: StrOrByteString,
Expand Down

0 comments on commit cc08cc6

Please sign in to comment.