Skip to content

Commit 36ef31d

Browse files
committed
add
1 parent 347e4d4 commit 36ef31d

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

app/src/test/java/hexlet/code/MapSchemaTest.java

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
import hexlet.code.schemas.BaseSchema;
55
import org.junit.jupiter.api.BeforeEach;
66
import org.junit.jupiter.api.Test;
7-
87
import java.util.HashMap;
98
import java.util.Map;
10-
119
import static org.junit.jupiter.api.Assertions.assertFalse;
1210
import static org.junit.jupiter.api.Assertions.assertTrue;
1311

@@ -41,73 +39,63 @@ void testRequired() {
4139
void testSizeof() {
4240
schema.sizeof(2);
4341
assertTrue(schema.isValid(null));
44-
4542
Map<String, String> data = new HashMap<>();
4643
data.put("key1", "value1");
4744
assertFalse(schema.isValid(data));
48-
4945
data.put("key2", "value2");
5046
assertTrue(schema.isValid(data));
5147
}
5248

5349
@Test
5450
void testMultipleConstraints() {
5551
schema.required().sizeof(2);
56-
5752
Map<String, String> data = new HashMap<>();
5853
assertFalse(schema.isValid(null));
5954
assertFalse(schema.isValid(data));
60-
6155
data.put("key1", "value1");
6256
assertFalse(schema.isValid(data));
63-
6457
data.put("key2", "value2");
6558
assertTrue(schema.isValid(data));
66-
6759
data.put("key3", "value3");
6860
assertFalse(schema.isValid(data));
6961
}
7062

7163
@Test
7264
void testOverridingConstraints() {
7365
schema.sizeof(3).sizeof(2);
74-
7566
Map<String, String> data = new HashMap<>();
7667
data.put("key1", "value1");
7768
data.put("key2", "value2");
78-
7969
assertTrue(schema.isValid(data));
80-
8170
data.put("key3", "value3");
8271
assertFalse(schema.isValid(data));
8372
}
8473

8574
@Test
8675
void testShape() {
8776
Map<String, BaseSchema<?>> schemas = new HashMap<>();
88-
schemas.put("firstName", validator.string().required());
89-
schemas.put("lastName", validator.string().required().minLength(2));
90-
77+
schemas.put("firstName", validator.string().required().minLength(3));
78+
schemas.put("lastName", validator.string().required().contains("ov"));
9179
schema.shape(schemas);
9280

9381
Map<String, String> human1 = new HashMap<>();
9482
human1.put("firstName", "John");
95-
human1.put("lastName", "Smith");
83+
human1.put("lastName", "Smithov");
9684
assertTrue(schema.isValid(human1));
9785

9886
Map<String, String> human2 = new HashMap<>();
99-
human2.put("firstName", "John");
100-
human2.put("lastName", null);
87+
human2.put("firstName", "Jo");
88+
human2.put("lastName", "Smithov");
10189
assertFalse(schema.isValid(human2));
10290

10391
Map<String, String> human3 = new HashMap<>();
10492
human3.put("firstName", "Anna");
105-
human3.put("lastName", "B");
93+
human3.put("lastName", "Brown");
10694
assertFalse(schema.isValid(human3));
10795

10896
Map<String, String> human4 = new HashMap<>();
10997
human4.put("firstName", "Anna");
110-
human4.put("lastName", "Brown");
98+
human4.put("lastName", "Petrov");
11199
assertTrue(schema.isValid(human4));
112100
}
113101

@@ -116,15 +104,14 @@ void testShapeWithOptionalFields() {
116104
Map<String, BaseSchema<?>> schemas = new HashMap<>();
117105
schemas.put("firstName", validator.string().required());
118106
schemas.put("lastName", validator.string().minLength(2));
119-
120107
schema.shape(schemas);
121108

122109
Map<String, String> human1 = new HashMap<>();
123110
human1.put("firstName", "John");
124111
assertTrue(schema.isValid(human1));
125112

126113
Map<String, String> human2 = new HashMap<>();
127-
human2.put("firstName", "John");
114+
human2.put("firstName", "Jo");
128115
human2.put("lastName", "B");
129116
assertFalse(schema.isValid(human2));
130117

@@ -139,7 +126,6 @@ void testShapeWithDifferentTypes() {
139126
Map<String, BaseSchema<?>> schemas = new HashMap<>();
140127
schemas.put("name", validator.string().required());
141128
schemas.put("age", validator.number().required().positive());
142-
143129
schema.shape(schemas);
144130

145131
Map<String, Object> person1 = new HashMap<>();
@@ -154,17 +140,12 @@ void testShapeWithDifferentTypes() {
154140

155141
Map<String, Object> person3 = new HashMap<>();
156142
person3.put("name", "John");
157-
person3.put("age", 0);
143+
person3.put("age", null);
158144
assertFalse(schema.isValid(person3));
159145

160146
Map<String, Object> person4 = new HashMap<>();
161-
person4.put("name", "John");
162-
person4.put("age", null);
147+
person4.put("name", null);
148+
person4.put("age", 30);
163149
assertFalse(schema.isValid(person4));
164-
165-
Map<String, Object> person5 = new HashMap<>();
166-
person5.put("name", null);
167-
person5.put("age", 30);
168-
assertFalse(schema.isValid(person5));
169150
}
170151
}

app/src/test/java/hexlet/code/ValidatorTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ void testContains() {
5454
@Test
5555
void testMultipleConstraints() {
5656
stringSchema.required().minLength(5).contains("hex");
57+
assertFalse(stringSchema.isValid(null));
58+
assertFalse(stringSchema.isValid(""));
59+
assertFalse(stringSchema.isValid("test"));
5760
assertTrue(stringSchema.isValid("hexagon"));
61+
assertTrue(stringSchema.isValid("hexlet"));
62+
assertFalse(stringSchema.isValid("hex"));
63+
assertTrue(stringSchema.isValid("hexagonhex"));
5864
}
5965

6066
@Test
@@ -75,9 +81,24 @@ void testShape() {
7581
person1.put("lastName", "Petrov");
7682
assertTrue(mapSchema.isValid(person1));
7783

84+
Map<String, String> person2 = new HashMap<>();
85+
person2.put("firstName", "Yan");
86+
person2.put("lastName", "Ivanov");
87+
assertFalse(mapSchema.isValid(person2));
88+
89+
Map<String, String> person3 = new HashMap<>();
90+
person3.put("firstName", "Ya");
91+
person3.put("lastName", "Petrov");
92+
assertFalse(mapSchema.isValid(person3));
93+
94+
Map<String, String> person4 = new HashMap<>();
95+
person4.put("firstName", "yana");
96+
person4.put("lastName", "Petro");
97+
assertFalse(mapSchema.isValid(person4));
98+
7899
Map<String, String> person5 = new HashMap<>();
79-
person5.put("firstName", "yana");
100+
person5.put("firstName", "Yan");
80101
person5.put("lastName", "Petrov");
81-
assertTrue(mapSchema.isValid(person5));
102+
assertFalse(mapSchema.isValid(person5));
82103
}
83104
}

0 commit comments

Comments
 (0)