55import static org .junit .jupiter .api .Assertions .assertEquals ;
66
77class DifferTest {
8- private static final String EXPECTED_DIFF = """
8+
9+ private static final String EXPECTED_JSON_STYLISH = """
10+ {
11+ chars1: [a, b, c]
12+ - chars2: [d, e, f]
13+ + chars2: false
14+ - checked: false
15+ + checked: true
16+ - default: null
17+ + default: [value1, value2]
18+ - id: 45
19+ + id: null
20+ - key1: value1
21+ + key2: value2
22+ numbers1: [1, 2, 3, 4]
23+ - numbers2: [2, 3, 4, 5]
24+ + numbers2: [22, 33, 44, 55]
25+ - numbers3: [3, 4, 5]
26+ + numbers4: [4, 5, 6]
27+ + obj1: {nestedKey=value, isNested=true}
28+ - setting1: Some value
29+ + setting1: Another value
30+ - setting2: 200
31+ + setting2: 300
32+ - setting3: true
33+ + setting3: none
34+ }
35+ """ ;
36+
37+
38+ private static final String EXPECTED_DIFF_PLAIN = """
939 Property 'chars2' was updated. From [complex value] to false
1040 Property 'checked' was updated. From false to true
1141 Property 'default' was updated. From null to [complex value]
@@ -21,78 +51,106 @@ class DifferTest {
2151 Property 'setting3' was updated. From true to 'none'
2252 """ ;
2353
54+
55+ private static final String EXPECTED_JSON = """
56+ [ {
57+ "type" : "unchanged",
58+ "value" : [ "a", "b", "c" ],
59+ "key" : "chars1"
60+ }, {
61+ "newValue" : false,
62+ "oldValue" : [ "d", "e", "f" ],
63+ "type" : "updated",
64+ "key" : "chars2"
65+ }, {
66+ "newValue" : true,
67+ "oldValue" : false,
68+ "type" : "updated",
69+ "key" : "checked"
70+ }, {
71+ "newValue" : [ "value1", "value2" ],
72+ "oldValue" : null,
73+ "type" : "updated",
74+ "key" : "default"
75+ }, {
76+ "newValue" : null,
77+ "oldValue" : 45,
78+ "type" : "updated",
79+ "key" : "id"
80+ }, {
81+ "oldValue" : "value1",
82+ "type" : "removed",
83+ "key" : "key1"
84+ }, {
85+ "newValue" : "value2",
86+ "type" : "added",
87+ "key" : "key2"
88+ }, {
89+ "type" : "unchanged",
90+ "value" : [ 1, 2, 3, 4 ],
91+ "key" : "numbers1"
92+ }, {
93+ "newValue" : [ 22, 33, 44, 55 ],
94+ "oldValue" : [ 2, 3, 4, 5 ],
95+ "type" : "updated",
96+ "key" : "numbers2"
97+ }, {
98+ "oldValue" : [ 3, 4, 5 ],
99+ "type" : "removed",
100+ "key" : "numbers3"
101+ }, {
102+ "newValue" : [ 4, 5, 6 ],
103+ "type" : "added",
104+ "key" : "numbers4"
105+ }, {
106+ "newValue" : {
107+ "nestedKey" : "value",
108+ "isNested" : true
109+ },
110+ "type" : "added",
111+ "key" : "obj1"
112+ }, {
113+ "newValue" : "Another value",
114+ "oldValue" : "Some value",
115+ "type" : "updated",
116+ "key" : "setting1"
117+ }, {
118+ "newValue" : 300,
119+ "oldValue" : 200,
120+ "type" : "updated",
121+ "key" : "setting2"
122+ }, {
123+ "newValue" : "none",
124+ "oldValue" : true,
125+ "type" : "updated",
126+ "key" : "setting3"
127+ } ]
128+ """ ;
129+
24130 @ Test
25- void testGenerateDiffForJson () throws IOException {
131+ void testGenerateDiffForJsonStylish () throws IOException {
26132 String filePath1 = "file1.json" ;
27133 String filePath2 = "file2.json" ;
28- String actualDiff = Differ .generate (filePath1 , filePath2 , "plain " );
29- assertEquals (EXPECTED_DIFF .trim (), actualDiff .trim ());
134+ String actualDiff = Differ .generate (filePath1 , filePath2 , "stylish " );
135+ assertEquals (EXPECTED_JSON_STYLISH .trim (), actualDiff .trim ());
30136 System .out .println ("ВСЕ РАБОТАЕТ!" );
31137 }
32138
33139 @ Test
34- void testGenerateDiffForYaml () throws IOException {
140+ void testGenerateDiffForJsonPlain () throws IOException {
35141 String filePath1 = "file1.yml" ;
36142 String filePath2 = "file2.yml" ;
37143 String actualDiff = Differ .generate (filePath1 , filePath2 , "plain" );
38- assertEquals (EXPECTED_DIFF .trim (), actualDiff .trim ());
144+ assertEquals (EXPECTED_DIFF_PLAIN .trim (), actualDiff .trim ());
39145 System .out .println ("ВСЕ РАБОТАЕТ! лала!" );
40146 }
41147
148+ @ Test
149+ void testGenerateDiffForJsonJson () throws IOException {
150+ String filePath1 = "file1.json" ;
151+ String filePath2 = "file2.json" ;
152+ String actualJson = Differ .generate (filePath1 , filePath2 , "json" );
153+ assertEquals (EXPECTED_JSON .trim (), actualJson .trim ());
154+ System .out .println ("ВСЕ РАБОТАЕТ! аддада" );
155+ }
42156}
43-
44-
45-
46-
47- //
48- //@Test
49- //void testGenerateDiffJson() throws IOException {
50- // // Указываем пути к фикстурам
51- // String filePath1 = "file1.json";
52- // String filePath2 = "file2.json";
53- //
54- // // Ожидаемый результат сравнения JSON-файлов
55- // String expectedDiff = """
56- // {
57- // - follow: false
58- // host: hexlet.io
59- // - proxy: 123.234.53.22
60- // - timeout: 50
61- // + timeout: 20
62- // + verbose: true
63- // }
64- // """;
65- //
66- // // Запускаем метод сравнения
67- // String actualDiff = Differ.generate(filePath1, filePath2);
68- //
69- // // Проверяем, что результат совпадает с ожидаемым
70- // assertEquals(expectedDiff.trim(), actualDiff.trim());
71- // System.out.println("ВСЕ РАБОТАЕТ!!");
72- //}
73- //
74- //@Test
75- //void testGenerateDiffYml() throws IOException {
76- // // Указываем пути к фикстурам
77- // String filePath1 = "file1.yml";
78- // String filePath2 = "file2.yml";
79- //
80- // // Ожидаемый результат сравнения YML-файлов
81- // String expectedDiff = """
82- // {
83- // - follow: false
84- // host: hexlet.io
85- // - proxy: 123.234.53.22
86- // - timeout: 50
87- // + timeout: 20
88- // + verbose: true
89- // }
90- // """;
91- //
92- // // Запускаем метод сравнения
93- // String actualDiff = Differ.generate(filePath1, filePath2);
94- //
95- // // Проверяем, что результат совпадает с ожидаемым
96- // assertEquals(expectedDiff.trim(), actualDiff.trim());
97- // System.out.println("ВСЕ РАБОТАЕТ! лала!");
98- //}
0 commit comments