Skip to content

Commit

Permalink
[TF] refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Homyakin committed Sep 4, 2024
1 parent 4d2168e commit c558af1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/main/java/ru/homyakin/iuliia/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ru/homyakin/iuliia/Schemas.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;

Expand Down Expand Up @@ -141,6 +142,7 @@ public enum Schemas {
/**
* Returns the schema specified by the {@link #name} field.
* The schema is read from a JSON file in the classpath.
*
* @return the schema
* @throws IOException if there is an error reading the schema file
*/
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/ru/homyakin/iuliia/TranslatorFullTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.homyakin.iuliia;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TranslatorFullTest {
@Test
public void Given_RealSchema_When_CreateTranslator_And_Translate_Then_Success() {
var translator = new Translator(Schemas.ICAO_DOC_9303);
var word = translator.translate("Юлия");
Assertions.assertEquals("Iuliia", word);
}
}
Original file line number Diff line number Diff line change
@@ -1,61 +1,51 @@
package ru.homyakin.iuliia.tests;
package ru.homyakin.iuliia;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import ru.homyakin.iuliia.Schema;
import ru.homyakin.iuliia.Schemas;
import ru.homyakin.iuliia.Translator;

public class MappingTest {

@Test
public void iuliiaTest() throws IOException {
var translator = new Translator(Schemas.ICAO_DOC_9303);
var word = translator.translate("Юлия");
Assertions.assertEquals("Iuliia", word);
}
import java.io.IOException;
import java.io.InputStream;

public class TranslatorTranslateTest {
@Test
public void mappingTest() throws IOException {
public void Given_LetterToNumbers_When_Translate_Then_Success() throws IOException {
var translator = createTranslator("test_mapping.json");
var word = translator.translate("Iuliia");
Assertions.assertEquals("243221", word);
}

@Test
public void prevMappingTest() throws IOException {
public void Given_TwoPrevLettersIntoZeroLetters_When_Translate_Then_RemoveLastLetter() throws IOException {
var translator = createTranslator("test_prev_mapping.json");
var word = translator.translate("Iuliia");
Assertions.assertEquals("Iulia", word);
}

@Test
public void nextMappingTest() throws IOException {
public void Given_TwoNextLettersIntoOneLetter_When_Translate_Then_ReplaceFirstLetter() throws IOException {
var translator = createTranslator("test_next_mapping.json");
var word = translator.translate("Iuliia");
Assertions.assertEquals("Yuliia", word);
}

@Test
public void endingMappingTest() throws IOException {
public void Given_TwoEndingLettersIntoTwoLetters_When_Translate_Then_ReplaceLastTwoLetters() throws IOException {
var translator = createTranslator("test_ending_mapping.json");
var word = translator.translate("Iuliia");
Assertions.assertEquals("Iuliya", word);
Assertions.assertEquals("Iuliyd", word);
}

@Test
public void shortWordTest() throws IOException {
public void Given_NoRules_When_Translate_Then_RemainWord() throws IOException {
var translator = createTranslator("test_empty.json");
var word = translator.translate("Iu");
Assertions.assertEquals("Iu", word);
}

@Test
public void emptyWordTest() throws IOException {
public void Given_EmptyWord_When_Translate_Then_EmptyWord() throws IOException {
var translator = createTranslator("test_empty.json");
var word = translator.translate("");
Assertions.assertEquals("", word);
Expand All @@ -70,9 +60,10 @@ private Translator createTranslator(String schemaName) throws IOException {


private Schema getSchema(String name) throws IOException {
var stream = getJsonStream(name);
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(stream, Schema.class);
try (var stream = getJsonStream(name)) {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(stream, Schema.class);
}
}

private InputStream getJsonStream(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import ru.homyakin.iuliia.Schemas;
import ru.homyakin.iuliia.Translator;

public class SchemasTest {
public class AllSchemasTest {

@Test
public void alaLcTest() throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/schemas/test_ending_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"mapping": {},
"prev_mapping": null,
"next_mapping": null,
"ending_mapping": {"ia": "ya"}
"ending_mapping": {"ia": "yd"}
}

0 comments on commit c558af1

Please sign in to comment.