Пример #1
0
    def test_import(self):
        @gl.lib('frag')
        def add(a: int, b: int) -> int:
            return a + b

        @gl.lib('frag')
        def assign_fn(a: int) -> int:
            c: int = add(a, 2)
            d: int = module_global_add(90, 2)
            d = c = 89
            c += 190 + d
            return c

        expected = ''.join([
            'int test_named_compiler_module_global_add(int a, int b){return (a) + (b);}',
            'int test_named_compiler_add(int a, int b){return (a) + (b);}',
            'int test_named_compiler_assign_fn(int a){int c = test_named_compiler_add(a, 2);int d = test_named_compiler_module_global_add(90, 2);d = c = 89;c += (190) + (d);return c;}',
        ])

        self.maxDiff = None
        self.assertEqual(compile_named_shader(assign_fn, TEST_CONFIG),
                         expected)

        @gl.lib('any')
        def error_import(a: int) -> int:
            return add(a, 12)

        with self.assertRaisesRegex(
                CompileError,
                'Forbidden import FragShader from AnyShader') as _:
            compile_named_shader(error_import, TEST_CONFIG)
Пример #2
0
    def test_params_qualifier(self):
        @gl.lib('any')
        def add(a: int, b: int) -> int:
            return a + b

        @gl.lib('frag')
        def vec_attr(_fragColor: gl.inout_p[gl.vec4]) -> None:
            _fragColor.x = add(12, int(_fragColor.x))

        expected = ''.join([
            'int test_named_compiler_add(int a, int b){return (a) + (b);}',
            'void test_named_compiler_vec_attr(inout vec4 _fragColor){_fragColor.x = test_named_compiler_add(12, int(_fragColor.x));}',
        ])

        self.maxDiff = None
        self.assertEqual(compile_named_shader(vec_attr, TEST_CONFIG), expected)

        @gl.lib('frag')
        def vec_attr2(_fragColor: gl.out_p[gl.vec4]) -> gl.out_p[gl.vec4]:
            return _fragColor

        with self.assertRaisesRegex(
                CompileError,
                'Return type must not have its parameter qualifier') as _:
            compile_named_shader(vec_attr2, TEST_CONFIG)
Пример #3
0
    def test_compare_and_bool_op(self):
        @gl.lib('any')
        def add(a: int, b: int) -> int:
            if a > 1:
                return -1
            return a + b

        expected = 'int test_named_compiler_add(int a, int b){if((a) > (1)){return -1;}return (a) + (b);}'

        self.assertEqual(compile_named_shader(add, TEST_CONFIG), expected)

        @gl.lib('any')
        def add2(a: int, b: int) -> int:
            if 1 <= a < 12:
                return -1
            return a + b

        with self.assertRaisesRegex(
                CompileError,
                'Multiple comparison operators in one expression') as _:
            compile_named_shader(add2)

        @gl.lib('any')
        def add3(a: int, b: int) -> int:
            if (1 <= a and a < 12) or b > 100 or a < -100:
                return -1
            return a + b

        expected3 = (
            'int test_named_compiler_add3(int a, int b){' +
            'if((((1) <= (a)) && ((a) < (12))) || ((b) > (100)) || ((a) < (-100))){return -1;}'
            + 'return (a) + (b);' + '}')

        self.assertEqual(compile_named_shader(add3, TEST_CONFIG), expected3)
Пример #4
0
    def test_undecorated_func(self):
        def add(a: int, b: int) -> int:
            return a + b

        with self.assertRaisesRegex(
                CompileError,
                'Named shader function must be decorated properly') as _:
            compile_named_shader(add)

        @gl.lib('error kind')  # type: ignore
        def add2(a: int, b: int) -> int:
            return a + b

        with self.assertRaisesRegex(CompileError, 'Invalid shader kind') as _:
            compile_named_shader(add2)
Пример #5
0
    def test_ternary_op(self):
        @gl.lib('any')
        def add(a: int, b: int) -> int:
            return a + b if bool(a) else 12

        expected = 'int test_named_compiler_add(int a, int b){return bool(a) ? (a) + (b) : 12;}'

        self.assertEqual(compile_named_shader(add, TEST_CONFIG), expected)

        @gl.lib('any')
        def add2(a: int, b: int) -> int:
            return a + b if bool(a) else 900 if bool(b) else 12

        expected2 = 'int test_named_compiler_add2(int a, int b){return bool(a) ? (a) + (b) : bool(b) ? 900 : 12;}'

        self.assertEqual(compile_named_shader(add2, TEST_CONFIG), expected2)
Пример #6
0
    def test_func_decl(self):
        @gl.lib('any')
        def decl_func(a: int, b: int) -> int:
            ...

        expected = 'int test_named_compiler_decl_func(int a, int b);'

        self.assertEqual(compile_named_shader(decl_func, TEST_CONFIG),
                         expected)
