Skip to content

Instantly share code, notes, and snippets.

@pavlix
Created January 13, 2019 17:55
Show Gist options
  • Save pavlix/8ad05c7044aebaa677ff239723cb0fa5 to your computer and use it in GitHub Desktop.
Save pavlix/8ad05c7044aebaa677ff239723cb0fa5 to your computer and use it in GitHub Desktop.
import pytest
import subprocess
class Expect:
def __init__(self):
self._input = []
self._output = []
def input(self, chunk):
print("Input: {!r}".format(chunk))
self._input.append(chunk)
def output(self, chunk):
print("Output: {!r}".format(chunk))
self._output.append(chunk)
def script(self, script, stdin="<<<", stdout=">>>"):
while script:
if script.startswith(stdin):
script = script[3:]
try:
size = script.index(stdout)
except ValueError:
size = len(script)
self.input(script[:size])
script = script[size:]
elif script.startswith(stdout):
script = script[3:]
try:
size = script.index(stdin)
except ValueError:
size = len(script)
self.output(script[:size])
script = script[size:]
else:
raise ValueError("Script doesn't start with a stdin/stdout mark")
@pytest.fixture
def c():
command = "./hw1.rb"
expect = Expect()
yield expect
result = subprocess.run(command,
universal_newlines=True,
stdout=subprocess.PIPE,
input="".join(expect._input),
timeout=5)
stdout = result.stdout
print("Running: {!r}".format(command))
for expected_chunk in expect._output:
# Do this for diffed pytest result
size = len(expected_chunk)
actual_chunk = stdout[:size]
assert actual_chunk == expected_chunk
print("Chunk: {!r}".format(actual_chunk))
stdout = stdout[size:]
assert stdout == ""
def test_ukazka1(c):
c.script("""\
>>>*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<2
>>>PRIHLASENI HRACI:
Nejsou registrovani zadni hraci
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<3
>>>Opravdu chcete ukoncit prihlasovani? (ANO/NE)
<<<ANO
>>>Nejsou registrovani zadni hraci
""")
def test_ukazka2(c):
c.script("""\
>>>*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Pavel
>>>Hrac Pavel zaregistrovan
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Pavel
>>>Hrac s timto jmenem jiz byl zaregistrovan.
Hraci bylo prideleno jmeno: Pavel 2
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<2
>>>PRIHLASENI HRACI:
1. Pavel
2. Pavel 2
Chcete nejakeho hrace odhlasit? (ANO/NE)
<<<ANO
>>>Cislo hrace ke smazani: <<<2
>>>Hrac Pavel 2 odhlasen.
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Pavel
>>>Hrac s timto jmenem jiz byl zaregistrovan.
Hraci bylo prideleno jmeno: Pavel 3
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<2
>>>PRIHLASENI HRACI:
1. Pavel
2. Pavel 3
Chcete nejakeho hrace odhlasit? (ANO/NE)
<<<NE
>>>*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<3
>>>Opravdu chcete ukoncit prihlasovani? (ANO/NE)
<<<NE
>>>*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<3
>>>Opravdu chcete ukoncit prihlasovani? (ANO/NE)
<<<ANO
>>>Nejcasteji se vysktytuje jmeno: Pavel
Rozpis:
Pavel - Pavel 3
""")
def test_ukazka3(c):
c.script("""\
>>>*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Pavel
>>>Hrac Pavel zaregistrovan
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Karel
>>>Hrac Karel zaregistrovan
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Petr
>>>Hrac Petr zaregistrovan
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Pavel
>>>Hrac s timto jmenem jiz byl zaregistrovan.
Hraci bylo prideleno jmeno: Pavel 2
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Jan
>>>Hrac Jan zaregistrovan
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<2
>>>PRIHLASENI HRACI:
1. Pavel
2. Karel
3. Petr
4. Pavel 2
5. Jan
Chcete nejakeho hrace odhlasit? (ANO/NE)
<<<NE
>>>*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<3
>>>Opravdu chcete ukoncit prihlasovani? (ANO/NE)
<<<ANO
>>>Nejcasteji se vysktytuje jmeno: Pavel
Rozpis:
Pavel - Karel
Pavel - Petr
Pavel - Pavel 2
Pavel - Jan
Karel - Petr
Karel - Pavel 2
Karel - Jan
Petr - Pavel 2
Petr - Jan
Pavel 2 - Jan
""")
def test_max(c):
c.script("""\
>>>*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Pavel
>>>Hrac Pavel zaregistrovan
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Karel
>>>Hrac Karel zaregistrovan
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Pavel
>>>Hrac s timto jmenem jiz byl zaregistrovan.
Hraci bylo prideleno jmeno: Pavel 2
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Karel
>>>Hrac s timto jmenem jiz byl zaregistrovan.
Hraci bylo prideleno jmeno: Karel 2
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<1
>>>Jmeno: <<<Karel
>>>Hrac s timto jmenem jiz byl zaregistrovan.
Hraci bylo prideleno jmeno: Karel 3
*** HLAVNI MENU ***
1. Prihlasit hrace
2. Zobrazit prihlasene
3. Ukoncit prihlasovani
Vase volba: <<<3
>>>Opravdu chcete ukoncit prihlasovani? (ANO/NE)
<<<ANO
>>>Nejcasteji se vysktytuje jmeno: Karel
Rozpis:
Pavel - Karel
Pavel - Pavel 2
Pavel - Karel 2
Pavel - Karel 3
Karel - Pavel 2
Karel - Karel 2
Karel - Karel 3
Pavel 2 - Karel 2
Pavel 2 - Karel 3
Karel 2 - Karel 3
""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment