def test_fixSemicolons_02(): """ don't put semicolons after comments """ src = ''' def afunc(): """ this is a comment """ doStuff() x = "this is not a comment" 'but this is' ''' nodes = parseSource( src ) fixSemicolons( nodes ) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines( nodes ) == [ "def afunc():", ' """ this', " is", " a", ' comment """', " doStuff();", " x = \"this is not a comment\";", " 'but this is'", ]
def test_FunctionToMochaItProcess_01(self): src = """ class Tests( unittest.TestCase ): def test_func1( self ): assert aval == 'bim' def test_func2( self ): assert afunc() def notATestFunc( self ): assert whatever == 'bam' """ nodes = parseSource(src) matches = PytestMethodToMochaItConverter(in_class=True).gather(nodes) PytestMethodToMochaItConverter(in_class=True).processAll(matches) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "class Tests( unittest.TestCase ):", " it( 'test_func1', () => {", " assert aval == 'bim'", " } );", " it( 'test_func2', () => {", " assert afunc()", " } );", " notATestFunc() {", " assert whatever == 'bam'", " }", ]
def test_processModule_02(): src = """ class Tests( unittest.TestCase ): def test_getAttr( self ): try: getattr( obj, 'bom' ) except: excepted = True assert excepted """ nodes = parseSource(src) infos = processTestModule(nodes) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "describe( 'Tests', () => {", " it( 'test_getAttr', () => {", " try {", " _pyjs.getAttr( obj, 'bom' );", " } catch( e ) {", " let excepted = true;", " }", "expect( excepted ).to.be.ok;", "", " } );", " } );", ]
def test_TryExceptProcess_04(): src = """ if 1: if 1: if 1: try: bim() except IndexError as exy: bam() raise """ nodes = parseSource(src) fixIndents(nodes) cvtr = TryExceptConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "if 1:", " if 1:", " if 1:", " try {", " bim()", " } catch( exy ) /* IndexError */ {", " bam()", " raise", " }", ]
def test_TryExceptProcess_02(): src = """ try: bim() except ( IndexError, KeyError ): bam() raise finally: bom() return 123 """ nodes = parseSource(src) cvtr = TryExceptConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "try {", " bim()", "} catch( e ) /* ( IndexError, KeyError ) */ {", " bam()", " raise", "} finally {", " bom()", "}", "return 123", ]
def test_processFunction_02(): src = ''' def addTagOpener( self, opener ): """ opener can be the first part of a tag ("<html_tag") or a complete tag up to the closing ">" """ if self.tag_closer: try: self.lines[ -1 ] += opener except IndexError: self.addLine( opener ) ''' nodes = parseSource(src) matches = FunctionConverter().gather(nodes) processFunction(matches[0].node) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "function addTagOpener( opener ) {", " /* opener can be the first part of a tag (\"<html_tag\") or a complete tag up", " to the closing \">\" */", " if( this.tag_closer ) {", " try {", " this.lines[ -1 ] += opener;", " } catch( e ) /* IndexError */ {", " this.addLine( opener );", " }", "", " }", "}", ]
def test_processFunction_04(): src = ''' class StringBuff( object ): """ test """ def numLines( self ): return len( self.lines ) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def addLine( self, line ): """ add a new line to the output """ self.lines.append( self.indent_act + self.tag_closer + line ) self.tag_closer = "" ''' nodes = parseSource(src) matches = FunctionConverter().gather(nodes) processFunction(matches[0].node) processFunction(matches[1].node) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "class StringBuff( object ):", " \"\"\" test \"\"\"", " function numLines() {", " return len( this.lines );", " }", "", " // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -", " function addLine( line ) {", " /* add a new line to the output */", " this.lines.push( this.indent_act + this.tag_closer + line );", " this.tag_closer = \"\";", " }", ]
def test_ImportNameProcess_01(): src = """ import amodule """ nodes = parseSource(src) cvtr = ImportNameConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) assert nodesToLines( nodes)[0] == """const amodule = require( './amodule' )"""
def test_SuperProcess_02(): src = """ super( SPVEBorderColorMixed, self ).__init__( abc ) """ nodes = parseSource(src) cvtr = SuperConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ 'super( abc )', ]
def test_TupleProcess_02(): src = """ l = ( 'a', ( 1, 2, 3 ), 'b' ) """ nodes = parseSource(src) cvtr = TupleConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) assert nodesToLines(nodes) == [ "l = [ 'a', [ 1, 2, 3 ], 'b' ]", ]
def test_TupleProcess_03(): src = """ ( a, b, c ) = func( x, y, z ) """ nodes = parseSource(src) cvtr = TupleConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) assert nodesToLines(nodes) == [ "[ a, b, c ] = func( x, y, z )", ]
def test_AssignmentProcess_04(): src = """ [ a, b, c ] = ( 1, 2, 3 ) """ nodes = parseSource(src) cvtr = AssignmentConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "let [ a, b, c ] = ( 1, 2, 3 )", ]
def test_AssertToChaiExpectProcess_05(self): src = """ def test_func1( self ): assert aval > 100 """ nodes = parseSource(src) cvtr = AssertToChaiExpectConverter() matches = cvtr.gather(nodes) cvtr.processOne(matches[0]) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "def test_func1( self ):", " expect( aval > 100 ).to.be.ok", ]
def test_AssertToChaiExpectProcess_02(self): src = """ def test_func1( self ): assert aval != 'bim' """ nodes = parseSource(src) cvtr = AssertToChaiExpectConverter() matches = cvtr.gather(nodes) cvtr.processOne(matches[0]) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "def test_func1( self ):", " expect( aval ).not.to.eql( 'bim' )", ]
def test_SuperProcess_03(): src = """ def meth1( self, aval ): return super( ABaseClass, self ).meth1( aval ) """ nodes = parseSource(src) cvtr = SuperConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "def meth1( self, aval ):", " return super.meth1( aval )", ]
def test_fixSimpleRenames_02(): src = ''' isinstance( a, AClass ) zip( a, b ) ''' nodes = parseSource(src) fixSimpleRenames(nodes) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ '_pyjs.isInstance( a, AClass )', '_pyjs.listZip( a, b )' ]
def test_processFunction_09(): src = ''' def stringInterp( self ): return "hello %s world %s" % ( self.a, self.b ) ''' nodes = parseSource(src) matches = FunctionConverter().gather(nodes) processFunction(matches[0].node) # dumpTree( nodes ) # dumpNodes( nodes ) if StringInterpolationConverter.USE_PYJS: assert nodesToLines(nodes) == [ "function stringInterp() {", " return _pyjs.stringInterpolate( \"hello %s world %s\", [ this.a, this.b ] );", "}", ] else: assert nodesToLines(nodes) == [ "function stringInterp() {", " return [ this.a, this.b ].reduce( ( a, c ) => a.replace( /%(s|i|r)?/, c.toString () ), \"hello %s world %s\" );", "}", ]
def test_processModule_02(): src = ''' class AClass( PInfo ): """ docstring for class """ def __init__( self, p1, p2, p3 ): super( AClass, self ).__init__( p1, p2, p3 ); self.doInitThing() def setup( self ): """ docstring for setup """ super( AClass, self ).setup( p1, p2, p3 ); self.doSetupThing() def whatevs( self ): a = 1 b = 2 ''' nodes = parseSource(src) # dumpTree( nodes ) infos = processModule(nodes) assert infos.class_names == ["AClass"] assert infos.func_names == [] # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "class AClass extends PInfo {", " /* docstring for class */", " constructor( p1, p2, p3 ) {", " super( p1, p2, p3 );", " this.doInitThing();", " }", "", " setup() {", " /* docstring for setup */", " super.setup( p1, p2, p3 );", " this.doSetupThing();", " }", "", " whatevs() {", " let a = 1;", " let b = 2;", " }", "", "}", ]
def test_ClassProcess_02(): src = """ class Bango( Bingo ): def __init__( self ): pass """ nodes = parseSource(src) cvtr = ClassConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpTree( nodes ) # dumpLines( nodesToString( nodes ) ) assert nodesToLines(nodes) == [ 'class Bango extends Bingo {', ' def __init__( self ): pass', '}', ]
def test_ImportFromProcess_03(): src = """ from Utils import ( OrderedDict, stripTags, elideString, crackString, padelide, escapeHtml, splitCamelCase, deQuote ) """ nodes = parseSource(src) cvtr = ImportFromConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) strs = nodesToLines(nodes) # dumpLines( strs ) assert strs == [ "const { OrderedDict, stripTags, elideString,", " crackString, padelide, escapeHtml,", " splitCamelCase, deQuote } = require( './Utils' )", ]
def test_ForLoopProcess_04(): src = """ for ( x, y ) in y: doit( x, y ) """ nodes = parseSource(src) fixIndents(nodes) cvtr = ForLoopConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpTree( nodes ) # dumpLines( nodesToString( nodes ) ) assert nodesToLines(nodes) == [ 'for( let [ x, y ] of y ) {', ' doit( x, y )', '}', ]
def test_AssignmentProcess_01(): src = """ x = 1 self.abc = self.xyz a, b, c = func() """ nodes = parseSource(src) cvtr = AssignmentConverter() matches = cvtr.gather(nodes) cvtr.processAll(matches) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "let x = 1", "self.abc = self.xyz", "let a, b, c = func()", ]
def test_processFunction_10(): src = ''' def listSlice( self ): return m_listy[ 123 : bam ] ''' nodes = parseSource(src) matches = FunctionConverter().gather(nodes) processFunction(matches[0].node) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "function listSlice() {", " return m_listy.slice( 123, bam );", "}", ]
def test_processFunction_08(): src = ''' def dictComp( self ): return { key: val for ( k, v ) in self.getDictItems() } ''' nodes = parseSource(src) matches = FunctionConverter().gather(nodes) processFunction(matches[0].node) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "function dictComp() {", " return this.getDictItems().reduce( ( __map, [ [ k, v ] ] ) => ( { ...__map, [ key ]: val } ), {} );", "}", ]
def test_processFunction_07(): src = ''' def dedent( self ): bam() ''' nodes = parseSource(src) matches = FunctionConverter().gather(nodes) processFunction(matches[0].node) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "function dedent() {", " bam();", "}", ]
def test_processFunction_05(): src = ''' def dedent( self, dedent ): x = dedent ''' nodes = parseSource(src) matches = FunctionConverter().gather(nodes) processFunction(matches[0].node) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "function dedent( dedent ) {", " let x = dedent;", "}", ]
def test_fixSemicolons_01(): src = """ x = 1 callFunc() y = callFunc() a, b, c = \ 1, 2, 3 """ nodes = parseSource( src ) fixSemicolons( nodes ) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines( nodes ) == [ "x = 1;", "callFunc();", "y = callFunc();", "a, b, c = 1, 2, 3;", ]
def test_KeyWordCallProcess_02(): src = """ self.style_enable_check.setJSCommandVals( style_path=style_sel.path, style_name=style_sel.name, enable="__value__" ) """ nodes = parseSource( src ) cvtr = KeyWordCallConverter() matches = cvtr.gather( nodes ) cvtr.processAll( matches ) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines( nodes ) == [ "self.style_enable_check.setJSCommandVals( {", " style_path: style_sel.path,", " style_name: style_sel.name,", " enable: \"__value__\" } )", ]
def test_processFunction_01(): src = """ def func1(): x = 1 return 123 """ nodes = parseSource(src) matches = FunctionConverter().gather(nodes) processFunction(matches[0].node) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "function func1() {", " let x = 1;", " return 123;", "}", ]
def test_processFunction_03(): src = ''' def trimEndChars( self, num_chars ): """ nessary in occasionally to allow addTagCloser() to be used after the an open tag has been fully output """ if num_chars > len( self.lines[ -1 ] ): raise Exception( "attempt to trim too many end chars" ) self.lines[ -1 ] = self.lines[ -1 ][ : - num_chars ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # these enable us to "backtrack" if an error occurs # bim # bam def getLinesNIndent( self ): return len( self.lines ), self.indent_lvl ''' nodes = parseSource(src) matches = FunctionConverter().gather(nodes) processFunction(matches[0].node) processFunction(matches[1].node) # dumpTree( nodes ) # dumpNodes( nodes ) assert nodesToLines(nodes) == [ "function trimEndChars( num_chars ) {", " /* nessary in occasionally to allow addTagCloser() to be used after the", " an open tag has been fully output */", " if( num_chars > len( this.lines[ -1 ] ) ) {", " throw new Error( \'Exception\', \"attempt to trim too many end chars\" );", " }", " this.lines[ -1 ] = this.lines[ -1 ].slice( 0, - num_chars );", "}", "", "// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -", "// these enable us to \"backtrack\" if an error occurs", " // bim", " // bam", "function getLinesNIndent() {", " return len( this.lines ), this.indent_lvl;", "}", ]