1 // Copyright 2020 Google LLC
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 "generator/internal/printer.h"
16 #include "google/cloud/internal/port_platform.h"
17 #include "absl/memory/memory.h"
18 #include <gmock/gmock.h>
19 
20 namespace google {
21 namespace cloud {
22 namespace generator_internal {
23 namespace {
24 
25 using testing::_;
26 using testing::Return;
27 
28 class MockGeneratorContext
29     : public google::protobuf::compiler::GeneratorContext {
30  public:
31   ~MockGeneratorContext() override = default;
32   MOCK_METHOD(google::protobuf::io::ZeroCopyOutputStream*, Open,
33               (std::string const&), (override));
34 };
35 
36 class MockZeroCopyOutputStream
37     : public google::protobuf::io::ZeroCopyOutputStream {
38  public:
39   ~MockZeroCopyOutputStream() override = default;
40   MOCK_METHOD(bool, Next, (void**, int*), (override));
41   MOCK_METHOD(void, BackUp, (int), (override));
42   MOCK_METHOD(int64_t, ByteCount, (), (const, override));
43   MOCK_METHOD(bool, WriteAliasedRaw, (void const*, int), (override));
44   MOCK_METHOD(bool, AllowsAliasing, (), (const, override));
45 };
46 
TEST(PrinterTest,PrintWithMap)47 TEST(PrinterTest, PrintWithMap) {
48   auto generator_context = absl::make_unique<MockGeneratorContext>();
49   auto output = absl::make_unique<MockZeroCopyOutputStream>();
50   EXPECT_CALL(*output, Next(_, _));
51   EXPECT_CALL(*generator_context, Open("foo"))
52       .WillOnce(Return(output.release()));
53   Printer printer(generator_context.get(), "foo");
54   std::map<std::string, std::string> vars;
55   vars["name"] = "Inigo Montoya";
56   printer.Print(42, "some_file", vars, "Hello! My name is $name$.\n");
57 }
58 
TEST(PrinterTest,PrintWithVariableArgs)59 TEST(PrinterTest, PrintWithVariableArgs) {
60   auto generator_context = absl::make_unique<MockGeneratorContext>();
61   auto output = absl::make_unique<MockZeroCopyOutputStream>();
62   EXPECT_CALL(*output, Next(_, _));
63   EXPECT_CALL(*generator_context, Open("foo"))
64       .WillOnce(Return(output.release()));
65   Printer printer(generator_context.get(), "foo");
66   printer.Print(42, "some_file", "Hello! My name is $name$.\n", "name",
67                 "Inigo Montoya");
68 }
69 
70 }  // namespace
71 }  // namespace generator_internal
72 }  // namespace cloud
73 }  // namespace google
74