Skip to content

Commit

Permalink
Uplift of #26590 (squashed) to release
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-builds committed Nov 16, 2024
1 parent 99ea028 commit a2f39fd
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "base/ranges/algorithm.h"
#include "base/strings/pattern.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "brave/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/epoch_operator_condition_matcher_util.h"
#include "brave/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/numerical_operator_condition_matcher_util.h"
#include "brave/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/pattern_condition_matcher_util.h"
Expand Down Expand Up @@ -55,19 +57,22 @@ bool MatchConditions(const PrefProviderInterface* const pref_provider,
condition_matchers, [pref_provider](const auto& condition_matcher) {
const auto& [pref_path, condition] = condition_matcher;

// If `has_not_operator` is `true`, the condition will match only if the
// pref path does not exist.
const bool has_not_operator = HasNotOperator(pref_path);

const std::string stripped_pref_path =
MaybeStripOperatorPrefix(pref_path);
if (const std::optional<std::string> value =
MaybeGetPrefValueAsString(pref_provider, stripped_pref_path)) {
return !has_not_operator && MatchCondition(*value, condition);
const std::optional<std::string> value =
MaybeGetPrefValueAsString(pref_provider, stripped_pref_path);

if (HasNotOperator(pref_path)) {
// Match if the pref path does not exist.
return !value;
}

if (IsNumericalOperator(condition)) {
// Default to "0" if the pref path does not exist.
return MatchCondition(value.value_or("0"), condition);
}

// Unknown pref path.
return has_not_operator;
return value ? MatchCondition(*value, condition) : false;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ TEST_F(BraveAdsConditionMatcherUtilTest, MatchEmptyConditions) {
EXPECT_TRUE(MatchConditions(&pref_provider_, /*condition_matchers=*/{}));
}

TEST_F(BraveAdsConditionMatcherUtilTest,
MatchConditionsIfAllConditionsAreTrue) {
TEST_F(BraveAdsConditionMatcherUtilTest, MatchMultipleConditions) {
// Arrange
const ConditionMatcherMap condition_matchers = {
{prefs::kSubdivisionTargetingUserSelectedSubdivision, "AUTO"},
Expand All @@ -55,6 +54,18 @@ TEST_F(BraveAdsConditionMatcherUtilTest, MatchEpochEqualOperatorCondition) {
EXPECT_TRUE(MatchConditions(&pref_provider_, condition_matchers));
}

TEST_F(BraveAdsConditionMatcherUtilTest,
DoNotMatchEpochEqualOperatorConditionForUnknownPrefPath) {
// Arrange
const ConditionMatcherMap condition_matchers = {
{"unknown_pref_path", "[T=]:7"}};

AdvanceClockBy(base::Days(7) - base::Milliseconds(1));

// Act & Assert
EXPECT_FALSE(MatchConditions(&pref_provider_, condition_matchers));
}

TEST_F(BraveAdsConditionMatcherUtilTest,
DoNotMatchEpochEqualOperatorCondition) {
// Arrange
Expand All @@ -77,6 +88,26 @@ TEST_F(BraveAdsConditionMatcherUtilTest, MatchNumericalEqualOperatorCondition) {
EXPECT_TRUE(MatchConditions(&pref_provider_, condition_matchers));
}

TEST_F(BraveAdsConditionMatcherUtilTest,
MatchNumericalhEqualOperatorConditionForUnknownPrefPath) {
// Arrange
const ConditionMatcherMap condition_matchers = {
{"unknown_pref_path", "[R=]:0"}};

// Act & Assert
EXPECT_TRUE(MatchConditions(&pref_provider_, condition_matchers));
}

TEST_F(BraveAdsConditionMatcherUtilTest,
DoNotMatchNumericalhEqualOperatorConditionForUnknownPrefPath) {
// Arrange
const ConditionMatcherMap condition_matchers = {
{"unknown_pref_path", "[R=]:1"}};

// Act & Assert
EXPECT_FALSE(MatchConditions(&pref_provider_, condition_matchers));
}

TEST_F(BraveAdsConditionMatcherUtilTest,
DoNotMatchNumericalEqualOperatorCondition) {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ constexpr char kLessThanOrEqualOperatorConditionMatcherPrefix[] = "[T≤]:";

} // namespace

bool IsEpochOperator(std::string_view condition) {
return base::MatchPattern(condition,
kEpochOperatorConditionMatcherPrefixPattern);
}

bool MatchEpochOperator(const std::string_view value,
const std::string_view condition) {
if (!base::MatchPattern(condition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace brave_ads {

// Returns true if the condition is an epoch operator.
bool IsEpochOperator(std::string_view condition);

// Matches a value against a condition using epoch operators. Supports equality,
// greater than, greater than or equal, less than. and less than or equal
// operators.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ namespace brave_ads {

class BraveAdsEpochOperatorConditionMatcherUtilTest : public test::TestBase {};

TEST_F(BraveAdsEpochOperatorConditionMatcherUtilTest, IsOperator) {
// Act & Assert
EXPECT_TRUE(IsEpochOperator("[T=]:1"));
}

TEST_F(BraveAdsEpochOperatorConditionMatcherUtilTest, IsNotOperator) {
// Act & Assert
EXPECT_FALSE(IsEpochOperator("[R=]:1"));
}

TEST_F(BraveAdsEpochOperatorConditionMatcherUtilTest, DoNotMatchNonOperator) {
// Act & Assert
EXPECT_FALSE(MatchEpochOperator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ constexpr char kLessThanOrEqualOperatorConditionMatcherPrefix[] = "[R≤]:";

} // namespace

bool IsNumericalOperator(std::string_view condition) {
return base::MatchPattern(condition,
kNumericalOperatorConditionMatcherPrefixPattern);
}

bool MatchNumericalOperator(const std::string_view value,
const std::string_view condition) {
if (!base::MatchPattern(condition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace brave_ads {

// Returns true if the condition is a numerical operator.
bool IsNumericalOperator(std::string_view condition);

// Matches a value against a condition using numerical operators. Supports
// equality, greater than, greater than or equal, less than, less than or equal
// and not equal operators.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ namespace brave_ads {
class BraveAdsNumericalOperatorConditionMatcherUtilTest
: public test::TestBase {};

TEST_F(BraveAdsNumericalOperatorConditionMatcherUtilTest, IsOperator) {
// Act & Assert
EXPECT_TRUE(IsNumericalOperator("[R=]:1"));
}

TEST_F(BraveAdsNumericalOperatorConditionMatcherUtilTest, IsNotOperator) {
// Act & Assert
EXPECT_FALSE(IsNumericalOperator("[T=]:1"));
}

TEST_F(BraveAdsNumericalOperatorConditionMatcherUtilTest,
DoNotMatchNonOperator) {
// Act & Assert
Expand Down

0 comments on commit a2f39fd

Please sign in to comment.