Flutter Impeller
switches_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 <initializer_list>
6 #include <vector>
7 
8 #include "flutter/fml/command_line.h"
9 #include "flutter/fml/file.h"
10 #include "flutter/fml/string_conversion.h"
11 #include "flutter/testing/testing.h"
15 
16 namespace impeller {
17 namespace compiler {
18 namespace testing {
19 
21  std::initializer_list<const char*> additional_options = {}) {
22  std::vector<const char*> options = {"--opengl-desktop", "--input=input.vert",
23  "--sl=output.vert",
24  "--spirv=output.spirv"};
25  options.insert(options.end(), additional_options.begin(),
26  additional_options.end());
27 
28  auto cl = fml::CommandLineFromIteratorsWithArgv0("impellerc", options.begin(),
29  options.end());
30  return Switches(cl);
31 }
32 
33 TEST(SwitchesTest, DoesntMangleUnicodeIncludes) {
34  const char* directory_name = "test_shader_include_Ã�";
35  fml::CreateDirectory(flutter::testing::OpenFixturesDirectory(),
36  {directory_name}, fml::FilePermission::kRead);
37 
38  auto include_path =
39  std::string(flutter::testing::GetFixturesPath()) + "/" + directory_name;
40  auto include_option = "--include=" + include_path;
41 
42  Switches switches = MakeSwitchesDesktopGL({include_option.c_str()});
43 
44  ASSERT_TRUE(switches.AreValid(std::cout));
45  ASSERT_EQ(switches.include_directories.size(), 1u);
46  ASSERT_NE(switches.include_directories[0].dir, nullptr);
47  ASSERT_EQ(switches.include_directories[0].name, include_path);
48 }
49 
50 TEST(SwitchesTest, SourceLanguageDefaultsToGLSL) {
51  Switches switches = MakeSwitchesDesktopGL();
52  ASSERT_TRUE(switches.AreValid(std::cout));
53  ASSERT_EQ(switches.source_language, SourceLanguage::kGLSL);
54 }
55 
56 TEST(SwitchesTest, SourceLanguageCanBeSetToHLSL) {
57  Switches switches = MakeSwitchesDesktopGL({"--source-language=hLsL"});
58  ASSERT_TRUE(switches.AreValid(std::cout));
59  ASSERT_EQ(switches.source_language, SourceLanguage::kHLSL);
60 }
61 
62 TEST(SwitchesTest, DefaultEntryPointIsMain) {
63  Switches switches = MakeSwitchesDesktopGL({});
64  ASSERT_TRUE(switches.AreValid(std::cout));
65  ASSERT_EQ(switches.entry_point, "main");
66 }
67 
68 TEST(SwitchesTest, EntryPointCanBeSetForHLSL) {
69  Switches switches = MakeSwitchesDesktopGL({"--entry-point=CustomEntryPoint"});
70  ASSERT_TRUE(switches.AreValid(std::cout));
71  ASSERT_EQ(switches.entry_point, "CustomEntryPoint");
72 }
73 
74 TEST(SwitchesTEst, ConvertToEntrypointName) {
75  ASSERT_EQ(ConvertToEntrypointName("mandelbrot_unrolled"),
76  "mandelbrot_unrolled");
77  ASSERT_EQ(ConvertToEntrypointName("mandelbrot-unrolled"),
78  "mandelbrotunrolled");
79  ASSERT_EQ(ConvertToEntrypointName("7_"), "i_7_");
80  ASSERT_EQ(ConvertToEntrypointName("415"), "i_415");
81  ASSERT_EQ(ConvertToEntrypointName("#$%"), "i_");
82  ASSERT_EQ(ConvertToEntrypointName(""), "");
83 }
84 
85 TEST(SwitchesTest, ShaderBundleModeValid) {
86  // Shader bundles process multiple shaders, and so the single-file input/spirv
87  // flags are not required.
88  std::vector<const char*> options = {
89  "--shader-bundle={}", "--sl=test.shaderbundle", "--runtime-stage-metal"};
90 
91  auto cl = fml::CommandLineFromIteratorsWithArgv0("impellerc", options.begin(),
92  options.end());
93  Switches switches(cl);
94  ASSERT_TRUE(switches.AreValid(std::cout));
95  ASSERT_EQ(switches.shader_bundle, "{}");
96 }
97 
98 TEST(SwitchesTest, EntryPointPrefixIsApplied) {
99  Switches switches =
100  MakeSwitchesDesktopGL({"--entry-point-prefix=my_prefix_"});
101  ASSERT_TRUE(switches.AreValid(std::cout));
102  EXPECT_EQ(switches.entry_point_prefix, "my_prefix_");
103 
104  switches.source_file_name = "test.frag";
105  auto options = switches.CreateSourceOptions();
106  EXPECT_EQ(options.entry_point_name, "my_prefix_test_fragment_main");
107 }
108 
109 TEST(SwitchesTest, CommandLinePathUtf8) {
110  std::u16string filename = u"test\u1234";
111  std::string input_flag = "--input=" + fml::Utf16ToUtf8(filename);
112  Switches switches = MakeSwitchesDesktopGL({input_flag.c_str()});
113  ASSERT_TRUE(switches.AreValid(std::cout));
114  ASSERT_EQ(switches.source_file_name, filename);
115 }
116 
117 } // namespace testing
118 } // namespace compiler
119 } // namespace impeller
std::string entry_point_prefix
Definition: switches.h:45
SourceLanguage source_language
Definition: switches.h:41
bool AreValid(std::ostream &explain) const
Definition: switches.cc:259
SourceOptions CreateSourceOptions() const
Definition: switches.cc:319
std::vector< IncludeDir > include_directories
Definition: switches.h:25
std::filesystem::path source_file_name
Definition: switches.h:26
Switches MakeSwitchesDesktopGL(std::initializer_list< const char * > additional_options={})
TEST(CompilerTest, Defines)
std::string ConvertToEntrypointName(std::string_view string)
Ensure that the entrypoint name is a valid identifier in the target language.
Definition: utilities.cc:68