1 // Copyright 2016 The Chromium 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 "gn/visual_studio_utils.h"
6
7 #include "base/strings/string_util.h"
8 #include "util/test/test.h"
9
TEST(VisualStudioUtils,MakeGuid)10 TEST(VisualStudioUtils, MakeGuid) {
11 std::string pattern = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}";
12 std::string guid = MakeGuid(__FILE__, "foo");
13 ASSERT_EQ(pattern.size(), guid.size());
14 for (size_t i = 0; i < pattern.size(); ++i) {
15 if (pattern[i] == 'x')
16 ASSERT_TRUE(base::IsAsciiAlpha(guid[i]) || base::IsAsciiDigit(guid[i]));
17 else
18 ASSERT_EQ(pattern[i], guid[i]);
19 }
20
21 // Calling function again should produce the same GUID.
22 ASSERT_EQ(guid, MakeGuid(__FILE__, "foo"));
23
24 // GUIDs should be different if path or seed is different.
25 ASSERT_NE(guid, MakeGuid(std::string(__FILE__) + ".txt", "foo"));
26 ASSERT_NE(guid, MakeGuid(__FILE__, "bar"));
27 }
28
TEST(VisualStudioUtils,ParseCompilerOption)29 TEST(VisualStudioUtils, ParseCompilerOption) {
30 CompilerOptions options;
31 ParseCompilerOption("/FIinclude.h", &options);
32 ParseCompilerOption("/FIC:/path/file.h", &options);
33 ASSERT_EQ("include.h;C:/path/file.h;", options.forced_include_files);
34
35 CHECK(options.buffer_security_check.empty());
36 ParseCompilerOption("/GS", &options);
37 ASSERT_EQ("true", options.buffer_security_check);
38 ParseCompilerOption("/GS-", &options);
39 ASSERT_EQ("false", options.buffer_security_check);
40
41 CHECK(options.runtime_library.empty());
42 ParseCompilerOption("/MD", &options);
43 ASSERT_EQ("MultiThreadedDLL", options.runtime_library);
44 ParseCompilerOption("/MDd", &options);
45 ASSERT_EQ("MultiThreadedDebugDLL", options.runtime_library);
46 ParseCompilerOption("/MT", &options);
47 ASSERT_EQ("MultiThreaded", options.runtime_library);
48 ParseCompilerOption("/MTd", &options);
49 ASSERT_EQ("MultiThreadedDebug", options.runtime_library);
50
51 CHECK(options.optimization.empty());
52 ParseCompilerOption("/O1", &options);
53 ASSERT_EQ("MinSpace", options.optimization);
54 ParseCompilerOption("/O2", &options);
55 ASSERT_EQ("MaxSpeed", options.optimization);
56 ParseCompilerOption("/Od", &options);
57 ASSERT_EQ("Disabled", options.optimization);
58 ParseCompilerOption("/Ox", &options);
59 ASSERT_EQ("Full", options.optimization);
60
61 CHECK(options.additional_options.empty());
62 ParseCompilerOption("/TC", &options);
63 ASSERT_TRUE(options.additional_options.empty());
64 ParseCompilerOption("/TP", &options);
65 ASSERT_TRUE(options.additional_options.empty());
66
67 CHECK(options.warning_level.empty());
68 ParseCompilerOption("/W0", &options);
69 ASSERT_EQ("Level0", options.warning_level);
70 ParseCompilerOption("/W1", &options);
71 ASSERT_EQ("Level1", options.warning_level);
72 ParseCompilerOption("/W2", &options);
73 ASSERT_EQ("Level2", options.warning_level);
74 ParseCompilerOption("/W3", &options);
75 ASSERT_EQ("Level3", options.warning_level);
76 ParseCompilerOption("/W4", &options);
77 ASSERT_EQ("Level4", options.warning_level);
78
79 CHECK(options.treat_warning_as_error.empty());
80 ParseCompilerOption("/WX", &options);
81 ASSERT_EQ("true", options.treat_warning_as_error);
82
83 CHECK(options.disable_specific_warnings.empty());
84 ParseCompilerOption("/wd1234", &options);
85 ParseCompilerOption("/wd56", &options);
86 ASSERT_EQ("1234;56;", options.disable_specific_warnings);
87
88 CHECK(options.additional_options.empty());
89 ParseCompilerOption("/MP", &options);
90 ParseCompilerOption("/bigobj", &options);
91 ParseCompilerOption("/Zc:sizedDealloc", &options);
92 ASSERT_EQ("/MP /bigobj /Zc:sizedDealloc ", options.additional_options);
93 }
94
TEST(VisualStudioUtils,ParseLinkerOption)95 TEST(VisualStudioUtils, ParseLinkerOption) {
96 LinkerOptions options;
97 ParseLinkerOption("/SUBSYSTEM:CONSOLE,5.02h", &options);
98 ASSERT_EQ("CONSOLE", options.subsystem);
99
100 ParseLinkerOption("/SUBSYSTEM:WINDOWS", &options);
101 ASSERT_EQ("WINDOWS", options.subsystem);
102 }
103