-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add StringSchema, Validator, ValidatorTest and update build.gradle.kts
- Loading branch information
Showing
5 changed files
with
113 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package hexlet.code; | ||
|
||
import hexlet.code.schemas.StringSchema; | ||
|
||
public class Validator { | ||
public StringSchema string() { | ||
return new StringSchema(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package hexlet.code.schemas; | ||
|
||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.AllArgsConstructor; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class StringSchema { | ||
private List<Object> listOfNotValidContent; | ||
private int minLength = 0; | ||
private String contentToBeContained = ""; | ||
|
||
|
||
public void required() { | ||
List<Object> list = Arrays.asList(null, ""); | ||
this.listOfNotValidContent.addAll(list); | ||
} | ||
|
||
public StringSchema minLength(int length) { | ||
this.minLength = length; | ||
return new StringSchema(this.listOfNotValidContent, this.minLength, this.contentToBeContained); | ||
} | ||
|
||
public StringSchema contains(String content) { | ||
this.contentToBeContained = content; | ||
return new StringSchema(this.listOfNotValidContent, this.minLength, this.contentToBeContained); | ||
} | ||
|
||
public boolean isValid(String text) { | ||
if (text.length() > minLength && containsText(text, contentToBeContained)) { | ||
if (isRequired(text)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public boolean isRequired(String text) { | ||
for (Object item : listOfNotValidContent) { | ||
if (text.equals(item)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public boolean containsText(String text, String content) { | ||
return text.matches(content); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package hexlet.code; | ||
|
||
import hexlet.code.schemas.StringSchema; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class ValidatorTest { | ||
private static StringSchema stringSchema; | ||
@BeforeEach | ||
public void preparation() { | ||
Validator val = new Validator(); | ||
stringSchema = val.string(); | ||
} | ||
|
||
@Test | ||
public void stringSchemaTestNormal() { | ||
assertTrue(stringSchema.isValid("hexlet")); | ||
assertTrue(stringSchema.isValid("")); | ||
assertTrue(stringSchema.isValid(null)); | ||
} | ||
|
||
@Test | ||
public void stringSchemaTestRequired() { | ||
stringSchema.required(); | ||
assertTrue(stringSchema.isValid("hexlet")); | ||
assertFalse(stringSchema.isValid("")); | ||
assertFalse(stringSchema.isValid(null)); | ||
} | ||
|
||
@Test | ||
public void stringSchemaTestMinLength() { | ||
stringSchema.minLength(4); | ||
assertTrue(stringSchema.isValid("hexlet")); | ||
assertFalse(stringSchema.isValid("hex")); | ||
} | ||
|
||
@Test | ||
public void stringSchemaTestContains() { | ||
assertTrue(stringSchema.contains("hex").isValid("hexlet")); | ||
assertFalse(stringSchema.contains("hes").isValid("hexlet")); | ||
} | ||
|
||
} |