Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
igagis committed Aug 21, 2024
1 parent a0e4b5a commit afd9d68
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 98 deletions.
96 changes: 68 additions & 28 deletions src/cssom/om.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,31 +283,77 @@ std::string to_string(
}
} // namespace

namespace {
// Produces deterministic list of CSS rules.
// The list is sorted by property groups and within a group is sorted by rule "name".
std::vector<std::pair<std::string, std::shared_ptr<std::string>>> to_string_styles(
utki::span<const style> styles, //
const std::function<std::string(uint32_t)>& property_id_to_name,
const std::function<std::string(uint32_t, const property_value_base&)>& property_value_to_string
)
{
std::map<property_list*, std::shared_ptr<std::string>> props_map;

std::vector<std::pair<std::string, std::shared_ptr<std::string>>> str_styles;

for (const auto& s : styles) {
auto props_ptr = s.properties.get();
ASSERT(props_ptr)
auto props_iter = props_map.find(props_ptr);
if (props_iter == props_map.end()) {
auto res = props_map.insert(
std::make_pair(
props_ptr,
std::make_shared<std::string>(to_string(
*s.properties, //
property_id_to_name,
property_value_to_string
))
)
);
ASSERT(res.second)
props_iter = res.first;
}
ASSERT(props_iter != props_map.end())

str_styles.emplace_back(
get_name(s), //
props_iter->second
);
}

std::sort(
str_styles.begin(), //
str_styles.end(),
[](const auto& a, const auto& b) {
// sort by property_list to make consequent gropus using same property list by name within group
if (*a.second < *b.second) {
return true;
} else if (*a.second > *b.second) {
return false;
}

// sort by name within the group
return a.first < b.first;
}
);

return str_styles;
}
} // namespace

