1 // Copyright 2019 The Amber Authors.
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 parseried.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "gtest/gtest.h"
16 #include "src/amberscript/parser.h"
17
18 namespace amber {
19 namespace amberscript {
20
21 using AmberScriptParserTest = testing::Test;
22
TEST_F(AmberScriptParserTest,ClearColor)23 TEST_F(AmberScriptParserTest, ClearColor) {
24 std::string in = R"(
25 SHADER vertex my_shader PASSTHROUGH
26 SHADER fragment my_fragment GLSL
27 # GLSL Shader
28 END
29 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
30
31 PIPELINE graphics my_pipeline
32 ATTACH my_shader
33 ATTACH my_fragment
34 END
35
36 CLEAR_COLOR my_pipeline 255 128 64 32)";
37
38 Parser parser;
39 Result r = parser.Parse(in);
40 ASSERT_TRUE(r.IsSuccess()) << r.Error();
41
42 auto script = parser.GetScript();
43 const auto& commands = script->GetCommands();
44 ASSERT_EQ(1U, commands.size());
45
46 auto* cmd = commands[0].get();
47 ASSERT_TRUE(cmd->IsClearColor());
48
49 auto* clr = cmd->AsClearColor();
50 EXPECT_FLOAT_EQ(255.f / 255.f, clr->GetR());
51 EXPECT_FLOAT_EQ(128.f / 255.f, clr->GetG());
52 EXPECT_FLOAT_EQ(64.f / 255.f, clr->GetB());
53 EXPECT_FLOAT_EQ(32.f / 255.f, clr->GetA());
54 }
55
TEST_F(AmberScriptParserTest,ClearColorWithComputePipeline)56 TEST_F(AmberScriptParserTest, ClearColorWithComputePipeline) {
57 std::string in = R"(
58 SHADER compute my_shader GLSL
59 # shader
60 END
61
62 PIPELINE compute my_pipeline
63 ATTACH my_shader
64 END
65
66 CLEAR_COLOR my_pipeline 255 128 64 32)";
67
68 Parser parser;
69 Result r = parser.Parse(in);
70 ASSERT_FALSE(r.IsSuccess());
71 EXPECT_EQ("10: CLEAR_COLOR command requires graphics pipeline", r.Error());
72 }
73
TEST_F(AmberScriptParserTest,ClearColorMissingPipeline)74 TEST_F(AmberScriptParserTest, ClearColorMissingPipeline) {
75 std::string in = "CLEAR_COLOR 255 255 255 255";
76
77 Parser parser;
78 Result r = parser.Parse(in);
79 ASSERT_FALSE(r.IsSuccess());
80 EXPECT_EQ("1: missing pipeline name for CLEAR_COLOR command", r.Error());
81 }
82
TEST_F(AmberScriptParserTest,ClearColorInvalidPipeline)83 TEST_F(AmberScriptParserTest, ClearColorInvalidPipeline) {
84 std::string in = "CLEAR_COLOR unknown_pipeline 255 255 255 255";
85
86 Parser parser;
87 Result r = parser.Parse(in);
88 ASSERT_FALSE(r.IsSuccess());
89
90 EXPECT_EQ("1: unknown pipeline for CLEAR_COLOR command: unknown_pipeline",
91 r.Error());
92 }
93
94 struct ClearColorTestData {
95 std::string data;
96 std::string error;
97 };
98 using AmberScriptParserClearColorTest =
99 testing::TestWithParam<ClearColorTestData>;
TEST_P(AmberScriptParserClearColorTest,InvalidParams)100 TEST_P(AmberScriptParserClearColorTest, InvalidParams) {
101 auto test_data = GetParam();
102
103 std::string in = R"(
104 SHADER vertex my_shader PASSTHROUGH
105 SHADER fragment my_fragment GLSL
106 # GLSL Shader
107 END
108 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
109
110 PIPELINE graphics my_pipeline
111 ATTACH my_shader
112 ATTACH my_fragment
113 END
114
115 CLEAR_COLOR my_pipeline )" +
116 test_data.data;
117
118 Parser parser;
119 Result r = parser.Parse(in);
120 ASSERT_FALSE(r.IsSuccess()) << test_data.data;
121 EXPECT_EQ(std::string("13: ") + test_data.error, r.Error()) << test_data.data;
122 }
123
124 INSTANTIATE_TEST_SUITE_P(
125 AmberScriptParserClearColorTests,
126 AmberScriptParserClearColorTest,
127 testing::Values(
128 ClearColorTestData{"", "missing R value for CLEAR_COLOR command"},
129 ClearColorTestData{"255", "missing G value for CLEAR_COLOR command"},
130 ClearColorTestData{"255 255",
131 "missing B value for CLEAR_COLOR command"},
132 ClearColorTestData{"255 255 255",
133 "missing A value for CLEAR_COLOR command"},
134 ClearColorTestData{"INVALID 255 255 255",
135 "invalid R value for CLEAR_COLOR command: INVALID"},
136 ClearColorTestData{"255 INVALID 255 255",
137 "invalid G value for CLEAR_COLOR command: INVALID"},
138 ClearColorTestData{"255 255 INVALID 255",
139 "invalid B value for CLEAR_COLOR command: INVALID"},
140 ClearColorTestData{"255 255 255 INVALID",
141 "invalid A value for CLEAR_COLOR command: INVALID"},
142 ClearColorTestData{"255 255 255 255 EXTRA",
143 "extra parameters after CLEAR_COLOR command: EXTRA"},
144 ClearColorTestData{"-1 255 255 255",
145 "invalid R value for CLEAR_COLOR command: -1"},
146 ClearColorTestData{"5.2 255 255 255",
147 "invalid R value for CLEAR_COLOR command: 5.2"},
148 ClearColorTestData{"256 255 255 255",
149 "invalid R value for CLEAR_COLOR command: 256"},
150 ClearColorTestData{"255 -1 255 255",
151 "invalid G value for CLEAR_COLOR command: -1"},
152 ClearColorTestData{"255 5.2 255 255",
153 "invalid G value for CLEAR_COLOR command: 5.2"},
154 ClearColorTestData{"255 256 255 255",
155 "invalid G value for CLEAR_COLOR command: 256"},
156 ClearColorTestData{"255 255 -1 255",
157 "invalid B value for CLEAR_COLOR command: -1"},
158 ClearColorTestData{"255 255 5.2 255",
159 "invalid B value for CLEAR_COLOR command: 5.2"},
160 ClearColorTestData{"255 255 256 255",
161 "invalid B value for CLEAR_COLOR command: 256"},
162 ClearColorTestData{"255 255 255 -1",
163 "invalid A value for CLEAR_COLOR command: -1"},
164 ClearColorTestData{"255 255 255 5.2",
165 "invalid A value for CLEAR_COLOR command: 5.2"},
166 ClearColorTestData{"255 255 255 256",
167 "invalid A value for CLEAR_COLOR "
168 "command: 256"})); // NOLINT(whitespace/parens)
169
170 } // namespace amberscript
171 } // namespace amber
172