akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 1 | // Copyright (c) 2010 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 "base/vlog.h" |
| 6 | |
| 7 | #include "base/basictypes.h" |
| 8 | #include "base/logging.h" |
glider@chromium.org | c9f8b5f | 2010-09-30 19:59:27 +0900 | [diff] [blame] | 9 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 10 | #include "base/time.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace logging { |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | class VlogTest : public testing::Test { |
| 18 | }; |
| 19 | |
| 20 | TEST_F(VlogTest, NoVmodule) { |
| 21 | EXPECT_EQ(0, VlogInfo("", "").GetVlogLevel("test1")); |
| 22 | EXPECT_EQ(0, VlogInfo("0", "").GetVlogLevel("test2")); |
| 23 | EXPECT_EQ(0, VlogInfo("blah", "").GetVlogLevel("test3")); |
| 24 | EXPECT_EQ(0, VlogInfo("0blah1", "").GetVlogLevel("test4")); |
| 25 | EXPECT_EQ(1, VlogInfo("1", "").GetVlogLevel("test5")); |
| 26 | EXPECT_EQ(5, VlogInfo("5", "").GetVlogLevel("test6")); |
| 27 | } |
| 28 | |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame^] | 29 | TEST_F(VlogTest, MatchVlogPattern) { |
| 30 | // Degenerate cases. |
| 31 | EXPECT_TRUE(MatchVlogPattern("", "")); |
| 32 | EXPECT_TRUE(MatchVlogPattern("", "****")); |
| 33 | EXPECT_FALSE(MatchVlogPattern("", "x")); |
| 34 | EXPECT_FALSE(MatchVlogPattern("x", "")); |
| 35 | |
| 36 | // Basic. |
| 37 | EXPECT_TRUE(MatchVlogPattern("blah", "blah")); |
| 38 | |
| 39 | // ? should match exactly one character. |
| 40 | EXPECT_TRUE(MatchVlogPattern("blah", "bl?h")); |
| 41 | EXPECT_FALSE(MatchVlogPattern("blh", "bl?h")); |
| 42 | EXPECT_FALSE(MatchVlogPattern("blaah", "bl?h")); |
| 43 | EXPECT_TRUE(MatchVlogPattern("blah", "?lah")); |
| 44 | EXPECT_FALSE(MatchVlogPattern("lah", "?lah")); |
| 45 | EXPECT_FALSE(MatchVlogPattern("bblah", "?lah")); |
| 46 | |
| 47 | // * can match any number (even 0) of characters. |
| 48 | EXPECT_TRUE(MatchVlogPattern("blah", "bl*h")); |
| 49 | EXPECT_TRUE(MatchVlogPattern("blabcdefh", "bl*h")); |
| 50 | EXPECT_TRUE(MatchVlogPattern("blh", "bl*h")); |
| 51 | EXPECT_TRUE(MatchVlogPattern("blah", "*blah")); |
| 52 | EXPECT_TRUE(MatchVlogPattern("ohblah", "*blah")); |
| 53 | EXPECT_TRUE(MatchVlogPattern("blah", "blah*")); |
| 54 | EXPECT_TRUE(MatchVlogPattern("blahhhh", "blah*")); |
| 55 | EXPECT_TRUE(MatchVlogPattern("blahhhh", "blah*")); |
| 56 | EXPECT_TRUE(MatchVlogPattern("blah", "*blah*")); |
| 57 | EXPECT_TRUE(MatchVlogPattern("blahhhh", "*blah*")); |
| 58 | EXPECT_TRUE(MatchVlogPattern("bbbblahhhh", "*blah*")); |
| 59 | |
| 60 | // Multiple *s should work fine. |
| 61 | EXPECT_TRUE(MatchVlogPattern("ballaah", "b*la*h")); |
| 62 | EXPECT_TRUE(MatchVlogPattern("blah", "b*la*h")); |
| 63 | EXPECT_TRUE(MatchVlogPattern("bbbblah", "b*la*h")); |
| 64 | EXPECT_TRUE(MatchVlogPattern("blaaah", "b*la*h")); |
| 65 | |
| 66 | // There should be no escaping going on. |
| 67 | EXPECT_TRUE(MatchVlogPattern("bl\\ah", "bl\\?h")); |
| 68 | EXPECT_FALSE(MatchVlogPattern("bl?h", "bl\\?h")); |
| 69 | EXPECT_TRUE(MatchVlogPattern("bl\\aaaah", "bl\\*h")); |
| 70 | EXPECT_FALSE(MatchVlogPattern("bl*h", "bl\\*h")); |
| 71 | |
| 72 | // Any slash matches any slash. |
| 73 | EXPECT_TRUE(MatchVlogPattern("/b\\lah", "/b\\lah")); |
| 74 | EXPECT_TRUE(MatchVlogPattern("\\b/lah", "/b\\lah")); |
| 75 | } |
| 76 | |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 77 | TEST_F(VlogTest, VmoduleBasic) { |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 78 | const char kVSwitch[] = "-1"; |
| 79 | const char kVModuleSwitch[] = |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 80 | "foo=,bar=0,baz=blah,,qux=0blah1,quux=1,corge.ext=5"; |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 81 | VlogInfo vlog_info(kVSwitch, kVModuleSwitch); |
| 82 | EXPECT_EQ(-1, vlog_info.GetVlogLevel("/path/to/grault.cc")); |
| 83 | EXPECT_EQ(0, vlog_info.GetVlogLevel("/path/to/foo.cc")); |
| 84 | EXPECT_EQ(0, vlog_info.GetVlogLevel("D:\\Path\\To\\bar-inl.mm")); |
| 85 | EXPECT_EQ(-1, vlog_info.GetVlogLevel("D:\\path\\to what/bar_unittest.m")); |
| 86 | EXPECT_EQ(0, vlog_info.GetVlogLevel("baz.h")); |
| 87 | EXPECT_EQ(0, vlog_info.GetVlogLevel("/another/path/to/qux.h")); |
| 88 | EXPECT_EQ(1, vlog_info.GetVlogLevel("/path/to/quux")); |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 89 | EXPECT_EQ(5, vlog_info.GetVlogLevel("c:\\path/to/corge.ext.h")); |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 90 | } |
| 91 | |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 92 | TEST_F(VlogTest, VmoduleDirs) { |
| 93 | const char kVModuleSwitch[] = |
| 94 | "foo/bar.cc=1,baz\\*\\qux.cc=2,*quux/*=3,*/*-inl.h=4"; |
| 95 | VlogInfo vlog_info("", kVModuleSwitch); |
| 96 | EXPECT_EQ(0, vlog_info.GetVlogLevel("/foo/bar.cc")); |
| 97 | EXPECT_EQ(0, vlog_info.GetVlogLevel("bar.cc")); |
| 98 | EXPECT_EQ(1, vlog_info.GetVlogLevel("foo/bar.cc")); |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 99 | |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 100 | EXPECT_EQ(0, vlog_info.GetVlogLevel("baz/grault/qux.h")); |
| 101 | EXPECT_EQ(0, vlog_info.GetVlogLevel("/baz/grault/qux.cc")); |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame^] | 102 | EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/qux.cc")); |
| 103 | EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/blah/qux.cc")); |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 104 | EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault\\qux.cc")); |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame^] | 105 | EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault//blah\\qux.cc")); |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 106 | |
| 107 | EXPECT_EQ(0, vlog_info.GetVlogLevel("/foo/bar/baz/quux.cc")); |
| 108 | EXPECT_EQ(3, vlog_info.GetVlogLevel("/foo/bar/baz/quux/grault.cc")); |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame^] | 109 | EXPECT_EQ(3, vlog_info.GetVlogLevel("/foo\\bar/baz\\quux/grault.cc")); |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 110 | |
| 111 | EXPECT_EQ(0, vlog_info.GetVlogLevel("foo/bar/test-inl.cc")); |
| 112 | EXPECT_EQ(4, vlog_info.GetVlogLevel("foo/bar/test-inl.h")); |
| 113 | EXPECT_EQ(4, vlog_info.GetVlogLevel("foo/bar/baz/blah-inl.h")); |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 114 | } |
| 115 | |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 116 | } // namespace |
| 117 | |
| 118 | } // namespace logging |