Пример #7
0
    def test_if(self):
        @gl.lib('any')
        def add(a: int, b: int) -> int:
            if bool(a):
                return -1
            return a + b

        expected = 'int test_named_compiler_add(int a, int b){if(bool(a)){return -1;}return (a) + (b);}'

        self.assertEqual(compile_named_shader(add, TEST_CONFIG), expected)

        @gl.lib('any')
        def add2(a: int, b: int) -> int:
            if bool(a):
                return -1
            else:
                return a + b

        expected2 = 'int test_named_compiler_add2(int a, int b){if(bool(a)){return -1;}else{return (a) + (b);}}'

        self.assertEqual(compile_named_shader(add2, TEST_CONFIG), expected2)

        @gl.lib('any')
        def add3(a: int, b: int) -> int:
            if bool(a):
                return -1
            elif bool(b):
                return -100
            else:
                return a + b

        expected3 = 'int test_named_compiler_add3(int a, int b){if(bool(a)){return -1;}else{if(bool(b)){return -100;}else{return (a) + (b);}}}'  # noqa: E501

        self.assertEqual(compile_named_shader(add3, TEST_CONFIG), expected3)

        @gl.lib('any')
        def add4(a: int, b: int) -> int:
            if bool(a):
                ...
            return a + b

        expected4 = 'int test_named_compiler_add4(int a, int b){if(bool(a)){;}return (a) + (b);}'

        self.assertEqual(compile_named_shader(add4, TEST_CONFIG), expected4)
Пример #8
0
    def test_inline_func(self):
        @gl.lib('any')
        def add(a: int, b: int) -> int:
            return a + b

        expected = '\n'.join([
            'int test_named_compiler_add(int a, int b){return (a) + (b);}',
        ])

        self.assertEqual(compile_named_shader(add, TEST_CONFIG), expected)
Пример #9
0
    def test_while(self):
        @gl.lib('any')
        def count() -> int:
            res: int = 0
            while res < 10:
                res += 1
            return res

        expected = ('int test_named_compiler_count(){' + 'int res = 0;' +
                    'while((res) < (10)){res += 1;}' + 'return res;' + '}')

        self.assertEqual(compile_named_shader(count, TEST_CONFIG), expected)

        @gl.lib('any')
        def count2() -> int:
            res: int = 0
            while res < 10:
                res += 1
            else:
                res = 100
            return res

        with self.assertRaisesRegex(CompileError,
                                    'Else with while is not supported') as _:
            compile_named_shader(count2)

        @gl.lib('any')
        def count3() -> int:
            res: int = 0
            while True:
                res += 1
                if res < 10:
                    continue
                else:
                    break
            return res

        expected3 = ('int test_named_compiler_count3(){' + 'int res = 0;' +
                     'while(true){res += 1;' +
                     'if((res) < (10)){continue;}else{break;}}' +
                     'return res;' + '}')

        self.assertEqual(compile_named_shader(count3, TEST_CONFIG), expected3)
Пример #10
0
    def test_paren_arith_func(self):
        @gl.lib('any')
        def paren_arith(a: int, b: int) -> int:
            return (100 - int(((12 * 90) + a) / b))

        expected = ''.join([
            'int test_named_compiler_paren_arith(int a, int b){return (100) - (int((((12) * (90)) + (a)) / (b)));}',
        ])

        self.assertEqual(compile_named_shader(paren_arith, TEST_CONFIG),
                         expected)
Пример #11
0
    def test_module_import(self):
        @gl.lib('frag')
        def add(a: int, b: int) -> int:
            return compiler_fixtures.boost_add(a, b)

        expected = ''.join([
            'int compiler_fixtures_boost(int v){return (v) * (1000);}',
            'int compiler_fixtures_boost_add(int a, int b){return (a) + (compiler_fixtures_boost(b));}',
            'int test_named_compiler_add(int a, int b){return compiler_fixtures_boost_add(a, b);}'
        ])

        self.assertEqual(compile_named_shader(add, TEST_CONFIG), expected)
Пример #12
0
    def test_for(self):
        @gl.lib('any')
        def count() -> int:
            res: int = 0
            for i in range(1, 10, 2):
                res += 1
            return res

        expected = ('int test_named_compiler_count(){' + 'int res = 0;' +
                    'for(int i=(1);i<(10);i+=(2)){res += 1;}' + 'return res;' +
                    '}')

        self.assertEqual(compile_named_shader(count, TEST_CONFIG), expected)

        @gl.lib('any')
        def count2() -> int:
            res: int = 0
            for arr in [1, 2, 3]:
                res += arr
            return res

        with self.assertRaisesRegex(CompileError, '^On For statement') as _:
            compile_named_shader(count2)

        @gl.lib('any')
        def count3() -> int:
            res: int = 0
            for i in range(10):
                res += i
            else:
                res = 100
            return res

        with self.assertRaisesRegex(CompileError,
                                    '^Else with For is not supported') as _:
            compile_named_shader(count3)
Пример #13
0
    def test_vec_attr(self):
        @gl.lib('frag')
        def vec_attr(_fragColor: gl.vec4) -> None:
            _fragColor.x = 12

        expected = ''.join([
            'void test_named_compiler_vec_attr(vec4 _fragColor){_fragColor.x = 12;}',
        ])

        self.assertEqual(compile_named_shader(vec_attr, TEST_CONFIG), expected)

        vec4 = gl.vec4

        @gl.lib('frag')
        def vec_attr2(_fragColor: vec4) -> gl.vec4:
            _fragColor.x = 12
            return _fragColor

        expected2 = ''.join([
            'vec4 test_named_compiler_vec_attr2(vec4 _fragColor){_fragColor.x = 12;return _fragColor;}',
        ])

        self.assertEqual(compile_named_shader(vec_attr2, TEST_CONFIG),
                         expected2)