Flutter Engine
The Flutter Engine
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"
13
14namespace impeller {
15namespace compiler {
16namespace testing {
17
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
31TEST(SwitchesTest, DoesntMangleUnicodeIncludes) {
32 const char* directory_name = "test_shader_include_�";
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
48TEST(SwitchesTest, SourceLanguageDefaultsToGLSL) {
50 ASSERT_TRUE(switches.AreValid(std::cout));
51 ASSERT_EQ(switches.source_language, SourceLanguage::kGLSL);
52}
53
54TEST(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
60TEST(SwitchesTest, DefaultEntryPointIsMain) {
61 Switches switches = MakeSwitchesDesktopGL({});
62 ASSERT_TRUE(switches.AreValid(std::cout));
63 ASSERT_EQ(switches.entry_point, "main");
64}
65
66TEST(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
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
83TEST(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
const char * options
SourceLanguage source_language
Definition: switches.h:40
bool AreValid(std::ostream &explain) const
Definition: switches.cc:239
std::vector< IncludeDir > include_directories
Definition: switches.h:24
const char * GetFixturesPath()
Returns the directory containing the test fixture for the target if this target has fixtures configur...
fml::UniqueFD OpenFixturesDirectory()
Opens the fixtures directory for the unit-test harness.
Definition: testing.cc:23
static fml::UniqueFD CreateDirectory(const fml::UniqueFD &base_directory, const std::vector< std::string > &components, FilePermission permission, size_t index)
Definition: file.cc:12
CommandLine CommandLineFromIteratorsWithArgv0(const std::string &argv0, InputIterator first, InputIterator last)
Definition: command_line.h:214
TEST(CompilerTest, ShaderKindMatchingIsSuccessful)
Switches MakeSwitchesDesktopGL(std::initializer_list< const char * > additional_options={})
std::string ConvertToEntrypointName(std::string_view string)
Ensure that the entrypoint name is a valid identifier in the target language.
Definition: utilities.cc:69