• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,Copy)23 TEST_F(AmberScriptParserTest, Copy) {
24   std::string in = R"(
25 BUFFER from FORMAT R32G32B32A32_SFLOAT
26 BUFFER dest FORMAT R32G32B32A32_SFLOAT
27 COPY from TO dest)";
28 
29   Parser parser;
30   Result r = parser.Parse(in);
31   ASSERT_TRUE(r.IsSuccess()) << r.Error();
32 
33   auto script = parser.GetScript();
34   const auto& commands = script->GetCommands();
35   ASSERT_EQ(1U, commands.size());
36 
37   auto* cmd = commands[0].get();
38   ASSERT_TRUE(cmd->IsCopy());
39 }
40 
TEST_F(AmberScriptParserTest,CopyUndeclaredOriginBuffer)41 TEST_F(AmberScriptParserTest, CopyUndeclaredOriginBuffer) {
42   std::string in = R"(
43 COPY from)";
44 
45   Parser parser;
46   Result r = parser.Parse(in);
47   ASSERT_FALSE(r.IsSuccess());
48   EXPECT_EQ("2: COPY origin buffer was not declared", r.Error());
49 }
50 
TEST_F(AmberScriptParserTest,CopyInvalidOriginBufferName)51 TEST_F(AmberScriptParserTest, CopyInvalidOriginBufferName) {
52   std::string in = R"(
53 COPY 123)";
54 
55   Parser parser;
56   Result r = parser.Parse(in);
57   ASSERT_FALSE(r.IsSuccess());
58   EXPECT_EQ("2: invalid buffer name after COPY", r.Error());
59 }
60 
TEST_F(AmberScriptParserTest,CopyUndeclaredDestinationBuffer)61 TEST_F(AmberScriptParserTest, CopyUndeclaredDestinationBuffer) {
62   std::string in = R"(
63 BUFFER from FORMAT R32G32B32A32_SFLOAT
64 COPY from TO dest)";
65 
66   Parser parser;
67   Result r = parser.Parse(in);
68   ASSERT_FALSE(r.IsSuccess());
69   EXPECT_EQ("3: COPY destination buffer was not declared", r.Error());
70 }
71 
TEST_F(AmberScriptParserTest,CopyMissingOriginBuffer)72 TEST_F(AmberScriptParserTest, CopyMissingOriginBuffer) {
73   std::string in = R"(
74 COPY)";
75 
76   Parser parser;
77   Result r = parser.Parse(in);
78   ASSERT_FALSE(r.IsSuccess());
79   EXPECT_EQ("2: missing buffer name after COPY", r.Error());
80 }
81 
TEST_F(AmberScriptParserTest,CopyMissingDestinationBuffer)82 TEST_F(AmberScriptParserTest, CopyMissingDestinationBuffer) {
83   std::string in = R"(
84 BUFFER from FORMAT R32G32B32A32_SFLOAT
85 COPY from TO)";
86 
87   Parser parser;
88   Result r = parser.Parse(in);
89   ASSERT_FALSE(r.IsSuccess());
90   EXPECT_EQ("3: missing buffer name after TO", r.Error());
91 }
92 
TEST_F(AmberScriptParserTest,CopyToSameBuffer)93 TEST_F(AmberScriptParserTest, CopyToSameBuffer) {
94   std::string in = R"(
95 BUFFER from FORMAT R32G32B32A32_SFLOAT
96 COPY from TO from)";
97 
98   Parser parser;
99   Result r = parser.Parse(in);
100   ASSERT_FALSE(r.IsSuccess());
101   EXPECT_EQ("3: COPY origin and destination buffers are identical", r.Error());
102 }
103 
TEST_F(AmberScriptParserTest,CopyMissingToKeyword)104 TEST_F(AmberScriptParserTest, CopyMissingToKeyword) {
105   std::string in = R"(
106 BUFFER from FORMAT R32G32B32A32_SFLOAT
107 BUFFER dest FORMAT R32G32B32A32_SFLOAT
108 COPY from dest)";
109 
110   Parser parser;
111   Result r = parser.Parse(in);
112   ASSERT_FALSE(r.IsSuccess());
113   EXPECT_EQ("4: expected 'TO' after COPY and buffer name", r.Error());
114 }
115 
116 }  // namespace amberscript
117 }  // namespace amber
118