Skip to content

Commit

Permalink
[#78] [WIP] Merged #80: Adding toString implementation for RuleViolat…
Browse files Browse the repository at this point in the history
…ion and GroupViolation
  • Loading branch information
holograph committed Jan 4, 2017
2 parents 537dfaf + 1e49064 commit 556c102
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/src/main/scala/com/wix/accord/Result.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ case class RuleViolation(value: Any,

def applyDescription( description: Description ) =
this.copy( description = Descriptions.combine( this.description, description ) )

override def toString: String = s"${Descriptions.render(description)} $constraint"
}

/** Describes the violation of a group of constraints. For example, the `Or` combinator found in the built-in
Expand All @@ -73,6 +75,8 @@ case class GroupViolation(value: Any,

def applyDescription( description: Description ) =
this.copy( description = Descriptions.combine( this.description, description ) )

override def toString: String = s"${Descriptions.render(description)} $constraint: ${children.toString}"
}

/** A base trait for validation results.
Expand Down
27 changes: 27 additions & 0 deletions api/src/test/scala/com/wix/accord/ViolationSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.wix.accord

import com.wix.accord.Descriptions.Generic
import org.scalatest.{FlatSpec, Matchers}

/**
* Created by grenville on 9/6/16.
*/
class ViolationSpec extends FlatSpec with Matchers {

val subSampleRule = RuleViolation("value", "is null", Generic("subSampleRule"))
val subSampleGroup = GroupViolation("subgroup", "is not valid",
children = Set(subSampleRule), Generic("request"))
val sampleRule1 = RuleViolation("value", "is null", Generic("sampleRule1"))
val sampleRule2 = RuleViolation("value", "is null", Generic("sampleRule2"))
val sampleGroup = GroupViolation("group", "is not valid",
children = Set(sampleRule1, sampleRule2, subSampleGroup), Generic("request"))

"Violation" should "produce a string representation for a RuleViolation" in {
sampleRule1.toString shouldEqual "sampleRule1 is null"
}

it should "produce a string representation for a GroupViolation" in {
sampleGroup.toString shouldEqual "request is not valid: Set(sampleRule1 is null, sampleRule2 is null, " +
"subgroup is not valid: Set(subSampleRule is null))"
}
}

0 comments on commit 556c102

Please sign in to comment.