Best Python code snippet using hypothesis
return_statements_test.py
Source:return_statements_test.py
...23class SingleReturnTest(converter_testing.TestCase):24 def assertTransformedEquivalent(self, test_fn, *inputs):25 ns = {'ops': ops}26 with self.converted(test_fn, return_statements, ns) as result:27 self.assertEqual(test_fn(*inputs), result.test_fn(*inputs))28 def test_straightline(self):29 def test_fn(x):30 return x * x31 self.assertTransformedEquivalent(test_fn, 2)32 def test_conditional(self):33 def test_fn(x):34 if x > 0:35 return x36 else:37 return x * x38 self.assertTransformedEquivalent(test_fn, 2)39 self.assertTransformedEquivalent(test_fn, -2)40 def test_missing_else(self):41 def test_fn(x):42 if x > 0:43 return x44 self.assertTransformedEquivalent(test_fn, 2)45 self.assertTransformedEquivalent(test_fn, -2)46 def test_missing_else_then_default(self):47 def test_fn(x):48 if x > 0:49 return x50 return x * x51 self.assertTransformedEquivalent(test_fn, 2)52 self.assertTransformedEquivalent(test_fn, -2)53 def test_else_only_then_default(self):54 def test_fn(x):55 if x < 0:56 x *= x57 else:58 return x59 return x60 self.assertTransformedEquivalent(test_fn, 2)61 self.assertTransformedEquivalent(test_fn, -2)62 def test_conditional_nested(self):63 def test_fn(x):64 if x > 0:65 if x < 5:66 return x67 else:68 return x * x69 else:70 return x * x * x71 self.assertTransformedEquivalent(test_fn, 2)72 self.assertTransformedEquivalent(test_fn, -2)73 self.assertTransformedEquivalent(test_fn, 5)74 def test_context_manager(self):75 def test_fn(x):76 with ops.name_scope(''):77 return x * x78 self.assertTransformedEquivalent(test_fn, 2)79 self.assertTransformedEquivalent(test_fn, -2)80 def test_context_manager_in_conditional(self):81 def test_fn(x):82 if x > 0:83 with ops.name_scope(''):84 return x * x85 else:86 return x87 self.assertTransformedEquivalent(test_fn, 2)88 self.assertTransformedEquivalent(test_fn, -2)89 def text_conditional_in_context_manager(self):90 def test_fn(x):91 with ops.name_scope(''):92 if x > 0:93 return x * x94 else:95 return x96 self.assertTransformedEquivalent(test_fn, 2)97 self.assertTransformedEquivalent(test_fn, -2)98 def test_no_return(self):99 def test_fn(x):100 x *= x101 self.assertTransformedEquivalent(test_fn, 2)102 def test_nested_function(self):103 def test_fn(x):104 def inner_fn(y):105 if y > 0:106 return y * y107 else:108 return y109 return inner_fn(x)110 self.assertTransformedEquivalent(test_fn, 2)111 self.assertTransformedEquivalent(test_fn, -2)112 def test_nested_function_in_control_flow(self):113 def test_fn(x):114 if x:115 def inner_fn(y):116 return y117 inner_fn(x)118 self.assertTransformedEquivalent(test_fn, 2)119 self.assertTransformedEquivalent(test_fn, -2)120 def test_for_loop(self):121 def test_fn(n):122 for _ in range(n):123 return 1124 self.assertTransformedEquivalent(test_fn, 2)125 self.assertTransformedEquivalent(test_fn, 0)126 def test_while_loop(self):127 def test_fn(n):128 i = 0129 s = 0130 while i < n:131 i += 1132 s += i133 if s > 4:134 return s135 return -1136 self.assertTransformedEquivalent(test_fn, 0)137 self.assertTransformedEquivalent(test_fn, 2)138 self.assertTransformedEquivalent(test_fn, 4)139 def test_null_return(self):140 def test_fn(n):141 if n > 4:142 return143 return144 self.assertTransformedEquivalent(test_fn, 4)145 self.assertTransformedEquivalent(test_fn, 5)146 def test_nested_multiple_withs(self):147 def test_fn(x):148 v = []149 while x > 0:150 x -= 1151 with ops.name_scope(''):152 if x % 2 == 0:153 return v154 with ops.name_scope(''):155 v.append(x)156 v.append(x)157 return v158 self.assertTransformedEquivalent(test_fn, 0)159 self.assertTransformedEquivalent(test_fn, 1)160 self.assertTransformedEquivalent(test_fn, 3)161 self.assertTransformedEquivalent(test_fn, 4)...
break_statements_test.py
Source:break_statements_test.py
...20from tensorflow.contrib.py2tf.converters import converter_test_base21from tensorflow.python.platform import test22class BreakCanonicalizationTest(converter_test_base.TestCase):23 def test_basic_break(self):24 def test_fn(x):25 v = []26 while x > 0:27 x -= 128 if x % 2 == 0:29 break30 v.append(x)31 return v32 node = self.parse_and_analyze(test_fn, {})33 node = break_statements.transform(node, self.ctx)34 with self.compiled(node) as result:35 self.assertEqual(test_fn(0), result.test_fn(0))36 self.assertEqual(test_fn(1), result.test_fn(1))37 self.assertEqual(test_fn(2), result.test_fn(2))38 self.assertEqual(test_fn(3), result.test_fn(3))39 self.assertEqual(test_fn(4), result.test_fn(4))40 def test_basic_break_for_loop(self):41 def test_fn(a):42 v = []43 for x in a:44 x -= 145 if x % 2 == 0:46 break47 v.append(x)48 return v49 # The break is incompletely canonicalized for for loops. Everything is50 # in place except for the condition verification.51 def test_equiv_fn(a):52 v = []53 for x in a:54 x -= 155 if x % 2 == 0:56 continue57 v.append(x)58 return v59 node = self.parse_and_analyze(test_fn, {})60 node = break_statements.transform(node, self.ctx)61 with self.compiled(node) as result:62 # The break is incompletely canonicalized. Everything is in place, but63 # the loop does not break.64 self.assertEqual(test_equiv_fn([]), result.test_fn([]))65 self.assertEqual(test_equiv_fn([1]), result.test_fn([1]))66 self.assertEqual(test_equiv_fn([2]), result.test_fn([2]))67 self.assertEqual(68 test_equiv_fn([1, 2, 3, 4]), result.test_fn([1, 2, 3, 4]))69 def test_continue_deeply_nested(self):70 def test_fn(x):71 v = []72 u = []73 w = []74 while x > 0:75 x -= 176 if x % 2 == 0:77 if x % 3 != 0:78 u.append(x)79 else:80 w.append(x)81 continue82 v.append(x)83 return v, u, w84 node = self.parse_and_analyze(test_fn, {})85 node = break_statements.transform(node, self.ctx)86 with self.compiled(node) as result:87 self.assertEqual(test_fn(0), result.test_fn(0))88 self.assertEqual(test_fn(1), result.test_fn(1))89 self.assertEqual(test_fn(2), result.test_fn(2))90 self.assertEqual(test_fn(3), result.test_fn(3))91 self.assertEqual(test_fn(4), result.test_fn(4))92if __name__ == '__main__':...
continue_statements_test.py
Source:continue_statements_test.py
...20from tensorflow.contrib.py2tf.converters import converter_test_base21from tensorflow.python.platform import test22class ContinueCanonicalizationTest(converter_test_base.TestCase):23 def test_basic_continue(self):24 def test_fn(x):25 v = []26 while x > 0:27 x -= 128 if x % 2 == 0:29 continue30 v.append(x)31 return v32 node = self.parse_and_analyze(test_fn, {})33 node = continue_statements.transform(node, self.ctx)34 with self.compiled(node) as result:35 self.assertEqual(test_fn(0), result.test_fn(0))36 self.assertEqual(test_fn(1), result.test_fn(1))37 self.assertEqual(test_fn(2), result.test_fn(2))38 self.assertEqual(test_fn(3), result.test_fn(3))39 self.assertEqual(test_fn(4), result.test_fn(4))40 def test_basic_continue_for_loop(self):41 def test_fn(a):42 v = []43 for x in a:44 x -= 145 if x % 2 == 0:46 continue47 v.append(x)48 return v49 node = self.parse_and_analyze(test_fn, {})50 node = continue_statements.transform(node, self.ctx)51 with self.compiled(node) as result:52 self.assertEqual(test_fn([]), result.test_fn([]))53 self.assertEqual(test_fn([1]), result.test_fn([1]))54 self.assertEqual(test_fn([2]), result.test_fn([2]))55 self.assertEqual(test_fn([1, 2, 3]), result.test_fn([1, 2, 3]))56 def test_continue_deeply_nested(self):57 def test_fn(x):58 v = []59 u = []60 w = []61 while x > 0:62 x -= 163 if x % 2 == 0:64 if x % 3 != 0:65 u.append(x)66 else:67 w.append(x)68 continue69 v.append(x)70 return v, u, w71 node = self.parse_and_analyze(test_fn, {})72 node = continue_statements.transform(node, self.ctx)73 with self.compiled(node) as result:74 self.assertEqual(test_fn(0), result.test_fn(0))75 self.assertEqual(test_fn(1), result.test_fn(1))76 self.assertEqual(test_fn(2), result.test_fn(2))77 self.assertEqual(test_fn(3), result.test_fn(3))78 self.assertEqual(test_fn(4), result.test_fn(4))79if __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!!