Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding toString implementation for RuleViolation and GroupViolation #80

Merged
merged 1 commit into from
Jan 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))"
}
}