def test_EvalField_editor(self): c = self.EvalFieldTest() codeGen = Python2CodeGenerator('test') c.x.constantValue = 10.0 c.y.constantValue = 20.0 self.assertEqual(10.0, c.x.getValueForEditor()) self.assertEqual(20.0, c.y.getValueForEditor()) self.assertEqual( Py.Call(target=Py.Load(name='C'), args=[ Py.FloatLiteral(value='10.0'), Py.FloatLiteral(value='20.0') ]), c.__py_evalmodel__(codeGen)) x_expr = EmbeddedPython2Expr.fromText('a+b') y_expr = EmbeddedPython2Expr.fromText('c+d') c.x.expr = x_expr c.y.expr = y_expr self.assertEqual(10.0, c.x.getValueForEditor()) self.assertEqual(20.0, c.y.getValueForEditor()) self.assertEqual( Py.Call(target=Py.Load(name='C'), args=[x_expr.model, y_expr.model]), c.__py_evalmodel__(codeGen))
def test_ChildField_editor(self): a1 = self.TypedFieldTest(x=10, y=20) a2 = self.TypedFieldTest(x=3, y=4) b = self.ChildFieldTest(p=a1, q=[a2]) codeGen = Python2CodeGenerator('test') self.assertEqual(a1, b.p.getValueForEditor()) self.assertEqual([a2], b.q.getValueForEditor()) self.assertEqual( Py.Call(target=Py.Load(name='B'), args=[ Py.Call(target=Py.Load(name='A'), args=[ Py.IntLiteral(format='decimal', numType='int', value='10'), Py.IntLiteral(format='decimal', numType='int', value='20') ]), Py.ListLiteral(values=[ Py.Call(target=Py.Load(name='A'), args=[ Py.IntLiteral(format='decimal', numType='int', value='3'), Py.IntLiteral(format='decimal', numType='int', value='4') ]) ]) ]), b.__py_evalmodel__(codeGen))
def unitTesting(codeGen): # Create the class suite first = True testing = [] for test in self._inlineTests: if not first: testing.append( Schema.BlankLine() ) testing.append( Schema.BlankLine() ) testing.append( test._createTestClass( codeGen ) ) first = False testing.append( Schema.BlankLine() ) testing.append( Schema.BlankLine() ) for test in self._inlineTests: testAst = codeGen.embeddedValue( test ) testing.append( Schema.ExprStmt( expr=Schema.Call( target=Schema.AttributeRef( target=testAst, name='_registerTestClass' ), args=[ Schema.Load( name=test._className ) ] ) ) ) selfAST = codeGen.embeddedValue( self ) moduleAST = codeGen.embeddedValue( codeGen.module ) globalsAST = Schema.Call( target=Schema.Load( name='globals' ), args=[] ) localsAST = Schema.Call( target=Schema.Load( name='locals' ), args=[] ) testing.append( Schema.ExprStmt( expr=Schema.Call( target=Schema.AttributeRef( target=selfAST, name='runTests' ), args=[] ) ) ) testing.append( Schema.ExprStmt( expr=Schema.Call( target=Schema.AttributeRef( target=selfAST, name='_registerScope' ), args=[ moduleAST, globalsAST, localsAST ] ) ) ) return Schema.PythonSuite( suite=testing )
def test_TypedField_editor(self): a = self.TypedFieldTest(x=10, y=20) codeGen = Python2CodeGenerator('test') self.assertEqual(10, a.x.getValueForEditor()) self.assertEqual(20, a.y.getValueForEditor()) self.assertEqual( Py.IntLiteral(format='decimal', numType='int', value='10'), a.x.__py_evalmodel__(codeGen)) self.assertEqual( Py.IntLiteral(format='decimal', numType='int', value='20'), a.y.__py_evalmodel__(codeGen)) self.assertEqual( Py.Call(target=Py.Load(name='A'), args=[ Py.IntLiteral(format='decimal', numType='int', value='10'), Py.IntLiteral(format='decimal', numType='int', value='20') ]), a.__py_evalmodel__(codeGen))
def test_ExprField_py_evalmodel(self): x_expr = EmbeddedPython2Expr.fromText('a+b') d = self.ExprFieldTest(x=x_expr) codeGen = Python2CodeGenerator('test') self.assertEqual( Py.Call(target=Py.Load(name='D'), args=[x_expr.model]), d.__py_evalmodel__(codeGen))
def test_ListLiteral(self): self._buildTest( Py.ListLiteral(values=[Py.Load( name='t'), Py.Load(name='v')]), ListLit([Load('t'), Load('v')]))
def test_Load(self): self._buildTest(Py.Load(name='x'), Load('x'))
def test_ExprBuilt(self): self._buildTest(Py.Load(name='x'), ExprBuilt(Py.Load(name='x')))
def build(self): return Py.Load(name=self.name)
def _statementsForExecutionAndEvaluationIntoValue(stmts, varName): for i in xrange( len( stmts ) - 1, -1, -1 ): stmt = stmts[i] if stmt.isInstanceOf( Schema.ExprStmt ): return stmts[:i] + [ Schema.AssignStmt( targets=[ Schema.SingleTarget( name=varName ) ], value=stmt['expr'] ) ] + stmts[i+1:] elif stmt.isInstanceOf( Schema.BlankLine ) or stmt.isInstanceOf( Schema.CommentStmt ): pass else: break return deepcopy( stmts ) + [ Schema.AssignStmt( targets=[ Schema.SingleTarget( name=varName ) ], value=Schema.Load( name='None' ) ) ]
def _createMethodAST(self, codeGen): valueStmts, resultVarName = self._createValueStmtsAndResultVarName( codeGen ) JythonExceptionAST = codeGen.embeddedValue( JythonException ) sysAST = codeGen.embeddedValue( sys ) getCurrentExceptionCallAST = Schema.Call( target=Schema.AttributeRef( target=JythonExceptionAST, name='getCurrentException' ), args=[] ) getExcInfoTypeAST = Schema.Subscript( target=Schema.Call( target=Schema.AttributeRef( target=sysAST, name='exc_info' ), args=[] ), index=Schema.IntLiteral( format='decimal', numType='int', value='0' ) ) selfAST = codeGen.embeddedValue( self ) testValueAST = Schema.AttributeRef( target=selfAST, name='_testValue' ) kindExceptionAST = Schema.StringLiteral( format='ascii', quotation='single', value='exception' ) kindValueAST = Schema.StringLiteral( format='ascii', quotation='single', value='value' ) exceptStmts = [ Schema.ExprStmt( expr=Schema.Call( target=testValueAST, args=[ kindExceptionAST, getCurrentExceptionCallAST, getExcInfoTypeAST ] ) ) ] elseStmts = [ Schema.ExprStmt( expr=Schema.Call( target=testValueAST, args=[ kindValueAST, Schema.Load( name=resultVarName ) ] ) ) ] methodBody = [ Schema.TryStmt( suite=valueStmts, exceptBlocks=[ Schema.ExceptBlock( suite=exceptStmts ) ], elseSuite=elseStmts ) ] methodAST = Schema.DefStmt( name=self._methodName, decorators=[], params=[ Schema.SimpleParam( name='self' ) ], suite=methodBody ) return methodAST
def __py_evalmodel__(self, codeGen): x = self.x.__py_evalmodel__(codeGen) return Py.Call(target=Py.Load(name='D'), args=[x])
def __py_evalmodel__(self, codeGen): p = self.p.__py_evalmodel__(codeGen) q = self.q.__py_evalmodel__(codeGen) return Py.Call(target=Py.Load(name='B'), args=[p, q])
def __py_evalmodel__(self, codeGen): value = self.__live.getValue() if value is None: return Py.Load(name='None') else: return value.__py_evalmodel__(codeGen)