Created
May 19, 2018 13:38
-
-
Save ramalho/fba91a4be9533939d40761eba6b9c53f to your computer and use it in GitHub Desktop.
Arquivos do Coding Dojo com pytest na Lendico (2018-05-18)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
def search(query, data): | |
query = query.replace('-',' ') | |
words = set(query.upper().split()) | |
for code, char, name in data: | |
name = name.replace('-',' ') | |
if words <= set(name.split()): | |
yield f'{code}\t{char}\t{name}' | |
def reader(): | |
with open('UnicodeData.txt') as _file: | |
for line in _file: | |
code, name = line.split(';')[:2] | |
char = chr(int(code, 16)) | |
yield f'U+{code}', char, name | |
def main(*words): | |
if len(words) < 1: | |
print("Please provide one word or more", file=sys.stderr) | |
return | |
query = ' '.join(words) | |
index = -1 | |
for index, line in enumerate(search(query, reader())): | |
print(line) | |
if index == -1: | |
print("No results", file=sys.stderr) | |
if __name__ == "__main__": | |
main(*sys.argv[1:]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from sinais import reader, search, main | |
@pytest.fixture | |
def data(): | |
return [ | |
('U+00AE', '®', 'REGISTERED SIGN'), | |
('U+265D', '♝', 'BLACK CHESS BISHOP'), | |
('U+2657', '♗', 'WHITE CHESS BISHOP') | |
] | |
def test_registered(data): | |
res = ['U+00AE\t®\tREGISTERED SIGN'] | |
assert list(search('registered', data)) == res | |
def test_multiple_results(data): | |
res = ['U+265D\t♝\tBLACK CHESS BISHOP', | |
'U+2657\t♗\tWHITE CHESS BISHOP'] | |
assert list(search('BISHOP', data)) == res | |
def test_multiple_words_query(data): | |
res = ['U+265D\t♝\tBLACK CHESS BISHOP', | |
'U+2657\t♗\tWHITE CHESS BISHOP'] | |
assert list(search('BISHOP CHESS', data)) == res | |
def test_reads_file(): | |
result = list(reader()) | |
assert len(result) >= 10000 | |
expected = 'U+0041', 'A', 'LATIN CAPITAL LETTER A' | |
assert expected in result | |
def test_main_single_result(capsys): | |
main('REGISTERED') | |
captured = capsys.readouterr() | |
assert captured.out == 'U+00AE\t®\tREGISTERED SIGN\n' | |
def test_main_multiple_results(capsys): | |
main('CHESS', 'BLACK') | |
captured = capsys.readouterr() | |
assert captured.out == '\n'.join([ | |
'U+265A\t♚\tBLACK CHESS KING', | |
'U+265B\t♛\tBLACK CHESS QUEEN', | |
'U+265C\t♜\tBLACK CHESS ROOK', | |
'U+265D\t♝\tBLACK CHESS BISHOP', | |
'U+265E\t♞\tBLACK CHESS KNIGHT', | |
'U+265F\t♟\tBLACK CHESS PAWN', | |
]) + '\n' | |
def test_we_dont_have_result(capsys): | |
main('batata') | |
captured = capsys.readouterr() | |
assert captured.err == 'No results\n' | |
def test_user_dont_input_anything(capsys): | |
main() | |
captured = capsys.readouterr() | |
assert captured.err == 'Please provide one word or more\n' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment