Skip to content

Commit ce8e1c3

Browse files
committed
fixed problems
1 parent 66964f6 commit ce8e1c3

File tree

8 files changed

+98
-20
lines changed

8 files changed

+98
-20
lines changed
48.4 KB
Binary file not shown.
0 Bytes
Binary file not shown.
50 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
646 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

app/src/main/java/hexlet/code/Parser.java

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,97 @@
66

77
import java.io.File;
88
import java.io.IOException;
9+
import java.net.URISyntaxException;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
912
import java.util.Map;
13+
import java.util.Objects;
1014

1115
public class Parser {
1216

17+
// Используется в тестах (ищет в resources/fixtures)
18+
public static Map<String, Object> parseFromResources(String fileName) throws IOException {
19+
ObjectMapper objectMapper;
20+
21+
if (fileName.endsWith(".json")) {
22+
objectMapper = new ObjectMapper();
23+
} else if (fileName.endsWith(".yaml") || fileName.endsWith(".yml")) {
24+
objectMapper = new ObjectMapper(new YAMLFactory());
25+
} else {
26+
throw new IllegalArgumentException("Unsupported file format: " + fileName);
27+
}
28+
29+
Path path;
30+
try {
31+
path = Paths.get(Objects.requireNonNull(
32+
Parser.class.getClassLoader().getResource("fixtures/" + fileName)).toURI());
33+
} catch (URISyntaxException e) {
34+
throw new RuntimeException("Invalid file path: " + fileName, e);
35+
}
36+
37+
return objectMapper.readValue(path.toFile(), new TypeReference<Map<String, Object>>() {});
38+
}
39+
40+
// Используется из CLI — напрямую с файловой системы
1341
public static Map<String, Object> parse(String filePath) throws IOException {
1442
ObjectMapper objectMapper;
1543

1644
if (filePath.endsWith(".json")) {
1745
objectMapper = new ObjectMapper();
1846
} else if (filePath.endsWith(".yaml") || filePath.endsWith(".yml")) {
19-
objectMapper = new ObjectMapper(new YAMLFactory()); // Используем YAML парсер
47+
objectMapper = new ObjectMapper(new YAMLFactory());
2048
} else {
2149
throw new IllegalArgumentException("Unsupported file format: " + filePath);
2250
}
2351

24-
return objectMapper.readValue(new File("src/main/resources/fixtures/" + filePath),
25-
new TypeReference<Map<String, Object>>() { });
52+
return objectMapper.readValue(new File(filePath), new TypeReference<Map<String, Object>>() {});
2653
}
2754
}
55+
56+
57+
58+
59+
60+
//package hexlet.code;
61+
//
62+
//import com.fasterxml.jackson.core.type.TypeReference;
63+
//import com.fasterxml.jackson.databind.ObjectMapper;
64+
//import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
65+
//
66+
//import java.io.File;
67+
//import java.io.IOException;
68+
//import java.net.URISyntaxException;
69+
//import java.nio.file.Path;
70+
//import java.nio.file.Paths;
71+
//import java.util.Map;
72+
//import java.util.Objects;
73+
//
74+
//public class Parser {
75+
//
76+
// public static Map<String, Object> parse(String filePath) throws IOException {
77+
// ObjectMapper objectMapper;
78+
//
79+
// if (filePath.endsWith(".json")) {
80+
// objectMapper = new ObjectMapper();
81+
// } else if (filePath.endsWith(".yaml") || filePath.endsWith(".yml")) {
82+
// objectMapper = new ObjectMapper(new YAMLFactory()); // Используем YAML парсер
83+
// } else {
84+
// throw new IllegalArgumentException("Unsupported file format: " + filePath);
85+
// }
86+
//
87+
// Path path;
88+
// try {
89+
// path = Paths.get(Objects.requireNonNull(
90+
// Parser.class.getClassLoader().getResource("fixtures/" + filePath)).toURI());
91+
// } catch (URISyntaxException e) {
92+
// throw new RuntimeException("Invalid file path: " + filePath, e);
93+
// }
94+
//
95+
// return objectMapper.readValue(path.toFile(), new TypeReference<Map<String, Object>>() {});
96+
// }
97+
//}
98+
99+
100+
101+
// return objectMapper.readValue(new File("src/main/resources/fixtures/" + filePath),
102+
// new TypeReference<Map<String, Object>>() { });

app/src/test/java/hexlet/code/DifferTest.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package hexlet.code;
22

3+
import hexlet.code.formatters.Formatter;
34
import org.junit.jupiter.api.Test;
45
import java.io.IOException;
56
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -129,30 +130,32 @@ class DifferTest {
129130

130131
@Test
131132
void testGenerateDiffForJsonStylish() throws IOException {
132-
String filePath1 = "file1.json";
133-
String filePath2 = "file2.json";
134-
String actualDiff = Differ.generate(filePath1, filePath2);
135-
assertEquals(EXPECTED_JSON_STYLISH.trim(), actualDiff.trim());
136-
System.out.println("ВСЕ РАБОТАЕТ!");
133+
var data1 = Parser.parseFromResources("file1.json");
134+
var data2 = Parser.parseFromResources("file2.json");
135+
var diff = Differ.calculateDiff(data1, data2);
136+
var actual = Formatter.format(diff, "stylish");
137+
assertEquals(EXPECTED_JSON_STYLISH.trim(), actual.trim());
138+
System.out.println("ВСЕ РАБОТАЕТ!!!");
137139
}
138140

139141
@Test
140142
void testGenerateDiffForJsonPlain() throws IOException {
141-
String filePath1 = "file1.yml";
142-
String filePath2 = "file2.yml";
143-
String format2 = "plain";
144-
String actualDiff = Differ.generate(filePath1, filePath2, format2);
145-
assertEquals(EXPECTED_DIFF_PLAIN.trim(), actualDiff.trim());
146-
System.out.println("ВСЕ РАБОТАЕТ! лала!");
143+
var data1 = Parser.parseFromResources("file1.yml");
144+
var data2 = Parser.parseFromResources("file2.yml");
145+
var diff = Differ.calculateDiff(data1, data2);
146+
var actual = Formatter.format(diff, "plain");
147+
assertEquals(EXPECTED_DIFF_PLAIN.trim(), actual.trim());
148+
System.out.println("ВСЕ РАБОТАЕТ! лала!!!");
147149
}
148150

151+
149152
@Test
150153
void testGenerateDiffForJsonJson() throws IOException {
151-
String filePath1 = "file1.json";
152-
String filePath2 = "file2.json";
153-
String format3 = "json";
154-
String actualJson = Differ.generate(filePath1, filePath2, format3 );
155-
assertEquals(EXPECTED_JSON.trim(), actualJson.trim());
156-
System.out.println("ВСЕ РАБОТАЕТ! дада");
154+
var data1 = Parser.parseFromResources("file1.json");
155+
var data2 = Parser.parseFromResources("file2.json");
156+
var diff = Differ.calculateDiff(data1, data2);
157+
var actual = Formatter.format(diff, "json");
158+
assertEquals(EXPECTED_JSON.trim(), actual.trim());
159+
System.out.println("ВСЕ РАБОТАЕТ! дада!!!");
157160
}
158161
}

0 commit comments

Comments
 (0)