This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> implicit class BVList[A](xs: List[A]) { | |
| def :=:(x: A): List[A] = x :: xs | |
| def containz(elem: A): Boolean = xs.contains(elem) | |
| } | |
defined class BVList | |
scala> val xs = List(1, 2, 3) | |
xs: List[Int] = List(1, 2, 3) | |
scala> 5.0 :: xs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Plan(estimate: Int, strategy: String) | |
case class Concept(idea: String) | |
case class Project(concept: Concept, plan: Plan) | |
def workOn(project: Project): String = | |
project match { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> import scalaz._ | |
import scalaz._ | |
scala> import Scalaz.{ToEqualOps => _, _} | |
import Scalaz.{ToEqualOps=>_, _} | |
scala> import equalitydemo.StrictScalazEquality._ // Import a "stricter" policy | |
import equalitydemo.StrictScalazEquality._ | |
scala> 1L === 1 // Now implicit conversions aren't used to fix an quality comparison |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Most ScalaTest matcher syntax is enabled by the existence of typeclasses. For example, | |
// 'have length 3' will work for any type T for which a Length[T] instead is available. | |
// The result type of 'have length 3' is a MatcherFactory1[Any, Length]. | |
// | |
scala> val haveLength3 = have length 3 | |
haveLength3: org.scalatest.matchers.MatcherFactory1[Any,org.scalatest.enablers.Length] = have length 3 | |
// | |
// A MatcherFactory1[Any, Length]'s 'matcher' method can produce a Matcher[T] for any |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// This is not yet at a good point for a milestone release, but I've been doing a lot of | |
// work to try and improve equality and containership checks in ScalaTest and Scalactic. One part of it is | |
// that I figured out how to get equal, be, and contain matchers to participate in the kind of type checking | |
// that previously only worked with === and should ===. The difficulty was that equal, be, and contain | |
// can be used with logical operators and, or, and not (whereas === did not). Here are some examples: | |
// | |
scala> 1 shouldBe 1L |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
organization := "com.example" | |
version := "0.1" | |
scalaVersion := "2.11.1" | |
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8") | |
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
feature("TV power button") { | |
scenario( | |
"User presses power button when TV is off" | |
Given "a TV set that is switched off" | |
When "the power button is pressed" | |
Then "the TV should switch on" | |
) { | |
// Given | |
val tv = new TVSet | |
assert(!tv.isOn) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In ScalaTest 2.1.0, the inspector and inspector shorthand constructs accept more types. | |
// Previously they just worked on Scala collections (though through implicit conversions | |
// you could pass strings and arrays to them). Now they work on any type C for which an implicit | |
// org.scalatest.enablers.Collecting[C] is available, which out-the-box includes Scala and Java | |
// collections, strings, and arrays. Here are a few examples: | |
scala> import org.scalatest._ | |
import org.scalatest._ | |
scala> import MustMatchers._ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In ScalaTest/ScalaUtils 2.1.0 the Prettifier, which in 2.0 just prettified objects at the top level, | |
// has been enhanced to prettify objects contained inside other objects, including Scala and Java | |
// collections, Option, Either, and Try. Here's an example: | |
scala> val actual = Set(List(Map(1 -> Some("2"), 2 -> Some("3")))) | |
actual: scala.collection.immutable.Set[List[scala.collection.immutable.Map[Int,Some[String]]]] = | |
Set(List(Map(1 -> Some(2), 2 -> Some(3)))) | |
scala> val expected = Set(List(Map('1' -> Some(2), '2' -> Some(3)))) | |
expected: scala.collection.immutable.Set[List[scala.collection.immutable.Map[Char,Some[Int]]]] = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.specs2.ScalaCheck | |
import org.specs2.mutable._ | |
case class Box(first: String) | |
class FooSpec extends Specification with ScalaCheck { | |
"this should compile without obfuscated error messages" in { | |
check { (left: String, right: String) => | |
import java.lang.String | |
if (false) |