Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions libs/client-sdk/src/client_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include "client_impl.hpp"
#include "data_sources/null_data_source.hpp"
#include "data_sources/polling_data_source.hpp"
Expand Down Expand Up @@ -244,7 +244,7 @@
}

template <typename T>
EvaluationDetail<T> ClientImpl::VariationInternal(FlagKey const& key,

Check warning on line 247 in libs/client-sdk/src/client_impl.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/client-sdk/src/client_impl.cpp:247:51 [bugprone-easily-swappable-parameters]

2 adjacent parameters of 'VariationInternal' of convertible types are easily swapped by mistake

Check warning on line 247 in libs/client-sdk/src/client_impl.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/client-sdk/src/client_impl.cpp:247:33 [readability-function-cognitive-complexity]

function 'VariationInternal' has cognitive complexity of 29 (threshold 25)
Value default_value,
bool check_type,
bool detailed,
Expand Down Expand Up @@ -368,6 +368,12 @@

event_processor_->SendAsync(std::move(event));

// If the flag has no value, the result is the default value.
if (detail.Value().IsNull()) {
return EvaluationDetail<T>(default_value, detail.VariationIndex(),
detail.Reason());
}

return EvaluationDetail<T>(detail.Value(), detail.VariationIndex(),
detail.Reason());
}
Expand Down
51 changes: 51 additions & 0 deletions libs/client-sdk/tests/client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,39 @@
#include <launchdarkly/client_side/client.hpp>
#include <launchdarkly/context_builder.hpp>
#include <map>
#include <memory>
#include <optional>
#include <string>

using namespace launchdarkly;
using namespace launchdarkly::client_side;

namespace {

/* Returns the same flag data for every context. */
class FlagDataPersistence : public IPersistence {

Check warning on line 15 in libs/client-sdk/tests/client_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/client-sdk/tests/client_test.cpp:15:7 [cppcoreguidelines-virtual-class-destructor]

destructor of 'FlagDataPersistence' is public and non-virtual
public:
explicit FlagDataPersistence(std::string flag_data)
: flag_data_(std::move(flag_data)) {}

void Set(std::string, std::string, std::string) noexcept override {}

Check warning on line 20 in libs/client-sdk/tests/client_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/client-sdk/tests/client_test.cpp:20:25 [readability-named-parameter]

all parameters should be named in a function

void Remove(std::string, std::string) noexcept override {}

Check warning on line 22 in libs/client-sdk/tests/client_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/client-sdk/tests/client_test.cpp:22:28 [readability-named-parameter]

all parameters should be named in a function

std::optional<std::string> Read(std::string,

Check warning on line 24 in libs/client-sdk/tests/client_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/client-sdk/tests/client_test.cpp:24:48 [readability-named-parameter]

all parameters should be named in a function
std::string key) noexcept override {
if (key == "ContextIndex") {
return std::nullopt;
}
return flag_data_;
}

private:
std::string flag_data_;
};

} // namespace

TEST(ClientTest, ClientConstructedWithMinimalConfigAndContext) {
tl::expected<Config, Error> config = ConfigBuilder("sdk-123").Build();
ASSERT_TRUE(config);
Expand All @@ -14,7 +43,7 @@

Client client(std::move(*config), context);

char const* version = client.Version();

Check warning on line 46 in libs/client-sdk/tests/client_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/client-sdk/tests/client_test.cpp:46:27 [readability-static-accessed-through-instance]

static member accessed through instance
ASSERT_TRUE(version);
ASSERT_STREQ(version, "3.13.3"); // {x-release-please-version}
}
Expand Down Expand Up @@ -85,6 +114,28 @@
}
}

TEST(ClientTest, JsonVariationReturnsDefaultWhenFlagHasNoValue) {
// The cached data has a flag with a null value and a flag without a
// value. Each flag has no variation.
auto builder = ConfigBuilder("sdk-123");
builder.Offline(true);
builder.Persistence().Custom(std::make_shared<FlagDataPersistence>(
R"({"null-value":{"version":1,"value":null,"variation":null,)"
R"("reason":{"kind":"OFF"}},)"
R"("omitted-value":{"version":1,"reason":{"kind":"OFF"}}})"));
Client client(builder.Build().value(),
ContextBuilder().Kind("cat", "shadow").Build());

// The JSON variation calls return the default value for each flag.
for (auto const& flag : {"null-value", "omitted-value"}) {
EXPECT_EQ(client.JsonVariation(flag, "default"), Value("default"))
<< flag;
auto const detail = client.JsonVariationDetail(flag, "default");
EXPECT_EQ(detail.Value(), Value("default")) << flag;
EXPECT_EQ(detail.Reason(), EvaluationReason::Off()) << flag;
}
}

// This test mainly serves to catch any changes made to the types in the Data
// Source Status API that are not backwards-compatible.
TEST(ClientTest, DataSourceStatus) {
Expand Down
Loading