-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This is an expectation for JSON Array type. It checks whether all elements of the array match to the one given expectation. For example: ``` [1, 2, 1, 1].matches(anyNumberOf(1)) // fails - mismatch for `2` [1, 1, 1, 1].matches(anyNumberOf(1)) // succeeds ``` Available for use via static factory-method `Expectations.anyNumberOf()`
- Loading branch information
Showing
7 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
jazon-core/src/main/java/com/zendesk/jazon/expectation/AnyNumberOf.java
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,12 @@ | ||
package com.zendesk.jazon.expectation; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
class AnyNumberOf { | ||
|
||
@Getter(AccessLevel.PACKAGE) | ||
private final Object elementExpectation; | ||
} |
67 changes: 67 additions & 0 deletions
67
jazon-core/src/main/java/com/zendesk/jazon/expectation/ArrayEachElementExpectation.java
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,67 @@ | ||
package com.zendesk.jazon.expectation; | ||
|
||
import com.zendesk.jazon.MatchResult; | ||
import com.zendesk.jazon.actual.*; | ||
import com.zendesk.jazon.mismatch.MismatchWithPath; | ||
import com.zendesk.jazon.mismatch.NullMismatch; | ||
import com.zendesk.jazon.mismatch.TypeMismatch; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.ListIterator; | ||
|
||
import static com.zendesk.jazon.MatchResult.failure; | ||
import static com.zendesk.jazon.MatchResult.success; | ||
|
||
@RequiredArgsConstructor | ||
@EqualsAndHashCode | ||
class ArrayEachElementExpectation implements JsonExpectation { | ||
|
||
private final JsonExpectation expectationForEachElement; | ||
|
||
@Override | ||
public MatchResult match(ActualJsonNumber actualNumber, String path) { | ||
return failure(typeMismatch(ActualJsonNumber.class, path)); | ||
} | ||
|
||
@Override | ||
public MatchResult match(ActualJsonObject actualObject, String path) { | ||
return failure(typeMismatch(ActualJsonObject.class, path)); | ||
} | ||
|
||
@Override | ||
public MatchResult match(ActualJsonString actualString, String path) { | ||
return failure(typeMismatch(ActualJsonString.class, path)); | ||
} | ||
|
||
@Override | ||
public MatchResult match(ActualJsonNull actualNull, String path) { | ||
return failure( | ||
new NullMismatch<>(this) | ||
.at(path) | ||
); | ||
} | ||
|
||
@Override | ||
public MatchResult match(ActualJsonArray actualArray, String path) { | ||
ListIterator<Actual> actualValues = actualArray.list().listIterator(); | ||
while (actualValues.hasNext()) { | ||
Actual actualValue = actualValues.next(); | ||
MatchResult matchResult = actualValue.accept(expectationForEachElement, path + "." + actualValues.previousIndex()); | ||
if (!matchResult.ok()) { | ||
return matchResult; | ||
} | ||
} | ||
return success(); | ||
} | ||
|
||
@Override | ||
public MatchResult match(ActualJsonBoolean actualBoolean, String path) { | ||
return failure(typeMismatch(ActualJsonBoolean.class, path)); | ||
} | ||
|
||
private MismatchWithPath typeMismatch(Class<? extends Actual> actualType, String path) { | ||
return new TypeMismatch(ActualJsonArray.class, actualType) | ||
.at(path); | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
jazon-core/src/main/java/com/zendesk/jazon/expectation/Expectations.java
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,8 @@ | ||
package com.zendesk.jazon.expectation; | ||
|
||
public class Expectations { | ||
|
||
public static AnyNumberOf anyNumberOf(Object element) { | ||
return new AnyNumberOf(element); | ||
} | ||
} |
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