示例#1
0
    def test_import_all(self, create_json):
        imports_all = create_json('spam.json', {
            '@import': {
                'eggs': True
            },
            '+': ['&foo', '&bar']
        })

        imports_foo = create_json('ham.json', {
            '@import': {
                'eggs': ['foo']
            },
            '+': ['&foo', '&bar']
        })

        create_json('eggs.json', {
            '@export': ['foo', 'bar'],
            '=foo': 41,
            '=bar': 1,
        })

        assert run(imports_all) == 42

        with pytest.raises(KeyError):
            run(imports_foo)
示例#2
0
    def test_this(self):
        assert run({
            'factory': {
                '=value': 42,
                '#': '&this'
            },
            '=spam': {
                'factory': []
            },
            'get': ['&spam', 'value']
        }) == 42

        assert run({
            'factory': {
                '=value': '&0',
                '#': '&this'
            },
            '=spam': {
                'factory': ['foo']
            },
            '=eggs': {
                'factory': ['bar']
            },
            '#': [
                {
                    'get': ['&spam', 'value']
                },
                {
                    'get': ['&eggs', 'value']
                },
            ]
        }) == ['foo', 'bar']
示例#3
0
    def test_args(self):
        assert run({'spam': {'#': '&args'}, '#spam': [1, 2, 3]}) == (1, 2, 3)

        assert run({
            'spam': {
                '#': {
                    '#': '&args'
                }
            },
            '#spam': [1, 2, 3]
        }) == (1, 2, 3)
示例#4
0
 def test_return(self):
     assert run({
         'spam': {
             '+': ['&0', 1]
         },
         '#': {
             'spam': [41]
         }
     }) == run({
         'spam': {
             '+': ['&0', 1]
         },
         '#spam': [41]
     }) == 42
示例#5
0
    def test_run_module_only_once(self, create_json, capsys):
        main = create_json(
            'spam.json', {
                '@import': {
                    'eggs': ['foo'],
                    'ham': ['bar']
                },
                '!print': ['spam'],
                '+': ['&foo', '&bar']
            })

        create_json(
            'eggs.json', {
                '@import': {
                    'ham': ['bar']
                },
                '@export': ['foo'],
                '!print': ['eggs'],
                '=foo': {
                    '+': [40, '&bar']
                },
            })

        create_json('ham.json', {
            '@export': ['bar'],
            '!print': ['ham'],
            '=bar': 1,
        })

        assert run(main) == 42
        assert capsys.readouterr().out == 'ham\neggs\nspam\n'
示例#6
0
    def test_imports(self, create_json):
        main = create_json(
            'spam.json', {
                '@import': {
                    'eggs': ['func', 'foo']
                },
                '=foo': {
                    '*': ['&foo', 5]
                },
                'func': [{
                    '+': ['&foo', 1]
                }]
            })

        create_json(
            'eggs.json', {
                '@export': ['foo', 'bar', 'func'],
                '=foo': 4,
                '=bar': 2,
                'func': {
                    '*': ['&0', '&bar']
                }
            })

        assert run(main) == 42
示例#7
0
 def test_map(self):
     assert run({
         'map': {
             '@params': ['list', 'callback'],
             '?if': [{
                 '==': [{
                     'length': ['&list']
                 }, 0]
             }, []],
             '=list': {
                 'copy': ['&list']
             },
             '=current': {
                 'callback': [{
                     'pop': ['&list']
                 }]
             },
             '=result': {
                 'map': ['&list', '&callback']
             },
             '!push': ['&result', '&current'],
             '#': '&result'
         },
         '#': {
             'inc': {
                 '+': ['&0', 1]
             },
             'map': [[1, 2, 3], '&inc']
         }
     }) == [2, 3, 4]
示例#8
0
 def test_evaluate_list(self):
     assert run({
         '=spam': 42,
         '=eggs': ['foo', '&spam', {
             '#': 'bar'
         }],
         '#': '&eggs'
     }) == ['foo', 42, 'bar']
示例#9
0
 def test_params(self):
     assert run({
         'spam': {
             '@params': ['foo', 'bar'],
             '+': ['&foo', '&bar']
         },
         '#spam': [40, 2]
     }) == 42
示例#10
0
 def test_pop(self):
     assert run({
         '=spam': [42, 'eggs'],
         '=eggs': {
             'pop': ['&spam']
         },
         '#': ['&spam', '&eggs']
     }) == [[42], 'eggs']
示例#11
0
 def test_bind_scope(self):
     assert run({
         '=eggs': 1,
         'spam': {
             '+': ['&0', '&eggs']
         },
         '#': {
             '=eggs': 41,
             '&spam': [1]
         }
     }) == 42
示例#12
0
 def test_preserve_scope(self):
     assert run({
         '=eggs': 1,
         'spam': {
             '+': ['&0', '&eggs']
         },
         '#': {
             '=eggs': 41,
             'spam': ['&eggs']
         }
     }) == 42
示例#13
0
 def test_use_scoped(self):
     assert run({
         '=spam': {
             '=foo': 41,
             '#': '&this'
         },
         'eggs': {
             '@use': ['spam'],
             '+': ['&foo', '&0']
         },
         '#eggs': [1]
     }) == 42
示例#14
0
 def test_referential_identity(self):
     assert run({
         '=spam': {
             '=foo': [1],
             '#': '&this'
         },
         '!': {
             '@use': ['spam'],
             'push': ['&foo', 2]
         },
         'get': ['&spam', 'foo']
     }) == [1, 2]
示例#15
0
 def test_callback_reference(self):
     assert run({
         'spam': {
             '@params': ['callback', 'value'],
             'callback': ['&value']
         },
         '#': {
             'eggs': {
                 '+': ['&0', 1]
             },
             'spam': ['&eggs', 41]
         }
     })
示例#16
0
 def test_pipe_functions(self):
     assert run({
         'foo': {
             '+': ['&0', 1]
         },
         'bar': {
             '*': ['&0', 2]
         },
         'baz': {
             '/': ['&0', '&1']
         },
         'baz|foo|bar': [60, 3]
     }) == 42
示例#17
0
 def test_override_values(self):
     assert run({
         '=spam': {
             '=foo': 41,
             '=bar': 7,
             '#': '&this'
         },
         'eggs': {
             '@use': ['spam'],
             '=bar': 1,
             '+': ['&foo', '&bar']
         },
         '#eggs': []
     }) == 42
示例#18
0
 def test_bound_reference(self):
     assert run({
         '=eggs': 1,
         'spam': {
             '+': ['&0', '&eggs']
         },
         'ham': {
             '@params': ['callback'],
             '&callback': [1]
         },
         '#': {
             '=eggs': 41,
             '&ham': ['&spam']
         }
     }) == 42
示例#19
0
 def test_factorial(self):
     assert run({
         'fac': {
             '?if': [{
                 '==': ['&0', 1]
             }, 1],
             '*': ['&0', {
                 'fac': [{
                     '-': ['&0', 1]
                 }]
             }]
         },
         '#': {
             'fac': [5]
         }
     }) == 120
示例#20
0
    def test_bind_prefixed(self, capsys):
        assert run({
            '=eggs': 1,
            'spam': {
                'print': [{
                    '+': ['&0', '&eggs']
                }]
            },
            '#': {
                '=eggs': 41,
                '!&spam': [1],
                '#': '&eggs'
            }
        }) == 41

        assert capsys.readouterr().out == '42\n'
示例#21
0
    def test_sublevel_exports(self, create_json):
        main = create_json('spam.json', {
            '@import': {
                'eggs': ['foo', 'bar'],
            },
            '+': ['&foo', '&bar']
        })

        create_json('eggs.json', {
            '@export': ['foo'],
            '=foo': 41,
            '#': {
                '@export': ['bar'],
                '=bar': 1
            }
        })

        assert run(main) == 42
示例#22
0
 def test_pipe_bound_function(self):
     assert run({
         '=spam': 2,
         'foo': {
             '+': ['&0', '&spam']
         },
         'bar': {
             '*': ['&0', '&spam']
         },
         'baz': {
             '/': ['&0', '&eggs']
         },
         '#': {
             '=spam': 1,
             '=eggs': 3,
             '&baz|&foo|bar': [60]
         }
     }) == 42
示例#23
0
    def test_relative_import_paths(self, create_json):
        main = create_json('foo/spam.json', {
            '@import': {
                '../bar/eggs': ['value'],
            },
            '#': '&value'
        })

        create_json('bar/eggs.json', {
            '@import': {
                'baz/ham.json': ['value']
            },
            '@export': ['value']
        })

        create_json('bar/baz/ham.json', {
            '@export': ['value'],
            '=value': 42,
        })

        assert run(main) == 42
示例#24
0
 def test_fibonacci(self):
     assert run({
         'fibo': {
             '?if': [{
                 'or': [{
                     '==': ['&0', 1]
                 }, {
                     '==': ['&0', 2]
                 }]
             }, 1],
             '+': [{
                 'fibo': [{
                     '-': ['&0', 1]
                 }]
             }, {
                 'fibo': [{
                     '-': ['&0', 2]
                 }]
             }]
         },
         '#': {
             'fibo': [7]
         }
     }) == 13
示例#25
0
 def test_maybe(self):
     assert run({'?': 42, '#': 'spam'}) == 42
     assert run({'?': None, '#': 'spam'}) == 'spam'
示例#26
0
 def test_maybe(self):
     assert run({'?if': [False, 'spam'], '#': {'?if': [True, 42]}}) == 42
示例#27
0
 def test_return(self):
     assert run({'#': 42}) == 42
示例#28
0
    def test_void(self, capsys):
        assert run({'!print': ['spam'], '#': 42}) == 42

        assert capsys.readouterr().out == 'spam\n'
示例#29
0
    def test_void_call(self, capsys):
        assert run({'!': {'print': ['spam']}, '#': 42}) == 42

        assert capsys.readouterr().out == 'spam\n'
示例#30
0
 def test_void(self):
     assert run({'!': 42}) is None