Skip to content

Commit

Permalink
refactoring and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovsenka committed Oct 2, 2024
1 parent b988d1b commit 3b6006d
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 40 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/java-project-71.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
plugins {
id("java")
application
checkstyle
jacoco
Expand Down
18 changes: 3 additions & 15 deletions app/src/main/java/hexlet/code/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,14 @@ public final class App implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println(Differ.generate(
getFilepath(),
getFilepath2(),
getFormat())
filepath,
filepath2,
format)
);
return 0;
}
public static void main(String[] args) {
int exitCode = new CommandLine(new App()).execute(args);
System.exit(exitCode);
}

public String getFormat() {
return format;
}

public String getFilepath() {
return filepath;
}

public String getFilepath2() {
return filepath2;
}
}
14 changes: 1 addition & 13 deletions app/src/main/java/hexlet/code/Differ.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,8 @@ public static String generate(String pathToFile, String pathToFile2, String form
Map<String, Object> mapFile = Parser.parse(filepath);
Map<String, Object> mapFile2 = Parser.parse(filepath2);

IFormatter formatter = null;
IFormatter formatter;
formatter = FormatterFactory.getFormatter(format);
return formatter.format(DiffBuilder.build(mapFile, mapFile2));
}

public static String generate(String pathToFile, String pathToFile2) throws Exception {
Path filepath = Paths.get(pathToFile).toAbsolutePath().normalize();
Path filepath2 = Paths.get(pathToFile2).toAbsolutePath().normalize();

Map<String, Object> mapFile = Parser.parse(filepath);
Map<String, Object> mapFile2 = Parser.parse(filepath2);

IFormatter formatter = null;
formatter = FormatterFactory.getFormatter("stylish");
return formatter.format(DiffBuilder.build(mapFile, mapFile2));
}
}
45 changes: 34 additions & 11 deletions app/src/test/java/DifferTests.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import hexlet.code.Differ;
import hexlet.code.Parser;
import org.junit.jupiter.api.Test;

import java.nio.file.Files;
Expand All @@ -9,15 +11,10 @@

public class DifferTests {

private static final String YAML_PATH = "src/test/resources/test1.yml";
private static final String YAML_PATH2 = "src/test/resources/test2.yml";
private static final String JSON_PATH = "src/test/resources/test1.json";
private static final String JSON_PATH2 = "src/test/resources/test2.json";

@Test
public void jsonDiffTestStylish() {

}
private static final String YAML_PATH = "src/test/resources/fixtures/test1.yml";
private static final String YAML_PATH2 = "src/test/resources/fixtures/test2.yml";
private static final String JSON_PATH = "src/test/resources/fixtures/test1.json";
private static final String JSON_PATH2 = "src/test/resources/fixtures/test2.json";

@Test
public void jsonDiffTest() throws Exception {
Expand All @@ -35,6 +32,19 @@ public void jsonDiffTest() throws Exception {
);
}

@Test
public void unexpectedFormatTest() {
Exception exception = assertThrows(Exception.class,
() -> Differ.generate(JSON_PATH, JSON_PATH2, "unknown").trim());
Exception exception2 = assertThrows(Exception.class,
() -> Differ.generate(YAML_PATH, YAML_PATH2, "").trim());

assertEquals("Unexpected format 'unknown'. Possible formats: [stylish, plain, json]",
exception.getMessage());
assertEquals("Unexpected format ''. Possible formats: [stylish, plain, json]",
exception2.getMessage());
}

@Test
public void yamlDiffTest() throws Exception {
assertEquals(
Expand All @@ -51,10 +61,23 @@ public void yamlDiffTest() throws Exception {
);
}

@Test
public void parserTest() {
assertEquals(Parser.parse(getPath("test1.json")),
Parser.parse(getPath("test1.yml"))
);
assertEquals(Parser.parse(getPath("test2.json")),
Parser.parse(getPath("test2.yml"))
);
}

private static String readFixture(String fileName) throws Exception {
Path filePath = Paths.get("src", "test", "resources", "fixtures", fileName)
return Files.readString(getPath(fileName)).trim();
}

private static Path getPath(String fileName) {
return Paths.get("src", "test", "resources", "fixtures", fileName)
.toAbsolutePath().normalize();
return Files.readString(filePath).trim();
}
}

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 3b6006d

Please sign in to comment.