1 // Copyright 2014 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chromeos-dbus-bindings/method_name_generator.h" 6 7 #include <string> 8 #include <vector> 9 10 #include <base/files/file_path.h> 11 #include <base/files/file_util.h> 12 #include <base/files/scoped_temp_dir.h> 13 #include <gtest/gtest.h> 14 15 #include "chromeos-dbus-bindings/interface.h" 16 17 using std::string; 18 using testing::Test; 19 20 namespace chromeos_dbus_bindings { 21 22 namespace { 23 24 const char kMethodName0[] = "Zircon"; 25 const char kMethodName1[] = "Encrusted"; 26 const char kMethodName2[] = "Tweezers"; 27 const char kExpectedOutput[] = R"( 28 namespace MyInterface { 29 const char kZirconMethod[] = "Zircon"; 30 const char kEncrustedMethod[] = "Encrusted"; 31 const char kTweezersMethod[] = "Tweezers"; 32 } // namespace MyInterface 33 )"; 34 35 } // namespace 36 37 class MethodNameGeneratorTest : public Test { 38 public: 39 void SetUp() override { 40 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 41 } 42 43 protected: 44 base::FilePath CreateInputFile(const string& contents) { 45 base::FilePath path; 46 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path)); 47 int written = base::WriteFile(path, contents.c_str(), contents.size()); 48 EXPECT_EQ(contents.size(), static_cast<size_t>(written)); 49 return path; 50 } 51 52 base::ScopedTempDir temp_dir_; 53 }; 54 55 TEST_F(MethodNameGeneratorTest, GnerateMethodNames) { 56 Interface interface; 57 interface.name = "MyInterface"; 58 interface.methods.emplace_back(kMethodName0); 59 interface.methods.emplace_back(kMethodName1); 60 interface.methods.emplace_back(kMethodName2); 61 base::FilePath output_path = temp_dir_.path().Append("output.h"); 62 EXPECT_TRUE(MethodNameGenerator::GenerateMethodNames({interface}, 63 output_path)); 64 string contents; 65 EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); 66 EXPECT_STREQ(kExpectedOutput, contents.c_str()); 67 } 68 69 } // namespace chromeos_dbus_bindings 70