Skip to content

Commit

Permalink
throw less exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Gourlay committed Dec 6, 2016
1 parent 840ecc6 commit 123cb12
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/main/scala/com/github/agourlay/cornichon/json/JsonSteps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ object JsonSteps {
val expectedJson = resolveParseJson(expected, session, resolver)
val sessionValue = session.get(sessionKey)
val sessionValueJson = resolveRunJsonPath(jsonPath, sessionValue, resolver)(session)
if (sessionValueJson.isNull) {
throw PathSelectsNothing(jsonPath, parseJsonUnsafe(sessionValue))
} else {
if (sessionValueJson.isNull) Assertion.failWith(PathSelectsNothing(jsonPath, parseJsonUnsafe(sessionValue)))
else {
if (whitelist) {
val expectedWhitelistedValue = whitelistingValue(expectedJson, sessionValueJson).fold(e throw e, identity)
GenericEqualityAssertion(expectedWhitelistedValue, sessionValueJson)
Expand Down Expand Up @@ -219,17 +218,16 @@ object JsonSteps {
AssertStep(
title = assertionTitle,
action = s {
val arrayFromSession = applyPathAndFindArray(jsonPath, resolver)(s, s.get(sessionKey))
val actualValue = removeIgnoredPathFromElements(s, arrayFromSession)
val expectedArray = resolveParseJson(expected, s, resolver).arrayOrObject(
throw NotAnArrayError(expected),
values values,
_ throw NotAnArrayError(expected)
)
if (ordered)
GenericEqualityAssertion(expectedArray, actualValue)
else
CollectionsContainSameElements(expectedArray, actualValue)
resolveParseJson(expected, s, resolver)
.asArray
.fold[Assertion](Assertion.failWith(NotAnArrayError(expected))) { expectedArray
val arrayFromSession = applyPathAndFindArray(jsonPath, resolver)(s, s.get(sessionKey))
val actualValue = removeIgnoredPathFromElements(s, arrayFromSession)
if (ordered)
GenericEqualityAssertion(expectedArray, actualValue)
else
CollectionsContainSameElements(expectedArray, actualValue)
}
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ object Assertion {
val alwaysValid: Assertion = new Assertion { val validated = valid(Done) }
def failWith(error: Throwable) = new Assertion { val validated: ValidatedNel[CornichonError, Done] = invalidNel(StepExecutionError(error)) }
def failWith(error: String) = new Assertion { val validated: ValidatedNel[CornichonError, Done] = invalidNel(BasicError(error)) }
def failWith(error: CornichonError) = new Assertion { val validated: ValidatedNel[CornichonError, Done] = invalidNel(error) }

def all(assertions: List[Assertion]): Assertion = assertions.reduce((acc, assertion) acc.and(assertion))
def any(assertions: List[Assertion]): Assertion = assertions.reduce((acc, assertion) acc.or(assertion))
Expand Down

0 comments on commit 123cb12

Please sign in to comment.