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