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,ClearStencil)23 TEST_F(AmberScriptParserTest, ClearStencil) {
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_STENCIL my_pipeline 15)";
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->IsClearStencil());
48 
49   auto* clr = cmd->AsClearStencil();
50   EXPECT_EQ(15u, clr->GetValue());
51 }
52 
TEST_F(AmberScriptParserTest,ClearStencilWithComputePipeline)53 TEST_F(AmberScriptParserTest, ClearStencilWithComputePipeline) {
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_STENCIL my_pipeline 0.0)";
64 
65   Parser parser;
66   Result r = parser.Parse(in);
67   ASSERT_FALSE(r.IsSuccess());
68   EXPECT_EQ("10: CLEAR_STENCIL command requires graphics pipeline", r.Error());
69 }
70 
TEST_F(AmberScriptParserTest,ClearStencilMissingPipeline)71 TEST_F(AmberScriptParserTest, ClearStencilMissingPipeline) {
72   std::string in = "CLEAR_STENCIL 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_STENCIL command", r.Error());
78 }
79 
TEST_F(AmberScriptParserTest,ClearStencilInvalidPipeline)80 TEST_F(AmberScriptParserTest, ClearStencilInvalidPipeline) {
81   std::string in = "CLEAR_STENCIL unknown_pipeline 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_STENCIL command: unknown_pipeline",
88             r.Error());
89 }
90 
91 struct ClearStencilTestData {
92   std::string data;
93   std::string error;
94 };
95 using AmberScriptParserClearStencilTest =
96     testing::TestWithParam<ClearStencilTestData>;
TEST_P(AmberScriptParserClearStencilTest,InvalidParams)97 TEST_P(AmberScriptParserClearStencilTest, 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_STENCIL 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     AmberScriptParserClearStencilTests,
123     AmberScriptParserClearStencilTest,
124     testing::Values(
125         ClearStencilTestData{"", "missing value for CLEAR_STENCIL command"},
126         ClearStencilTestData{
127             "INVALID", "invalid value for CLEAR_STENCIL command: INVALID"},
128         ClearStencilTestData{"-5",
129                              "invalid value for CLEAR_STENCIL command: -5"},
130         ClearStencilTestData{"256",
131                              "invalid value for CLEAR_STENCIL command: 256"},
132         ClearStencilTestData{"10 EXTRA",
133                              "extra parameters after CLEAR_STENCIL command: "
134                              "EXTRA"}));  // NOLINT(whitespace/parens)
135 
136 }  // namespace amberscript
137 }  // namespace amber
138