| 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/testing/testing.h"
11 #include "impeller/compiler/switches.h"
12 #include "impeller/compiler/utilities.h"
13
14 namespace impeller {
15 namespace compiler {
16 namespace testing {
17
18 Switches MakeSwitchesDesktopGL(
19 std::initializer_list<const char*> additional_options = {}) {
20 std::vector<const char*> options = {"--opengl-desktop", "--input=input.vert",
21 "--sl=output.vert",
22 "--spirv=output.spirv"};
23 options.insert(options.end(), additional_options.begin(),
24 additional_options.end());
25
26 auto cl = fml::CommandLineFromIteratorsWithArgv0("impellerc", options.begin(),
27 options.end());
28 return Switches(cl);
29 }
30
31 TEST(SwitchesTest, DoesntMangleUnicodeIncludes) {
32 const char* directory_name = "test_shader_include_Ã�";
33 fml::CreateDirectory(flutter::testing::OpenFixturesDirectory(),
34 {directory_name}, fml::FilePermission::kRead);
35
36 auto include_path =
37 std::string(flutter::testing::GetFixturesPath()) + "/" + directory_name;
38 auto include_option = "--include=" + include_path;
39
40 Switches switches = MakeSwitchesDesktopGL({include_option.c_str()});
41
42 ASSERT_TRUE(switches.AreValid(std::cout));
43 ASSERT_EQ(switches.include_directories.size(), 1u);
44 ASSERT_NE(switches.include_directories[0].dir, nullptr);
45 ASSERT_EQ(switches.include_directories[0].name, include_path);
46 }
47
48 TEST(SwitchesTest, SourceLanguageDefaultsToGLSL) {
49 Switches switches = MakeSwitchesDesktopGL();
50 ASSERT_TRUE(switches.AreValid(std::cout));
51 ASSERT_EQ(switches.source_language, SourceLanguage::kGLSL);
52 }
53
54 TEST(SwitchesTest, SourceLanguageCanBeSetToHLSL) {
55 Switches switches = MakeSwitchesDesktopGL({"--source-language=hLsL"});
56 ASSERT_TRUE(switches.AreValid(std::cout));
57 ASSERT_EQ(switches.source_language, SourceLanguage::kHLSL);
58 }
59
60 TEST(SwitchesTest, DefaultEntryPointIsMain) {
61 Switches switches = MakeSwitchesDesktopGL({});
62 ASSERT_TRUE(switches.AreValid(std::cout));
63 ASSERT_EQ(switches.entry_point, "main");
64 }
65
66 TEST(SwitchesTest, EntryPointCanBeSetForHLSL) {
67 Switches switches = MakeSwitchesDesktopGL({"--entry-point=CustomEntryPoint"});
68 ASSERT_TRUE(switches.AreValid(std::cout));
69 ASSERT_EQ(switches.entry_point, "CustomEntryPoint");
70 }
71
72 TEST(SwitchesTEst, ConvertToEntrypointName) {
73 ASSERT_EQ(ConvertToEntrypointName("mandelbrot_unrolled"),
74 "mandelbrot_unrolled");
75 ASSERT_EQ(ConvertToEntrypointName("mandelbrot-unrolled"),
76 "mandelbrotunrolled");
77 ASSERT_EQ(ConvertToEntrypointName("7_"), "i_7_");
78 ASSERT_EQ(ConvertToEntrypointName("415"), "i_415");
79 ASSERT_EQ(ConvertToEntrypointName("#$%"), "i_");
80 ASSERT_EQ(ConvertToEntrypointName(""), "");
81 }
82
83 TEST(SwitchesTest, ShaderBundleModeValid) {
84 // Shader bundles process multiple shaders, and so the single-file input/spirv
85 // flags are not required.
86 std::vector<const char*> options = {
87 "--shader-bundle={}", "--sl=test.shaderbundle", "--runtime-stage-metal"};
88
89 auto cl = fml::CommandLineFromIteratorsWithArgv0("impellerc", options.begin(),
90 options.end());
91 Switches switches(cl);
92 ASSERT_TRUE(switches.AreValid(std::cout));
93 ASSERT_EQ(switches.shader_bundle, "{}");
94 }
95
96 } // namespace testing
97 } // namespace compiler
98 } // namespace impeller
impeller::compiler::testing::TEST
TEST(CompilerTest, ShaderKindMatchingIsSuccessful)
Definition: compiler_unittests.cc:18
impeller::compiler::ConvertToEntrypointName
std::string ConvertToEntrypointName(std::string_view string)
Ensure that the entrypoint name is a valid identifier in the target language.
Definition: utilities.cc:69
switches.h
impeller::compiler::SourceLanguage::kGLSL
@ kGLSL
impeller::compiler::SourceLanguage::kHLSL
@ kHLSL
utilities.h
impeller::compiler::testing::MakeSwitchesDesktopGL
Switches MakeSwitchesDesktopGL(std::initializer_list< const char * > additional_options={})
Definition: switches_unittests.cc:18
impeller::compiler::Switches::source_language
SourceLanguage source_language
Definition: switches.h:40
impeller::compiler::Switches::entry_point
std::string entry_point
Definition: switches.h:43
impeller::compiler::Switches
Definition: switches.h:21
impeller::compiler::Switches::include_directories
std::vector< IncludeDir > include_directories
Definition: switches.h:24
impeller
Definition: aiks_context.cc:10
impeller::compiler::Switches::shader_bundle
std::string shader_bundle
Definition: switches.h:32
impeller::compiler::Switches::AreValid
bool AreValid(std::ostream &explain) const
Definition: switches.cc:239