diff options
Diffstat (limited to 'tests/test-diff.cpp')
-rw-r--r-- | tests/test-diff.cpp | 110 |
1 files changed, 109 insertions, 1 deletions
diff --git a/tests/test-diff.cpp b/tests/test-diff.cpp index b6b703a..7061ef0 100644 --- a/tests/test-diff.cpp +++ b/tests/test-diff.cpp @@ -4,6 +4,8 @@ #include <string> #include <system_error> +#include <stdlib.h> + #include "libreichwein/file.h" #include "diff.h" @@ -23,6 +25,112 @@ protected: TEST_F(DiffTest, constructor) { - Diff diff{}; + // empty constructor + { + Diff d{}; + EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>0</end><data/></diff>"); + } + + // constructor via xml diff + { + EXPECT_THROW(Diff d{""}, std::exception); + } + { + EXPECT_THROW(Diff d{"<diff><begin></begin></diff>"}, std::exception); + EXPECT_THROW(Diff d{"<diff><end>0</end><data>abc</data></diff>"}, std::exception); + EXPECT_THROW(Diff d{"<diff><start>0</start><data>abc</data></diff>"}, std::exception); + EXPECT_THROW(Diff d{"<diff><start>0</start><end>0</end></diff>"}, std::exception); + EXPECT_THROW(Diff d{"<diff><start>5</start><end>0</end><data>abc</data></diff>"}, std::exception); + EXPECT_THROW(Diff d{"<diff><start></start><end>0</end><data>abc</data></diff>"}, std::exception); + } + + { + Diff d{"<diff><start>0</start><end>0</end><data>abc</data></diff>"}; + EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>0</end><data>abc</data></diff>"); + } + + { + Diff d{"<diff><start>5</start><end>5</end><data>abc</data></diff>"}; + EXPECT_EQ(d.get_xml(), "<diff><start>5</start><end>5</end><data>abc</data></diff>"); + } + + { + 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 versions + { + Diff d{"", ""}; + EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>0</end><data/></diff>"); + } + { + Diff d{"a", "a"}; + EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>0</end><data/></diff>"); + } + { + Diff d{"a", "b"}; + EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>1</end><data>b</data></diff>"); + } + { + Diff d{"0a1", "0b1"}; + EXPECT_EQ(d.get_xml(), "<diff><start>1</start><end>2</end><data>b</data></diff>"); + } + { + Diff d{"0abc1", "00b01"}; + EXPECT_EQ(d.get_xml(), "<diff><start>1</start><end>4</end><data>0b0</data></diff>"); + } + { + Diff d{"0ab1", "00b01"}; + EXPECT_EQ(d.get_xml(), "<diff><start>1</start><end>3</end><data>0b0</data></diff>"); + } + { + Diff d{"0abc1", "00b1"}; + EXPECT_EQ(d.get_xml(), "<diff><start>1</start><end>4</end><data>0b</data></diff>"); + } + { + Diff d{"0abc1", ""}; + EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>5</end><data/></diff>"); + } + { + Diff d{"bc1", "0abc1"}; + EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>0</end><data>0a</data></diff>"); + } + { + Diff d{"0abc1", "0ab"}; + EXPECT_EQ(d.get_xml(), "<diff><start>3</start><end>5</end><data/></diff>"); + } + { + Diff d{"0abc1", "0abc123"}; + EXPECT_EQ(d.get_xml(), "<diff><start>5</start><end>5</end><data>23</data></diff>"); + } + { + Diff d{"0abc1", "010abc1"}; + EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>0</end><data>01</data></diff>"); + } + { + Diff d{"0abc1", "0ac1"}; + EXPECT_EQ(d.get_xml(), "<diff><start>2</start><end>3</end><data/></diff>"); + } + { + Diff d{"0abc1", "0abxc1"}; + EXPECT_EQ(d.get_xml(), "<diff><start>3</start><end>3</end><data>x</data></diff>"); + } +} + +TEST_F(DiffTest, diff_create) +{ + const char* result {diff_create("0abc1", "0ab")}; + + EXPECT_EQ(std::string(result), "<diff><start>3</start><end>5</end><data/></diff>"); + free((void*)result); // this will be done by javascript side in real scenario +} + +TEST_F(DiffTest, diff_apply) +{ + const char* result {diff_apply("0abc1", "<diff><start>3</start><end>5</end><data/></diff>")}; + + EXPECT_EQ(std::string(result), "0ab"); + free((void*)result); // this will be done by javascript side in real scenario } |