OLD | NEW |
(Empty) | |
| 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 "chrome/install_static/install_util.h" |
| 6 |
| 7 #include "testing/gmock/include/gmock/gmock.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 using ::testing::ElementsAre; |
| 11 |
| 12 namespace install_static { |
| 13 |
| 14 // Tests the MatchPattern function in the install_static library. |
| 15 TEST(InstallStaticTest, MatchPattern) { |
| 16 EXPECT_TRUE(MatchPattern(L"", L"")); |
| 17 EXPECT_TRUE(MatchPattern(L"", L"*")); |
| 18 EXPECT_FALSE(MatchPattern(L"", L"*a")); |
| 19 EXPECT_FALSE(MatchPattern(L"", L"abc")); |
| 20 EXPECT_TRUE(MatchPattern(L"Hello1234", L"He??o*1*")); |
| 21 EXPECT_TRUE(MatchPattern(L"Foo", L"F*?")); |
| 22 EXPECT_TRUE(MatchPattern(L"Foo", L"F*")); |
| 23 EXPECT_FALSE(MatchPattern(L"Foo", L"F*b")); |
| 24 EXPECT_TRUE(MatchPattern(L"abcd", L"*c*d")); |
| 25 EXPECT_TRUE(MatchPattern(L"abcd", L"*?c*d")); |
| 26 EXPECT_FALSE(MatchPattern(L"abcd", L"abcd*efgh")); |
| 27 EXPECT_TRUE(MatchPattern(L"foobarabc", L"*bar*")); |
| 28 } |
| 29 |
| 30 // Tests the TokenizeString function in the install_static library. |
| 31 TEST(InstallStaticTest, TokenizeString) { |
| 32 // Test if the string is tokenized correctly with all tokens stripped of |
| 33 // leading and trailing spaces. |
| 34 std::vector<std::string> results = |
| 35 TokenizeString("un |deux\t|trois\n|quatre", '|', true); |
| 36 ASSERT_EQ(4u, results.size()); |
| 37 EXPECT_THAT(results, ElementsAre("un", "deux", "trois", "quatre")); |
| 38 |
| 39 // Test if the string is tokenized correctly with all tokens having |
| 40 // leading and trailing spaces intact. |
| 41 results = TokenizeString("un |deux\t|trois\n|quatre", '|', false); |
| 42 ASSERT_EQ(4u, results.size()); |
| 43 EXPECT_THAT(results, ElementsAre("un ", "deux\t", "trois\n", "quatre")); |
| 44 |
| 45 // Test if tokenize returns the original string if a string containing only |
| 46 // one token and no delimiters is passed. |
| 47 results = TokenizeString("un |deux\t|trois\n|quatre", '!', false); |
| 48 ASSERT_EQ(1u, results.size()); |
| 49 ASSERT_EQ(results[0], "un |deux\t|trois\n|quatre"); |
| 50 |
| 51 // Test if tokenize handles a space character as delimiter. |
| 52 results = TokenizeString("foo bar bleh blah boo", ' ', false); |
| 53 ASSERT_EQ(5u, results.size()); |
| 54 EXPECT_THAT(results, ElementsAre("foo", "bar", "bleh", "blah", "boo")); |
| 55 |
| 56 // Test string with only delimiters. |
| 57 results = TokenizeString("||||", '|', false); |
| 58 ASSERT_EQ(4u, results.size()); |
| 59 EXPECT_THAT(results, ElementsAre("", "", "", "")); |
| 60 |
| 61 // Test string with spaces separated by delimiters. |
| 62 results = TokenizeString(" | | | |", '|', false); |
| 63 ASSERT_EQ(4u, results.size()); |
| 64 EXPECT_THAT(results, ElementsAre(" ", " ", " ", " ")); |
| 65 |
| 66 results = TokenizeString("one|two||four", '|', false); |
| 67 ASSERT_EQ(4u, results.size()); |
| 68 EXPECT_THAT(results, ElementsAre("one", "two", "", "four")); |
| 69 } |
| 70 |
| 71 // Tests the CompareVersionString function in the install_static library. |
| 72 TEST(InstallStaticTest, CompareVersions) { |
| 73 // Case 1. Invalid versions. |
| 74 int result = 0; |
| 75 EXPECT_FALSE(CompareVersionStrings("", "", &result)); |
| 76 EXPECT_FALSE(CompareVersionStrings("0.0.0.0", "A.B.C.D", &result)); |
| 77 EXPECT_FALSE(CompareVersionStrings("A.0.0.0", "0.0.0.0", &result)); |
| 78 |
| 79 // Case 2. Equal versions. |
| 80 EXPECT_TRUE(CompareVersionStrings("0.0.0.0", "0.0.0.0", &result)); |
| 81 EXPECT_EQ(0, result); |
| 82 |
| 83 // Case 3. Version1 > Version 2. |
| 84 EXPECT_TRUE(CompareVersionStrings("1.0.0.0", "0.0.0.0", &result)); |
| 85 EXPECT_EQ(1, result); |
| 86 |
| 87 // Case 4. Version1 < Version 2. |
| 88 EXPECT_TRUE(CompareVersionStrings("0.0.0.0", "1.0.0.0", &result)); |
| 89 EXPECT_EQ(-1, result); |
| 90 |
| 91 // Case 5. Version1 < Version 2. |
| 92 EXPECT_TRUE(CompareVersionStrings("0.0", "0.0.1.0", &result)); |
| 93 EXPECT_EQ(-1, result); |
| 94 |
| 95 // Case 6. Version1 > Version 2. |
| 96 EXPECT_TRUE(CompareVersionStrings("0.0.0.2", "0.0", &result)); |
| 97 EXPECT_EQ(1, result); |
| 98 |
| 99 // Case 7. Version1 > Version 2. |
| 100 EXPECT_TRUE(CompareVersionStrings("1.1.1.2", "1.1.1.1", &result)); |
| 101 EXPECT_EQ(1, result); |
| 102 |
| 103 // Case 8. Version1 < Version2 |
| 104 EXPECT_TRUE(CompareVersionStrings("0.0.0.2", "0.0.0.3", &result)); |
| 105 EXPECT_EQ(-1, result); |
| 106 |
| 107 // Case 9. Version1 > Version2 |
| 108 EXPECT_TRUE(CompareVersionStrings("0.0.0.4", "0.0.0.3", &result)); |
| 109 EXPECT_EQ(1, result); |
| 110 |
| 111 // Case 10. Version1 > Version2. Multiple digit numbers. |
| 112 EXPECT_TRUE(CompareVersionStrings("0.0.12.1", "0.0.10.3", &result)); |
| 113 EXPECT_EQ(1, result); |
| 114 |
| 115 // Case 11. Version1 < Version2. Multiple digit number. |
| 116 EXPECT_TRUE(CompareVersionStrings("10.11.12.13", "12.11.12.13", &result)); |
| 117 EXPECT_EQ(-1, result); |
| 118 } |
| 119 |
| 120 } // namespace install_static |
OLD | NEW |