1 // Copyright 2020 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,ClearDepth)23 TEST_F(AmberScriptParserTest, ClearDepth) {
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_DEPTH my_pipeline 1.5)";
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->IsClearDepth());
48
49 auto* clr = cmd->AsClearDepth();
50 EXPECT_FLOAT_EQ(1.5, clr->GetValue());
51 }
52
TEST_F(AmberScriptParserTest,ClearDepthWithComputePipeline)53 TEST_F(AmberScriptParserTest, ClearDepthWithComputePipeline) {
54 std::string in = R"(
55 SHADER compute my_shader GLSL
56 # shader
57 END
58
59 PIPELINE compute my_pipeline
60 ATTACH my_shader
61 END
62
63 CLEAR_DEPTH my_pipeline 0.0)";
64
65 Parser parser;
66 Result r = parser.Parse(in);
67 ASSERT_FALSE(r.IsSuccess());
68 EXPECT_EQ("10: CLEAR_DEPTH command requires graphics pipeline", r.Error());
69 }
70
TEST_F(AmberScriptParserTest,ClearDepthMissingPipeline)71 TEST_F(AmberScriptParserTest, ClearDepthMissingPipeline) {
72 std::string in = "CLEAR_DEPTH 0.0";
73
74 Parser parser;
75 Result r = parser.Parse(in);
76 ASSERT_FALSE(r.IsSuccess());
77 EXPECT_EQ("1: missing pipeline name for CLEAR_DEPTH command", r.Error());
78 }
79
TEST_F(AmberScriptParserTest,ClearDepthInvalidPipeline)80 TEST_F(AmberScriptParserTest, ClearDepthInvalidPipeline) {
81 std::string in = "CLEAR_DEPTH unknown_pipeline 0.0";
82
83 Parser parser;
84 Result r = parser.Parse(in);
85 ASSERT_FALSE(r.IsSuccess());
86
87 EXPECT_EQ("1: unknown pipeline for CLEAR_DEPTH command: unknown_pipeline",
88 r.Error());
89 }
90
91 struct ClearDepthTestData {
92 std::string data;
93 std::string error;
94 };
95 using AmberScriptParserClearDepthTest =
96 testing::TestWithParam<ClearDepthTestData>;
TEST_P(AmberScriptParserClearDepthTest,InvalidParams)97 TEST_P(AmberScriptParserClearDepthTest, InvalidParams) {
98 auto test_data = GetParam();
99
100 std::string in = R"(
101 SHADER vertex my_shader PASSTHROUGH
102 SHADER fragment my_fragment GLSL
103 # GLSL Shader
104 END
105 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
106
107 PIPELINE graphics my_pipeline
108 ATTACH my_shader
109 ATTACH my_fragment
110 END
111
112 CLEAR_DEPTH my_pipeline )" +
113 test_data.data;
114
115 Parser parser;
116 Result r = parser.Parse(in);
117 ASSERT_FALSE(r.IsSuccess()) << test_data.data;
118 EXPECT_EQ(std::string("13: ") + test_data.error, r.Error()) << test_data.data;
119 }
120
121 INSTANTIATE_TEST_SUITE_P(
122 AmberScriptParserClearDepthTests,
123 AmberScriptParserClearDepthTest,
124 testing::Values(
125 ClearDepthTestData{"", "missing value for CLEAR_DEPTH command"},
126 ClearDepthTestData{"INVALID",
127 "invalid value for CLEAR_DEPTH command: INVALID"},
128 ClearDepthTestData{"5", "invalid value for CLEAR_DEPTH command: 5"},
129 ClearDepthTestData{"1.0 EXTRA",
130 "extra parameters after CLEAR_DEPTH command: "
131 "EXTRA"})); // NOLINT(whitespace/parens)
132
133 } // namespace amberscript
134 } // namespace amber
135