diff options
-rw-r--r-- | diff.cpp | 10 | ||||
-rw-r--r-- | tests/test-diff.cpp | 27 |
2 files changed, 37 insertions, 0 deletions
@@ -81,6 +81,16 @@ void Diff::create(const std::string& old_version, const std::string& new_version return; } + // re-adjust if search crossed + if (old_pos0 > old_pos1) { + old_pos0 = old_pos1; + new_pos0 = old_pos1; + } + if (new_pos0 > new_pos1) { + old_pos0 = new_pos1; + new_pos0 = new_pos1; + } + // insert in the middle if (old_pos0 == old_pos1) { m_pos0 = old_pos0; diff --git a/tests/test-diff.cpp b/tests/test-diff.cpp index 1704cfd..1625716 100644 --- a/tests/test-diff.cpp +++ b/tests/test-diff.cpp @@ -6,11 +6,14 @@ #include <stdlib.h> +#include <boost/property_tree/ptree.hpp> + #include "libreichwein/file.h" #include "diff.h" namespace fs = std::filesystem; +namespace pt = boost::property_tree; using namespace Reichwein; class DiffTest: public ::testing::Test @@ -58,6 +61,18 @@ TEST_F(DiffTest, constructor) Diff d{"<diff><start>5</start><end>50</end><data>abc</data></diff>"}; EXPECT_EQ(d.get_xml(), "<diff><start>5</start><end>50</end><data>abc</data></diff>"); } + + // constructor via ptree + { + pt::ptree ptree; + EXPECT_THROW(Diff d{ptree}, std::exception); + + ptree.put("diff.start", 0); + ptree.put("diff.end", 0); + ptree.put("diff.data", "abc"); + Diff d{ptree}; + EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>0</end><data>abc</data></diff>"); + } // constructor via versions { @@ -120,6 +135,18 @@ TEST_F(DiffTest, constructor) Diff d{"abc", "c"}; EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>2</end><data/></diff>"); } + { + Diff d{"aaaa", "aa"}; + EXPECT_EQ(d.get_xml(), "<diff><start>2</start><end>4</end><data/></diff>"); + } + { + Diff d{"baaaa", "baa"}; + EXPECT_EQ(d.get_xml(), "<diff><start>3</start><end>5</end><data/></diff>"); + } + { + Diff d{"baaaab", "baab"}; + EXPECT_EQ(d.get_xml(), "<diff><start>1</start><end>3</end><data/></diff>"); + } } TEST_F(DiffTest, diff_create) |