-
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.
- Loading branch information
1 parent
d39ed21
commit ba0e8df
Showing
7 changed files
with
151 additions
and
107 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 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 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 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 |
---|---|---|
@@ -1,28 +1,30 @@ | ||
package hexlet.code.schemas; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.Objects; | ||
|
||
public class StringSchema extends BaseSchema { | ||
|
||
public StringSchema() { | ||
super(); | ||
addCheck(item -> item instanceof String || Objects.isNull(item)); | ||
addCheck(item -> Objects.isNull(item) || item instanceof String || StringUtils.isEmpty(item.toString())); | ||
} | ||
|
||
@Override | ||
public StringSchema required() { | ||
super.required(); | ||
addCheck(item -> !item.equals("")); | ||
addCheck(item -> !StringUtils.isEmpty((String) item)); | ||
return this; | ||
} | ||
|
||
public StringSchema minLength(int length) { | ||
addCheck(item -> Objects.nonNull(item) && ((String) item).length() >= length); | ||
addCheck(item -> Objects.isNull(item) || item.equals("") || ((String) item).length() >= length); | ||
return this; | ||
} | ||
|
||
public StringSchema contains(String phrase) { | ||
addCheck(item -> Objects.nonNull(item) && ((String) item).contains(phrase)); | ||
addCheck(item -> Objects.isNull(item) || item.equals("") || ((String) item).contains(phrase)); | ||
return this; | ||
} | ||
} |
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 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 |
---|---|---|
@@ -1,57 +1,62 @@ | ||
package hexlet.code; | ||
|
||
import hexlet.code.schemas.NumberSchema; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.NullSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class NumberSchemaTest { | ||
|
||
private static final int MIN_VALUE = 2; | ||
private static final int MAX_VALUE = 90; | ||
private static final Object ITEM_NULL = null; | ||
private static final int ITEM_BASIC = 31; | ||
private static final int ITEM_EXTREME = 109; | ||
private static final int ITEM_ZERO = 0; | ||
private static final int ITEM_NEGATIVE = -6; | ||
private static final String ITEM_LINE = "no way"; | ||
|
||
private NumberSchema schema; | ||
private static final int NUMBER_FROM = 2; | ||
private static final int NUMBER_TO = 90; | ||
|
||
@BeforeEach | ||
void getSchema() { | ||
schema = new Validator().number(); | ||
@Test | ||
void testIsValid() { | ||
NumberSchema schema1 = new Validator().number(); | ||
|
||
assertTrue(schema1.isValid(ITEM_NULL)); | ||
assertTrue(schema1.isValid(ITEM_BASIC)); | ||
assertTrue(schema1.isValid(ITEM_ZERO)); | ||
assertTrue(schema1.isValid(ITEM_NEGATIVE)); | ||
assertFalse(schema1.isValid(ITEM_LINE)); | ||
} | ||
|
||
@ParameterizedTest | ||
@NullSource | ||
void testNumberSchemaNullValue(Object obj) { | ||
assertTrue(schema.isValid(obj)); | ||
assertFalse(schema.required().isValid(obj)); | ||
assertFalse(schema.positive().isValid(obj)); | ||
assertFalse(schema.range(MIN_VALUE, MAX_VALUE).isValid(obj)); | ||
@Test | ||
void testRequired() { | ||
NumberSchema schema2 = new Validator().number().required(); | ||
|
||
assertFalse(schema2.isValid(ITEM_NULL)); | ||
assertTrue(schema2.isValid(ITEM_BASIC)); | ||
assertTrue(schema2.isValid(ITEM_ZERO)); | ||
assertTrue(schema2.isValid(ITEM_NEGATIVE)); | ||
} | ||
|
||
@Test | ||
void testPositive() { | ||
NumberSchema schema3 = new Validator().number().positive(); | ||
|
||
assertTrue(schema3.isValid(ITEM_NULL)); | ||
assertTrue(schema3.isValid(ITEM_BASIC)); | ||
assertFalse(schema3.isValid(ITEM_ZERO)); | ||
assertFalse(schema3.isValid(ITEM_NEGATIVE)); | ||
} | ||
|
||
@Test | ||
void testNumberSchema() { | ||
final int item1 = 31; | ||
final int item2 = 109; | ||
final int item3 = 0; | ||
final int item4 = -6; | ||
|
||
assertTrue(schema.isValid(item1)); | ||
assertTrue(schema.isValid(item3)); | ||
assertTrue(schema.isValid(item4)); | ||
assertFalse(schema.isValid("nice")); | ||
|
||
assertTrue(schema.required().isValid(item1)); | ||
assertTrue(schema.required().isValid(item3)); | ||
assertTrue(schema.required().isValid(item4)); | ||
|
||
assertTrue(schema.required().positive().isValid(item1)); | ||
assertFalse(schema.required().positive().isValid(item3)); | ||
assertFalse(schema.required().positive().isValid(item4)); | ||
|
||
assertTrue(schema.required().range(MIN_VALUE, MAX_VALUE).isValid(item1)); | ||
assertFalse(schema.required().range(MIN_VALUE, MAX_VALUE).isValid(item2)); | ||
assertFalse(schema.required().range(MIN_VALUE, MAX_VALUE).isValid(item4)); | ||
void testRange() { | ||
NumberSchema schema4 = new Validator().number().range(NUMBER_FROM, NUMBER_TO); | ||
|
||
assertTrue(schema4.isValid(ITEM_NULL)); | ||
assertTrue(schema4.isValid(ITEM_BASIC)); | ||
assertFalse(schema4.isValid(ITEM_ZERO)); | ||
assertFalse(schema4.isValid(ITEM_NEGATIVE)); | ||
assertFalse(schema4.isValid(ITEM_EXTREME)); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,45 +1,67 @@ | ||
package hexlet.code; | ||
|
||
import hexlet.code.schemas.StringSchema; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.NullSource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class StringSchemaTest { | ||
|
||
private static final Object LINE_NULL = null; | ||
private static final String LINE_EMPTY = ""; | ||
private static final String LINE = "Winter is coming!"; | ||
private static final int MIN_VALUE = 5; | ||
private static final int MAX_VALUE = 25; | ||
private static final String LINE_TRUE = " is"; | ||
private static final String LINE_FALSE = "no way"; | ||
|
||
private static StringSchema schema; | ||
@Test | ||
void testIsValid() { | ||
StringSchema schema1 = new Validator().string(); | ||
|
||
@BeforeEach | ||
void getSchema() { | ||
schema = new Validator().string(); | ||
assertTrue(schema1.isValid(LINE)); | ||
assertTrue(schema1.isValid(LINE_NULL)); | ||
assertTrue(schema1.isValid(LINE_EMPTY)); | ||
assertFalse(schema1.isValid(MIN_VALUE)); | ||
} | ||
|
||
@ParameterizedTest | ||
@NullSource | ||
@ValueSource(strings = {""}) | ||
void testStringSchemaNullValue(String obj) { | ||
assertTrue(schema.isValid(obj)); | ||
assertFalse(schema.minLength(MIN_VALUE).isValid(obj)); | ||
assertFalse(schema.contains("nice").isValid(obj)); | ||
assertFalse(schema.required().isValid(obj)); | ||
@Test | ||
void testRequired() { | ||
StringSchema schema2 = new Validator().string().required(); | ||
|
||
assertTrue(schema2.isValid(LINE)); | ||
assertFalse(schema2.isValid(LINE_NULL)); | ||
assertFalse(schema2.isValid(LINE_EMPTY)); | ||
} | ||
|
||
@Test | ||
void testStringSchema() { | ||
final String line = "Winter is coming!"; | ||
|
||
assertTrue(schema.isValid(line)); | ||
assertTrue(schema.required().contains(" is").minLength(MIN_VALUE).isValid(line)); | ||
assertFalse(schema.minLength(MAX_VALUE).contains("no way").isValid(line)); | ||
assertFalse(schema.minLength(MIN_VALUE).contains("no way").isValid(line)); | ||
assertFalse(schema.minLength(MAX_VALUE).contains(" is").isValid(line)); | ||
assertFalse(schema.required().contains(" is").minLength(MIN_VALUE).isValid(MAX_VALUE)); | ||
void testMinLength() { | ||
StringSchema schema3 = new Validator().string().minLength(MIN_VALUE); | ||
|
||
assertTrue(schema3.isValid(LINE)); | ||
assertTrue(schema3.isValid(LINE_NULL)); | ||
assertTrue(schema3.isValid(LINE_EMPTY)); | ||
|
||
StringSchema schema4 = new Validator().string().minLength(MAX_VALUE); | ||
|
||
assertFalse(schema4.isValid(LINE)); | ||
assertTrue(schema4.isValid(LINE_NULL)); | ||
assertTrue(schema4.isValid(LINE_EMPTY)); | ||
} | ||
|
||
@Test | ||
void testContains() { | ||
StringSchema schema5 = new Validator().string().contains(LINE_TRUE); | ||
|
||
assertTrue(schema5.isValid(LINE)); | ||
assertTrue(schema5.isValid(LINE_NULL)); | ||
assertTrue(schema5.isValid(LINE_EMPTY)); | ||
|
||
StringSchema schema6 = new Validator().string().contains(LINE_FALSE); | ||
|
||
assertFalse(schema6.isValid(LINE)); | ||
assertTrue(schema6.isValid(LINE_NULL)); | ||
assertTrue(schema6.isValid(LINE_EMPTY)); | ||
} | ||
} |