Best Python code snippet using hypothesis
wxpython_test.py
Source:wxpython_test.py
1import textwrap2from libcst.codemod import CodemodTest3from codemods.wxpython import (4 ColorToColourCommand,5 ConstantsRenameCommand,6 DeprecationWarningsCommand,7 FixImportFromAdvCommand,8 FlexGridSizerCommand,9 ListCtrlInsertColumnCommand,10 MakeModalCommand,11 MenuAppendCommand,12 SizerAddCommand,13 ToolbarAddToolCommand,14)15class ColorToColourCommandTests(CodemodTest):16 TRANSFORM = ColorToColourCommand17 def test_call_substitution(self) -> None:18 before = "wx.Color(255, 255, 255)"19 after = "wx.Colour(255, 255, 255)"20 self.assertCodemod(before, after)21 def test_reference_substitution(self) -> None:22 before = "isinstance(a, wx.Color)"23 after = "isinstance(a, wx.Colour)"24 self.assertCodemod(before, after)25class ConstantsRenameCommandTests(CodemodTest):26 TRANSFORM = ConstantsRenameCommand27 def test_WXK_PRIOR_substitution(self) -> None:28 before = "wx.WXK_PRIOR"29 after = "wx.WXK_PAGEUP"30 self.assertCodemod(before, after)31 def test_WXK_NEXT_substitution(self) -> None:32 before = "wx.WXK_NEXT"33 after = "wx.WXK_PAGEDOWN"34 self.assertCodemod(before, after)35 def test_WXK_NUMPAD_PRIOR_substitution(self) -> None:36 before = "wx.WXK_NUMPAD_PRIOR"37 after = "wx.WXK_NUMPAD_PAGEUP"38 self.assertCodemod(before, after)39 def test_WXK_NUMPAD_NEXT_substitution(self) -> None:40 before = "wx.WXK_NUMPAD_NEXT"41 after = "wx.WXK_NUMPAD_PAGEDOWN"42 self.assertCodemod(before, after)43 def test_OPEN_substitution(self) -> None:44 before = "wx.OPEN"45 after = "wx.FD_OPEN"46 self.assertCodemod(before, after)47 def test_FILE_MUST_EXIST_substitution(self) -> None:48 before = "wx.FILE_MUST_EXIST"49 after = "wx.FD_FILE_MUST_EXIST"50 self.assertCodemod(before, after)51 def test_TE_LINEWRAP_substitution(self) -> None:52 before = "wx.TE_LINEWRAP"53 after = "wx.TE_BESTWRAP"54 self.assertCodemod(before, after)55class FixImportFromAdvCommandTests(CodemodTest):56 TRANSFORM = FixImportFromAdvCommand57 def test_add_import_once_substitution(self) -> None:58 before = textwrap.dedent(59 """60 import wx.adv61 wx.DP_ALLOWNONE62 """63 )64 after = textwrap.dedent(65 """66 import wx.adv67 wx.adv.DP_ALLOWNONE68 """69 )70 self.assertCodemod(before, after)71 def test_constants_DP_ALLOWNONE_substitution(self) -> None:72 before = "wx.DP_ALLOWNONE"73 after = textwrap.dedent(74 """75 import wx.adv76 wx.adv.DP_ALLOWNONE77 """78 )79 self.assertCodemod(before, after)80 def test_constants_DP_DROPDOWN_substitution(self) -> None:81 before = "wx.DP_DROPDOWN"82 after = textwrap.dedent(83 """84 import wx.adv85 wx.adv.DP_DROPDOWN86 """87 )88 self.assertCodemod(before, after)89 def test_constants_DP_SHOWCENTURY_substitution(self) -> None:90 before = "wx.DP_SHOWCENTURY"91 after = textwrap.dedent(92 """93 import wx.adv94 wx.adv.DP_SHOWCENTURY95 """96 )97 self.assertCodemod(before, after)98 def test_symbols_DatePickerCtrl_substitution(self) -> None:99 before = "wx.DatePickerCtrl"100 after = textwrap.dedent(101 """102 import wx.adv103 wx.adv.DatePickerCtrl104 """105 )106 self.assertCodemod(before, after)107class FlexGridSizerCommandTests(CodemodTest):108 TRANSFORM = FlexGridSizerCommand109 def test_no_op(self) -> None:110 before = "wx.FlexGridSizer(1, 0, 0)"111 after = "wx.FlexGridSizer(1, 0, 0)"112 self.assertCodemod(before, after)113 def test_substitution(self) -> None:114 before = "wx.FlexGridSizer(1, 0)"115 after = "wx.FlexGridSizer(1, 0, 0)"116 self.assertCodemod(before, after)117class MenuAppendCommandTests(CodemodTest):118 TRANSFORM = MenuAppendCommand119 def test_keywords_substitution(self) -> None:120 before = textwrap.dedent(121 """122 menu.Append(123 help="",124 id=1,125 kind=wx.ITEM_NORMAL,126 text="Menu item",127 )128 """129 )130 after = textwrap.dedent(131 """132 menu.Append(133 helpString="",134 id=1,135 kind=wx.ITEM_NORMAL,136 item="Menu item",137 )138 """139 )140 self.assertCodemod(before, after)141 def test_deprecated_method_substitution(self) -> None:142 before = "menu.AppendItem(menu_item)"143 after = "menu.Append(menu_item)"144 self.assertCodemod(before, after)145 def test_deprecated_method_substitution_skip_other_signatures(self) -> None:146 before = "menu.AppendItem(parent, text, image=-1, selImage=-1, data=None)"147 after = "menu.AppendItem(parent, text, image=-1, selImage=-1, data=None)"148 self.assertCodemod(before, after)149class ToolbarAddToolCommandTests(CodemodTest):150 TRANSFORM = ToolbarAddToolCommand151 def test_substitution(self) -> None:152 before = textwrap.dedent(153 """154 toolbar.DoAddTool(155 bitmap=my_bitmap,156 id=1,157 label="Toolbar tool"158 )159 """160 )161 after = textwrap.dedent(162 """163 toolbar.AddTool(164 bitmap=my_bitmap,165 toolId=1,166 label="Toolbar tool"167 )168 """169 )170 self.assertCodemod(before, after)171class ListCtrlInsertColumnCommandTests(CodemodTest):172 TRANSFORM = ListCtrlInsertColumnCommand173 def test_substitution(self) -> None:174 before = "self.InsertColumnInfo(0, info)"175 after = "self.InsertColumn(0, info)"176 self.assertCodemod(before, after)177class DeprecationWarningsCommandTests(CodemodTest):178 TRANSFORM = DeprecationWarningsCommand179 def test_substitution(self) -> None:180 before = "wx.BitmapFromImage()"181 after = "wx.Bitmap()"182 self.assertCodemod(before, after)183 def test_substitution_with_tuple(self) -> None:184 before = "wx.DateTimeFromDMY(*args)"185 after = "wx.DateTime.FromDMY(*args)"186 self.assertCodemod(before, after)187 def test_imported_symbols_substitution(self) -> None:188 before = textwrap.dedent(189 """190 from wx import BitmapFromImage191 BitmapFromImage()192 """193 )194 after = textwrap.dedent(195 """196 import wx197 wx.Bitmap()198 """199 )200 self.assertCodemod(before, after)201class SizerAddWindowCommandTests(CodemodTest):202 TRANSFORM = SizerAddCommand203 def test_substitution(self) -> None:204 before = "sizer.AddWindow(panel, 0, border=0, flag=wx.EXPAND)"205 after = "sizer.Add(panel, 0, border=0, flag=wx.EXPAND)"206 self.assertCodemod(before, after)207class MakeModalCommandTests(CodemodTest):208 TRANSFORM = MakeModalCommand209 def test_substitution(self) -> None:210 before = textwrap.dedent(211 """212 class MyModal(wx.Frame):213 def __init__(self):214 super().__init__()215 self.MakeModal()216 """217 )218 after = textwrap.dedent(219 """220 class MyModal(wx.Frame):221 def __init__(self):222 super().__init__()223 self.MakeModal()224 def MakeModal(self, modal=True):225 if modal and not hasattr(self, '_disabler'):226 self._disabler = wx.WindowDisabler(self)227 if not modal and hasattr(self, '_disabler'):228 del self._disabler229 """230 )231 self.assertCodemod(before, after)232 def test_no_op(self) -> None:233 before = textwrap.dedent(234 """235 class MyModal(wx.Frame):236 def __init__(self):237 super().__init__()238 self.MakeModal()239 def MakeModal(self, modal=True):240 if modal and not hasattr(self, '_disabler'):241 self._disabler = wx.WindowDisabler(self)242 if not modal and hasattr(self, '_disabler'):243 del self._disabler244 """245 )246 after = textwrap.dedent(247 """248 class MyModal(wx.Frame):249 def __init__(self):250 super().__init__()251 self.MakeModal()252 def MakeModal(self, modal=True):253 if modal and not hasattr(self, '_disabler'):254 self._disabler = wx.WindowDisabler(self)255 if not modal and hasattr(self, '_disabler'):256 del self._disabler257 """258 )...
codemod.js
Source:codemod.js
1var jscodeshift = require('jscodeshift');2require('babel-register')({only: /node_modules\/(shopify-codemod|js-codemod)/});3var TRANSFORMS = [4 {path: 'shopify-codemod/transforms/split-if-assignments'},5 {path: 'shopify-codemod/transforms/split-assignment-sequences'},6 {path: 'shopify-codemod/transforms/coffeescript-soak-to-condition'},7 {path: 'shopify-codemod/transforms/ternary-statement-to-if-statement'},8 {path: 'shopify-codemod/transforms/remove-useless-return-from-test', test: true},9 {path: 'shopify-codemod/transforms/mocha-context-to-global-reference', test: true},10 {path: 'shopify-codemod/transforms/mocha-context-to-closure', test: true},11 {path: 'shopify-codemod/transforms/coffeescript-range-output-to-helper'},12 {path: 'shopify-codemod/transforms/remove-addeventlistener-returns'},13 {path: 'shopify-codemod/transforms/conditional-assign-to-if-statement'},14 // This transform must appear before the constant-function-expression-to-statement and15 // object-shorthand transforms in order to catch the most empty functions possible. It16 // must also go before global-identifier-to-import so that lodash is correctly imported.17 {path: 'shopify-codemod/transforms/empty-func-to-lodash-noop'},18 {path: 'shopify-codemod/transforms/global-assignment-to-default-export', test: false},19 {path: 'shopify-codemod/transforms/convert-default-export-objects-to-named-exports', test: false},20 {path: 'shopify-codemod/transforms/add-missing-parseint-radix'},21 {path: 'shopify-codemod/transforms/implicit-coercion-to-explicit'},22 // Order is significant for these initial assert transforms; think carefully before reordering.23 {path: 'shopify-codemod/transforms/assert/assert-false-to-assert-fail', test: true},24 {path: 'shopify-codemod/transforms/assert/assert-to-assert-ok', test: true},25 {path: 'shopify-codemod/transforms/assert/negated-assert-ok-to-assert-not-ok', test: true},26 {path: 'shopify-codemod/transforms/assert/move-literals-to-expected-argument', test: true},27 {path: 'shopify-codemod/transforms/assert/equality-comparisons-to-assertions', test: true},28 // These transforms can be executed in any order.29 {path: 'shopify-codemod/transforms/assert/called-equals-boolean-to-assert-called', test: true},30 {path: 'shopify-codemod/transforms/assert/call-count-equals-to-assert-called', test: true},31 {path: 'shopify-codemod/transforms/assert/called-method-to-assert-called', test: true},32 {path: 'shopify-codemod/transforms/assert/called-with-methods-to-assert-called-with', test: true},33 {path: 'shopify-codemod/transforms/assert/falsy-called-method-to-assert-not-called', test: true},34 // These transforms must appear before remove-empty-returns and remove-unused-expressions35 {path: 'shopify-codemod/transforms/remove-trailing-else-undefined-return'},36 {path: 'shopify-codemod/transforms/avoid-returning-unused-results'},37 {path: 'shopify-codemod/transforms/avoid-returning-useless-expressions'},38 // These are more generic, stylistic transforms, so they should come last to catch any39 // new nodes introduced by other transforms40 {path: 'shopify-codemod/transforms/remove-empty-returns'},41 {path: 'shopify-codemod/transforms/function-to-arrow'},42 {path: 'js-codemod/transforms/arrow-function'},43 {path: 'js-codemod/transforms/template-literals'},44 {path: 'shopify-codemod/transforms/strip-template-literal-parenthesis'},45 {path: 'js-codemod/transforms/object-shorthand'},46 {path: 'shopify-codemod/transforms/iife-to-ternary-expression'},47 {path: 'shopify-codemod/transforms/existential-assignment-to-if-statement'},48 {path: 'shopify-codemod/transforms/arguments-to-args-spread'},49 // These are run very late to ensure they catch any identifiers/ member expressions50 // added in earlier transforms51 {path: 'js-codemod/transforms/unquote-properties'},52 {path: 'shopify-codemod/transforms/computed-literal-keys-to-dot-notation'},53 {path: 'shopify-codemod/transforms/rename-identifier'},54 {path: 'shopify-codemod/transforms/rename-property'},55 {path: 'shopify-codemod/transforms/split-return-assignments'},56 // constant-function-expression-to-statement and global-reference-to-import need57 // `const` references, so they must happen after `no-vars`58 {path: 'js-codemod/transforms/no-vars'},59 {path: 'shopify-codemod/transforms/constant-function-expression-to-statement'},60 {path: 'shopify-codemod/transforms/global-reference-to-import'},61 {path: 'shopify-codemod/transforms/global-identifier-to-import'},62 // Must appear after constant-function-expression-to-statement in order to remove63 // unneeded semicolons from exported function declarations64 {path: 'shopify-codemod/transforms/remove-unused-expressions'},65 {path: 'shopify-codemod/transforms/remove-empty-statements'},66];67function runTransform(file, code, name, options) {68 var module = require(name);69 if (module.__esModule) { module = module.default; }70 var newCode = module({path: file, source: code}, {jscodeshift: jscodeshift}, options);71 return newCode == null ? code : newCode;72}73module.exports = function transform(details) {74 var source = details.source;75 var options = details.options;76 var testTransforms = (details.file.indexOf('test') >= 0);77 TRANSFORMS.forEach(function(transformer) {78 if (transformer.test == null || transformer.test === testTransforms) {79 source = runTransform(details.file, source, transformer.path, options);80 }81 });82 return source;...
mods_test.py
Source:mods_test.py
1import textwrap2import pytest3from libcst import PartialParserConfig, parse_module4from libcst.codemod import CodemodContext, CodemodTest, SkipFile5from addexports.mods import AddExportsToDunderAllCommand6class AddExportsToDunderAllTest(CodemodTest):7 TRANSFORM = AddExportsToDunderAllCommand8 def test_substitution(self) -> None:9 before = textwrap.dedent(10 """11 import features12 from .tasks import Task1, Task213 # use alias name14 from .tasks import TASK3 as Task315 # treat as private because of underscore16 from .tasks import Task4 as _Task417 # ignore18 from .tasks import Task5 as Task519 """20 )21 after = textwrap.dedent(22 """23 import features24 from .tasks import Task1, Task225 # use alias name26 from .tasks import TASK3 as Task327 # treat as private because of underscore28 from .tasks import Task4 as _Task429 # ignore30 from .tasks import Task5 as Task531 __all__ = ['Task1', 'Task2', 'Task3']32 """33 )34 self.assertCodemod(before, after, ignore=["Task5"])35 def test_ignore_existing_exports_single_quoted(self) -> None:36 before = textwrap.dedent(37 """38 from .tasks import Task139 __all__ = ['Task1']40 """41 )42 with pytest.raises(SkipFile):43 self.assertCodemod(before, before)44 def test_ignore_existing_exports_double_quoted(self) -> None:45 before = textwrap.dedent(46 """47 from .tasks import Task148 __all__ = ["Task1"]49 """50 )51 self.assertCodemod(before, before, expected_skip=True)52 def test_append_to_existing_exports(self) -> None:53 before = textwrap.dedent(54 """55 from .tasks import Task1, Task256 __all__ = ["Task2"]57 """58 )59 after = textwrap.dedent(60 """61 from .tasks import Task1, Task262 __all__ = ['Task1', 'Task2']63 """64 )65 self.assertCodemod(before, after)66 def test_command_reset_between_modules(self) -> None:67 # reuse single instance in the same manner as parallel_exec_transform_with_prettyprint68 transform_instance = AddExportsToDunderAllCommand(CodemodContext())69 before = textwrap.dedent(70 """71 from .tasks import Task172 """73 )74 input_tree = parse_module(75 CodemodTest.make_fixture_data(before),76 config=(PartialParserConfig()),77 )78 transform_instance.transform_module(input_tree)79 before_skip = textwrap.dedent(80 """81 from .tasks import Task282 __all__ = ['Task2']83 """84 )85 input_tree_skip = parse_module(86 CodemodTest.make_fixture_data(before_skip),87 config=(PartialParserConfig()),88 )89 # should skip unless it sees Task1 from first invocation90 with pytest.raises(SkipFile):...
test_codemods.py
Source:test_codemods.py
1import importlib2import os3import pytest4codemods = [5 "not_in",6 # "remove_builtins_imports",7 # "detect_past_builtins_imports",8 "remove_float_conversion",9]10def _get_files(codemod):11 input_file = os.path.join(os.path.dirname(__file__), "input", codemod + ".py")12 expected_file = os.path.join(os.path.dirname(__file__), "expected", codemod + ".py")13 return input_file, expected_file14def _run_test(codemod, input_file, expected_file, monkeypatch):15 monkeypatch.setattr(16 "sys.argv", ["codemod_" + codemod, "--test", input_file, expected_file]17 )18 module = importlib.import_module("octoprint_codemods." + codemod)19 getattr(module, "main")()20def _run_batch(input_file, expected_file, monkeypatch):21 argv = ["codemod_batch"]22 for codemod in codemods:23 argv += ["--check", codemod]24 argv += ["--test", input_file, expected_file]25 monkeypatch.setattr("sys.argv", argv)26 module = importlib.import_module("octoprint_codemods.batch")27 getattr(module, "main")()28@pytest.mark.parametrize(29 "codemod", [pytest.param(codemod, id=codemod) for codemod in codemods]30)31def test_codemods(codemod, monkeypatch):32 input_file, expected_file = _get_files(codemod)33 if not os.path.exists(input_file) or not os.path.exists(expected_file):34 pytest.fail("Input file or expected file missing for {}".format(codemod))35 with pytest.raises(SystemExit) as exc:36 _run_test(codemod, input_file, expected_file, monkeypatch)37 assert exc.value.code == 038def test_batch(monkeypatch):39 codemod = "batch"40 input_file, expected_file = _get_files(codemod)41 if not os.path.exists(input_file) or not os.path.exists(expected_file):42 pytest.fail("Input file or expected file missing for batch")43 with pytest.raises(SystemExit) as exc:44 _run_batch(45 input_file,46 expected_file,47 monkeypatch,48 )...
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!!