Skip to content

Commit

Permalink
refactor(test, tools): apply string_view usages
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Nov 13, 2024
1 parent 36cbf5d commit af7e7cb
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 101 deletions.
8 changes: 4 additions & 4 deletions test/component_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

using namespace rime;

class Greeting : public Class<Greeting, const string&> {
class Greeting : public Class<Greeting, string_view> {
public:
virtual string Say() = 0;
virtual ~Greeting() = default;
Expand All @@ -31,10 +31,10 @@ class Hello : public Greeting {
// customize a hello component with parameters
class HelloComponent : public Hello::Component {
public:
HelloComponent(const string& word) : word_(word) {}
HelloComponent(string_view word) : word_(word) {}
// define a custom creator to provide an additional argument
Hello* Create(const string& name) {
return new Hello(make_pair(word_, name));
Hello* Create(string_view name) {
return new Hello(make_pair(word_, string{name}));
}

private:
Expand Down
232 changes: 143 additions & 89 deletions test/config_compiler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <gtest/gtest.h>
#include <rime/component.h>
#include <rime/config.h>
#include <rime/algo/strings.h>

using namespace rime;

Expand Down Expand Up @@ -35,73 +36,90 @@ TEST_F(RimeConfigCompilerTest, IncludeLocalReference) {
std::ostringstream oss;
oss << "include_local_reference/@" << i << "/";
const auto& prefix(oss.str());
EXPECT_TRUE(config_->IsNull(prefix + "__include"));
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__include")));
string actual;
EXPECT_TRUE(config_->GetString(prefix + "terrans/player", &actual));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "terrans/player"), &actual));
EXPECT_EQ("slayers_boxer", actual);
EXPECT_TRUE(config_->GetString(prefix + "protoss/player", &actual));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "protoss/player"), &actual));
EXPECT_EQ("grrrr", actual);
EXPECT_TRUE(config_->GetString(prefix + "zerg/player", &actual));
EXPECT_TRUE(config_->GetString(rime::strings::concat(prefix, "zerg/player"),
&actual));
EXPECT_EQ("yellow", actual);
}
}

TEST_F(RimeConfigCompilerTest, IncludeExternalReference) {
const string& prefix = "include_external_reference/";
EXPECT_TRUE(config_->IsNull(prefix + "terrans/__include"));
string_view prefix = "include_external_reference/";
EXPECT_TRUE(
config_->IsNull(rime::strings::concat(prefix, "terrans/__include")));
int mineral = 0;
EXPECT_TRUE(config_->GetInt(prefix + "terrans/tank/cost/mineral", &mineral));
EXPECT_TRUE(config_->GetInt(
rime::strings::concat(prefix, "terrans/tank/cost/mineral"), &mineral));
EXPECT_EQ(150, mineral);
int gas = 0;
EXPECT_TRUE(config_->GetInt(prefix + "terrans/tank/cost/gas", &gas));
EXPECT_TRUE(config_->GetInt(
rime::strings::concat(prefix, "terrans/tank/cost/gas"), &gas));
EXPECT_EQ(100, gas);
EXPECT_TRUE(config_->IsNull(prefix + "protoss"));
EXPECT_TRUE(config_->IsNull(prefix + "zerg"));
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "protoss")));
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "zerg")));
}

TEST_F(RimeConfigCompilerTest, IncludeExternalFile) {
const string& prefix = "include_external_file/";
EXPECT_TRUE(config_->IsNull(prefix + "__include"));
EXPECT_TRUE(config_->IsMap(prefix + "terrans"));
EXPECT_TRUE(config_->IsMap(prefix + "protoss"));
EXPECT_TRUE(config_->IsMap(prefix + "zerg"));
string_view prefix = "include_external_file/";
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__include")));
EXPECT_TRUE(config_->IsMap(rime::strings::concat(prefix, "terrans")));
EXPECT_TRUE(config_->IsMap(rime::strings::concat(prefix, "protoss")));
EXPECT_TRUE(config_->IsMap(rime::strings::concat(prefix, "zerg")));
int number = 0;
EXPECT_TRUE(config_->GetInt(prefix + "terrans/supply/produced", &number));
EXPECT_TRUE(config_->GetInt(
rime::strings::concat(prefix, "terrans/supply/produced"), &number));
EXPECT_EQ(28, number);
EXPECT_TRUE(config_->IsList(prefix + "protoss/air_force"));
EXPECT_TRUE(
config_->IsList(rime::strings::concat(prefix, "protoss/air_force")));
string name;
EXPECT_TRUE(config_->GetString(prefix + "zerg/queen", &name));
EXPECT_TRUE(
config_->GetString(rime::strings::concat(prefix, "zerg/queen"), &name));
EXPECT_EQ("Kerrigan", name);
}

TEST_F(RimeConfigCompilerTest, PatchReference) {
const string& prefix = "patch_reference/";
EXPECT_TRUE(config_->IsNull(prefix + "__patch"));
EXPECT_EQ(4, config_->GetListSize(prefix + "battlefields"));
string_view prefix = "patch_reference/";
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__patch")));
EXPECT_EQ(
4, config_->GetListSize(rime::strings::concat(prefix, "battlefields")));
string map;
EXPECT_TRUE(config_->GetString(prefix + "battlefields/@3", &map));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "battlefields/@3"), &map));
EXPECT_EQ("match point", map);
}

TEST_F(RimeConfigCompilerTest, PatchLiteral) {
const string& prefix = "patch_literal/";
EXPECT_TRUE(config_->IsNull(prefix + "__patch"));
EXPECT_EQ(6, config_->GetListSize(prefix + "zerg/ground_units"));
string_view prefix = "patch_literal/";
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__patch")));
EXPECT_EQ(6, config_->GetListSize(
rime::strings::concat(prefix, "zerg/ground_units")));
string unit;
EXPECT_TRUE(config_->GetString(prefix + "zerg/ground_units/@5", &unit));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "zerg/ground_units/@5"), &unit));
EXPECT_EQ("lurker", unit);
// assert that we patched on a copy of the included node
EXPECT_EQ(5, config_->GetListSize("/starcraft/zerg/ground_units"));
}

TEST_F(RimeConfigCompilerTest, PatchList) {
const string& prefix = "patch_list/";
EXPECT_TRUE(config_->IsNull(prefix + "protoss/__patch"));
EXPECT_EQ(8, config_->GetListSize(prefix + "protoss/ground_units"));
string_view prefix = "patch_list/";
EXPECT_TRUE(
config_->IsNull(rime::strings::concat(prefix, "protoss/__patch")));
EXPECT_EQ(8, config_->GetListSize(
rime::strings::concat(prefix, "protoss/ground_units")));
string unit;
EXPECT_TRUE(config_->GetString(prefix + "protoss/ground_units/@6", &unit));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "protoss/ground_units/@6"), &unit));
EXPECT_EQ("dark templar", unit);
EXPECT_TRUE(config_->GetString(prefix + "protoss/ground_units/@7", &unit));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "protoss/ground_units/@7"), &unit));
EXPECT_EQ("dark archon", unit);
// assert that we patched on a copy of the included node
EXPECT_EQ(6, config_->GetListSize("/starcraft/protoss/ground_units"));
Expand All @@ -113,30 +131,41 @@ class RimeConfigDependencyTest : public RimeConfigCompilerTestBase {
};

TEST_F(RimeConfigDependencyTest, DependencyChaining) {
const string& prefix = "dependency_chaining/";
EXPECT_TRUE(config_->IsNull(prefix + "alpha/__include"));
EXPECT_TRUE(config_->IsNull(prefix + "beta/__include"));
EXPECT_TRUE(config_->IsNull(prefix + "delta/__include"));
string_view prefix = "dependency_chaining/";
EXPECT_TRUE(
config_->IsNull(rime::strings::concat(prefix, "alpha/__include")));
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "beta/__include")));
EXPECT_TRUE(
config_->IsNull(rime::strings::concat(prefix, "delta/__include")));
string value;
EXPECT_TRUE(config_->GetString(prefix + "alpha", &value));
EXPECT_TRUE(
config_->GetString(rime::strings::concat(prefix, "alpha"), &value));
EXPECT_EQ("success", value);
EXPECT_TRUE(config_->GetString(prefix + "beta", &value));
EXPECT_TRUE(
config_->GetString(rime::strings::concat(prefix, "beta"), &value));
EXPECT_EQ("success", value);
EXPECT_TRUE(config_->GetString(prefix + "delta", &value));
EXPECT_TRUE(
config_->GetString(rime::strings::concat(prefix, "delta"), &value));
EXPECT_EQ("success", value);
}

// Unit test for https://github.com/rime/librime/issues/141
TEST_F(RimeConfigDependencyTest, DependencyPriorities) {
const string& prefix = "dependency_priorities/";
EXPECT_TRUE(config_->IsNull(prefix + "terrans/__include"));
EXPECT_TRUE(config_->IsNull(prefix + "terrans/__patch"));
EXPECT_TRUE(config_->IsNull(prefix + "protoss/__include"));
EXPECT_TRUE(config_->IsNull(prefix + "protoss/__patch"));
string_view prefix = "dependency_priorities/";
EXPECT_TRUE(
config_->IsNull(rime::strings::concat(prefix, "terrans/__include")));
EXPECT_TRUE(
config_->IsNull(rime::strings::concat(prefix, "terrans/__patch")));
EXPECT_TRUE(
config_->IsNull(rime::strings::concat(prefix, "protoss/__include")));
EXPECT_TRUE(
config_->IsNull(rime::strings::concat(prefix, "protoss/__patch")));
string player;
EXPECT_TRUE(config_->GetString(prefix + "terrans/player", &player));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "terrans/player"), &player));
EXPECT_EQ("nada", player);
EXPECT_TRUE(config_->GetString(prefix + "protoss/player", &player));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "protoss/player"), &player));
EXPECT_EQ("bisu", player);
}

