Path: blob/main/libshaderc_util/src/message_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// Some of the tests here check code paths that are not checked by15// integration tests.16// Generally, these would be conditions not generated by the Glslang17// compiler. It's easier to write these unit tests than to inject18// a dependency on a fake compiler.19#include "libshaderc_util/message.h"2021#include <gtest/gtest.h>2223using shaderc_util::MessageType;24using shaderc_util::ParseGlslangOutput;25using shaderc_util::string_piece;2627namespace {2829TEST(ParseGlslangOutputTest, EmptyMessageBody) {30string_piece segment_number;31string_piece line_number;32string_piece rest;33EXPECT_EQ(MessageType::Unknown,34ParseGlslangOutput("WARNING: ", false, false, &segment_number,35&line_number, &rest));36EXPECT_EQ(MessageType::Unknown,37ParseGlslangOutput("ERROR: ", false, false, &segment_number,38&line_number, &rest));39}4041TEST(ParseGlslangOutputTest, GlobalError) {42string_piece segment_number;43string_piece line_number;44string_piece rest;45EXPECT_EQ(46MessageType::GlobalError,47ParseGlslangOutput("ERROR: too many functions: got 1666473 of them",48false, false, &segment_number, &line_number, &rest));49EXPECT_EQ("too many functions: got 1666473 of them", rest.str());5051EXPECT_EQ(52MessageType::GlobalError,53ParseGlslangOutput(54"ERROR: #version: versions before 150 do not allow a profile token",55false, false, &segment_number, &line_number, &rest));56EXPECT_EQ("#version: versions before 150 do not allow a profile token",57rest.str());58}5960TEST(ParseGlslangOutputTest, GlobalWarning) {61string_piece segment_number;62string_piece line_number;63string_piece rest;64EXPECT_EQ(MessageType::GlobalWarning,65ParseGlslangOutput("Warning, version 1000 is unknown.", false,66false, &segment_number, &line_number, &rest));67EXPECT_EQ("version 1000 is unknown.", rest.str());68}6970TEST(ParseGlslangOutputTest, InvalidSuffixAfterSegmentNumber) {71string_piece segment_number;72string_piece line_number;73string_piece rest;74EXPECT_EQ(MessageType::GlobalWarning,75ParseGlslangOutput("WARNING: 12a", false, false, &segment_number,76&line_number, &rest));77EXPECT_EQ(MessageType::GlobalError,78ParseGlslangOutput("WARNING: 12a", true, false, &segment_number,79&line_number, &rest));80EXPECT_EQ(MessageType::GlobalError,81ParseGlslangOutput("ERROR: 42!", false, false, &segment_number,82&line_number, &rest));83}8485TEST(ParseGlslangOutputTest, OnlyANumber) {86string_piece source_name;87string_piece line_number;88string_piece rest;89EXPECT_EQ(MessageType::GlobalWarning,90ParseGlslangOutput("WARNING: 12", false, false, &source_name,91&line_number, &rest));92EXPECT_TRUE(source_name.empty());93EXPECT_TRUE(line_number.empty());94EXPECT_EQ("12", rest.str());9596EXPECT_EQ(MessageType::GlobalError,97ParseGlslangOutput("WARNING: 12", true, false, &source_name,98&line_number, &rest));99EXPECT_TRUE(source_name.empty());100EXPECT_TRUE(line_number.empty());101EXPECT_EQ("12", rest.str());102103EXPECT_EQ(MessageType::GlobalError,104ParseGlslangOutput("ERROR: 42", false, false, &source_name,105&line_number, &rest));106EXPECT_TRUE(source_name.empty());107EXPECT_TRUE(line_number.empty());108EXPECT_EQ("42", rest.str());109}110111TEST(ParseGlslangOutputTest, InvalidSuffixAfterSegmentNumberColon) {112string_piece segment_number;113string_piece line_number;114string_piece rest;115EXPECT_EQ(MessageType::GlobalWarning,116ParseGlslangOutput("WARNING: 12:0", false, false, &segment_number,117&line_number, &rest));118EXPECT_EQ(MessageType::GlobalError,119ParseGlslangOutput("ERROR: 42:1234", false, false, &segment_number,120&line_number, &rest));121}122123TEST(ParseGlslangOutputTest, CompletelyUnrecognized) {124string_piece segment_number;125string_piece line_number;126string_piece rest;127EXPECT_EQ(MessageType::Unknown,128ParseGlslangOutput("hello world!", false, false, &segment_number,129&line_number, &rest));130}131132TEST(ParseGlslangOutputTest, LocationSpecification) {133string_piece segment_number;134string_piece line_number;135string_piece rest;136137// Glslang reading from strings can give string segment numbers as138// the filename part.139EXPECT_EQ(140MessageType::Error,141ParseGlslangOutput("ERROR: 0:2: '#' : invalid directive: foo", false,142false, &segment_number, &line_number, &rest));143EXPECT_EQ("0", segment_number.str());144EXPECT_EQ("2", line_number.str());145EXPECT_EQ("'#' : invalid directive: foo", rest.str());146147EXPECT_EQ(148MessageType::Warning,149ParseGlslangOutput("WARNING: 15:36: The following extension must be "150"enabled to use this feature:",151false, false, &segment_number, &line_number, &rest));152EXPECT_EQ("15", segment_number.str());153EXPECT_EQ("36", line_number.str());154EXPECT_EQ("The following extension must be enabled to use this feature:",155rest.str());156}157158TEST(ParseGlslangOutputTest, FileName_BaseAndExtension) {159string_piece source_name;160string_piece line_number;161string_piece rest;162163EXPECT_EQ(MessageType::Error,164ParseGlslangOutput("ERROR: shader.vert:5: something wrong", false,165false, &source_name, &line_number, &rest));166EXPECT_EQ("shader.vert", source_name.str());167EXPECT_EQ("5", line_number.str());168EXPECT_EQ("something wrong", rest.str());169}170171TEST(ParseGlslangOutputTest, FileName_BaseOnly) {172string_piece source_name;173string_piece line_number;174string_piece rest;175176EXPECT_EQ(MessageType::Warning,177ParseGlslangOutput("WARNING: file:42: something wrong", false,178false, &source_name, &line_number, &rest));179EXPECT_EQ("file", source_name.str());180EXPECT_EQ("42", line_number.str());181EXPECT_EQ("something wrong", rest.str());182}183184TEST(ParseGlslangOutputTest, FileName_HexNumber) {185string_piece source_name;186string_piece line_number;187string_piece rest;188189EXPECT_EQ(MessageType::Warning,190ParseGlslangOutput("WARNING: 0xdeedbeef:0: wa:ha:ha", false, false,191&source_name, &line_number, &rest));192EXPECT_EQ("0xdeedbeef", source_name.str());193EXPECT_EQ("0", line_number.str());194EXPECT_EQ("wa:ha:ha", rest.str());195}196197TEST(ParseGlslangOutputTest, FileName_ContainsColons) {198string_piece source_name;199string_piece line_number;200string_piece rest;201202EXPECT_EQ(MessageType::Warning,203ParseGlslangOutput("WARNING: foo:bar:0: wa:ha:ha", false, false,204&source_name, &line_number, &rest));205EXPECT_EQ("foo:bar", source_name.str());206EXPECT_EQ("0", line_number.str());207EXPECT_EQ("wa:ha:ha", rest.str());208}209210TEST(ParseGlslangOutputTest, NoFile) {211string_piece source_name;212string_piece line_number;213string_piece rest;214215EXPECT_EQ(MessageType::Warning,216ParseGlslangOutput("WARNING: :12: abc", false, false, &source_name,217&line_number, &rest));218EXPECT_EQ("", source_name.str());219EXPECT_EQ("12", line_number.str());220EXPECT_EQ("abc", rest.str());221}222223TEST(ParseGlslangOutputTest, NoLineNumber_InferredAsGlobalNoLocation) {224string_piece source_name;225string_piece line_number;226string_piece rest;227228// No solution since there is no room for digits.229EXPECT_EQ(MessageType::GlobalWarning,230ParseGlslangOutput("WARNING: foo:: abc", false, false,231&source_name, &line_number, &rest));232EXPECT_EQ("", source_name.str());233EXPECT_EQ("", line_number.str());234EXPECT_EQ("foo:: abc", rest.str());235}236237TEST(ParseGlslangOutputTest, NoSpaceAfterColon_InferredAsGlobalNoLocation) {238string_piece source_name;239string_piece line_number;240string_piece rest;241242// No solution since there is no space after the line-number-and-colon.243EXPECT_EQ(MessageType::GlobalWarning,244ParseGlslangOutput("WARNING: foo:12:abc", false, false,245&source_name, &line_number, &rest));246EXPECT_EQ("", source_name.str());247EXPECT_EQ("", line_number.str());248EXPECT_EQ("foo:12:abc", rest.str());249}250251TEST(ParseGlslangOutputTest, WindowsPath) {252string_piece source_name;253string_piece line_number;254string_piece rest;255256EXPECT_EQ(257MessageType::Error,258ParseGlslangOutput(R"(ERROR: C:\path\to\shader.glsl:5: something wrong)",259false, false, &source_name, &line_number, &rest));260EXPECT_EQ(R"(C:\path\to\shader.glsl)", source_name.str());261EXPECT_EQ("5", line_number.str());262EXPECT_EQ("something wrong", rest.str());263264EXPECT_EQ(265MessageType::Warning,266ParseGlslangOutput(R"(WARNING: \\path\without\drive.vert:42: BOOM!)",267false, false, &source_name, &line_number, &rest));268EXPECT_EQ(R"(\\path\without\drive.vert)", source_name.str());269EXPECT_EQ("42", line_number.str());270EXPECT_EQ("BOOM!", rest.str());271272EXPECT_EQ(MessageType::Warning,273ParseGlslangOutput(R"(WARNING: X:\123.456\789:0: wa:ha:ha)", false,274false, &source_name, &line_number, &rest));275EXPECT_EQ(R"(X:\123.456\789)", source_name.str());276EXPECT_EQ("0", line_number.str());277EXPECT_EQ("wa:ha:ha", rest.str());278}279280} // anonymous namespace281282283