File: scoped_mutually_exclusive_feature_list.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (109 lines) | stat: -rw-r--r-- 3,315 bytes parent folder | download | duplicates (9)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_TEST_SCOPED_MUTUALLY_EXCLUSIVE_FEATURE_LIST_H_
#define NET_TEST_SCOPED_MUTUALLY_EXCLUSIVE_FEATURE_LIST_H_

#include <concepts>
#include <ranges>
#include <vector>

#include "base/test/scoped_feature_list.h"

namespace net::test {

// A helper for parameterized tests where one of a set of features should be
// enabled depending on the value of an enum parameter. Features in the set that
// are not enabled will be explicitly disabled so that the test functions the
// same regardless of the features' default values.
//
// Example usage:
//
//
// using ::testing::Values;
//
// // First define an enum:
// enum class FooConfiguration {
//   kConfig1,
//   kConfig2,
// };
//
// // Define the mapping from enum to feature. The struct must have exactly two
// // members, named `test_case` and `feature`, in that order.
// const struct {
//   const FooConfiguration test_case;
//   base::test::FeatureRef feature;
// } kFooConfigurationToFeatureMapping[] = {
//   { FooConfiguration::kConfig1, features::FooFeature1 },
//   { FooConfiguration::kConfig2, features::FooFeature2 },
// };
//
// // Define the text fixture.
// class FooConfigurationTest :
//   public testing::TestWithParam<FooConfiguration> {
//  public:
//   FooConfigurationTest() :
//     foo_feature_list_(GetParam(), kFooConfigurationToFeatureMapping) {}
//
//  private:
//   ScopedMutuallyExclusiveFeatureList foo_feature_list_;
// };
//
// // Instantiate the test suite.
// INSTANTIATE_TEST_SUITE_P(
//   All,
//   FooConfigurationTest,
//   Values(FooConfiguration::kConfig1, FooConfiguration::kConfig2),
//   [](const testing::TestParamInfo<FooConfiguration>& info) {
//     return info.param == kConfig1 ? "Config1" : "Config2";
//   });
//
// // Write some tests.
//
// TEST_P(FooConfigurationTest, Something) {
//   Foo foo;
//   foo.DoSomething();
//   EXPECT_TRUE(foo.something_was_done());
// }
//
//
// This will result in two tests being run,
// All/FooConfigurationTest.Something/Config1 and
// All/FooConfigurationTest.Something/Config2. The first will run with
// FooFeature1 enabled and FooFeature2 disabled, and the second will run with
// the opposite configuration.

class ScopedMutuallyExclusiveFeatureList {
 public:
  template <typename Enum, typename Range>
    requires std::ranges::forward_range<Range> && requires(const Range& r) {
      {
        std::ranges::begin(r)->test_case
      } -> std::equality_comparable_with<Enum>;
      {
        std::ranges::begin(r)->feature
      } -> std::same_as<const base::test::FeatureRef&>;
    }
  ScopedMutuallyExclusiveFeatureList(Enum param, const Range& mapping) {
    std::vector<base::test::FeatureRef> enabled_features;
    std::vector<base::test::FeatureRef> disabled_features;

    for (const auto& [test_case, feature] : mapping) {
      if (param == test_case) {
        enabled_features.push_back(feature);
      } else {
        disabled_features.push_back(feature);
      }
    }

    feature_list_.InitWithFeatures(enabled_features, disabled_features);
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

}  // namespace net::test

#endif  // NET_TEST_SCOPED_MUTUALLY_EXCLUSIVE_FEATURE_LIST_H_