tests/utils.py
def assertCodeExecution(
            self, code,
            message=None,
            extra_code=None,
            run_in_global=True, run_in_function=True, exits_early=False,
            args=None, substitutions=None):
        "Run code as native python, and under Java and check the output is identical"
        self.maxDiff = None
        # ==================================================
        # Pass 1 - run the code in the global context
        # ==================================================
        if run_in_global:
            try:
                self.makeTempDir()
                # Run the code as Python and as Java.
                adj_code = adjust(code, run_in_function=False)
                adj_code += '\nprint("%s")\n' % END_OF_CODE_STRING
                py_out = runAsPython(self.temp_dir, adj_code, extra_code, args=args)
                java_out = self.runAsJava(adj_code, extra_code, args=args)
            except Exception as e:
                self.fail(e)
            finally:
                # Clean up the test directory where the class file was written.
                shutil.rmtree(self.temp_dir)
                # print(java_out)

            # Cleanse the Python and Java output, producing a simple
            # normalized format for exceptions, floats etc.
            java_out = cleanse_java(java_out, substitutions)
            py_out = cleanse_python(py_out, substitutions)

            # Confirm that the output of the Java code is the same as the Python code.
            if message:
                context = 'Global context: %s' % message
            else:
                context = 'Global context'
            self.assertEqual(java_out, py_out, context)

            # Confirm that both output strings end with the canary statement
            substring_start = - (len(END_OF_CODE_STRING)+1)
            if exits_early:
                self.assertNotEqual(java_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
                self.assertNotEqual(py_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
            else:
                self.assertEqual(java_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
                self.assertEqual(py_out[substring_start:], END_OF_CODE_STRING_NEWLINE)

        # ==================================================
        # Pass 2 - run the code in a function's context
        # ==================================================
        if run_in_function:
            try:
                self.makeTempDir()
                # Run the code as Python and as Java.
                adj_code = adjust(code, run_in_function=True)
                adj_code += '\nprint("%s")\n' % END_OF_CODE_STRING
                py_out = runAsPython(self.temp_dir, adj_code, extra_code, args=args)
                java_out = self.runAsJava(adj_code, extra_code, args=args)
            except Exception as e:
                self.fail(e)
            finally:
                # Clean up the test directory where the class file was written.
                shutil.rmtree(self.temp_dir)
                # print(java_out)

            # Cleanse the Python and Java output, producing a simple
            # normalized format for exceptions, floats etc.
            java_out = cleanse_java(java_out, substitutions)
            py_out = cleanse_python(py_out, substitutions)

            # Confirm that the output of the Java code is the same as the Python code.
            if message:
                context = 'Function context: %s' % message
            else:
                context = 'Function context'
            self.assertEqual(java_out, py_out, context)

            # Confirm that both output strings end with the canary statement
            substring_start = - (len(END_OF_CODE_STRING)+1)
            if exits_early:
                self.assertNotEqual(java_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
                self.assertNotEqual(py_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
            else:
                self.assertEqual(java_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
                self.assertEqual(py_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
Run code as native python, and under Java and check the output is identical
Run code as native python, and under Java and check the output is identical
Run code as native python and under Java and check the output is identical
def assertCodeExecution(
            self, code,
            message=None,
            extra_code=None,
            run_in_global=True, run_in_function=True, exits_early=False,
            args=None, substitutions=None):
        self.maxDiff = None
        if run_in_global:
            try:
                self.makeTempDir()
                adj_code = adjust(code, run_in_function=False)
                adj_code += '\nprint("%s")\n' % END_OF_CODE_STRING
                py_out = runAsPython(self.temp_dir, adj_code, extra_code, args=args)
                java_out = self.runAsJava(adj_code, extra_code, args=args)
            except Exception as e:
                self.fail(e)
            finally:
                shutil.rmtree(self.temp_dir)
            java_out = cleanse_java(java_out, substitutions)
            py_out = cleanse_python(py_out, substitutions)
            if message:
                context = 'Global context: %s' % message
            else:
                context = 'Global context'
            self.assertEqual(java_out, py_out, context)
            substring_start = - (len(END_OF_CODE_STRING)+1)
            if exits_early:
                self.assertNotEqual(java_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
                self.assertNotEqual(py_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
            else:
                self.assertEqual(java_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
                self.assertEqual(py_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
        if run_in_function:
            try:
                self.makeTempDir()
                adj_code = adjust(code, run_in_function=True)
                adj_code += '\nprint("%s")\n' % END_OF_CODE_STRING
                py_out = runAsPython(self.temp_dir, adj_code, extra_code, args=args)
                java_out = self.runAsJava(adj_code, extra_code, args=args)
            except Exception as e:
                self.fail(e)
            finally:
                shutil.rmtree(self.temp_dir)
            java_out = cleanse_java(java_out, substitutions)
            py_out = cleanse_python(py_out, substitutions)
            if message:
                context = 'Function context: %s' % message
            else:
                context = 'Function context'
            self.assertEqual(java_out, py_out, context)
            substring_start = - (len(END_OF_CODE_STRING)+1)
            if exits_early:
                self.assertNotEqual(java_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
                self.assertNotEqual(py_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
            else:
                self.assertEqual(java_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
                self.assertEqual(py_out[substring_start:], END_OF_CODE_STRING_NEWLINE)
def assertCodeExecution ( self , code , message = None , extra_code = None , run_in_global = True , run_in_function = True , exits_early = False , args = None , substitutions = None ) : self . maxDiff = None if run_in_global : try : self . makeTempDir ( ) adj_code = adjust ( code , run_in_function = False ) adj_code += '\nprint("%s")\n' % END_OF_CODE_STRING py_out = runAsPython ( self . temp_dir , adj_code , extra_code , args = args ) java_out = self . runAsJava ( adj_code , extra_code , args = args ) except Exception as e : self . fail ( e ) finally : shutil . rmtree ( self . temp_dir ) java_out = cleanse_java ( java_out , substitutions ) py_out = cleanse_python ( py_out , substitutions ) if message : context = 'Global context: %s' % message else : context = 'Global context' self . assertEqual ( java_out , py_out , context ) substring_start = - ( len ( END_OF_CODE_STRING ) + 1 ) if exits_early : self . assertNotEqual ( java_out [ substring_start : ] , END_OF_CODE_STRING_NEWLINE ) self . assertNotEqual ( py_out [ substring_start : ] , END_OF_CODE_STRING_NEWLINE ) else : self . assertEqual ( java_out [ substring_start : ] , END_OF_CODE_STRING_NEWLINE ) self . assertEqual ( py_out [ substring_start : ] , END_OF_CODE_STRING_NEWLINE ) if run_in_function : try : self . makeTempDir ( ) adj_code = adjust ( code , run_in_function = True ) adj_code += '\nprint("%s")\n' % END_OF_CODE_STRING py_out = runAsPython ( self . temp_dir , adj_code , extra_code , args = args ) java_out = self . runAsJava ( adj_code , extra_code , args = args ) except Exception as e : self . fail ( e ) finally : shutil . rmtree ( self . temp_dir ) java_out = cleanse_java ( java_out , substitutions ) py_out = cleanse_python ( py_out , substitutions ) if message : context = 'Function context: %s' % message else : context = 'Function context' self . assertEqual ( java_out , py_out , context ) substring_start = - ( len ( END_OF_CODE_STRING ) + 1 ) if exits_early : self . assertNotEqual ( java_out [ substring_start : ] , END_OF_CODE_STRING_NEWLINE ) self . assertNotEqual ( py_out [ substring_start : ] , END_OF_CODE_STRING_NEWLINE ) else : self . assertEqual ( java_out [ substring_start : ] , END_OF_CODE_STRING_NEWLINE ) self . assertEqual ( py_out [ substring_start : ] , END_OF_CODE_STRING_NEWLINE )
Run code as native python, and under Java and check the output is identical
Run code as native python and under Java and check the output is identical
"Run code as native python, and under Java and check the output is identical" # ================================================== # Pass 1 - run the code in the global context # ================================================== # Run the code as Python and as Java. # Clean up the test directory where the class file was written. # print(java_out) # Cleanse the Python and Java output, producing a simple # normalized format for exceptions, floats etc. # Confirm that the output of the Java code is the same as the Python code. # Confirm that both output strings end with the canary statement # ================================================== # Pass 2 - run the code in a function's context # ================================================== # Run the code as Python and as Java. # Clean up the test directory where the class file was written. # print(java_out) # Cleanse the Python and Java output, producing a simple # normalized format for exceptions, floats etc. # Confirm that the output of the Java code is the same as the Python code. # Confirm that both output strings end with the canary statement
self
code
message
extra_code
run_in_global
run_in_function
exits_early
args
substitutions