11package hexlet .code ;
22
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
34import hexlet .code .formatters .Formatter ;
45import org .junit .jupiter .api .Test ;
56import java .io .IOException ;
7+ import java .nio .file .Files ;
8+ import java .nio .file .Paths ;
9+
610import static org .junit .jupiter .api .Assertions .assertEquals ;
711
812class DifferTest {
913
10- private static final String EXPECTED_JSON_STYLISH = """
11- {
12- chars1: [a, b, c]
13- - chars2: [d, e, f]
14- + chars2: false
15- - checked: false
16- + checked: true
17- - default: null
18- + default: [value1, value2]
19- - id: 45
20- + id: null
21- - key1: value1
22- + key2: value2
23- numbers1: [1, 2, 3, 4]
24- - numbers2: [2, 3, 4, 5]
25- + numbers2: [22, 33, 44, 55]
26- - numbers3: [3, 4, 5]
27- + numbers4: [4, 5, 6]
28- + obj1: {nestedKey=value, isNested=true}
29- - setting1: Some value
30- + setting1: Another value
31- - setting2: 200
32- + setting2: 300
33- - setting3: true
34- + setting3: none
35- }
36- """ ;
37-
38-
39- private static final String EXPECTED_DIFF_PLAIN = """
40- Property 'chars2' was updated. From [complex value] to false
41- Property 'checked' was updated. From false to true
42- Property 'default' was updated. From null to [complex value]
43- Property 'id' was updated. From 45 to null
44- Property 'key1' was removed
45- Property 'key2' was added with value: 'value2'
46- Property 'numbers2' was updated. From [complex value] to [complex value]
47- Property 'numbers3' was removed
48- Property 'numbers4' was added with value: [complex value]
49- Property 'obj1' was added with value: [complex value]
50- Property 'setting1' was updated. From 'Some value' to 'Another value'
51- Property 'setting2' was updated. From 200 to 300
52- Property 'setting3' was updated. From true to 'none'
53- """ ;
54-
55-
56- private static final String EXPECTED_JSON = """
57- [ {
58- "type" : "unchanged",
59- "value" : [ "a", "b", "c" ],
60- "key" : "chars1"
61- }, {
62- "newValue" : false,
63- "oldValue" : [ "d", "e", "f" ],
64- "type" : "updated",
65- "key" : "chars2"
66- }, {
67- "newValue" : true,
68- "oldValue" : false,
69- "type" : "updated",
70- "key" : "checked"
71- }, {
72- "newValue" : [ "value1", "value2" ],
73- "oldValue" : null,
74- "type" : "updated",
75- "key" : "default"
76- }, {
77- "newValue" : null,
78- "oldValue" : 45,
79- "type" : "updated",
80- "key" : "id"
81- }, {
82- "oldValue" : "value1",
83- "type" : "removed",
84- "key" : "key1"
85- }, {
86- "newValue" : "value2",
87- "type" : "added",
88- "key" : "key2"
89- }, {
90- "type" : "unchanged",
91- "value" : [ 1, 2, 3, 4 ],
92- "key" : "numbers1"
93- }, {
94- "newValue" : [ 22, 33, 44, 55 ],
95- "oldValue" : [ 2, 3, 4, 5 ],
96- "type" : "updated",
97- "key" : "numbers2"
98- }, {
99- "oldValue" : [ 3, 4, 5 ],
100- "type" : "removed",
101- "key" : "numbers3"
102- }, {
103- "newValue" : [ 4, 5, 6 ],
104- "type" : "added",
105- "key" : "numbers4"
106- }, {
107- "newValue" : {
108- "nestedKey" : "value",
109- "isNested" : true
110- },
111- "type" : "added",
112- "key" : "obj1"
113- }, {
114- "newValue" : "Another value",
115- "oldValue" : "Some value",
116- "type" : "updated",
117- "key" : "setting1"
118- }, {
119- "newValue" : 300,
120- "oldValue" : 200,
121- "type" : "updated",
122- "key" : "setting2"
123- }, {
124- "newValue" : "none",
125- "oldValue" : true,
126- "type" : "updated",
127- "key" : "setting3"
128- } ]
129- """ ;
14+ private static String readExpected (String fileName ) throws IOException {
15+ var path = Paths .get ("src/test/resources/fixtures/" + fileName );
16+ return Files .readString (path ).strip ();
17+ }
13018
13119 @ Test
13220 void testGenerateDiffForJsonStylish () throws IOException {
13321 var data1 = Parser .parseFromResources ("file1.json" );
13422 var data2 = Parser .parseFromResources ("file2.json" );
13523 var diff = Differ .calculateDiff (data1 , data2 );
13624 var actual = Formatter .format (diff , "stylish" );
137- System .out .println ("Actual output: \n " + actual );
138- assertEquals (EXPECTED_JSON_STYLISH .strip (), actual .strip ());
25+ assertEquals (readExpected ("expectedStylish.txt" ), actual .strip ());
13926 System .out .println ("ВСЕ РАБОТАЕТ!!!" );
14027 }
14128
@@ -145,20 +32,20 @@ void testGenerateDiffForJsonPlain() throws IOException {
14532 var data2 = Parser .parseFromResources ("file2.yml" );
14633 var diff = Differ .calculateDiff (data1 , data2 );
14734 var actual = Formatter .format (diff , "plain" );
148- System .out .println ("Actual output: " + actual );
149- assertEquals (EXPECTED_DIFF_PLAIN .strip (), actual .strip ());
35+ assertEquals (readExpected ("expectedPlain.txt" ), actual .strip ());
15036 System .out .println ("ВСЕ РАБОТАЕТ! лала!!!" );
15137 }
15238
153-
15439 @ Test
15540 void testGenerateDiffForJsonJson () throws IOException {
15641 var data1 = Parser .parseFromResources ("file1.json" );
15742 var data2 = Parser .parseFromResources ("file2.json" );
15843 var diff = Differ .calculateDiff (data1 , data2 );
15944 var actual = Formatter .format (diff , "json" );
160- System .out .println ("Actual output: " + actual );
161- assertEquals (EXPECTED_JSON .strip (), actual .strip ());
45+ ObjectMapper mapper = new ObjectMapper ();
46+ var expected = mapper .readTree (readExpected ("expectedJson.json" ));
47+ var actualTree = mapper .readTree (actual );
48+ assertEquals (expected , actualTree );
16249 System .out .println ("ВСЕ РАБОТАЕТ! дада!!!" );
16350 }
16451}
0 commit comments