Path: blob/main/libshaderc_util/src/io_shaderc_test.cc
1560 views
// Copyright 2015 The Shaderc Authors. All rights reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#include "libshaderc_util/io_shaderc.h"1516#include <gmock/gmock.h>1718#include <fstream>1920namespace {2122using shaderc_util::GetBaseFileName;23using shaderc_util::GetOutputStream;24using shaderc_util::IsAbsolutePath;25using shaderc_util::ReadFile;26using shaderc_util::WriteFile;27using testing::Eq;28using testing::HasSubstr;2930std::string ToString(const std::vector<char>& v) {31return std::string(v.data(), v.size());32}3334class ReadFileTest : public testing::Test {35protected:36// A vector to pass to ReadFile.37std::vector<char> read_data;38};3940TEST(IsAbsolutePathTest, Linux) {41EXPECT_FALSE(IsAbsolutePath(""));42EXPECT_TRUE(IsAbsolutePath("/"));43EXPECT_FALSE(IsAbsolutePath("."));44EXPECT_FALSE(IsAbsolutePath(".."));45EXPECT_TRUE(IsAbsolutePath("/bin/echo"));46EXPECT_TRUE(IsAbsolutePath("//etc/shadow"));47EXPECT_TRUE(IsAbsolutePath("/../../../lib"));48EXPECT_FALSE(IsAbsolutePath("./something"));49EXPECT_FALSE(IsAbsolutePath("input"));50EXPECT_FALSE(IsAbsolutePath("../test"));51EXPECT_FALSE(IsAbsolutePath(" /abc"));52EXPECT_TRUE(IsAbsolutePath("/abc def/ttt"));53}5455TEST(IsAbsolutePathTest, Windows) {56EXPECT_TRUE(IsAbsolutePath(R"(\\Server1000\superuser\file)"));57EXPECT_TRUE(IsAbsolutePath(R"(\\zzzz 1000\user with space\file with space)"));58EXPECT_TRUE(59IsAbsolutePath(R"(C:\Program Files (x86)\Windows Folder\shader.glsl)"));60EXPECT_FALSE(IsAbsolutePath(R"(third_party\gmock)"));61EXPECT_FALSE(IsAbsolutePath(R"(C:..\File.txt)"));62}6364TEST(GetBaseFileName, Linux) {65EXPECT_EQ("", GetBaseFileName(""));66EXPECT_EQ("", GetBaseFileName("/"));67EXPECT_EQ("", GetBaseFileName("."));68EXPECT_EQ("", GetBaseFileName(".."));69EXPECT_EQ("echo", GetBaseFileName("/bin/echo"));70EXPECT_EQ("shadow", GetBaseFileName("//etc/shadow"));71EXPECT_EQ("lib", GetBaseFileName("/../../../lib"));72EXPECT_EQ("something", GetBaseFileName("./something"));73EXPECT_EQ("input", GetBaseFileName("input"));74EXPECT_EQ("test", GetBaseFileName("../test"));75EXPECT_EQ("abc", GetBaseFileName(" /abc"));76EXPECT_EQ("ttt", GetBaseFileName("/abc def/ttt"));77}7879TEST(GetBaseFileName, Windows) {80EXPECT_EQ("file", GetBaseFileName(R"(\\Server1000\superuser\file)"));81EXPECT_EQ("file with space",82GetBaseFileName(R"(\\zzzz 1000\user with space\file with space)"));83EXPECT_EQ(84"shader.glsl",85GetBaseFileName(R"(C:\Program Files (x86)\Windows Folder\shader.glsl)"));86EXPECT_EQ("gmock", GetBaseFileName(R"(third_party\gmock)"));87EXPECT_EQ("File.txt", GetBaseFileName(R"(C:..\File.txt)"));88}8990TEST_F(ReadFileTest, CorrectContent) {91ASSERT_TRUE(ReadFile("include_file.1", &read_data));92EXPECT_EQ("The quick brown fox jumps over a lazy dog.", ToString(read_data));93}9495TEST_F(ReadFileTest, EmptyContent) {96ASSERT_TRUE(ReadFile("dir/subdir/include_file.2", &read_data));97EXPECT_TRUE(read_data.empty());98}99100TEST_F(ReadFileTest, FileNotFound) {101EXPECT_FALSE(ReadFile("garbage garbage vjoiarhiupo hrfewi", &read_data));102}103104TEST_F(ReadFileTest, EmptyFilename) { EXPECT_FALSE(ReadFile("", &read_data)); }105106TEST(WriteFiletest, BadStream) {107std::ofstream fstream;108std::ostringstream err;109std::ostream* output_stream = GetOutputStream(110"/this/should/not/be/writable/asdfasdfasdfasdf", &fstream, &err);111EXPECT_EQ(nullptr, output_stream);112EXPECT_TRUE(fstream.fail());113EXPECT_EQ(nullptr, output_stream);114EXPECT_THAT(err.str(), HasSubstr("cannot open output file"));115}116117TEST(WriteFileTest, Roundtrip) {118const std::string content = "random content 12345";119const std::string filename = "WriteFileTestOutput.tmp";120std::ofstream fstream;121std::ostringstream err;122std::ostream* output_stream = GetOutputStream(filename, &fstream, &err);123ASSERT_EQ(output_stream, &fstream);124EXPECT_THAT(err.str(), Eq(""));125ASSERT_TRUE(WriteFile(output_stream, content));126std::vector<char> read_data;127ASSERT_TRUE(ReadFile(filename, &read_data));128EXPECT_EQ(content, ToString(read_data));129}130131TEST(OutputStreamTest, Stdout) {132std::ofstream fstream;133std::ostringstream err;134std::ostream* output_stream = GetOutputStream("-", &fstream, &err);135EXPECT_EQ(&std::cout, output_stream);136EXPECT_THAT(err.str(), Eq(""));137}138} // anonymous namespace139140141