Best Python code snippet using hypothesis
test_launcher.py
Source:test_launcher.py
1"""2Tests for some launcher commands.3"""4import pytest5import os.path6import tempfile7from test_common.fs.temp import temp_dir8import cifra.cifra_launcher as cifra_launcher9import cifra.cipher.substitution as substitution10from cifra.tests.test_dictionaries import loaded_dictionaries, LoadedDictionaries11from cifra.tests.test_caesar import ORIGINAL_MESSAGE as caesar_ORIGINAL_MESSAGE12from cifra.tests.test_caesar import CIPHERED_MESSAGE_KEY_13 as caesar_CIPHERED_MESSAGE_KEY_1313from cifra.tests.test_caesar import TEST_KEY as caesar_TEST_KEY14from cifra.tests.test_substitution import ORIGINAL_MESSAGE as substitution_ORIGINAL_MESSAGE15from cifra.tests.test_substitution import CIPHERED_MESSAGE as substitution_CIPHERED_MESSAGE16from cifra.tests.test_substitution import TEST_KEY as substitution_TEST_KEY17from cifra.tests.test_substitution import TEST_CHARSET as substitution_TEST_CHARSET18@pytest.mark.quick_slow19def test_cipher_caesar(temp_dir, loaded_dictionaries: LoadedDictionaries):20 with tempfile.NamedTemporaryFile(mode="w") as message_file:21 message_file.write(caesar_ORIGINAL_MESSAGE)22 message_file.flush()23 output_file_pathname = os.path.join(temp_dir, "ciphered_message.txt")24 provided_args = f"cipher caesar {caesar_TEST_KEY} {message_file.name} --ciphered_file {output_file_pathname}".split()25 cifra_launcher.main(provided_args, loaded_dictionaries.temp_dir)26 with open(output_file_pathname, mode="r") as output_file:27 recovered_content = output_file.read()28 assert caesar_CIPHERED_MESSAGE_KEY_13 == recovered_content29@pytest.mark.quick_slow30def test_decipher_caesar(temp_dir, loaded_dictionaries: LoadedDictionaries):31 with tempfile.NamedTemporaryFile(mode="w") as message_file:32 message_file.write(caesar_CIPHERED_MESSAGE_KEY_13)33 message_file.flush()34 output_file_pathname = os.path.join(temp_dir, "deciphered_message.txt")35 provided_args = f"decipher caesar {caesar_TEST_KEY} {message_file.name} --deciphered_file {output_file_pathname}".split()36 cifra_launcher.main(provided_args, loaded_dictionaries.temp_dir)37 with open(output_file_pathname, mode="r") as output_file:38 recovered_content = output_file.read()39 assert caesar_ORIGINAL_MESSAGE == recovered_content40@pytest.mark.quick_slow41def test_cipher_substitution(temp_dir, loaded_dictionaries: LoadedDictionaries):42 with tempfile.NamedTemporaryFile(mode="w") as message_file:43 message_file.write(substitution_ORIGINAL_MESSAGE)44 message_file.flush()45 output_file_pathname = os.path.join(temp_dir, "ciphered_message.txt")46 provided_args = f"cipher substitution {substitution_TEST_KEY} {message_file.name} --ciphered_file {output_file_pathname} --charset {substitution_TEST_CHARSET}".split()47 cifra_launcher.main(provided_args, loaded_dictionaries.temp_dir)48 with open(output_file_pathname, mode="r") as output_file:49 recovered_content = output_file.read()50 assert substitution_CIPHERED_MESSAGE == recovered_content51@pytest.mark.quick_slow52def test_decipher_substitution(temp_dir, loaded_dictionaries: LoadedDictionaries):53 with tempfile.NamedTemporaryFile(mode="w") as message_file:54 message_file.write(substitution_CIPHERED_MESSAGE)55 message_file.flush()56 output_file_pathname = os.path.join(temp_dir, "deciphered_message.txt")57 provided_args = f"decipher substitution {substitution_TEST_KEY} {message_file.name} --deciphered_file {output_file_pathname} --charset {substitution_TEST_CHARSET}".split()58 cifra_launcher.main(provided_args, loaded_dictionaries.temp_dir)59 with open(output_file_pathname, mode="r") as output_file:60 recovered_content = output_file.read()61 assert substitution_ORIGINAL_MESSAGE == recovered_content62@pytest.mark.quick_slow63def test_attack_caesar(temp_dir, loaded_dictionaries: LoadedDictionaries):64 with tempfile.NamedTemporaryFile(mode="w") as message_file:65 message_file.write(caesar_CIPHERED_MESSAGE_KEY_13)66 message_file.flush()67 output_file_pathname = os.path.join(temp_dir, "recovered_message.txt")68 provided_args = f"attack caesar {message_file.name} --deciphered_file {output_file_pathname}".split()69 cifra_launcher.main(provided_args, loaded_dictionaries.temp_dir)70 with open(output_file_pathname, mode="r") as output_file:71 recovered_content = output_file.read()72 assert caesar_ORIGINAL_MESSAGE == recovered_content73@pytest.mark.quick_slow74def test_attack_substitution(temp_dir, loaded_dictionaries: LoadedDictionaries):75 with tempfile.NamedTemporaryFile(mode="w") as message_file, \76 open(os.path.join(os.getcwd(), "cifra", "tests", "resources/english_book_c1.txt")) as english_book:77 original_message = english_book.read()78 ciphered_text = substitution.cipher(original_message, substitution_TEST_KEY, substitution_TEST_CHARSET)79 message_file.write(ciphered_text)80 message_file.flush()81 output_file_pathname = os.path.join(temp_dir, "recovered_message.txt")82 provided_args = f"attack substitution {message_file.name} --deciphered_file {output_file_pathname} --charset {substitution_TEST_CHARSET}".split()83 cifra_launcher.main(provided_args, loaded_dictionaries.temp_dir)84 with open(output_file_pathname, mode="r") as output_file:85 recovered_content = output_file.read()...
test_Substitution.py
Source:test_Substitution.py
1import random2import sets3import unittest4import SloppyCell.ExprManip as ExprManip5x = random.random()6y = random.random()7z = random.random()8p = [random.random(), random.random()]9f = lambda x: x**410g = lambda x, y, z: x**4 * y**6 * z**511class test_Substitution(unittest.TestCase):12 def test_sub_for_var(self):13 cases = [('x', 'x', 'y', 14 'y'),15 ('x + 1', 'x', 'y', 16 'y + 1'),17 ('f(x)**2 - y + z', 'x', 'y*z/(x-2)', 18 'f(y*z/(x-2))**2 - y + z'),19 ('x**2', 'x', 'y+2', 20 '(y+2)**2'),21 ('p[0]**x', 'x', 'y+2', 22 'p[0]**(y+2)'),23 ('g(x, y, f(z)) != y+2 and z == y', 'z', 'y',24 '(g(x, y, f(y)) != (y + 2)) and (y == y)'),25 ]26 for expr, out_var, in_expr, answer in cases:27 subbed = ExprManip.sub_for_var(expr, out_var, in_expr)28 assert eval(answer) == eval(subbed)29 def test_sub_for_vars(self):30 cases = [('x', {'x':'y'},31 'y'),32 ('x + 1', {'x':'y'}, 33 'y + 1'),34 ('f(x)**2 - y + z', {'x':'y*z/(x-2)'}, 35 'f(y*z/(x-2))**2 - y + z'),36 ('x**2', {'x':'y+2'}, 37 '(y+2)**2'),38 ('p[0]**x', {'x':'y+2'}, 39 'p[0]**(y+2)'),40 # In these cases substituting one-by-one will fail41 ('x*y', {'x':'y', 'y':'z'},42 'y*z'),43 ('z*y', {'z':'y', 'y':'z'},44 'y*z'),45 ]46 for expr, mapping, answer in cases:47 subbed = ExprManip.sub_for_vars(expr, mapping)48 assert eval(answer) == eval(subbed)49 def test_sub_for_comps(self):50 cases = [('not x < 3 and True', {'x < 3': 'True'}, False),51 ('not x < 3 and x < 3', {'x < 3': 'True'}, False),52 ('x < 3 and x < 3', {'x < 3': 'True'}, True),53 ('x < 3 and y > 4', {'x < 3': 'True', 'y > 4':'False'}, False),54 ('x < 3 and not y > 4', {'x < 3': 'True', 'y > 4':'False'}, 55 True),]56 for expr, mapping, result in cases:57 subbed = ExprManip.sub_for_comps(expr, mapping)58 assert eval(subbed) == result59 def test_sub_for_func(self):60 cases = [('f(x)', 'f', 'y', 'y+1',61 'x+1'),62 ('f(x)**2', 'f', 'y', 'y+1',63 '(x+1)**2'),64 ('f(g(x, y, z))**2', 'f', 'y', 'y+1',65 '(g(x,y,z)+1)**2'),66 ('g(f(x), y, z)**2', 'f', 'y', 'y+1',67 'g(x+1,y,z)**2'),68 ('g(f(x), p[0], z)**2', 'f', 'y', 'y+1',69 'g(x+1,p[0],z)**2'),70 ('g(z, y, f(x)) == y+2 and f(x) + f(y) != f(x)', 71 'g', ('x','y', 'z'), 'f(x + y/z)',72 '(f(z + y/f(x)) == (y + 2)) and (f(x) + f(y) != f(x))'),73 ]74 for expr, func_name, func_vars, func_expr, answer in cases:75 subbed = ExprManip.sub_for_func(expr, func_name, func_vars,76 func_expr)77 assert eval(answer) == eval(subbed)78 def test_var_args_subs(self):79 subbed = ExprManip.sub_for_func('or_func(x,y,z)', 'or_func', '*',80 'a or b')81 x,y,z = True,False,False82 assert eval(subbed)83 x,y,z = False,False,False84 assert not eval(subbed)85 self.assertRaises(ValueError, ExprManip.sub_for_func,86 'or_func(x,y,z)', 'or_func', '*', 'a + b')87suite = unittest.makeSuite(test_Substitution)88if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!