Best Python code snippet using pytest-bdd_python
test_lineprotocol.py
Source:test_lineprotocol.py  
...53        self.mock.prevent_timeout = True54        network.LineProtocol.on_receive(self.mock, {'received': 'data'})55        self.mock.connection.disable_timeout.assert_called_once_with()56        self.assertEqual(0, self.mock.connection.enable_timeout.call_count)57    def test_on_receive_no_new_lines_calls_parse_lines(self):58        self.mock.connection = Mock(spec=network.Connection)59        self.mock.recv_buffer = ''60        self.mock.parse_lines.return_value = []61        network.LineProtocol.on_receive(self.mock, {'received': 'data'})62        self.mock.parse_lines.assert_called_once_with()63        self.assertEqual(0, self.mock.on_line_received.call_count)64    def test_on_receive_with_new_line_calls_decode(self):65        self.mock.connection = Mock(spec=network.Connection)66        self.mock.recv_buffer = ''67        self.mock.parse_lines.return_value = [sentinel.line]68        network.LineProtocol.on_receive(self.mock, {'received': 'data\n'})69        self.mock.parse_lines.assert_called_once_with()70        self.mock.decode.assert_called_once_with(sentinel.line)71    def test_on_receive_with_new_line_calls_on_recieve(self):72        self.mock.connection = Mock(spec=network.Connection)73        self.mock.recv_buffer = ''74        self.mock.parse_lines.return_value = [sentinel.line]75        self.mock.decode.return_value = sentinel.decoded76        network.LineProtocol.on_receive(self.mock, {'received': 'data\n'})77        self.mock.on_line_received.assert_called_once_with(sentinel.decoded)78    def test_on_receive_with_new_line_with_failed_decode(self):79        self.mock.connection = Mock(spec=network.Connection)80        self.mock.recv_buffer = ''81        self.mock.parse_lines.return_value = [sentinel.line]82        self.mock.decode.return_value = None83        network.LineProtocol.on_receive(self.mock, {'received': 'data\n'})84        self.assertEqual(0, self.mock.on_line_received.call_count)85    def test_on_receive_with_new_lines_calls_on_recieve(self):86        self.mock.connection = Mock(spec=network.Connection)87        self.mock.recv_buffer = ''88        self.mock.parse_lines.return_value = ['line1', 'line2']89        self.mock.decode.return_value = sentinel.decoded90        network.LineProtocol.on_receive(91            self.mock, {'received': 'line1\nline2\n'})92        self.assertEqual(2, self.mock.on_line_received.call_count)93    def test_parse_lines_emtpy_buffer(self):94        self.mock.delimiter = re.compile(r'\n')95        self.mock.recv_buffer = ''96        lines = network.LineProtocol.parse_lines(self.mock)97        with self.assertRaises(StopIteration):98            lines.next()99    def test_parse_lines_no_terminator(self):100        self.mock.delimiter = re.compile(r'\n')101        self.mock.recv_buffer = 'data'102        lines = network.LineProtocol.parse_lines(self.mock)103        with self.assertRaises(StopIteration):104            lines.next()105    def test_parse_lines_termintor(self):106        self.mock.delimiter = re.compile(r'\n')107        self.mock.recv_buffer = 'data\n'108        lines = network.LineProtocol.parse_lines(self.mock)109        self.assertEqual('data', lines.next())110        with self.assertRaises(StopIteration):111            lines.next()112        self.assertEqual('', self.mock.recv_buffer)113    def test_parse_lines_termintor_with_carriage_return(self):114        self.mock.delimiter = re.compile(r'\r?\n')115        self.mock.recv_buffer = 'data\r\n'116        lines = network.LineProtocol.parse_lines(self.mock)117        self.assertEqual('data', lines.next())118        with self.assertRaises(StopIteration):119            lines.next()120        self.assertEqual('', self.mock.recv_buffer)121    def test_parse_lines_no_data_before_terminator(self):122        self.mock.delimiter = re.compile(r'\n')123        self.mock.recv_buffer = '\n'124        lines = network.LineProtocol.parse_lines(self.mock)125        self.assertEqual('', lines.next())126        with self.assertRaises(StopIteration):127            lines.next()128        self.assertEqual('', self.mock.recv_buffer)129    def test_parse_lines_extra_data_after_terminator(self):130        self.mock.delimiter = re.compile(r'\n')131        self.mock.recv_buffer = 'data1\ndata2'132        lines = network.LineProtocol.parse_lines(self.mock)133        self.assertEqual('data1', lines.next())134        with self.assertRaises(StopIteration):135            lines.next()136        self.assertEqual('data2', self.mock.recv_buffer)137    def test_parse_lines_unicode(self):138        self.mock.delimiter = re.compile(r'\n')139        self.mock.recv_buffer = 'æøå\n'.encode('utf-8')140        lines = network.LineProtocol.parse_lines(self.mock)141        self.assertEqual('æøå'.encode('utf-8'), lines.next())142        with self.assertRaises(StopIteration):143            lines.next()144        self.assertEqual('', self.mock.recv_buffer)145    def test_parse_lines_multiple_lines(self):146        self.mock.delimiter = re.compile(r'\n')147        self.mock.recv_buffer = 'abc\ndef\nghi\njkl'148        lines = network.LineProtocol.parse_lines(self.mock)149        self.assertEqual('abc', lines.next())150        self.assertEqual('def', lines.next())151        self.assertEqual('ghi', lines.next())152        with self.assertRaises(StopIteration):153            lines.next()154        self.assertEqual('jkl', self.mock.recv_buffer)155    def test_parse_lines_multiple_calls(self):156        self.mock.delimiter = re.compile(r'\n')157        self.mock.recv_buffer = 'data1'158        lines = network.LineProtocol.parse_lines(self.mock)159        with self.assertRaises(StopIteration):160            lines.next()161        self.assertEqual('data1', self.mock.recv_buffer)162        self.mock.recv_buffer += '\ndata2'163        lines = network.LineProtocol.parse_lines(self.mock)164        self.assertEqual('data1', lines.next())165        with self.assertRaises(StopIteration):166            lines.next()167        self.assertEqual('data2', self.mock.recv_buffer)168    def test_send_lines_called_with_no_lines(self):169        self.mock.connection = Mock(spec=network.Connection)170        network.LineProtocol.send_lines(self.mock, [])171        self.assertEqual(0, self.mock.encode.call_count)172        self.assertEqual(0, self.mock.connection.queue_send.call_count)173    def test_send_lines_calls_join_lines(self):174        self.mock.connection = Mock(spec=network.Connection)175        self.mock.join_lines.return_value = 'lines'176        network.LineProtocol.send_lines(self.mock, sentinel.lines)177        self.mock.join_lines.assert_called_once_with(sentinel.lines)...day-twenty-four.py
Source:day-twenty-four.py  
...21# To enable as many submarine features as possible, find the largest valid22# fourteen-digit model number that contains no 0 digits.23def parse_line(line):24    return line.strip().split(" ")25def parse_lines(lines):26    return [parse_line(line) for line in lines if line[0] != "#"]27def add(memory, a, b):28    """29    add a b - Add the value of a to the value of b, then store the result in30    variable a.31    """32    memory[a] = memory[a] + (memory[b] if memory.get(b) is not None else int(b))33def mul(memory, a, b):34    """35    mul a b - Multiply the value of a by the value of b, then store the result in36    variable a.37    """38    memory[a] = memory[a] * (memory[b] if memory.get(b) is not None else int(b))39def div(memory, a, b):40    """41    div a b - Divide the value of a by the value of b, truncate the result to an42    integer, then store the result in variable a. (Here, "truncate" means to43    round the value toward zero.)44    """45    memory[a] = math.floor(memory[a] / (memory[b] if memory.get(b) is not None else int(b)))46def mod(memory, a, b):47    """48    mod a b - Divide the value of a by the value of b, then store the remainder49    in variable a. (This is also called the modulo operation.)50    """51    memory[a] = memory[a] % (memory[b] if memory.get(b) is not None else int(b))52def eql(memory, a, b):53    """54    eql a b - If the value of a and b are equal, then store the value 1 in55    variable a. Otherwise, store the value 0 in variable a.56    """57    memory[a] = 1 if memory[a] == (memory[b] if memory.get(b) is not None else int(b)) else 058def run_program(program, input_data, memory=None):59    if (memory is None):60        memory = {"x": 0, "y": 0, "w": 0, "z": 0}61    instruction_set = {"add": add, "mul": mul, "div": div, "mod": mod, "eql": eql}62    remaining_input = input_data63    for index, line in enumerate(program):64        instruction = line[0]65        if instruction == "inp":66            memory[line[1]] = int(remaining_input[0])67            remaining_input = remaining_input[1:]68            continue69        a, b = line[1:]70        try:71            instruction_set.get(instruction)(memory, a, b)72            if (b == "add_x"):73                print("memory %s" % input_data)74                print(memory)75        except:76            print("Error at line %d" % (index + 1))77            print(memory)78            raise79    return (memory.get('x'), memory.get('y'), memory.get('w'), memory.get('z'))80x, y, w, z = run_program(parse_lines(test_data_one), "1")81assert x == -182x, y, w, z = run_program(parse_lines(test_data_two), "13")83assert z == 184x, y, w, z = run_program(parse_lines(test_data_two), "14")85assert z == 086x, y, w, z = run_program(parse_lines(test_data_three), "9")87assert w == 188assert x == 089assert y == 090assert z == 191x, y, w, z = run_program(parse_lines(real_data), "12345678901234")92x, y, w, z = run_program(93    parse_lines([94        "inp w\n", "mul x 0\n", "add x z\n", "mod x 26\n", "div z 1\n", "add x 11\n", "eql x w\n", "eql x 0\n",95        "mul y 0\n", "add y 25\n", "mul y x\n", "add y 1\n", "mul z y\n", "mul y 0\n", "add y w\n", "add y 10\n",96        "mul y x\n", "add z y\n"97    ]), "1")98real_data_monad_params = [(1, 12, 4), (1, 11, 10), (1, 14, 12), (26, -6, 14), (1, 15, 6), (1, 12, 16), (26, -9, 1), (1, 14, 7),99                          (1, 14, 8), (26, -5, 11), (26, -9, 8), (26, -5, 3), (26, -2, 1), (26, -7, 8)]100# inp w101# mul x 0102# add x z103# mod x 26104# div z div_z105# add x add_x106# eql x w107# eql x 0108# mul y 0109# add y 25110# mul y x111# add y 1112# mul z y113# mul y 0114# add y w115# add y add_y116# mul y x117# add z y118def monad_digit(div_z, add_x, add_y, memory, digit):119    memory = {**memory, **{"div_z": div_z, "add_x": add_x, "add_y": add_y}}120    x, y, w, z = run_program(121        parse_lines([122            "inp w\n", "mul x 0\n", "add x z\n", "mod x 26\n", "div z div_z\n", "add x add_x\n", "eql x w\n",123            "eql x 0\n", "mul y 0\n", "add y 25\n", "mul y x\n", "add y 1\n", "mul z y\n", "mul y 0\n", "add y w\n",124            "add y add_y\n", "mul y x\n", "add z y\n"125        ]), str(digit), memory)126    return {"x": x, "y": y, "w": w, "z": z}127def monad(input):128    memory = {"x": 0, "y": 0, "w": 0, "z": 0}129    for index, params in enumerate(real_data_monad_params):130        div_z, add_x, add_y = params131        print(params)132        memory = monad_digit(div_z, add_x, add_y, memory, input[index:])133        print(memory)134    return (memory.get('x'), memory.get('y'), memory.get('w'), memory.get('z'))135# Zero in the z variable means a valid number136assert run_program(parse_lines(real_data), "12345678901234") == monad("12345678901234")137# real_data_monad_params = [(1, 12, 4), (1, 11, 10), (1, 14, 12), (26, -6, 14), (1, 15, 6), (1, 12, 16), (26, -9, 1), (1, 14, 7),138#                           (1, 14, 8), (26, -5, 11), (26, -9, 8), (26, -5, 3), (26, -2, 1), (26, -7, 8)]139# div_z, add_x, add_y140#141# x gets the previous z142# x = (x % 26) + add_x (-7)143# z = z / div_z (26)144#145# if x == input:146#   x = 0147# else:148#   x = 1149#150# y = 25...commit_msg_tests.py
Source:commit_msg_tests.py  
2import unittest3from commit_msg import GitCommitMessage4class TestCommitMsg(unittest.TestCase):5    def testNoInput(self):6        m = GitCommitMessage().parse_lines([])7        self.assertEqual(len(m.body_lines), 0)8        m = GitCommitMessage().parse_lines(None)9        self.assertEqual(len(m.body_lines), 0)10    def testParsing(self):11        m = GitCommitMessage().parse_lines(['This is a subject line', '   ', 'body line 1', 'body line 2'])12        self.assertEqual(m.subject, 'This is a subject line')13        self.assertTrue(m.has_subject_body_separator)14        self.assertEqual(m.body_lines[0], 'body line 1')15        self.assertEqual(m.body_lines[1], 'body line 2')16    def testNonImperative(self):17        m = GitCommitMessage().parse_lines(['Adds new file'])18        self.assertFalse(m.check_subject_imperative())19        m.parse_lines(['Adding new file'])20        self.assertFalse(m.check_subject_imperative())21        # Default to accept22        m.parse_lines(['Foo bar'])23        self.assertTrue(m.check_subject_imperative())24    def testSubjectBodySeparator(self):25        m = GitCommitMessage().parse_lines(['This is a subject line'])26        self.assertTrue(m.check_subject_body_separtor())27        m = GitCommitMessage().parse_lines(['This is a subject line', 'body'])28        self.assertFalse(m.check_subject_body_separtor())29        m = GitCommitMessage().parse_lines(['This is a subject line', '', 'body'])30        self.assertTrue(m.check_subject_body_separtor())31        m = GitCommitMessage().parse_lines(['This is a subject line', '   ', 'body'])32        self.assertTrue(m.check_subject_body_separtor())33    def testSubjectLimit(self):34        m = GitCommitMessage().parse_lines(['This subject line is exactly 48 characters long'])35        self.assertTrue(m.check_subject_limit())36        m = GitCommitMessage().parse_lines(['This is a very long subject line that will obviously exceed the limit'])37        self.assertFalse(m.check_subject_limit())38        m = GitCommitMessage().parse_lines(['This 50-character subject line ends with an LF EOL\n'])39        self.assertTrue(m.check_subject_limit())40    def testSubjectCapitalized(self):41        m = GitCommitMessage().parse_lines(['This subject line is capitalized'])42        self.assertTrue(m.check_subject_capitalized())43        m = GitCommitMessage().parse_lines(['this subject line is not capitalized'])44        self.assertFalse(m.check_subject_capitalized())45    def testSubjectNoPeriod(self):46        m = GitCommitMessage().parse_lines(['This subject line ends with a period.'])47        self.assertFalse(m.check_subject_no_period())48        m = GitCommitMessage().parse_lines(['This subject line does not end with a period'])49        self.assertTrue(m.check_subject_no_period())50    def testBodyLimit(self):51        m = GitCommitMessage().parse_lines(['This is a subject line', ' ', 'A short body line'])52        self.assertTrue(m.check_body_limit())53        m = GitCommitMessage().parse_lines(['This is a subject line', ' ', 'A very long body line which certainly exceeds the 72 char recommended limit'])54        self.assertFalse(m.check_body_limit())55        m = GitCommitMessage().parse_lines(['This is a subject line', ' ', 'A body line with exactly 72 characters, followed by an EOL (Unix-style).\n'])56        self.assertTrue(m.check_body_limit())57    def testCheckAllRules(self):58        m = GitCommitMessage().parse_lines(['This is a subject line', '', 'A short body line'])59        self.assertEqual(0, m.check_the_seven_rules())60if __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!!
