Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/shaderc
Path: blob/main/glslc/test/placeholder.py
1560 views
1
# Copyright 2015 The Shaderc Authors. All rights reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
# http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
15
"""A number of placeholders and their rules for expansion when used in tests.
16
17
These placeholders, when used in glslc_args or expected_* variables of
18
GlslCTest, have special meanings. In glslc_args, they will be substituted by
19
the result of instantiate_for_glslc_args(), while in expected_*, by
20
instantiate_for_expectation(). A TestCase instance will be passed in as
21
argument to the instantiate_*() methods.
22
"""
23
import os
24
import tempfile
25
from string import Template
26
from builtins import bytes
27
28
29
class PlaceHolderException(Exception):
30
"""Exception class for PlaceHolder."""
31
pass
32
33
34
class PlaceHolder(object):
35
"""Base class for placeholders."""
36
37
def instantiate_for_glslc_args(self, testcase):
38
"""Instantiation rules for glslc_args.
39
40
This method will be called when the current placeholder appears in
41
glslc_args.
42
43
Returns:
44
A string to replace the current placeholder in glslc_args.
45
"""
46
raise PlaceHolderException('Subclass should implement this function.')
47
48
def instantiate_for_expectation(self, testcase):
49
"""Instantiation rules for expected_*.
50
51
This method will be called when the current placeholder appears in
52
expected_*.
53
54
Returns:
55
A string to replace the current placeholder in expected_*.
56
"""
57
raise PlaceHolderException('Subclass should implement this function.')
58
59
60
class FileShader(PlaceHolder):
61
"""Stands for a shader whose source code is in a file."""
62
63
def __init__(self, source, suffix, assembly_substr=None):
64
assert isinstance(source, str)
65
assert isinstance(suffix, str)
66
self.source = source
67
self.suffix = suffix
68
self.filename = None
69
# If provided, this is a substring which is expected to be in
70
# the disassembly of the module generated from this input file.
71
self.assembly_substr = assembly_substr
72
73
def instantiate_for_glslc_args(self, testcase):
74
"""Creates a temporary file and writes the source into it.
75
76
Returns:
77
The name of the temporary file.
78
"""
79
shader, self.filename = tempfile.mkstemp(
80
dir=testcase.directory, suffix=self.suffix)
81
shader_object = os.fdopen(shader, 'w')
82
shader_object.write(self.source)
83
shader_object.close()
84
return self.filename
85
86
def instantiate_for_expectation(self, testcase):
87
assert self.filename is not None
88
return self.filename
89
90
91
class StdinShader(PlaceHolder):
92
"""Stands for a shader whose source code is from stdin."""
93
94
def __init__(self, source):
95
assert isinstance(source, str)
96
self.source = source
97
self.filename = None
98
99
def instantiate_for_glslc_args(self, testcase):
100
"""Writes the source code back to the TestCase instance."""
101
testcase.stdin_shader = bytes(self.source, 'utf-8')
102
self.filename = '-'
103
return self.filename
104
105
def instantiate_for_expectation(self, testcase):
106
assert self.filename is not None
107
return self.filename
108
109
110
class TempFileName(PlaceHolder):
111
"""Stands for a temporary file's name."""
112
113
def __init__(self, filename):
114
assert isinstance(filename, str)
115
assert filename != ''
116
self.filename = filename
117
118
def instantiate_for_glslc_args(self, testcase):
119
return os.path.join(testcase.directory, self.filename)
120
121
def instantiate_for_expectation(self, testcase):
122
return os.path.join(testcase.directory, self.filename)
123
124
125
class SpecializedString(PlaceHolder):
126
"""Returns a string that has been specialized based on TestCase.
127
128
The string is specialized by expanding it as a string.Template
129
with all of the specialization being done with each $param replaced
130
by the associated member on TestCase.
131
"""
132
133
def __init__(self, filename):
134
assert isinstance(filename, str)
135
assert filename != ''
136
self.filename = filename
137
138
def instantiate_for_glslc_args(self, testcase):
139
return Template(self.filename).substitute(vars(testcase))
140
141
def instantiate_for_expectation(self, testcase):
142
return Template(self.filename).substitute(vars(testcase))
143
144
145