Text: add checked patch apply and unified rendering (M-TEXT-06)
This commit is contained in:
1 parent
48e5e223af
commit
17a23bf88a
2 files changed
+232
No files matched your search
@@ -0,0 +1,117 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Text/Diff.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace Void::Diff;
|
||||
|
||||
std::vector<LineDiff> sample_edits()
|
||||
{
|
||||
return {
|
||||
LineDiff{Op::Equal, "a"},
|
||||
LineDiff{Op::Delete, "b"},
|
||||
LineDiff{Op::Insert, "B"},
|
||||
LineDiff{Op::Equal, "c"},
|
||||
};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test text_apply_checked_round_trips_and_rejects_mismatch()
|
||||
{
|
||||
// Round trip over generic sequences: applying a diff to its source
|
||||
// reconstructs the target.
|
||||
for (int i = 0; i < 40; ++i) {
|
||||
std::vector<int> left;
|
||||
std::vector<int> right;
|
||||
for (int k = 0; k < i % 7; ++k) {
|
||||
left.push_back((k * 3 + i) % 5);
|
||||
}
|
||||
for (int k = 0; k < i % 5; ++k) {
|
||||
right.push_back((k * 2 + i) % 4);
|
||||
}
|
||||
auto edits = diff_bounded(std::span<const int>{left}, std::span<const int>{right});
|
||||
Void::Testing::require(edits.has_value());
|
||||
auto applied = apply_checked(std::span<const int>{left}, *edits);
|
||||
Void::Testing::require(applied.has_value());
|
||||
Void::Testing::expect(*applied == right);
|
||||
|
||||
// A source that does not match the script is rejected.
|
||||
std::vector<int> wrong = left;
|
||||
wrong.push_back(99);
|
||||
auto rejected = apply_checked(std::span<const int>{wrong}, *edits);
|
||||
Void::Testing::expect(!rejected.has_value());
|
||||
Void::Testing::expect(rejected.error() == PatchFailure::SourceMismatch);
|
||||
|
||||
// The output bound is enforced.
|
||||
auto bounded = apply_checked(std::span<const int>{left}, *edits, 0);
|
||||
if (!right.empty()) {
|
||||
Void::Testing::expect(!bounded.has_value());
|
||||
Void::Testing::expect(bounded.error() == PatchFailure::OutputTooLarge);
|
||||
}
|
||||
}
|
||||
|
||||
// Line text round trip using the same split semantics as line_diff.
|
||||
const std::string a{"one\ntwo\nthree\n"};
|
||||
const std::string b{"one\n2\nthree\nfour\n"};
|
||||
auto line_edits = line_diff(a, b);
|
||||
std::vector<std::string> a_lines;
|
||||
std::size_t pos = 0;
|
||||
while (pos <= a.size()) {
|
||||
const auto nl = a.find('\n', pos);
|
||||
a_lines.push_back(a.substr(pos, nl == std::string::npos ? nl : nl - pos));
|
||||
if (nl == std::string::npos) break;
|
||||
pos = nl + 1;
|
||||
}
|
||||
auto applied = apply_checked(std::span<const std::string>{a_lines}, line_edits);
|
||||
Void::Testing::require(applied.has_value());
|
||||
std::string rebuilt;
|
||||
for (std::size_t i = 0; i < applied->size(); ++i) {
|
||||
if (i != 0) {
|
||||
rebuilt += '\n';
|
||||
}
|
||||
rebuilt += (*applied)[i];
|
||||
}
|
||||
Void::Testing::expect(rebuilt == b);
|
||||
}
|
||||
|
||||
Void::Testing::Test text_render_unified_hunks()
|
||||
{
|
||||
const auto edits = sample_edits();
|
||||
const std::string patch = render_unified(edits, "left", "right");
|
||||
|
||||
Void::Testing::expect(patch.starts_with("--- left\n+++ right\n"));
|
||||
Void::Testing::expect(patch.find("@@ -1,3 +1,3 @@") != std::string::npos);
|
||||
Void::Testing::expect(patch.find("\n a\n") != std::string::npos);
|
||||
Void::Testing::expect(patch.find("\n-b\n") != std::string::npos);
|
||||
Void::Testing::expect(patch.find("\n+B\n") != std::string::npos);
|
||||
Void::Testing::expect(patch.find("\n c\n") != std::string::npos);
|
||||
|
||||
// No changes: only the file headers are emitted.
|
||||
const std::vector<LineDiff> unchanged{LineDiff{Op::Equal, "x"}};
|
||||
Void::Testing::expect(render_unified(unchanged) == "--- a\n+++ b\n");
|
||||
|
||||
// Distant changes produce separate hunks; nearby ones merge.
|
||||
std::vector<LineDiff> distant;
|
||||
for (int i = 0; i < 30; ++i) {
|
||||
distant.push_back(LineDiff{Op::Equal, "line" + std::to_string(i)});
|
||||
if (i == 5 || i == 25) {
|
||||
distant.push_back(LineDiff{Op::Insert, "added" + std::to_string(i)});
|
||||
}
|
||||
}
|
||||
const std::string distant_patch = render_unified(distant, "a", "b", 1);
|
||||
std::size_t hunks = 0;
|
||||
for (std::size_t at = 0;
|
||||
(at = distant_patch.find("@@ -", at)) != std::string::npos; at += 4) {
|
||||
++hunks;
|
||||
}
|
||||
Void::Testing::expect_eq(hunks, std::size_t{2});
|
||||
|
||||
// The output bound truncates rather than growing without limit.
|
||||
const std::string bounded = render_unified(distant, "a", "b", 3, 40);
|
||||
Void::Testing::expect(bounded.size() <= 40);
|
||||
}
|
||||
Reference in new issue
Block a user