Best Python code snippet using hypothesis
return_statements_test.py
Source:return_statements_test.py  
1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14# ==============================================================================15"""Tests for return_statements module."""16from __future__ import absolute_import17from __future__ import division18from __future__ import print_function19from tensorflow.python.autograph.converters import return_statements20from tensorflow.python.autograph.core import converter_testing21from tensorflow.python.framework import ops22from tensorflow.python.platform import test23class 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_superfluous_returns(self):33    def test_fn():34      retval = 135      return retval36      retval = 2  # pylint:disable=unreachable37      return retval38    self.assertTransformedEquivalent(test_fn)39  def test_superfluous_returns_adjacent(self):40    def test_fn():41      return 142      return 2  # pylint:disable=unreachable43    self.assertTransformedEquivalent(test_fn)44  def test_conditional(self):45    def test_fn(x):46      if x > 0:47        return x48      else:49        return x * x50    self.assertTransformedEquivalent(test_fn, 2)51    self.assertTransformedEquivalent(test_fn, -2)52  def test_conditional_missing_else(self):53    def test_fn(x):54      if x > 0:55        return x56    self.assertTransformedEquivalent(test_fn, 2)57    self.assertTransformedEquivalent(test_fn, -2)58  def test_conditional_missing_else_then_default(self):59    def test_fn(x):60      if x > 0:61        return x62      return x * x63    self.assertTransformedEquivalent(test_fn, 2)64    self.assertTransformedEquivalent(test_fn, -2)65  def test_conditional_else_only_then_default(self):66    def test_fn(x):67      if x < 0:68        x *= x69      else:70        return x71      return x72    self.assertTransformedEquivalent(test_fn, 2)73    self.assertTransformedEquivalent(test_fn, -2)74  def test_conditional_nested(self):75    def test_fn(x):76      if x > 0:77        if x < 5:78          return x79        else:80          return x * x81      else:82        return x * x * x83    self.assertTransformedEquivalent(test_fn, 2)84    self.assertTransformedEquivalent(test_fn, -2)85    self.assertTransformedEquivalent(test_fn, 5)86  def test_context_manager(self):87    def test_fn(x):88      with ops.name_scope(''):89        return x * x90    self.assertTransformedEquivalent(test_fn, 2)91    self.assertTransformedEquivalent(test_fn, -2)92  def test_context_manager_in_conditional(self):93    def test_fn(x):94      if x > 0:95        with ops.name_scope(''):96          return x * x97      else:98        return x99    self.assertTransformedEquivalent(test_fn, 2)100    self.assertTransformedEquivalent(test_fn, -2)101  def text_conditional_in_context_manager(self):102    def test_fn(x):103      with ops.name_scope(''):104        if x > 0:105          return x * x106        else:107          return x108    self.assertTransformedEquivalent(test_fn, 2)109    self.assertTransformedEquivalent(test_fn, -2)110  def test_no_return(self):111    def test_fn(x):112      x *= x113    self.assertTransformedEquivalent(test_fn, 2)114  def test_nested_function(self):115    def test_fn(x):116      def inner_fn(y):117        if y > 0:118          return y * y119        else:120          return y121      return inner_fn(x)122    self.assertTransformedEquivalent(test_fn, 2)123    self.assertTransformedEquivalent(test_fn, -2)124  def test_nested_function_in_control_flow(self):125    def test_fn(x):126      if x:127        def inner_fn(y):128          return y129        inner_fn(x)130    self.assertTransformedEquivalent(test_fn, 2)131    self.assertTransformedEquivalent(test_fn, -2)132  def test_for_loop(self):133    def test_fn(n):134      for _ in range(n):135        return 1136    self.assertTransformedEquivalent(test_fn, 2)137    self.assertTransformedEquivalent(test_fn, 0)138  def test_while_loop(self):139    def test_fn(n):140      i = 0141      s = 0142      while i < n:143        i += 1144        s += i145        if s > 4:146          return s147      return -1148    self.assertTransformedEquivalent(test_fn, 0)149    self.assertTransformedEquivalent(test_fn, 2)150    self.assertTransformedEquivalent(test_fn, 4)151  def test_null_return(self):152    def test_fn(n):153      if n > 4:154        return155      return156    self.assertTransformedEquivalent(test_fn, 4)157    self.assertTransformedEquivalent(test_fn, 5)158  def test_nested_multiple_withs(self):159    def test_fn(x):160      v = []161      while x > 0:162        x -= 1163        with ops.name_scope(''):164          if x % 2 == 0:165            return v166        with ops.name_scope(''):167          v.append(x)168        v.append(x)169      return v170    self.assertTransformedEquivalent(test_fn, 0)171    self.assertTransformedEquivalent(test_fn, 1)172    self.assertTransformedEquivalent(test_fn, 3)173    self.assertTransformedEquivalent(test_fn, 4)174  def test_multiple_returns_in_nested_scope(self):175    def test_fn(a):176      v = []177      for x in a:178        x -= 1179        if x > 100:180          return v181        try:182          raise ValueError('intentional')183        except ValueError:  # pylint:disable=bare-except184          return v185        v.append(x)186      return v187    self.assertTransformedEquivalent(test_fn, [])188    self.assertTransformedEquivalent(test_fn, [1])189    self.assertTransformedEquivalent(test_fn, [2])190    self.assertTransformedEquivalent(test_fn, [1, 2, 3])191if __name__ == '__main__':...continue_statements_test.py
Source:continue_statements_test.py  
1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14# ==============================================================================15"""Tests for continue_statements module."""16from __future__ import absolute_import17from __future__ import division18from __future__ import print_function19from tensorflow.python.autograph.converters import continue_statements20from tensorflow.python.autograph.core import converter_testing21from tensorflow.python.framework import constant_op22from tensorflow.python.framework import ops23from tensorflow.python.platform import test24class ContinueCanonicalizationTest(converter_testing.TestCase):25  def assertTransformedEquivalent(self, test_fn, *inputs):26    with self.converted(test_fn, continue_statements, {'ops': ops},27                        (constant_op.constant,)) as result:28      self.assertEqual(test_fn(*inputs), result.test_fn(*inputs))29  def test_basic(self):30    def test_fn(x):31      v = []32      while x > 0:33        x -= 134        if x % 2 == 0:35          continue36        v.append(x)37      return v38    self.assertTransformedEquivalent(test_fn, 0)39    self.assertTransformedEquivalent(test_fn, 1)40    self.assertTransformedEquivalent(test_fn, 3)41    self.assertTransformedEquivalent(test_fn, 4)42  def test_multiple_continues(self):43    def test_fn(x):44      v = []45      while x > 0:46        x -= 147        if x > 1:48          continue49        if x > 2:50          continue51        v.append(x)52      return v53    self.assertTransformedEquivalent(test_fn, 0)54    self.assertTransformedEquivalent(test_fn, 1)55    self.assertTransformedEquivalent(test_fn, 3)56    self.assertTransformedEquivalent(test_fn, 4)57  def test_multiple_continues_in_nested_scope(self):58    def test_fn(a):59      v = []60      for x in a:61        x -= 162        if x > 100:63          continue64        try:65          raise ValueError('intentional')66        except ValueError:67          continue68        v.append(x)69      return v70    self.assertTransformedEquivalent(test_fn, [])71    self.assertTransformedEquivalent(test_fn, [1])72    self.assertTransformedEquivalent(test_fn, [2])73    self.assertTransformedEquivalent(test_fn, [1, 2, 3])74  def test_for_loop(self):75    def test_fn(a):76      v = []77      for x in a:78        x -= 179        if x % 2 == 0:80          continue81        v.append(x)82      return v83    self.assertTransformedEquivalent(test_fn, [])84    self.assertTransformedEquivalent(test_fn, [1])85    self.assertTransformedEquivalent(test_fn, [2])86    self.assertTransformedEquivalent(test_fn, [1, 2, 3])87  def test_nested_with(self):88    def test_fn(x):89      v = []90      while x > 0:91        x -= 192        with ops.name_scope(''):93          if x % 2 == 0:94            continue95        v.append(x)96      return v97    self.assertTransformedEquivalent(test_fn, 0)98    self.assertTransformedEquivalent(test_fn, 1)99    self.assertTransformedEquivalent(test_fn, 3)100    self.assertTransformedEquivalent(test_fn, 4)101  def test_nested_multiple_withs(self):102    def test_fn(x):103      v = []104      while x > 0:105        x -= 1106        with ops.name_scope(''):107          if x % 2 == 0:108            continue109        with ops.name_scope(''):110          v.append(x)111        v.append(x)112      return v113    self.assertTransformedEquivalent(test_fn, 0)114    self.assertTransformedEquivalent(test_fn, 1)115    self.assertTransformedEquivalent(test_fn, 3)116    self.assertTransformedEquivalent(test_fn, 4)117  def test_nested_multiple_withs_and_statements(self):118    def test_fn(x):119      v = []120      while x > 0:121        x -= 1122        with ops.name_scope(''):123          if x % 2 == 0:124            continue125          v.append(x)126        v.append(x)127        with ops.name_scope(''):128          v.append(x)129        v.append(x)130      return v131    self.assertTransformedEquivalent(test_fn, 0)132    self.assertTransformedEquivalent(test_fn, 1)133    self.assertTransformedEquivalent(test_fn, 3)134    self.assertTransformedEquivalent(test_fn, 4)135  def test_nested_multiple_withs_and_nested_withs(self):136    def test_fn(x):137      v = []138      while x > 0:139        x -= 1140        with ops.name_scope(''):141          if x % 2 == 0:142            continue143          with ops.name_scope(''):144            v.append(x)145        v.append(x)146        with ops.name_scope(''):147          v.append(x)148        v.append(x)149      return v150    self.assertTransformedEquivalent(test_fn, 0)151    self.assertTransformedEquivalent(test_fn, 1)152    self.assertTransformedEquivalent(test_fn, 3)153    self.assertTransformedEquivalent(test_fn, 4)154  def test_nested(self):155    def test_fn(x):156      v = []157      u = []158      w = []159      while x > 0:160        x -= 1161        if x % 2 == 0:162          if x % 3 != 0:163            u.append(x)164          else:165            w.append(x)166            continue167        v.append(x)168      return v, u, w169    self.assertTransformedEquivalent(test_fn, 0)170    self.assertTransformedEquivalent(test_fn, 1)171    self.assertTransformedEquivalent(test_fn, 3)172    self.assertTransformedEquivalent(test_fn, 4)173  def test_multiple_guarded_continues_with_side_effects(self):174    def test_fn(x):175      def track(u, x):176        u.append(x)177        return x178      u = []179      v = []180      while x > 0:181        x -= 1182        if track(u, x) > 1:183          continue184        if track(u, x) > 2:185          continue186        v.append(x)187      return u, v188    self.assertTransformedEquivalent(test_fn, 3)189    self.assertTransformedEquivalent(test_fn, 2)190if __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!!
