def test_ImportNameGather_03(): src = """ import core.pscript.PCoreWidget as PCoreWidget """ matches = ImportNameConverter().gather(parseSource(src)) match = matches[0] assert nodesToString(match.import_word) == 'import' assert nodesToString(match.imported) == 'core.pscript.PCoreWidget' assert "as_word" in match assert nodesToString(match.as_word) == 'as' assert nodesToString(match.as_name) == 'PCoreWidget'
def test_ComparisonGather_01(): src = """ x == y; x < y; x > y; x >= y; x <= y; x <> y; x != y; x in y; x is y """ # dumpTree( parseSource( src ) ) matches = ComparisonConverter().gather(parseSource(src)) match = matches[0] assert nodesToString(match.left) == 'x' assert nodesToString(match.comp_op) == '==' assert nodesToString(match.right) == 'y' assert nodesToString(matches[3].comp_op) == '>=' assert nodesToString(matches[7].comp_op) == 'in'
def test_ComparisonGather_02(): src = """ x is not y; x not in y """ # dumpTree( parseSource( src ) ) matches = ComparisonConverter().gather(parseSource(src)) match = matches[0] assert nodesToString(match.left) == 'x' assert nodesToString(match.comp_op) == 'is not' assert nodesToString(match.right) == 'y' assert nodesToString(matches[1].comp_op) == 'not in'
def test_StringInterpolationProcess_01(): src = """ astring % aval """ nodes = parseSource( src ) cvtr = StringInterpolationConverter() matches = cvtr.gather( nodes ) cvtr.processAll( matches ) # dumpTree( nodes ) # dumpNodes( nodes ) if cvtr.USE_PYJS: assert nodesToString( nodes ) == \ """_pyjs.stringInterpolate( astring, [ aval ] )""" else: assert nodesToString( nodes ) == \ """[ aval ].reduce( ( a, c ) => a.replace( /%(s|i|r)?/, c.toString () ), astring )"""
def test_StringInterpolationProcess_02(): src = """ "%s:%s" % ( aval, bval ) """ nodes = parseSource( src ) cvtr = StringInterpolationConverter() matches = cvtr.gather( nodes ) cvtr.processAll( matches ) # dumpTree( nodes ) # dumpNodes( nodes ) if cvtr.USE_PYJS: assert nodesToString( nodes ) == \ """_pyjs.stringInterpolate( \"%s:%s\", ( aval, bval ) )""" else: assert nodesToString( nodes ) == \ """( aval, bval ).reduce( ( a, c ) => a.replace( /%(s|i|r)?/, c.toString () ), "%s:%s" )"""
def test_StringInterpolationProcess_03(): src = """ return "/__utr-%s/__kyz-%s/__pqw-%s" % self.getSessIds() """ nodes = parseSource( src ) cvtr = StringInterpolationConverter() matches = cvtr.gather( nodes ) cvtr.processAll( matches ) # dumpTree( nodes ) # dumpNodes( nodes ) if cvtr.USE_PYJS: assert nodesToString( nodes ) == \ """return _pyjs.stringInterpolate( \"/__utr-%s/__kyz-%s/__pqw-%s\", self.getSessIds() )""" else: assert nodesToString( nodes ) == \ """return self.getSessIds().reduce( ( a, c ) => a.replace( /%(s|i|r)?/, c.toString () ), "/__utr-%s/__kyz-%s/__pqw-%s" )"""
def test_ImportNameGather_01(): src = """ import amodule """ matches = ImportNameConverter().gather(parseSource(src)) match = matches[0] assert match.import_word.toString() == 'import' assert nodesToString(match.imported) == 'amodule'
def test_NegateGather_01(): src = """ x = not y """ matches = NegateConverter().gather(parseSource(src)) match = matches[0] assert match.not_word.toString() == 'not' assert nodesToString(match.right) == 'y'
def test_KeyWordCallProcess_01(): src = """ x = func( a, b="hello", c=doIt() ) """ nodes = parseSource( src ) cvtr = KeyWordCallConverter() matches = cvtr.gather( nodes ) cvtr.processAll( matches ) assert nodesToString( nodes ) == """x = func( a, { b: "hello", c: doIt() } )"""
def test_ImportFromProcess_01(): src = """ from xyz import funcy """ nodes = parseSource(src) cvtr = ImportFromConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) assert nodesToString(nodes) == """const { funcy } = require( './xyz' )"""
def test_ExceptionProcess_02(): src = """ raise NameError( "bimbambom" ) """ nodes = parseSource( src ) cvtr = ExceptionConverter() matches = cvtr.gather( nodes ) cvtr.processAll( matches ) assert nodesToString( nodes ) == """throw new Error( 'NameError', "bimbambom" )"""
def test_ComparisonProcess_07(): src = """ dflt is ... """ nodes = parseSource(src) cvtr = ComparisonConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) assert nodesToString(nodes) == """!_pyjs.isDef( dflt )"""
def test_ComparisonProcess_06(): src = """ x is not y """ nodes = parseSource(src) cvtr = ComparisonConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) assert nodesToString(nodes) == """!Object.is( x, y )"""
def test_ComparisonProcess_04(): src = """ x is not None """ nodes = parseSource(src) cvtr = ComparisonConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) assert nodesToString(nodes) == """x !== null"""
def test_ComparisonProcess_01(): src = """ x == y """ nodes = parseSource(src) cvtr = ComparisonConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) assert nodesToString(nodes) == """x === y"""
def test_KeyWordCallProcess_03(): src = """ return sorted( props, key=sortKey ) """ nodes = parseSource( src ) cvtr = KeyWordCallConverter() matches = cvtr.gather( nodes ) cvtr.processAll( matches ) assert nodesToString( nodes ) == """return sorted( props, { key: sortKey } )"""
def test_ImportNameProcess_02(): src = """ import amodule, bmodule, cmodule """ nodes = parseSource(src) cvtr = ImportNameConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) assert nodesToString(nodes) == """const amodule = require( './amodule' )
def test_ListSliceProcess_05(): src = """ alist[ : ] """ nodes = parseSource(src) cvtr = ListSliceConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) assert nodesToString(nodes) == """alist.slice()"""
def test_ExceptionProcess_03(): src = """ raise CustomError( 1, [ 'a', 'b', 'c' ], 'hello' ) """ nodes = parseSource( src ) cvtr = ExceptionConverter() matches = cvtr.gather( nodes ) cvtr.processAll( matches ) assert nodesToString( nodes ) == \ """throw new Error( 'CustomError', 1, [ 'a', 'b', 'c' ], 'hello' )"""
def test_ImportFromProcess_02(): src = """ from amodule import athing, bthing """ nodes = parseSource(src) cvtr = ImportFromConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) assert nodesToString( nodes) == """const { athing, bthing } = require( './amodule' )"""
def test_fixPassStatements_01(): src = """ class DUnitToolErr( Exception ): pass """ nodes = parseSource(src) fixPassStatements(nodes) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToString( nodes) == """class DUnitToolErr( Exception ): /* pass */"""
def test_fixComments_01(): src = """ # comment-1 doOne() doTwo() # comment-2 """ nodes = parseSource(src) fixComments(nodes) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToString(nodes) == """// comment-1
def test_fixComments_02(): src = ''' def aFunc(): """ single-line comment """ doOne() ''' nodes = parseSource(src) fixComments(nodes) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToString(nodes) == """def aFunc():
def test_NegateProcess_02(): src = """ x = not ( fun(a) or nuf( b ) ) """ nodes = parseSource(src) cvtr = NegateConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToString(nodes) == """x = !( fun(a) or nuf( b ) )"""
def test_ListComprehensionProcess_01(): src = """ res = [ func( k, v ) for k, v in d.items() ] """ nodes = parseSource(src) cvtr = ListComprehensionConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpNodes( nodes ) assert nodesToString( nodes) == """res = d.items().map( ( [ k, v ] ) => func( k, v ) )"""
def test_ListComprehensionProcess_02(): src = """ res = [ v for v in d.values() if v > 123 ] """ nodes = parseSource(src) cvtr = ListComprehensionConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpNodes( nodes ) assert nodesToString( nodes) == """res = d.values().filter( v => v > 123 )"""
def test_SelfProcess_01(): src = """ self.callMethA( self.callMethB( self.bam ) ) """ nodes = parseSource( src ) cvtr = SelfConverter() matches = cvtr.gather( nodes ) cvtr.processAll( matches ) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToString( nodes ) == """this.callMethA( this.callMethB( this.bam ) )"""
def test_BoolOpsProcess_02(): src = """ y = 3 and 4 and 5 or 6 """ nodes = parseSource(src) cvtr = BoolOpsConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToString(nodes) == """y = 3 && 4 and 5 || 6"""
def test_ImportNameProcess_03(): src = """ import core.pscript.PCoreWidget as PCoreWidget """ nodes = parseSource(src) cvtr = ImportNameConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpNodes( nodes ) assert nodesToString( nodes ) == """const PCoreWidget = require( './core/pscript/PCoreWidget' )"""
def test_FunctionProcess_01(): src = """ def func1(): return 123 """ nodes = parseSource(src) cvtr = FunctionConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToString(nodes) == """function func1() {