Skip to content

Instantly share code, notes, and snippets.

@Siapran
Created March 3, 2020 22:19
Show Gist options
  • Save Siapran/14746713c808133ba806807ba421c7b9 to your computer and use it in GitHub Desktop.
Save Siapran/14746713c808133ba806807ba421c7b9 to your computer and use it in GitHub Desktop.
TEST_CASE("lenses, fallback")
{
auto first = at_i(0);
auto first_name = first | optlift(attr(&person::name)) | fallback("NULL");
auto v1 = immer::vector<person>{};
CHECK(view(first_name, v1) == "NULL");
CHECK(view(first_name, set(at_i(0), v1, person{{}, "foo"})) == "NULL");
v1 = v1.push_back({{}, "foo"});
CHECK(view(first_name, v1) == "foo");
CHECK(view(first_name, set(at_i(0), v1, person{{}, "bar"})) == "bar");
CHECK(view(first_name, set(first_name, v1, "bar")) == "bar");
}
TEST_CASE("lenses, optlift")
{
auto first = at_i(0);
auto birthday = attr(&person::birthday);
auto month = attr(&yearday::month);
auto birthday_month = birthday | month;
SECTION("lifting composed lenses") {
auto first_month = first
| optlift(birthday_month);
auto p1 = person{{5, 4}, "juanpe"};
auto v1 = immer::vector<person>{};
CHECK(view(first_month, v1) == std::nullopt);
CHECK(view(first_month, set(at_i(0), v1, p1)) == std::nullopt);
v1 = v1.push_back(p1);
CHECK(view(first_month, v1) == 4);
p1.birthday.month = 6;
CHECK(view(first_month, set(at_i(0), v1, p1)) == 6);
CHECK(view(first_month, set(first_month, v1, 8)) == 8);
}
SECTION("composing lifted lenses") {
auto first_month = first
| optlift(birthday)
| optlift(month);
auto p1 = person{{5, 4}, "juanpe"};
auto v1 = immer::vector<person>{};
CHECK(view(first_month, v1) == std::nullopt);
CHECK(view(first_month, set(at_i(0), v1, p1)) == std::nullopt);
v1 = v1.push_back(p1);
CHECK(view(first_month, v1) == 4);
p1.birthday.month = 6;
CHECK(view(first_month, set(at_i(0), v1, p1)) == 6);
CHECK(view(first_month, set(first_month, v1, 8)) == 8);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment