Beispiel #1
0
    def test_from_docs(self):
        """A test based on Engine docs."""
        with open('miniscript/tests/example-docs.yaml') as fp:
            code = yaml.safe_load(fp)

        engine = miniscript.Engine({'add': AddTask})
        context = miniscript.Context(engine, values=[23423, 43874, 22834])
        result = engine.execute(code, context)
        self.assertEqual(90131, result)
Beispiel #2
0
    def test_copy(self):
        orig = {"key": "value", "dict": {}}
        ctx = miniscript.Context(self.engine, orig.copy())

        copy = ctx.copy()
        copy["key"] = "value2"
        self.assertEqual(orig, ctx)

        ns = ctx["dict"].copy()
        ns["key"] = "value3"
        self.assertEqual(orig, ctx)
Beispiel #3
0
 def test_complex(self):
     data = {
         "answer": 42,
         "text": "answer is {{ answer }}",
         "key": {
             "value": "{{ text | upper }}"
         }
     }
     ctx = miniscript.Context(self.engine, data)
     sub_ctx = ctx["key"]
     self.assertIsInstance(sub_ctx, _context.Namespace)
     self.assertEqual("ANSWER IS 42", sub_ctx["value"])
Beispiel #4
0
 def test_evaluate_recursive_infinite(self):
     data = {
         "key": "{{ indirect }}",
         "value": {
             "key": "{{ key }}"
         },
         "indirect": ["{{ value }}"],
     }
     # It is safe to create recursive dicts
     context = miniscript.Context(self.engine, data)
     result = self.env.evaluate_recursive("{{ value }}", context)
     self.assertIsInstance(result, _context.Namespace)
Beispiel #5
0
 def test_evaluate_recursive(self):
     data = {
         "answer": 42,
         "text": "answer is {{ answer }}",
         "key": {
             "value": "{{ list }}"
         },
         "list": [
             "{{ answer }}",
             "{{ answer * 10 }}",
             "{{ text | upper }}"
         ]
     }
     context = miniscript.Context(self.engine, data)
     result = self.env.evaluate_recursive("{{ key }}", context)
     self.assertEqual([42, 420, 'ANSWER IS 42'], result["value"])
     result = self.env.evaluate_recursive(result, context)
     self.assertEqual([42, 420, 'ANSWER IS 42'], result["value"])
Beispiel #6
0
 def test_eq(self):
     ctx = miniscript.Context(self.engine, {"key": "value"})
     self.assertEqual({"key": "value"}, ctx)
Beispiel #7
0
 def test_iter(self):
     ctx = miniscript.Context(self.engine, {"key": "value"})
     self.assertEqual(["key"], list(ctx))
Beispiel #8
0
 def test_string(self):
     data = {"answer": 42, "key": "answer is {{ answer }}"}
     ctx = miniscript.Context(self.engine, data)
     self.assertEqual("answer is 42", ctx["key"])
Beispiel #9
0
 def test_simple(self):
     ctx = miniscript.Context(self.engine, {"key": "value"})
     self.assertEqual("value", ctx["key"])
     self.assertRaises(KeyError, ctx.__getitem__, "foo")
Beispiel #10
0
 def test_evaluate_slashes_fixup(self):
     expr = r"A slash outside \ {{ '\foo\\' | replace('f', '\1') }}"
     context = miniscript.Context(self.engine)
     result = self.env.evaluate(expr, context)
     self.assertEqual(r"A slash outside \ \\1oo\\", result)
Beispiel #11
0
 def test_evaluate_with_quotes(self):
     context = miniscript.Context(self.engine, answer='"42"!')
     result = self.env.evaluate("answer is {{answer}}", context)
     self.assertEqual('answer is "42"!', result)
Beispiel #12
0
 def test_evaluate(self):
     context = miniscript.Context(self.engine, answer=42)
     result = self.env.evaluate("answer is {{answer}}", context)
     self.assertEqual("answer is 42", result)
Beispiel #13
0
 def test_execute_context(self):
     context = miniscript.Context(self.engine)
     self.engine.execute([{"test": None}], context)
     self.assertEqual(42, context["answer"])
Beispiel #14
0
 def setUp(self):
     super().setUp()
     self.engine = miniscript.Engine({})
     self.context = miniscript.Context(
         self.engine, answer=42, items=["{{ answer }}", 43, 44])
Beispiel #15
0
 def setUp(self):
     super().setUp()
     self.engine = miniscript.Engine({})
     self.context = miniscript.Context(self.engine, answer=42,
                                       result=miniscript.Result({}))
Beispiel #16
0
 def test_set_del(self):
     ctx = miniscript.Context(self.engine, {"key": "value"})
     ctx["key"] = "value2"
     self.assertEqual({"key": "value2"}, ctx)
     del ctx["key"]
     self.assertEqual({}, ctx)
Beispiel #17
0
 def eval(self, expr: str, **ctx):
     context = miniscript.Context(self.engine, ctx)
     return self.env.evaluate_code(expr, context)
Beispiel #18
0
 def setUp(self):
     super().setUp()
     self.engine = miniscript.Engine({'test': TestTask})
     self.context = miniscript.Context(self.engine, answer=42)