Skip to content

Instantly share code, notes, and snippets.

View NTCoding's full-sized avatar

Nick Tune NTCoding

View GitHub Profile
@NTCoding
NTCoding / VO.cs
Last active November 25, 2018 12:14
public abstract class Comment
{
public static ProposedComment Parse(String proposedComment)
{
return new ProposedComment(proposedComment);
}
}
public class ProposedComment : Comment
{
lazy val form = Form(mapping(
"name" -> nonEmptyText,
"age" -> number,
"favouriteColour" -> mandatoryIf(
isEqual("name", "Francoise") and isEqual("age", "21"),
nonEmptyText
)
)(Model.apply)(Model.unapply))
behavior of "conditional mappings"
it should "contain field level errors for fields using conditional mappings" in {
val data = Map("nonUkResident" -> "true")
val res = form.bind(data)
assert(res.errors.length == 2)
assert(res.errors.head.key === "country")
assert(res.errors.tail.head.key === "email")
}
behavior of "vanilla play conditional mapping"
it should "not contain an error for the conditional validation when there is a field-level error" in {
val data = Map("nonUkResident" -> "true")
val res = form.bind(data)
assert(res.errors.length == 1)
assert(res.errors.head.key === "email")
}
class SetExample extends FreeSpec with Matchers {
val userEmail = "abc@mailinator.com"
var sentEmailTo = "NOTSENT"
"When the winner is selected they are immediately notified by email" in {
SelectWinnerOfCompetition(ret(userEmail), set(sentEmailTo = _))()
assert(sentEmailTo === userEmail)
}
def ret[A](a: A): Unit => A = _ => a
def retF[A,B](a: A)(b: B): A => Future[B] =
x => if (x == a) Future.successful(b) else throw ReturnFailed(s"$x did not equal $a")
// return the stubbed value if the actual arguments match the expected arguments
def ret[A,B](a: A)(b: B): A => B =
x => if (x == a) b else throw ReturnFailed(s"$x did not equal $a")
def ret[A,B,C](a: A, b: B)(c: C): (A,B) => C =
(x, y) => if (x == a && y == b) c else throw ReturnFailed(s"$x and $y did not equal $a and $b")
class TestWithoutMocks extends FreeSpec with Matchers with FutureAwaits with DefaultAwaitTimeout {
val userId = "12345"
val user = User(userId)
val friend = User("friendId")
val friendsDetails = NewAccountDetails("abc@mailinator.com", "abb", 1)
val results = Seq(PromoteToGoldStatus(userId))
"When an existing user" - {
"recommends a friend using details that validate" - {
"the friend is created and the referral policy's decision is returned" in {
class RecommendAFriendSpecs extends FreeSpec with Matchers with FutureAwaits with DefaultAwaitTimeout {
val repo = mock(classOf[UserRepository])
val validator = mock(classOf[NewAccountValidator])
val policy = mock(classOf[ReferralPolicy])
val userId = "12345"
val user = User(userId)
val friend = User("friendId")
val friendsDetails = NewAccountDetails("abc@mailinator.com", "abb", 1)
val results = Seq(PromoteToGoldStatus(userId))
v(n) map { err } getOrElse { f(u) ..