void sheet::write(
papki::file& fi,
const std::function<std::string(uint32_t)>& property_id_to_name,
const std::function<std::string(uint32_t, const property_value_base&)>& property_value_to_string,
std::string_view indent
) const
{
auto styles_to_save = this->styles; // copy

// TODO: make this sorting deterministic
// std::sort(
// styles_to_save.begin(), //
// styles_to_save.end(),
// [](const auto& a, const auto& b) -> bool {
// // sort by property_list to make consequent gropus using same property list by name within group
// if (a.properties.get() < b.properties.get()) {
// return true;
// } else if (a.properties.get() > b.properties.get()) {
// return false;
// }

// // sort by name within the group
// return a.get_name() < b.get_name();
// }
// );
auto styles_to_save = to_string_styles(
this->styles, //
property_id_to_name,
property_value_to_string
);

papki::file::guard file_guard(fi, papki::file::mode::create);

Expand All @@ -317,7 +363,7 @@ void sheet::write(
// such selector chains will go in a row.
auto selector_group_start_iter = i;
for (auto j = selector_group_start_iter; j != styles_to_save.end(); ++j) {
if (j->properties.get() != selector_group_start_iter->properties.get()) {
if (j->second.get() != selector_group_start_iter->second.get()) {
ASSERT(j > i)
i = --j;
break;
Expand All @@ -331,23 +377,17 @@ void sheet::write(
fi.write(utki::make_span(indent));
}

fi.write(get_name(*j));
fi.write(j->first);
}

// write properties
fi.write(open_curly_brace);
fi.write(utki::make_span(indent));
fi.write(tab_char);

ASSERT(selector_group_start_iter->properties)

auto props_str = to_string(
*selector_group_start_iter->properties, //
property_id_to_name,
property_value_to_string
);
ASSERT(selector_group_start_iter->second)

fi.write(props_str);
fi.write(*selector_group_start_iter->second);

fi.write(new_line_char);
fi.write(utki::make_span(indent));
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/samples_data/sample3.css.cmp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
.cls-1, .cls-3 {
fill: none;
.cls-4 {
fill-rule: evenodd;
}
.cls-2, .cls-4 {
fill: #47506a;
}
.cls-1, .cls-3 {
fill: none;
}
.cls-3 {
stroke: #3866f0; stroke-width: 4px;
}
.cls-4 {
fill-rule: evenodd;
}
6 changes: 3 additions & 3 deletions tests/unit/samples_data/sample4.css.cmp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tag-1, tag-2 {
fill: #445566;
}
tag-3, tag-4 {
fill: #334455;
}
tag-1, tag-2 {
fill: #445566;
}
6 changes: 3 additions & 3 deletions tests/unit/samples_data/sample5.css.cmp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#id-1, #id-2 {
fill: #445566;
}
#id-3, #id-4 {
fill: #334455;
}
#id-1, #id-2 {
fill: #445566;
}
6 changes: 3 additions & 3 deletions tests/unit/samples_data/sample6.css.cmp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.cls-1, .cls-2 {
fill: #445566;
}
.cls-3, .cls-4 {
fill: #334455;
}
.cls-1, .cls-2 {
fill: #445566;
}
5 changes: 1 addition & 4 deletions tests/unit/samples_data/sample7.css.cmp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#id-1 {
#id-1, .cls-3.cls-4, tag-1 {
fill: #334455;
}
.cls-1.cls-2 {
fill: #445566;
}
.cls-3.cls-4, tag-1 {
fill: #334455;
}
92 changes: 46 additions & 46 deletions tests/unit/samples_data/sample8.css.cmp
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
.fence {
fill: none; stroke: #c4e3c3; stroke-width: 1;
.task {
fill: #000;
}
.shadow {
filter: drop-shadow(0 0 2px #000);
.trees {
fill: #144043;
}
.stairs {
fill: #FFD700;
.building {
fill: #1a2632;
}
.task {
fill: #000;
.land {
fill: #1f5054;
}
.danger {
fill: red; stroke: red; stroke-width: 2; stroke-dasharray: 4,2; fill-opacity: .4;
.locked {
fill: #37414c;
}
.powerline {
fill: none; stroke: #ffce00; stroke-width: 2; stroke-dasharray: 6,6; stroke-miterlimit: 10;
.water {
fill: #4a6b96;
}
.railroad {
fill: none; stroke: #914833; stroke-width: 3; stroke-dasharray: 6;
.wood {
fill: #593700;
}
.road_large {
stroke-width: 12;
.floor {
fill: #70777f;
}
.road_medium {
stroke-width: 8;
.tarmac {
fill: #768089;
}
.road_small {
stroke-width: 5;
.gravel {
fill: #946d3e;
}
.road_gravel {
fill: none; stroke: #946d3e;
.stairs {
fill: #FFD700;
}
.road_tarmac {
fill: none; stroke: #888;
.cement {
fill: #c6c2c2;
}
.trees {
fill: #144043;
.rock {
fill: #dcd5b6;
}
.map_border {
fill: none; stroke: #000; stroke-width: 2;
}
.locked {
fill: #37414c;
.road_tarmac {
fill: none; stroke: #888;
}
.floor {
fill: #70777f;
.railroad {
fill: none; stroke: #914833; stroke-width: 3; stroke-dasharray: 6;
}
.building {
fill: #1a2632;
.road_gravel {
fill: none; stroke: #946d3e;
}
.gravel {
fill: #946d3e;
.fence {
fill: none; stroke: #c4e3c3; stroke-width: 1;
}
.tarmac {
fill: #768089;
.powerline {
fill: none; stroke: #ffce00; stroke-width: 2; stroke-dasharray: 6,6; stroke-miterlimit: 10;
}
.wood {
fill: #593700;
.danger {
fill: red; stroke: red; stroke-width: 2; stroke-dasharray: 4,2; fill-opacity: .4;
}
.water {
fill: #4a6b96;
.shadow {
filter: drop-shadow(0 0 2px #000);
}
.rock {
fill: #dcd5b6;
.road_large {
stroke-width: 12;
}
.land {
fill: #1f5054;
.road_small {
stroke-width: 5;
}
.cement {
fill: #c6c2c2;
.road_medium {
stroke-width: 8;
}
4 changes: 2 additions & 2 deletions tests/unit/samples_data/simple0.css.cmp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
body h1 {
body {
background-color: green;
}
body {
body h1 {
background-color: green;
}
8 changes: 4 additions & 4 deletions tests/unit/samples_data/simple1.css.cmp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
body.foo h1.bar {
.foo {
background-color: green;
}
body.foo.bar {
.foo h1.bar {
background-color: green;
}
.foo h1.bar {
body.foo h1.bar {
background-color: green;
}
.foo {
body.foo.bar {
background-color: green;
}

0 comments on commit afd9d68

Please sign in to comment.