Expand All @@ -148,11 +177,12 @@ class RimeConfigOptionalReferenceTest : public RimeConfigCompilerTestBase {
};

TEST_F(RimeConfigOptionalReferenceTest, OptionalReference) {
const string& prefix = "/";
EXPECT_TRUE(config_->IsNull(prefix + "__include"));
EXPECT_TRUE(config_->IsNull(prefix + "__patch"));
string_view prefix = "/";
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__include")));
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__patch")));
bool untouched;
EXPECT_TRUE(config_->GetBool(prefix + "untouched", &untouched));
EXPECT_TRUE(
config_->GetBool(rime::strings::concat(prefix, "untouched"), &untouched));
EXPECT_TRUE(untouched);
}

Expand All @@ -162,39 +192,46 @@ class RimeConfigMergeTest : public RimeConfigCompilerTestBase {
};

TEST_F(RimeConfigMergeTest, AppendWithInclude) {
const string& prefix = "append_with_include/";
EXPECT_TRUE(config_->IsNull(prefix + "list/__include"));
EXPECT_TRUE(config_->IsNull(prefix + "list/__append"));
string_view prefix = "append_with_include/";
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "list/__include")));
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "list/__append")));

EXPECT_TRUE(config_->IsList(prefix + "list"));
EXPECT_EQ(6 + 2, config_->GetListSize(prefix + "list"));
EXPECT_TRUE(config_->IsList(rime::strings::concat(prefix, "list")));
EXPECT_EQ(6 + 2, config_->GetListSize(rime::strings::concat(prefix, "list")));
string unit;
EXPECT_TRUE(config_->GetString(prefix + "list/@6", &unit));
EXPECT_TRUE(
config_->GetString(rime::strings::concat(prefix, "list/@6"), &unit));
EXPECT_EQ("dark templar", unit);
EXPECT_TRUE(config_->GetString(prefix + "list/@7", &unit));
EXPECT_TRUE(
config_->GetString(rime::strings::concat(prefix, "list/@7"), &unit));
EXPECT_EQ("dark archon", unit);

// verify that we append to a copy and the original list is untouched
EXPECT_EQ(6, config_->GetListSize("/starcraft/protoss/ground_units"));
}

TEST_F(RimeConfigMergeTest, AppendWithPatch) {
const string& prefix = "append_with_patch/";
EXPECT_TRUE(config_->IsNull(prefix + "__include"));
EXPECT_TRUE(config_->IsNull(prefix + "__patch"));
string_view prefix = "append_with_patch/";
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__include")));
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__patch")));

string player;
EXPECT_TRUE(config_->GetString(prefix + "terrans/player", &player));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "terrans/player"), &player));
EXPECT_EQ("slayers_boxer, nada", player);

EXPECT_EQ(2, config_->GetListSize(prefix + "terrans/air_units"));
EXPECT_EQ(2, config_->GetListSize(
rime::strings::concat(prefix, "terrans/air_units")));
string unit;
EXPECT_TRUE(config_->GetString(prefix + "terrans/air_units/@0", &unit));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "terrans/air_units/@0"), &unit));
EXPECT_EQ("wraith", unit);
EXPECT_TRUE(config_->GetString(prefix + "terrans/air_units/@1", &unit));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "terrans/air_units/@1"), &unit));
EXPECT_EQ("battlecruiser", unit);

EXPECT_EQ(6 + 2, config_->GetListSize(prefix + "protoss/ground_units"));
EXPECT_EQ(6 + 2, config_->GetListSize(
rime::strings::concat(prefix, "protoss/ground_units")));

// verify that we append to a copy and the original list is untouched
EXPECT_TRUE(config_->GetString("/starcraft/terrans/player", &player));
Expand All @@ -204,35 +241,50 @@ TEST_F(RimeConfigMergeTest, AppendWithPatch) {
}

TEST_F(RimeConfigMergeTest, MergeTree) {
const string& prefix = "merge_tree/";
EXPECT_TRUE(config_->IsNull(prefix + "__include"));
string_view prefix = "merge_tree/";
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__include")));

string player;
EXPECT_TRUE(config_->GetString(prefix + "terrans/player", &player));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "terrans/player"), &player));
EXPECT_EQ("slayers_boxer", player);
EXPECT_TRUE(config_->IsNull(prefix + "terrans/air_units"));
EXPECT_TRUE(config_->IsList(prefix + "terrans/ground_units"));
EXPECT_EQ(5 + 2, config_->GetListSize(prefix + "terrans/ground_units"));
EXPECT_TRUE(
config_->IsNull(rime::strings::concat(prefix, "terrans/air_units")));
EXPECT_TRUE(
config_->IsList(rime::strings::concat(prefix, "terrans/ground_units")));
EXPECT_EQ(5 + 2, config_->GetListSize(
rime::strings::concat(prefix, "terrans/ground_units")));
string unit;
EXPECT_TRUE(config_->GetString(prefix + "terrans/ground_units/@5", &unit));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "terrans/ground_units/@5"), &unit));
EXPECT_EQ("medic", unit);
EXPECT_TRUE(config_->GetString(prefix + "terrans/ground_units/@6", &unit));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "terrans/ground_units/@6"), &unit));
EXPECT_EQ("goliath", unit);

EXPECT_TRUE(config_->GetString(prefix + "protoss/player", &player));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "protoss/player"), &player));
EXPECT_EQ("grrrr", player);
EXPECT_TRUE(config_->IsNull(prefix + "protoss/ground_units/__append"));
EXPECT_TRUE(config_->IsList(prefix + "protoss/ground_units"));
EXPECT_EQ(6 + 2, config_->GetListSize(prefix + "protoss/ground_units"));
EXPECT_TRUE(config_->GetString(prefix + "protoss/ground_units/@6", &unit));
EXPECT_TRUE(config_->IsNull(
rime::strings::concat(prefix, "protoss/ground_units/__append")));
EXPECT_TRUE(
config_->IsList(rime::strings::concat(prefix, "protoss/ground_units")));
EXPECT_EQ(6 + 2, config_->GetListSize(
rime::strings::concat(prefix, "protoss/ground_units")));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "protoss/ground_units/@6"), &unit));
EXPECT_EQ("dark templar", unit);
EXPECT_TRUE(config_->GetString(prefix + "protoss/ground_units/@7", &unit));
EXPECT_TRUE(config_->GetString(
rime::strings::concat(prefix, "protoss/ground_units/@7"), &unit));
EXPECT_EQ("dark archon", unit);

EXPECT_TRUE(config_->GetString(prefix + "zerg/player", &player));
EXPECT_TRUE(config_->GetString(rime::strings::concat(prefix, "zerg/player"),
&player));
EXPECT_EQ("yellow", player);
EXPECT_TRUE(config_->IsList(prefix + "zerg/ground_units"));
EXPECT_EQ(0, config_->GetListSize(prefix + "zerg/ground_units"));
EXPECT_TRUE(
config_->IsList(rime::strings::concat(prefix, "zerg/ground_units")));
EXPECT_EQ(0, config_->GetListSize(
rime::strings::concat(prefix, "zerg/ground_units")));

// verify that we merge to a copy and the original list is untouched
EXPECT_TRUE(config_->IsNull("/starcraft/terrans/ground_units"));
Expand All @@ -248,19 +300,21 @@ class RimeConfigCircularDependencyTest : public RimeConfigCompilerTestBase {
};

TEST_F(RimeConfigMergeTest, CreateListWithInplacePatch) {
const string& prefix = "create_list_with_inplace_patch/";
EXPECT_TRUE(config_->IsList(prefix + "all_ground_units"));
EXPECT_EQ(16, config_->GetListSize(prefix + "all_ground_units"));
string_view prefix = "create_list_with_inplace_patch/";
EXPECT_TRUE(
config_->IsList(rime::strings::concat(prefix, "all_ground_units")));
EXPECT_EQ(16, config_->GetListSize(
rime::strings::concat(prefix, "all_ground_units")));
}

TEST_F(RimeConfigCircularDependencyTest, BestEffortResolution) {
const string& prefix = "test/";
EXPECT_TRUE(config_->IsNull(prefix + "__patch"));
EXPECT_TRUE(config_->IsNull(prefix + "work/__include"));
string_view prefix = "test/";
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__patch")));
EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "work/__include")));
string home;
EXPECT_TRUE(config_->GetString(prefix + "home", &home));
EXPECT_TRUE(config_->GetString(rime::strings::concat(prefix, "home"), &home));
EXPECT_EQ("naive", home);
string work;
EXPECT_TRUE(config_->GetString(prefix + "work", &work));
EXPECT_TRUE(config_->GetString(rime::strings::concat(prefix, "work"), &work));
EXPECT_EQ("excited", work);
}
2 changes: 1 addition & 1 deletion tools/rime_console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class RimeConsole {
}
}

void ProcessLine(const string& line) {
void ProcessLine(string_view line) {
KeySequence keys;
if (!keys.Parse(line)) {
LOG(ERROR) << "error parsing input: '" << line << "'";
Expand Down
Loading

0 comments on commit af7e7cb

Please sign in to comment.