| 1 |
/* |
| 2 |
* Copyright 2018 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <gtest/gtest.h> |
| 18 |
|
| 19 |
#include "GlslMinify.h" |
| 20 |
|
| 21 |
#include <string> |
| 22 |
|
| 23 |
using std::string; |
| 24 |
|
| 25 |
using namespace glslminifier; |
| 26 |
|
| 27 |
class GlslminifierTest : public testing::Test {}; |
| 28 |
|
| 29 |
TEST_F(GlslminifierTest, EmptyString) { |
| 30 |
EXPECT_EQ(minifyGlsl(""), ""); |
| 31 |
} |
| 32 |
|
| 33 |
TEST_F(GlslminifierTest, GlslNoChanges) { |
| 34 |
std::string glsl = R"glsl("void main() { gl_FragColor = vec4(1.0); })glsl"; |
| 35 |
EXPECT_EQ(minifyGlsl(glsl, GlslMinifyOptions::ALL), glsl); |
| 36 |
} |
| 37 |
|
| 38 |
TEST_F(GlslminifierTest, RemoveSlashSlashComments) { |
| 39 |
std::string glsl = R"glsl( |
| 40 |
#version 330 |
| 41 |
void main() {// This is a comment at the end of a line. |
| 42 |
// This is a comment, and this whole line should be removed. |
| 43 |
gl_FragColor = vec4(2.0 / 4.0, 0.0, 0.0, 1.0); |
| 44 |
// Another comment. |
| 45 |
} |
| 46 |
// This is a comment without a trailing newline.)glsl"; |
| 47 |
std::string expected = R"glsl( |
| 48 |
#version 330 |
| 49 |
void main() { |
| 50 |
|
| 51 |
gl_FragColor = vec4(2.0 / 4.0, 0.0, 0.0, 1.0); |
| 52 |
|
| 53 |
} |
| 54 |
)glsl"; |
| 55 |
EXPECT_EQ(minifyGlsl(glsl, GlslMinifyOptions::STRIP_COMMENTS), expected); |
| 56 |
} |
| 57 |
|
| 58 |
TEST_F(GlslminifierTest, RemoveStarComments) { |
| 59 |
std::string glsl = R"glsl( |
| 60 |
#version 330 |
| 61 |
void main() { /* this is a comment block at the end of a line */ |
| 62 |
/* this is a comment block |
| 63 |
that spans |
| 64 |
multiple lines */ |
| 65 |
gl_FragColor = vec4(2.0 / 4.0, 0.0, 0.0, 1.0); |
| 66 |
/* this is another comment block */ |
| 67 |
} |
| 68 |
)glsl"; |
| 69 |
std::string expected = R"glsl( |
| 70 |
#version 330 |
| 71 |
void main() { |
| 72 |
|
| 73 |
gl_FragColor = vec4(2.0 / 4.0, 0.0, 0.0, 1.0); |
| 74 |
|
| 75 |
} |
| 76 |
)glsl"; |
| 77 |
EXPECT_EQ(minifyGlsl(glsl, GlslMinifyOptions::STRIP_COMMENTS), expected); |
| 78 |
} |
| 79 |
|
| 80 |
TEST_F(GlslminifierTest, RemoveBlankLines) { |
| 81 |
std::string glsl = R"glsl( |
| 82 |
#version 330 |
| 83 |
|
| 84 |
|
| 85 |
void main() { |
| 86 |
|
| 87 |
gl_FragColor = vec4(2.0 / 4.0, 0.0, 0.0, 1.0); |
| 88 |
|
| 89 |
} |
| 90 |
)glsl"; |
| 91 |
std::string expected = R"glsl( |
| 92 |
#version 330 |
| 93 |
void main() { |
| 94 |
gl_FragColor = vec4(2.0 / 4.0, 0.0, 0.0, 1.0); |
| 95 |
} |
| 96 |
)glsl"; |
| 97 |
EXPECT_EQ(minifyGlsl(glsl, GlslMinifyOptions::STRIP_EMPTY_LINES), expected); |
| 98 |
} |
| 99 |
|
| 100 |
TEST_F(GlslminifierTest, RemoveBlankLinesWindows) { |
| 101 |
std::string glsl = "line one\r\n\r\n\r\nline two"; |
| 102 |
std::string expected = "line one\r\nline two"; |
| 103 |
EXPECT_EQ(minifyGlsl(glsl, GlslMinifyOptions::STRIP_EMPTY_LINES), expected); |
| 104 |
} |
| 105 |
|
| 106 |
TEST_F(GlslminifierTest, RemoveIndentation) { |
| 107 |
std::string glsl = R"glsl( #version 330 |
| 108 |
void main() { |
| 109 |
gl_FragColor = vec4(2.0 / 4.0, 0.0, 0.0, 1.0); |
| 110 |
} |
| 111 |
)glsl"; |
| 112 |
std::string expected = R"glsl(#version 330 |
| 113 |
void main() { |
| 114 |
gl_FragColor = vec4(2.0 / 4.0, 0.0, 0.0, 1.0); |
| 115 |
} |
| 116 |
)glsl"; |
| 117 |
EXPECT_EQ(minifyGlsl(glsl, GlslMinifyOptions::STRIP_INDENTATION), expected); |
| 118 |
} |
| 119 |
|
| 120 |
TEST_F(GlslminifierTest, TrailingNewline) { |
| 121 |
// Ensure trailing newlines are acceptable when stripping identation. |
| 122 |
std::string glsl = R"glsl(trailing newline; |
| 123 |
)glsl"; |
| 124 |
EXPECT_EQ(minifyGlsl(glsl, GlslMinifyOptions::STRIP_INDENTATION), glsl); |
| 125 |
} |
| 126 |
|
| 127 |
int main(int argc, char** argv) { |
| 128 |
::testing::InitGoogleTest(&argc, argv); |
| 129 |
return RUN_ALL_TESTS(); |
| 130 |
} |