How to use wrapper_generator method in autotest

Best Python code snippet using autotest_python

test_wrapper_generator.py

Source:test_wrapper_generator.py Github

copy

Full Screen

1import unittest2from pycparser import CParser3from . import WrapperGenerator, wrapper_generator4class TestDeclVisitor(unittest.TestCase):5 def setUp(self):6 source = """7 typedef struct { int i; } newtComponent;8 int newtInit(void);9 int newtFoo(int i, newtComponent string);10 """11 self.ast = CParser().parse(source, "aname")12 def testDeclVisitorReturnAllNames(self):13 v = wrapper_generator.DeclVisitor()14 v.visit(self.ast)15 self.assertEqual(2, len(v.functions))16 def testDeclVisitorReturnFuncName(self):17 v = wrapper_generator.DeclVisitor()18 ast = CParser().parse("int newtInit(void);", "aname")19 v.visit(ast)20 self.assertEqual('newtInit', v.functions[0].name)21 def testDeclVisitorReturnEmptyParam(self):22 v = wrapper_generator.DeclVisitor()23 ast = CParser().parse("int newtInit(void);", "aname")24 v.visit(ast)25 self.assertEqual([], v.functions[0].args)26 def testDeclVisitorReturnTwoParam(self):27 v = wrapper_generator.DeclVisitor()28 v.visit(self.ast)29 self.assertEqual(2, len(v.functions[1].args))30 argtype, name = v.functions[1].args[0]31 self.assertEqual('i', name)32 self.assertEqual('int', argtype)33 argtype, name = v.functions[1].args[1]34 self.assertEqual('newtComponent', argtype)35 def testDeclVisitorReturnPointerParam(self):36 v = wrapper_generator.DeclVisitor()37 ast = CParser().parse("int newtInit(char * foo, char ** bar);",38 "aname")39 v.visit(ast)40 argtype, name = v.functions[0].args[0]41 self.assertEqual('foo', name)42 self.assertEqual('char *', argtype)43 argtype, name = v.functions[0].args[1]44 self.assertEqual('bar', name)45 self.assertEqual('char **', argtype)46 def testDeclVisitorReturnEnumParam(self):47 v = wrapper_generator.DeclVisitor()48 ast = CParser().parse("""49 void f(enum newtFlagsSense sense);50 """,51 "aname")52 v.visit(ast)53 argtype, name = v.functions[0].args[0]54 self.assertEqual('sense', name)55 self.assertEqual('enum newtFlagsSense', argtype)56 def testDeclVisitorReturnStructParam(self):57 v = wrapper_generator.DeclVisitor()58 ast = CParser().parse("""59 void f(struct bar a);60 """,61 "aname")62 v.visit(ast)63 argtype, name = v.functions[0].args[0]64 self.assertEqual('a', name)65 self.assertEqual('struct bar', argtype)66 def testDeclVisitorReturnEllipsis(self):67 v = wrapper_generator.DeclVisitor()68 ast = CParser().parse("""69 void f(int foo, ...);70 """,71 "aname")72 v.visit(ast)73 self.assertEqual(2, len(v.functions[0].args))74 self.assertEqual(('ellipsis', '...'), v.functions[0].args[1])75 def testDeclVisitorVoidReturnTypeWhenFunctionIsVoid(self):76 v = wrapper_generator.DeclVisitor()77 ast = CParser().parse("""78 void f(int foo, ...);79 """,80 "aname")81 v.visit(ast)82 self.assertEqual("void", v.functions[0].return_type)83 def testDeclVisitorIntReturnTypeWhenFunctionIsInt(self):84 v = wrapper_generator.DeclVisitor()85 ast = CParser().parse("""86 int f(int foo, ...);87 """,88 "aname")89 v.visit(ast)90 self.assertEqual("int", v.functions[0].return_type)91 def testDeclVisitorCharStarReturnTypeWhenFunctionIsString(self):92 v = wrapper_generator.DeclVisitor()93 ast = CParser().parse("""94 char * f(int foo, ...);95 """,96 "aname")97 v.visit(ast)98 self.assertEqual("char *", v.functions[0].return_type)99class TestFilters(unittest.TestCase):100 def testWithoutVariadic(self):101 funcs = []102 args = [('int', 'a'), ('ellipsis', '...')]103 funcs.append(wrapper_generator.Function('a', args, ''))104 args = [('int', 'a')]105 funcs.append(wrapper_generator.Function('b', args, ''))106 filtered = wrapper_generator.without_variadic(funcs)107 self.assertEqual(1, len(filtered))108 def testTypeToConvertFuncName(self):109 type_name = "int"110 self.assertEqual("string_to_int",111 wrapper_generator.string_to_type(type_name))112 type_name = "char *"113 self.assertEqual("string_to_char___ptr__",114 wrapper_generator.string_to_type(type_name))115class TestWrapperGenerator(unittest.TestCase):116 def testHeaderFromTemplate(self):117 gen = WrapperGenerator("int newtInit(void);",118 "aname")119 template = """\120 {%- for func in funcs %}\121int bash_{{ func.name }}(WORD_LIST *args);\122{%- endfor %}"""123 self.assertEqual("int bash_newtInit(WORD_LIST *args);",124 gen.render(template))125 def testArgsFromTemplate(self):126 gen = WrapperGenerator("int newtInit(int i);",127 "aname")128 template = """{%- for func in funcs %}129{%- for argtype, argname in func.args %}130{{ argtype }} {{ argname }};131{%- endfor %}132{%- endfor %}"""133 self.assertIn("int i;",134 gen.render(template))135 def testCaptureReturnFromTemplateWhenTypeisVoid(self):136 gen = WrapperGenerator("void newtInit(int i);",137 "aname")138 template = """\139 {%- for func in funcs %}\140{%- if func.return_type == "void" %}bash_{{ func.name }}(WORD_LIST *args);{%- endif %}\141{%- endfor %}"""142 self.assertEqual("bash_newtInit(WORD_LIST *args);",143 gen.render(template))144 def testCaptureReturnFromTemplateWhenTypeisString(self):145 gen = WrapperGenerator("char * newtInit(int i);",146 "aname")147 template = """\148 {%- for func in funcs %}\149{%- if func.return_type != "void" %}{{ func.return_type }} return_value = bash_{{ func.name }}(WORD_LIST *args);{%- endif %}\150{%- endfor %}"""151 self.assertEqual("char * return_value = bash_newtInit(WORD_LIST *args);",152 gen.render(template))153 154 def testFilteringVariadicFunction(self):155 gen = WrapperGenerator("""156int newtInit(int i);157int newtVariadic(int i, ...);158""",159 "aname")160 template = """{%- for func in funcs | without_variadic %}161{%- for argtype, argname in func.args %}{{ argtype }} {{ argname }};162{%- endfor %}163{%- endfor %}"""164 self.assertEqual("int i;",165 gen.render(template))166if __name__ == '__main__':...

