summaryrefslogtreecommitdiffhomepage
path: root/tests/test-diff.cpp
blob: 1704cfdb0d1b25943fae31ae55421aaab2cee58b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <gtest/gtest.h>

#include <filesystem>
#include <string>
#include <system_error>

#include <stdlib.h>

#include "libreichwein/file.h"

#include "diff.h"

namespace fs = std::filesystem;
using namespace Reichwein;

class DiffTest: public ::testing::Test
{
protected:
 DiffTest(){
 }

 ~DiffTest(){
 }
};

TEST_F(DiffTest, constructor)
{
 // 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>");
 }
 {
  Diff d{"abc", "c"};
  EXPECT_EQ(d.get_xml(), "<diff><start>0</start><end>2</end><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
}