alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (TryTest.scala)

This example Scala source code file (TryTest.scala) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Scala by Example" TM.

Learn more about this Scala project at its project page.

Java - Scala tags/keywords

equal, failure, int, show, speclite, success, throwable, trytest, validation

The TryTest.scala Scala example source code

package scalaz
package std

import std.AllInstances._
import scalaz.scalacheck.ScalazArbitrary._
import org.scalacheck.Prop.forAll
import scala.util.{Failure, Success}

object TryTest extends SpecLite {

  // these probably aren't a great idea, but they are just for tests
  implicit val showThrowable: Show[Throwable] = Show.showA
  implicit val equalThrowable: Equal[Throwable] = Equal.equalA


  "toDisjunction Failure" ! forAll { t: Throwable =>
    `try`.toDisjunction(Failure[Int](t)).isLeft
  }

  "toDisjunction Success" ! forAll { i: Int =>
    `try`.toDisjunction(Success(i)).isRight
  }

  "disjunction round trip" ! forAll { d: Throwable \/ Int =>
    val iso = `try`.tryDisjunctionIso
    iso.to(iso.from(d)) must_=== d
  }

  "toValidation Failure" ! forAll { t: Throwable =>
    `try`.toValidation(Failure[Int](t)).isFailure
  }

  "toValidation Success" ! forAll { i: Int =>
    `try`.toValidation(Success[Int](i)).isSuccess
  }

  "toValidationNel Failure" ! forAll {t: Throwable =>
    `try`.toValidationNel(Failure[Int](t)).isFailure
  }

  "toValidationNel Success" ! forAll {i: Int =>
    `try`.toValidationNel(Success[Int](i)).isSuccess
  }

  "validation round trip" ! forAll { v: Validation[Throwable, Int] =>
    val iso = `try`.tryValidationIso
    iso.to(iso.from(v)) must_=== v
   }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala TryTest.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.