1 // Copyright 2020 The Tint 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 implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <memory>
16 #include <vector>
17
18 #include "gtest/gtest.h"
19 #include "src/ast/identifier_expression.h"
20 #include "src/ast/module.h"
21 #include "src/ast/unary_op_expression.h"
22 #include "src/writer/msl/generator_impl.h"
23
24 namespace tint {
25 namespace writer {
26 namespace msl {
27 namespace {
28
29 struct UnaryOpData {
30 const char* name;
31 ast::UnaryOp op;
32 };
operator <<(std::ostream & out,UnaryOpData data)33 inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) {
34 out << data.op;
35 return out;
36 }
37 using MslUnaryOpTest = testing::TestWithParam<UnaryOpData>;
TEST_P(MslUnaryOpTest,Emit)38 TEST_P(MslUnaryOpTest, Emit) {
39 auto params = GetParam();
40
41 auto expr = std::make_unique<ast::IdentifierExpression>("expr");
42 ast::UnaryOpExpression op(params.op, std::move(expr));
43
44 ast::Module m;
45 GeneratorImpl g(&m);
46 ASSERT_TRUE(g.EmitExpression(&op)) << g.error();
47 EXPECT_EQ(g.result(), std::string(params.name) + "(expr)");
48 }
49 INSTANTIATE_TEST_SUITE_P(MslGeneratorImplTest,
50 MslUnaryOpTest,
51 testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
52 UnaryOpData{"-",
53 ast::UnaryOp::kNegation}));
54
55 } // namespace
56 } // namespace msl
57 } // namespace writer
58 } // namespace tint
59