Ejemplo n.º 1
0
    def test_visit_YieldFrom(self):
        node = MagicMock()
        node.value = "value"

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_YieldFrom(node))
        self.assertEqual(analyzer.parts, ["yield from ", "value"])
Ejemplo n.º 2
0
    def test_visit_Index(self):
        node = MagicMock()
        node.value = "value"

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Index(node))
        self.assertEqual(analyzer.parts, ["value"])

        node_value = MagicMock()
        node_value.mock_add_spec(ast.Tuple)
        node_value.elts = ["el1", "el2"]
        node.value = node_value
        self.assertIsNone(analyzer.visit_Index(node))
        self.assertEqual(
            analyzer.parts,
            [
                "value",
                RenderPart.MULTI_LINE_INDENT,
                "el1",
                ",",
                RenderPart.SINGLE_LINE_SPACE,
                RenderPart.MULTI_LINE_BREAK,
                "el2",
                RenderPart.MULTI_LINE_COMMA,
                RenderPart.MULTI_LINE_UNINDENT,
            ],
        )
Ejemplo n.º 3
0
    def test_visit_Await(self):
        node = MagicMock()
        node.value = "value"

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Await(node))
        self.assertEqual(analyzer.parts, ["await ", "value"])
Ejemplo n.º 4
0
 def test_visit_Dict(self):
     node = MagicMock()
     node.keys = ["key1", "key2"]
     node.values = ["value1", "value2"]
     analyzer = ExpressionAnalyzer()
     self.assertIsNone(analyzer.visit_Dict(node))
     self.assertEqual(
         analyzer.parts,
         [
             "{",
             RenderPart.MULTI_LINE_INDENT,
             "key1",
             ": ",
             "value1",
             ",",
             RenderPart.SINGLE_LINE_SPACE,
             RenderPart.MULTI_LINE_BREAK,
             "key2",
             ": ",
             "value2",
             RenderPart.MULTI_LINE_COMMA,
             RenderPart.MULTI_LINE_UNINDENT,
             "}",
         ],
     )
Ejemplo n.º 5
0
 def test_visit_Attribute(self):
     node = MagicMock()
     node.value = "node_value"
     node.attr = "node_attr"
     analyzer = ExpressionAnalyzer()
     self.assertIsNone(analyzer.visit_Attribute(node))
     self.assertEqual(analyzer.parts, ["node_value", ".", "node_attr"])
Ejemplo n.º 6
0
    def test_visit_FormattedValue(self):
        node = MagicMock()
        node.value = "value"

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_FormattedValue(node))
        self.assertEqual(analyzer.parts, ["{", "value", "}"])
Ejemplo n.º 7
0
    def test_visit_Lambda(self):
        node = MagicMock()
        node.args = "args"
        node.body = "body"

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Lambda(node))
        self.assertEqual(analyzer.parts, ["lambda ", "args", ": ", "body"])
Ejemplo n.º 8
0
    def test_generic_visit(self, get_logger_mock):
        node = MagicMock()
        node.mock_add_spec(ast.Import)

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.generic_visit(node))
        self.assertEqual(analyzer.parts, ["..."])
        get_logger_mock().warning.assert_called_once_with(ANY)
Ejemplo n.º 9
0
    def test_visit_GeneratorExp(self):
        node = MagicMock()
        node.elt = "elt"
        node.generators = ["generator1", "generator2"]

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_GeneratorExp(node))
        self.assertEqual(analyzer.parts,
                         ["(", "elt", " ", "generator1", "generator2", ")"])
Ejemplo n.º 10
0
    def test_visit_IfExp(self):
        node = MagicMock()
        node.body = "body"
        node.test = "test"
        node.orelse = "orelse"

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_IfExp(node))
        self.assertEqual(analyzer.parts,
                         ["body", " if ", "test", " else ", "orelse"])
Ejemplo n.º 11
0
    def test_visit_comprehension(self):
        node = MagicMock()
        node.target = "target"
        node.iter = "iter"
        node.ifs = ["if1", "if2"]

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_comprehension(node))
        self.assertEqual(
            analyzer.parts,
            ["for ", "target", " in ", "iter", " if ", "if1", " if ", "if2"],
        )
Ejemplo n.º 12
0
    def test_visit_DictComp(self):
        node = MagicMock()
        node.key = "key"
        node.value = "value"
        node.generators = ["generator1", "generator2"]

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_DictComp(node))
        self.assertEqual(
            analyzer.parts,
            ["{", "key", ": ", "value", " ", "generator1", "generator2", "}"],
        )
Ejemplo n.º 13
0
    def test_visit_JoinedStr(self):
        node = MagicMock()
        node_value = MagicMock()
        node_value.mock_add_spec(ast.Str)
        node_value.s = "node_value"
        node_value_2 = MagicMock()
        node_value_2.s = b"node_value_2"
        node_value_2.mock_add_spec(ast.Str)
        node_value_3 = "not_str"
        node.values = [node_value, node_value_2, node_value_3]

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_JoinedStr(node))
        self.assertEqual(analyzer.parts,
                         ["f'", "node_value", "node_value_2", "not_str", "'"])
Ejemplo n.º 14
0
 def test_visit_List(self):
     node = MagicMock()
     node.elts = ["el1", "el2"]
     analyzer = ExpressionAnalyzer()
     self.assertIsNone(analyzer.visit_List(node))
     self.assertEqual(
         analyzer.parts,
         [
             "[",
             RenderPart.MULTI_LINE_INDENT,
             "el1",
             ",",
             RenderPart.SINGLE_LINE_SPACE,
             RenderPart.MULTI_LINE_BREAK,
             "el2",
             RenderPart.MULTI_LINE_COMMA,
             RenderPart.MULTI_LINE_UNINDENT,
             "]",
         ],
     )
Ejemplo n.º 15
0
class ExpressionRecord(NodeRecord):
    """
    Wrapper for an `ast.expr` node.

    Arguments:
        node -- AST node.
    """

    _str_split_re = re.compile(r"[\]\[ ,]")

    def __init__(self, node):
        # type: (ast.AST) -> None
        super(ExpressionRecord, self).__init__(node)
        self.parts = []  # type: List[DirtyRenderExpr]
        self.analyzer = ExpressionAnalyzer()

    @property
    def related_names(self):
        # type: () -> Set[Text]
        return set(self.analyzer.related_names)

    def _parse(self):
        # type: () -> None
        if isinstance(self.node, ast.Name):
            self.name = self.node.id

        if isinstance(self.node, ast.AST):
            self.analyzer.visit(self.node)
            self.parts = self.analyzer.parts
            return

    def _render_parts(self, indent=0):
        # type: (int) -> List[RenderExpr]
        result = []  # type: List[RenderExpr]
        for part in self.parts:
            if isinstance(part, ast.AST):
                result.append(ExpressionRecord(part))
                continue

            result.append(part)
        return result
Ejemplo n.º 16
0
    def test_visit_Subscript(self):
        node = MagicMock()
        node.value = "node_value"
        node_slice_value = MagicMock()
        node_slice_value.mock_add_spec(ast.Tuple)
        node_slice_value.elts = ["el1", "el2"]
        node_slice = MagicMock()
        node_slice.mock_add_spec(ast.Index)
        node_slice.value = node_slice_value
        node.slice = node_slice
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Subscript(node))
        self.assertEqual(
            analyzer.parts,
            [
                "node_value",
                "[",
                RenderPart.MULTI_LINE_INDENT,
                "el1",
                ",",
                RenderPart.SINGLE_LINE_SPACE,
                RenderPart.MULTI_LINE_BREAK,
                "el2",
                RenderPart.MULTI_LINE_COMMA,
                RenderPart.MULTI_LINE_UNINDENT,
                "]",
            ],
        )

        node.slice = "node_slice"
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Subscript(node))
        self.assertEqual(analyzer.parts,
                         ["node_value", "[", "node_slice", "]"])
Ejemplo n.º 17
0
    def test_visit_BoolOp(self, get_logger_mock):
        node = MagicMock()
        node.values = ["left", "right"]
        node.op = ast.And()

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_BoolOp(node))
        self.assertEqual(analyzer.parts, ["left", " ", "and", " ", "right"])

        node.op = ast.Or()
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_BoolOp(node))
        self.assertEqual(analyzer.parts, ["left", " ", "or", " ", "right"])

        node.op = ast.Import()
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_BoolOp(node))
        self.assertEqual(analyzer.parts, ["left", " ", "...", " ", "right"])
        get_logger_mock().warning.assert_called_once_with(ANY)
Ejemplo n.º 18
0
    def test_visit_Str(self):
        node = MagicMock()
        node.s = "value"
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Str(node))
        self.assertEqual(analyzer.parts, ["'value'"])

        node.s = b"value"
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Str(node))
        self.assertEqual(analyzer.parts, ["'value'"])
Ejemplo n.º 19
0
    def test_visit_keyword(self):
        node = MagicMock()
        node.arg = "node_arg"
        node.value = "node_value"
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_keyword(node))
        self.assertEqual(analyzer.parts, ["node_arg", "=", "node_value"])

        node.arg = None
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_keyword(node))
        self.assertEqual(analyzer.parts, ["**", "node_value"])
Ejemplo n.º 20
0
    def test_visit_arg(self):
        node = MagicMock()
        node.arg = "arg"
        node.annotation = "annotation"

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_arg(node))
        self.assertEqual(analyzer.parts, ["arg", ": ", "annotation"])

        node.annotation = None
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_arg(node))
        self.assertEqual(analyzer.parts, ["arg"])
Ejemplo n.º 21
0
    def test_visit_BinOp(self, get_logger_mock):
        node = MagicMock()
        node.left = "left"
        node.right = "right"
        node.op = ast.LShift()

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_BinOp(node))
        self.assertEqual(analyzer.parts, ["left", " ", "<<", " ", "right"])

        node.op = ast.Import()
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_BinOp(node))
        self.assertEqual(analyzer.parts, ["left", " ", "...", " ", "right"])
        get_logger_mock().warning.assert_called_once_with(ANY)
Ejemplo n.º 22
0
    def test_visit_Compare(self, get_logger_mock):
        node = MagicMock()
        node.left = "left"
        node.comparators = ["middle", "right"]
        node.ops = [ast.LtE(), ast.IsNot()]
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Compare(node))
        self.assertEqual(
            analyzer.parts,
            ["left", " ", "<=", " ", "middle", " ", "is not", " ", "right"],
        )

        node.ops = [ast.LtE(), ast.Import]
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Compare(node))
        self.assertEqual(
            analyzer.parts,
            ["left", " ", "<=", " ", "middle", " ", "...", " ", "right"])
        get_logger_mock().warning.assert_called_once_with(ANY)
Ejemplo n.º 23
0
 def test_visit_NameConstant(self):
     node = MagicMock()
     node.value = "node_value"
     analyzer = ExpressionAnalyzer()
     self.assertIsNone(analyzer.visit_NameConstant(node))
     self.assertEqual(analyzer.parts, ["'node_value'"])
Ejemplo n.º 24
0
    def test_visit_UnaryOp(self, get_logger_mock):
        node = MagicMock()
        node.operand = "operand"
        node.op = ast.Not()

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_UnaryOp(node))
        self.assertEqual(analyzer.parts, ["not", " ", "operand"])

        node.op = ast.Invert()
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_UnaryOp(node))
        self.assertEqual(analyzer.parts, ["~", "operand"])

        node.op = ast.UAdd()
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_UnaryOp(node))
        self.assertEqual(analyzer.parts, ["+", "operand"])

        node.op = ast.USub()
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_UnaryOp(node))
        self.assertEqual(analyzer.parts, ["-", "operand"])

        node.op = ast.Import()
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_UnaryOp(node))
        self.assertEqual(analyzer.parts, ["...", "operand"])
        get_logger_mock().warning.assert_called_once_with(ANY)
Ejemplo n.º 25
0
 def test_visit_Num(self):
     node = MagicMock()
     node.n = 123.456
     analyzer = ExpressionAnalyzer()
     self.assertIsNone(analyzer.visit_Num(node))
     self.assertEqual(analyzer.parts, ["123.456"])
Ejemplo n.º 26
0
 def test_visit_Ellipsis(self):
     analyzer = ExpressionAnalyzer()
     self.assertIsNone(analyzer.visit_Ellipsis("node"))
     self.assertEqual(analyzer.parts, ["..."])
Ejemplo n.º 27
0
    def test_visit_Slice(self):
        node = MagicMock()
        node.lower = "lower"
        node.upper = "upper"
        node.step = "step"

        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Slice(node))
        self.assertEqual(analyzer.parts, ["lower", ":", "upper", ":", "step"])

        node.upper = None
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Slice(node))
        self.assertEqual(analyzer.parts, ["lower", ":", ":", "step"])

        node.step = None
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Slice(node))
        self.assertEqual(analyzer.parts, ["lower", ":"])

        node.lower = None
        node.upper = None
        node.step = "step"
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Slice(node))
        self.assertEqual(analyzer.parts, [":", ":", "step"])
Ejemplo n.º 28
0
 def test_visit_Starred(self):
     node = MagicMock()
     node.value = "node_value"
     analyzer = ExpressionAnalyzer()
     self.assertIsNone(analyzer.visit_Starred(node))
     self.assertEqual(analyzer.parts, ["*", "node_value"])
Ejemplo n.º 29
0
 def __init__(self, node):
     # type: (ast.AST) -> None
     super(ExpressionRecord, self).__init__(node)
     self.parts = []  # type: List[DirtyRenderExpr]
     self.analyzer = ExpressionAnalyzer()
Ejemplo n.º 30
0
 def test_visit_Name(self):
     node = MagicMock()
     node.id = "name"
     analyzer = ExpressionAnalyzer()
     self.assertIsNone(analyzer.visit_Name(node))
     self.assertEqual(analyzer.parts, ["name"])