def processOne( self, match ): parent = match.lsqb.parent new = makeStatement() num_locals = len( str( match.locals ).split( "," ) ) match.looper.remove() new.append_child( match.looper ) if "test" in match: new.append_child( makeLeaf( "PYJS", ".filter", "" ) ) new.append_child( makeLeaf( "LPAR", "(", '' ) ) if num_locals > 1: new.append_child( makeLeaf( "LPAR", "(", ' ' ) ) new.append_child( makeLeaf( "LSQB", "[", ' ' ) ) new.append_child( match.locals.clone() ) if num_locals > 1: new.append_child( makeLeaf( "RSQB", "]", ' ' ) ) new.append_child( makeLeaf( "RPAR", ")", ' ' ) ) new.append_child( makeLeaf( "PYJS", "=>", ' ' ) ) match.test.remove() new.append_child( match.test ) new.append_child( makeLeaf( "RPAR", ")", ' ' ) ) if self.needsMap( match.item_value, match.locals ): new.append_child( makeLeaf( "PYJS", ".map", "" ) ) new.append_child( makeLeaf( "LPAR", "(", '' ) ) if num_locals > 1: new.append_child( makeLeaf( "LPAR", "(", ' ' ) ) new.append_child( makeLeaf( "LSQB", "[", ' ' ) ) new.append_child( match.locals.clone() ) if num_locals > 1: new.append_child( makeLeaf( "RSQB", "]", ' ' ) ) new.append_child( makeLeaf( "RPAR", ")", ' ' ) ) new.append_child( makeLeaf( "PYJS", "=>", ' ' ) ) match.item_value.remove() new.append_child( match.item_value ) new.append_child( makeLeaf( "RPAR", ")", ' ' ) ) parent.replace( new )
def processOne(self, class_info): if not ('args' in class_info and class_info.args.toString() in self.test_superclass_strings): super(UnittestClassToMochaDescribeConverter, self).processOne(class_info) return class_name = class_info.name.toString() class_indent = self.calcIndent(class_info.node) parent = class_info.class_word.parent new = makeStatement() new.append_child(makeLeaf("PYJS", "describe", '')) new.append_child(makeLeaf("LPAR", "(", '')) new.append_child(makeLeaf("STRING", f"'{ class_name }'", ' ')) new.append_child(makeLeaf("COMMA", ",", '')) new.append_child(makeLeaf("LPAR", "(", ' ')) new.append_child(makeLeaf("RPAR", ")", '')) new.append_child(makeLeaf("PYJS", "=>", ' ')) new.append_child(makeLeaf("LBRACE", "{", ' ')) new.append_child(class_info.suite) new.append_child(makeLeaf("DEDENT", "", class_indent)) new.append_child(makeLeaf("RBRACE", "}", '')) new.append_child(makeLeaf("RPAR", ")", ' ')) new.append_child(makeLeaf("SEMI", ";", '')) new.append_child(makeLeaf("NEWLINE", "\n", '')) new.append_child(makeLeaf("DEDENT", "", class_indent)) parent.replace(new)
def processOne(self, func_info): func_name = func_info.name.toString() if not func_name.startswith('test_'): super(PytestMethodToMochaItConverter, self).processOne(func_info) return func_indent = self.calcIndent(func_info.node) parent = func_info.def_word.parent new = makeStatement() new.append_child(makeLeaf("PYJS", "it", '')) new.append_child(makeLeaf("LPAR", "(", '')) new.append_child(makeLeaf("STRING", f"'{ func_name }'", ' ')) new.append_child(makeLeaf("COMMA", ",", '')) new.append_child(makeLeaf("LPAR", "(", ' ')) new.append_child(makeLeaf("RPAR", ")", '')) new.append_child(makeLeaf("PYJS", "=>", ' ')) new.append_child(makeLeaf("LBRACE", "{", ' ')) new.append_child(func_info.suite) new.append_child(makeLeaf("DEDENT", "", func_indent)) new.append_child(makeLeaf("RBRACE", "}", '')) new.append_child(makeLeaf("RPAR", ")", ' ')) new.append_child(makeLeaf("SEMI", ";", '')) new.append_child(makeLeaf("NEWLINE", "\n", '')) new.append_child(makeLeaf("DEDENT", "", func_indent)) parent.replace(new)
def processOne(self, match): parent = match.assert_word.parent new = makeStatement() new.append_child(makeLeaf("PYJS", "expect", match.assert_word.prefix)) new.append_child(makeLeaf("LPAR", "(", '')) if 'comp' in match: if match.comp_op.toString() in ["==", "!="]: new.append_child(match.left) else: new.append_child(match.comp) else: new.append_child(match.truthy) new.append_child(makeLeaf("RPAR", ")", ' ')) new.append_child(makeLeaf("DOT", ".", '')) if ('comp' in match and match.comp_op.toString() == "!=") or "not_word" in match: new.append_child(makeLeaf("PYJS", "not", '')) new.append_child(makeLeaf("DOT", ".", '')) new.append_child(makeLeaf("PYJS", "to", '')) new.append_child(makeLeaf("DOT", ".", '')) if 'comp' in match and match.comp_op.toString() in ["==", "!="]: new.append_child(makeLeaf("PYJS", "eql", '')) new.append_child(makeLeaf("LPAR", "(", '')) new.append_child(match.right) new.append_child(makeLeaf("RPAR", ")", ' ')) else: new.append_child(makeLeaf("PYJS", "be", '')) new.append_child(makeLeaf("DOT", ".", '')) new.append_child(makeLeaf("PYJS", "ok", '')) parent.replace(new)
def processOne(self, match): parent = match.lbrace.parent new = makeStatement() num_locals = len(str(match.locals).split(",")) match.looper.remove() new.append_child(match.looper) if "test" in match: new.append_child(makeLeaf("PYJS", ".filter", "")) new.append_child(makeLeaf("LPAR", "(", '')) if num_locals > 1: new.append_child(makeLeaf("LPAR", "(", ' ')) new.append_child(makeLeaf("LSQB", "[", ' ')) new.append_child(match.locals.clone()) if num_locals > 1: new.append_child(makeLeaf("RSQB", "]", ' ')) new.append_child(makeLeaf("RPAR", ")", ' ')) new.append_child(makeLeaf("PYJS", "=>", ' ')) match.test.remove() new.append_child(match.test) new.append_child(makeLeaf("RPAR", ")", ' ')) new.append_child(makeLeaf("DOT", ".", "")) new.append_child(makeLeaf("PYJS", "reduce", "")) new.append_child(makeLeaf("LPAR", "(", '')) new.append_child(makeLeaf("LPAR", "(", ' ')) new.append_child(makeLeaf("PYJS", self.MAP, " ")) new.append_child(makeLeaf("COMMA", ",", '')) if num_locals > 1: new.append_child(makeLeaf("LSQB", "[", ' ')) match.locals.remove() new.append_child(match.locals) new.append_child(match.item_value) if num_locals > 1: new.append_child(makeLeaf("RSQB", "]", ' ')) new.append_child(makeLeaf("RPAR", ")", ' ')) new.append_child(makeLeaf("PYJS", "=>", ' ')) new.append_child(makeLeaf("LPAR", "(", ' ')) new.append_child(makeLeaf("LBRACE", "{", ' ')) new.append_child(makeLeaf("PYJS", "...", ' ')) new.append_child(makeLeaf("PYJS", self.MAP, "")) new.append_child(makeLeaf("COMMA", ",", '')) new.append_child(makeLeaf("LSQB", "[", ' ')) match.item_key.remove() new.append_child(match.item_key) new.append_child(makeLeaf("RSQB", "]", ' ')) new.append_child(makeLeaf("COLON", ":", "")) match.item_value.remove() new.append_child(match.item_value) new.append_child(makeLeaf("RBRACE", "}", ' ')) new.append_child(makeLeaf("RPAR", ")", ' ')) new.append_child(makeLeaf("COMMA", ",", '')) new.append_child(makeLeaf("LBRACE", "{", ' ')) new.append_child(makeLeaf("RBRACE", "}", '')) new.append_child(makeLeaf("RPAR", ")", ' ')) parent.replace(new)
def processOne(self, match): new = makeStatement() new.append_child(makeLeaf("PYJS", "throw", match.raise_word.prefix)) new.append_child(makeLeaf("PYJS", "new", ' ')) new.append_child(makeLeaf("PYJS", "Error", ' ')) new.append_child(makeLeaf("LPAR", "(", "")) new.append_child( makeLeaf("PYJS", "'%s'" % match.exc_name.toString(), ' ')) if "args" in match: new.append_child(makeLeaf("COMMA", ",", "")) new.append_child(match.args) new.append_child(makeLeaf("RPAR", ")", " ")) match.node.replace(new)
def processOne_NO_PYJS( self, match ): """ this results in right.reduce( ( a, c ) => a.replace( /%(s|i|r)?/, c.toString () ), left ); which is self-contained but a bit long-winded """ is_multi = getNodeKind( match.right ) == "power" or \ len( str( match.right ).split( "," ) ) > 1 new = makeStatement() if not is_multi: new.append_child( makeLeaf( "LSQB", "[", ' ' ) ) new.append_child( match.right ) if not is_multi: new.append_child( makeLeaf( "RSQB", "]", ' ' ) ) new.append_child( makeLeaf( "DOT", ".", '' ) ) new.append_child( makeLeaf( "PYJS", "reduce", '' ) ) new.append_child( makeLeaf( "LPAR", "(", '' ) ) new.append_child( makeLeaf( "LPAR", "(", ' ' ) ) new.append_child( makeLeaf( "PYJS", "a", ' ' ) ) new.append_child( makeLeaf( "COMMA", ",", "" ) ) new.append_child( makeLeaf( "PYJS", "c", ' ' ) ) new.append_child( makeLeaf( "RPAR", ")", ' ' ) ) new.append_child( makeLeaf( "PYJS", "=>", ' ' ) ) new.append_child( makeLeaf( "PYJS", "a", ' ' ) ) new.append_child( makeLeaf( "DOT", ".", '' ) ) new.append_child( makeLeaf( "PYJS", "replace", '' ) ) new.append_child( makeLeaf( "LPAR", "(", '' ) ) new.append_child( makeLeaf( "PYJS", "/%(s|i|r)?/", ' ' ) ) new.append_child( makeLeaf( "COMMA", ",", "" ) ) new.append_child( makeLeaf( "PYJS", "c", ' ' ) ) new.append_child( makeLeaf( "DOT", ".", '' ) ) new.append_child( makeLeaf( "PYJS", "toString", '' ) ) new.append_child( makeLeaf( "LPAR", "(", ' ' ) ) new.append_child( makeLeaf( "RPAR", ")", '' ) ) new.append_child( makeLeaf( "RPAR", ")", ' ' ) ) new.append_child( makeLeaf( "COMMA", ",", "" ) ) match.left.prefix = ' ' new.append_child( match.left ) new.append_child( makeLeaf( "RPAR", ")", ' ' ) ) match.percent_sym.parent.replace( new )
def processOne_PYJS( self, match ): """ this results in _pyjs.stringInterpolate( left, right ); which is a short but needs the external function """ is_multi = getNodeKind( match.right ) == "power" or \ len( str( match.right ).split( "," ) ) > 1 new = makeStatement() new.append_child( makeLeaf( "NAME", "_pyjs.stringInterpolate", match.left.prefix ) ) new.append_child( makeLeaf( "LPAR", "(" ) ) match.left.prefix = ' ' new.append_child( match.left ) new.append_child( makeLeaf( "COMMA", ",", "" ) ) if not is_multi: new.append_child( makeLeaf( "LSQB", "[", ' ' ) ) new.append_child( match.right ) if not is_multi: new.append_child( makeLeaf( "RSQB", "]", ' ' ) ) new.append_child( makeLeaf( "RPAR", ")", " " ) ) match.percent_sym.parent.replace( new )