Skip to content

Commit

Permalink
Add NumberSchema and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niyatanya committed Apr 10, 2024
1 parent 542c0ef commit 5c462a9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/hexlet/code/Validator.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package hexlet.code;

import hexlet.code.schemas.NumberSchema;
import hexlet.code.schemas.StringSchema;

public class Validator {
public StringSchema string() {
return new StringSchema();
}
public NumberSchema number() {
return new NumberSchema();
}
}
26 changes: 26 additions & 0 deletions app/src/main/java/hexlet/code/schemas/NumberSchema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package hexlet.code.schemas;

import lombok.NoArgsConstructor;
import java.util.function.Predicate;

@NoArgsConstructor
public class NumberSchema extends BaseSchema<Integer> {
@Override
public NumberSchema required() {
Predicate<Integer> requiredFunction = ((value) -> value != null);
checkers.put("required", requiredFunction);
return this;
}

public NumberSchema positive() {
Predicate<Integer> positiveFunction = ((value) -> value > 0);
checkers.put("positive", positiveFunction);
return this;
}

public NumberSchema range(int startIndex, int finalIndex) {
Predicate<Integer> rangeFunction = ((value) -> value >= startIndex || value <= finalIndex);
checkers.put("range", rangeFunction);
return this;
}
}
29 changes: 29 additions & 0 deletions app/src/test/java/hexlet/code/ValidatorTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hexlet.code;

import hexlet.code.schemas.NumberSchema;
import hexlet.code.schemas.StringSchema;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -9,10 +10,13 @@

public class ValidatorTest {
private static StringSchema stringSchema;
private static NumberSchema numberSchema;
@BeforeEach
public void preparation() {
Validator val = new Validator();
stringSchema = val.string();
Validator val1 = new Validator();
numberSchema = val1.number();
}

@Test
Expand Down Expand Up @@ -42,4 +46,29 @@ public void stringSchemaTestContains() {
assertTrue(stringSchema.contains("hex").isValid("hexlet"));
assertFalse(stringSchema.contains("hes").isValid("hexlet"));
}

@Test
public void numberSchemaTestNormal() {
assertTrue(numberSchema.isValid(2));
assertTrue(numberSchema.isValid(null));
}

@Test
public void numberSchemaTestRequired() {
numberSchema.required();
assertFalse(numberSchema.isValid(null));
}

@Test
public void numberSchemaTestPositive() {
assertTrue(numberSchema.isValid(-10));
numberSchema.positive();
assertFalse(numberSchema.isValid(-10));
}

@Test
public void numberSchemaTestRange() {
numberSchema.range(5, 10);
assertTrue(numberSchema.isValid(10));
}
}

0 comments on commit 5c462a9

Please sign in to comment.