summaryrefslogtreecommitdiffstats
path: root/xpcom/tests/gtest/TestNSPRLogModulesParser.cpp
blob: 4fd9241753498f57a0e56d4c7b00c92fc5710599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "NSPRLogModulesParser.h"
#include "mozilla/ArrayUtils.h"
#include "gtest/gtest.h"

using namespace mozilla;

TEST(NSPRLogModulesParser, Empty)
{
  bool callbackInvoked = false;
  auto callback = [&](const char*, mozilla::LogLevel, int32_t) mutable { callbackInvoked = true; };

  mozilla::NSPRLogModulesParser(nullptr, callback);
  EXPECT_FALSE(callbackInvoked);

  mozilla::NSPRLogModulesParser("", callback);
  EXPECT_FALSE(callbackInvoked);
}

TEST(NSPRLogModulesParser, DefaultLevel)
{
  bool callbackInvoked = false;
  auto callback =
      [&](const char* aName, mozilla::LogLevel aLevel, int32_t) {
        EXPECT_STREQ("Foo", aName);
        EXPECT_EQ(mozilla::LogLevel::Error, aLevel);
        callbackInvoked = true;
      };

  mozilla::NSPRLogModulesParser("Foo", callback);
  EXPECT_TRUE(callbackInvoked);

  callbackInvoked = false;
  mozilla::NSPRLogModulesParser("Foo:", callback);
  EXPECT_TRUE(callbackInvoked);
}

TEST(NSPRLogModulesParser, LevelSpecified)
{
  std::pair<const char*, mozilla::LogLevel> expected[] = {
    { "Foo:0", mozilla::LogLevel::Disabled },
    { "Foo:1", mozilla::LogLevel::Error },
    { "Foo:2", mozilla::LogLevel::Warning },
    { "Foo:3", mozilla::LogLevel::Info },
    { "Foo:4", mozilla::LogLevel::Debug },
    { "Foo:5", mozilla::LogLevel::Verbose },
    { "Foo:25", mozilla::LogLevel::Verbose },  // too high
    { "Foo:-12", mozilla::LogLevel::Disabled } // too low
  };

  auto* currTest = expected;

  for (size_t i = 0; i < MOZ_ARRAY_LENGTH(expected); i++) {
    bool callbackInvoked = false;
    mozilla::NSPRLogModulesParser(currTest->first,
        [&](const char* aName, mozilla::LogLevel aLevel, int32_t) {
          EXPECT_STREQ("Foo", aName);
          EXPECT_EQ(currTest->second, aLevel);
          callbackInvoked = true;
        });
    EXPECT_TRUE(callbackInvoked);
    currTest++;
  }
}

TEST(NSPRLogModulesParser, Multiple)
{
  std::pair<const char*, mozilla::LogLevel> expected[] = {
    { "timestamp", mozilla::LogLevel::Error },
    { "Foo", mozilla::LogLevel::Info },
    { "Bar", mozilla::LogLevel::Error },
    { "Baz", mozilla::LogLevel::Warning },
    { "Qux", mozilla::LogLevel::Verbose },
  };

  const size_t kExpectedCount = MOZ_ARRAY_LENGTH(expected);

  auto* currTest = expected;

  size_t count = 0;
  mozilla::NSPRLogModulesParser("timestamp,Foo:3, Bar,Baz:2,    Qux:5",
      [&](const char* aName, mozilla::LogLevel aLevel, int32_t) mutable {
        ASSERT_LT(count, kExpectedCount);
        EXPECT_STREQ(currTest->first, aName);
        EXPECT_EQ(currTest->second, aLevel);
        currTest++;
        count++;
     });

  EXPECT_EQ(kExpectedCount, count);
}

TEST(NSPRLogModulesParser, RawArg)
{
  bool callbackInvoked = false;
  auto callback =
    [&](const char* aName, mozilla::LogLevel aLevel, int32_t aRawValue) {
    EXPECT_STREQ("Foo", aName);
    EXPECT_EQ(mozilla::LogLevel::Verbose, aLevel);
    EXPECT_EQ(1000, aRawValue);
    callbackInvoked = true;
  };

  mozilla::NSPRLogModulesParser("Foo:1000", callback);
  EXPECT_TRUE(callbackInvoked);
}