Full Screen

Full Screen

reflection_generator.py

Source:reflection_generator.py Github

copy

Full Screen

1#!/usr/bin/env python2# Copyright (c) 2014 Intel Corporation. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5import optparse6import os7import shutil8import sys9from bridge_generator import BridgeGenerator10from interface_generator import InterfaceGenerator11from java_class import JavaClassLoader12from string import Template13from wrapper_generator import WrapperGenerator14# Classes list that have to generate bridge and wrap code.15CLASSES_TO_BE_PROCESS = [16 'XWalkExtensionInternal',17 'XWalkViewInternal',18 'XWalkUIClientInternal',19 'XWalkResourceClientInternal',20 'XWalkPreferencesInternal',21 'XWalkNavigationItemInternal',22 'XWalkNavigationHistoryInternal',23 'XWalkJavascriptResultHandlerInternal',24 'XWalkJavascriptResultInternal',25]26REFLECTION_HERLPER = [27 'ReflectMethod.java',28 'ReflectField.java',29 'ReflectConstructor.java',30 'ReflectExceptionHandler.java',31]32WRAPPER_PACKAGE = 'org.xwalk.core'33BRIDGE_PACKAGE = 'org.xwalk.core.internal'34bridge_path = ''35wrapper_path = ''36def PerformSerialize(output_path, generator):37 file_name = generator.GetGeneratedClassFileName()38 with open(os.path.join(output_path, file_name), 'w') as f:39 f.write(generator.GetGeneratedCode())40 print('%s has been generated!' % file_name)41def GenerateJavaBindingClass(input_dir):42 class_loader = JavaClassLoader(input_dir, CLASSES_TO_BE_PROCESS)43 for input_class in CLASSES_TO_BE_PROCESS:44 print('Generate bridge and wrapper code for %s' % input_class)45 java_data = class_loader.GetJavaData(input_class)46 if java_data.class_type == 'interface':47 # Generate Interface code.48 interface_generator = InterfaceGenerator(java_data, class_loader)49 interface_generator.RunTask()50 PerformSerialize(wrapper_path, interface_generator)51 else:52 # Generate Bridge code.53 bridge_generator = BridgeGenerator(java_data, class_loader)54 bridge_generator.RunTask()55 PerformSerialize(bridge_path, bridge_generator)56 # Generate Wrapper code.57 wrapper_generator = WrapperGenerator(java_data, class_loader)58 wrapper_generator.RunTask()59 PerformSerialize(wrapper_path, wrapper_generator)60def GenerateJavaReflectClass(input_dir):61 for helper in REFLECTION_HERLPER:62 with open(os.path.join(wrapper_path, helper), 'w') as f:63 for line in open(os.path.join(input_dir, helper), 'r'):64 if line.startswith('package '):65 f.write('package ' + WRAPPER_PACKAGE + ';\n')66 else:67 f.write(line)68def GenerateJavaTemplateClass(template_dir,69 target_arch, sdk_version, min_sdk_version):70 template_file = os.path.join(template_dir, 'XWalkCoreVersion.template')71 template = Template(open(template_file, 'r').read())72 value = {'TARGET_ARCH': target_arch,73 'LIB_VERSION': sdk_version,74 'MIN_LIB_VERSION': min_sdk_version}75 output_file = os.path.join(bridge_path, "XWalkCoreVersion.java")76 with open(output_file, 'w') as f:77 f.write(template.substitute(value))78 template_file = os.path.join(template_dir, 'XWalkSdkVersion.template')79 template = Template(open(template_file, 'r').read())80 value = {'SDK_VERSION': sdk_version}81 output_file = os.path.join(wrapper_path, "XWalkSdkVersion.java")82 with open(output_file, 'w') as f:83 f.write(template.substitute(value))84def Touch(path):85 if not os.path.isdir(os.path.dirname(path)):86 os.makedirs(os.path.dirname(path))87 with open(path, 'a'):88 os.utime(path, None)89def main(argv):90 usage = """Usage: %prog [OPTIONS]91This script can generate bridge and wrap source files for given directory.92\'input_dir\' is provided as directory containing source files.93 """94 option_parser = optparse.OptionParser(usage=usage)95 option_parser.add_option('--input-dir',96 help=('Input source file directory which contains '97 'input files'))98 option_parser.add_option('--template-dir',99 help=('Templates directory to generate java source '100 'file'))101 option_parser.add_option('--bridge-output',102 help=('Output directory where the bridge code is '103 'placed.'))104 option_parser.add_option('--wrapper-output',105 help=('Output directory where the wrap code is '106 'placed.'))107 option_parser.add_option('--stamp', help='the file to touch on success.')108 option_parser.add_option('--target-arch', help='Target Architecture')109 option_parser.add_option('--sdk-version', help='API Version')110 option_parser.add_option('--min-sdk-version', help='Min API Version')111 options, _ = option_parser.parse_args(argv)112 if (not options.input_dir or113 not options.bridge_output or114 not options.wrapper_output):115 print('Error: Must specify input and output.')116 return 1117 if os.path.isdir(options.bridge_output):118 shutil.rmtree(options.bridge_output)119 if os.path.isdir(options.wrapper_output):120 shutil.rmtree(options.wrapper_output)121 global bridge_path122 bridge_path = os.path.join(options.bridge_output,123 os.path.sep.join(BRIDGE_PACKAGE.split('.')))124 os.makedirs(bridge_path)125 global wrapper_path126 wrapper_path = os.path.join(options.wrapper_output,127 os.path.sep.join(WRAPPER_PACKAGE.split('.')))128 os.makedirs(wrapper_path)129 if options.input_dir:130 GenerateJavaBindingClass(options.input_dir)131 GenerateJavaReflectClass(options.input_dir)132 if options.template_dir:133 GenerateJavaTemplateClass(options.template_dir,134 options.target_arch, options.sdk_version, options.min_sdk_version)135 if options.stamp:136 Touch(options.stamp)137if __name__ == '__main__':...

Full Screen

Full Screen

fizzbuzz.py

Source:fizzbuzz.py Github

copy

Full Screen

1#!/usr/bin/env python2# FizzBuzz as decorator function3# MZvk 20184def fizzbuzz(end):5 def wrapper_generator():6 start = 07 while start < end:8 start += 19 if not start % 15: msg = 'FizzBuzz!'10 elif not start % 3: msg = 'Fizz!'11 elif not start % 5: msg = 'Buzz!'12 else: msg = ''13 yield '{:3d} \033[31m{}\033[0m'.format(start, msg)14 return wrapper_generator15test = fizzbuzz(100)16gen = test()17for x in gen:...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful