Ejemplo n.º 1
0
def test_dict():
    assert c.dict((1, c.escaped_string("1+1")), (2, 3)).gen_converter()(
        100
    ) == {1: 2, 2: 3}
    assert c({1: c.escaped_string("1+1"), 2: 3}).gen_converter()(100) == {
        1: 2,
        2: 3,
    }
Ejemplo n.º 2
0
    def _gen_code_and_update_ctx(self, code_input, ctx):
        suffix = self.gen_name("_", ctx, ("take_while", self, code_input))
        converter_name = f"drop_while{suffix}"
        var_chain = f"chain{suffix}"
        var_it = f"it{suffix}"
        var_item = f"item{suffix}"

        (
            code_args,
            positional_args_as_conversions,
            keyword_args_as_conversions,
            namespace_ctx,
        ) = self.get_args_def_info(ctx)
        with namespace_ctx:
            condition_code = self.condition.gen_code_and_update_ctx(
                var_item, ctx)

            code = Code()
            code.add_line(
                f"def {converter_name}({var_it},{var_chain}{code_args}):", 1)
            code.add_line(f"{var_it} = iter({var_it})", 0)
            code.add_line(f"for {var_item} in {var_it}:", 1)
            code.add_line(f"if not ({condition_code}):", 1)
            code.add_line("break", -2)
            code.add_line("else:", 1)
            code.add_line("return ()", -1)
            code.add_line(f"return {var_chain}(({var_item},), {var_it})", -1)
            self._code_to_converter(converter_name, code.to_string(0), ctx)
            return (c.escaped_string(converter_name).call(
                c.this,
                c.naive(chain),
                *positional_args_as_conversions,
                **keyword_args_as_conversions,
            ).gen_code_and_update_ctx(code_input, ctx))
Ejemplo n.º 3
0
def test_is_independent():
    assert c(0).is_independent()
    assert c(int).is_independent()
    assert c(int).call().is_independent()
    assert c.label("a").is_independent()
    assert c.inline_expr("{}()").pass_args(int).is_independent()
    assert c.escaped_string("int()").is_independent()
    assert c({"a": c.input_arg("key")}).is_independent()
    assert not c.iter({"a": 1}).is_independent()
    assert not c.this.is_independent()
    assert not c({"a": 1}).item("a").is_independent()
    assert not c({"a": 1}).item(c.item("a")).is_independent()
    assert not c.inline_expr("{}()").pass_args(c.this).is_independent()
    assert not c.aggregate({"a": 1}).is_independent()
    assert not c.this.add_label("a").is_independent()
    assert not c(int).call(c.item(0)).is_independent()
Ejemplo n.º 4
0
def test_escaped_string_conversion():
    assert c.escaped_string("1 == 1").gen_converter()(1) is True
    assert c.escaped_string("'1 == 1'").gen_converter()(1) == "1 == 1"