chromium/third_party/blink/common/interest_group/test/interest_group_test_utils.cc

// 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.

#include "third_party/blink/public/common/interest_group/test/interest_group_test_utils.h"

#include <stddef.h>

#include <optional>
#include <string_view>
#include <vector>

#include "base/check.h"
#include "base/containers/flat_map.h"
#include "base/strings/strcat.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/interest_group/ad_display_size.h"
#include "third_party/blink/public/common/interest_group/interest_group.h"

namespace blink {

namespace {

// Macros are used to keep the field names in the failure output of EXPECT_EQ().
// These should only be used to implement InterestGroupCompare(), and #undef'd
// after.

// Compare `actual` and `expected`, either expecting equality, or non-equality,
// depending on the value of `expect_equals` passed to InterestGroupCompare().
#define IG_COMPARE

// Vectors and maps are a special case -- a parameter `func` is used to compare
// individual elements of the std::vector. IG_COMPARE_MAP() supports
// base::flat_map. std::optional-wrapped vectors and maps are also allowed.
//
// NOTE: Do **NOT** pass a lambda literal directly as `func`, as commas in the
// lambda definition may get mishandled by the  preprocessor, and lines get
// concatenated (making debugging harder). Instead, assign the lambda to a
// variable using "auto", then pass that.
#define IG_COMPARE_VEC

#define IG_COMPARE_MAP

// NOTE: Template template parameters could have been used here to match any
// list-like or map-like type, but the downside is they add complexity and can
// overmatch against non-desired types. Since we only use std::vector and
// base::flat_map, it makes sense to just manually implement those types. C++
// concepts might make it easier to be more general here in the future.

// Helper for IG_COMPARE_VEC() -- do not call directly.
//
// Handles plain std::vector instances *not* wrapped in std::optional.
template <typename T, typename Func>
void IgCompareVecInternal(std::string_view a_name,
                          std::string_view b_name,
                          const std::vector<T>& actual,
                          const std::vector<T>& expected,
                          const bool expect_equals,
                          bool& found_unequal,
                          Func f) {}

// Helper for IG_COMPARE_VEC() -- do not call directly.
//
// Handles plain std::vector instances that *are* wrapped in std::optional.
template <typename T, typename Func>
void IgCompareVecInternal(std::string_view a_name,
                          std::string_view b_name,
                          const std::optional<std::vector<T>>& actual,
                          const std::optional<std::vector<T>>& expected,
                          const bool expect_equals,
                          bool& found_unequal,
                          Func f) {}

// Helper for IG_COMPARE_MAP() -- do not call directly.
//
// Handles plain base::flat_map instances *not* wrapped in std::optional.
template <typename K, typename V, typename Func>
void IgCompareMapInternal(std::string_view a_name,
                          std::string_view b_name,
                          const base::flat_map<K, V>& actual,
                          const base::flat_map<K, V>& expected,
                          const bool expect_equals,
                          bool& found_unequal,
                          Func f) {}

// Helper for IG_COMPARE_MAP() -- do not call directly.
//
// Handles plain base::flat_map instances that *are* wrapped in std::optional.
template <typename K, typename V, typename Func>
void IgCompareMapInternal(std::string_view a_name,
                          std::string_view b_name,
                          const std::optional<base::flat_map<K, V>>& actual,
                          const std::optional<base::flat_map<K, V>>& expected,
                          const bool expect_equals,
                          bool& found_unequal,
                          Func f) {}

// Compares all fields and subfields of blink::InterestGroup using the
// IG_COMPARE*() macros implemented above.
//
// Used to implement IgExpectEqualsForTesting() and
// IgExpectNotEqualsForTesting().
//
// Technically `expected` is `not_expected` in the IgExpectNotEqualsForTesting()
// case, but only the `found_unequal` expectation can fail in that case. For the
// IgExpectEqualsForTesting() case, the name `expected` is appropriate for error
// messages.
void InterestGroupCompare(const blink::InterestGroup& actual,
                          const blink::InterestGroup& expected,
                          bool expect_equals) {}

#undef IG_COMPARE_MAP
#undef IG_COMPARE_VEC
#undef IG_COMPARE

}  // namespace

void IgExpectEqualsForTesting(const blink::InterestGroup& actual,
                              const blink::InterestGroup& expected) {}

void IgExpectNotEqualsForTesting(const blink::InterestGroup& actual,
                                 const blink::InterestGroup& not_expected) {}

}  // namespace blink