This file contains 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
// Proposed ehancement to ScalaTest to surface more info about compiler errors | |
// Existing syntax | |
// Assertions: | |
assertDoesNotCompile("val i: String = 1") // Expects either a type or parse error | |
assertTypeError("val i: String = 1") // Expects just a type error (not a parse error) | |
assertCompiles("val i: Int = 1") // Expects no error during compilation | |
// Matchers: | |
"val i: String = 1" shouldNot compile // Expects either a type or parse error | |
"val i: String = 1" shouldNot typeCheck // Expects either a type error |
This file contains 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
:paste | |
type ErrorMessage = String | |
trait LeftBiased | |
trait RightBiased | |
sealed abstract class Or[+G, +B] extends LeftBiased { | |
def map[H](f: G => H): H Or B |
This file contains 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 org.scalatest.Assertions._ | |
import org.scalatest.Assertions._ | |
scala> val x = 1 | |
x: Int = 1 | |
scala> assert(List(1, 2, 3).contains(x) && x > 1) | |
org.scalatest.exceptions.TestFailedException: List(1, 2, 3) contained 1, but 1 was not greater than 1 | |
at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:541) | |
at org.scalatest.Assertions$.newAssertionFailedException(Assertions.scala:1414) |
This file contains 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
// Here's a typeclass trait for types that can be sliced | |
trait Slicing[T] { | |
def slice(o: T, from: Int, until: Int): T | |
} | |
// The companion object provides impicits for String and Vector | |
object Slicing { | |
implicit val slicingNatureOfString: Slicing[String] = | |
new Slicing[String] { |
This file contains 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
// | |
// TLDR: In ScalaTest, matcher composition is simply function composition. | |
// | |
// | |
// The beOdd matcher in the documentation for org.scalatest.matchers.Matcher is simple, but it will | |
// concatenate strings that are not needed. If you want to avoid that as a (premature) performance | |
// optimization, you can write it like this: | |
// | |
scala> val beOdd = |
This file contains 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 org.scalactic.numbers._ | |
import numbers._ | |
// Pos is a type that ensures its "contained" value is a positive Int. I put "contained" | |
// in scare quotes because Pos is an AnyVal, so it should only box when an Int would box. | |
// You can create them directly with positive integer literals, like this: | |
scala> Pos(3) | |
res0: org.scalactic.numbers.Pos = Pos(3) | |
// If you pass an Int variable, or a non-positive Int literal, to this factory method, it |
This file contains 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 org.scalactic._ | |
import org.scalactic._ | |
// A Validation is either a Pass or a Fail. A Validation is like an | |
// Option with the reverse attitude: Pass is like None, it contains no | |
// value and just means the value being validated is OK as is. Fail is | |
// like Some, it holds an error value describing the validation failure. | |
scala> def isPositive(i: Int): Validation[ErrorMessage] = | |
| if (i > 0) Pass else Fail(s"$i was not positive") | |
isPositive: (i: Int)org.scalactic.Validation[org.scalactic.ErrorMessage] |
This file contains 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
// Can also do this: | |
scala> def addBippy(set: EquaSets[String]#EquaSet): set.path.EquaSet = { | |
| set.copyInto(set.path) union set.path.EquaSet("bippy") | |
| } | |
addBippy: (set: org.scalactic.EquaSets[String]#EquaSet)set.path.EquaSet | |
scala> val lowered = SortedEquaSets[String](StringNormalizations.lowerCased.toOrderingEquality) | |
lowered: org.scalactic.SortedEquaSets[String] = org.scalactic.SortedEquaSets@26273e89 |
This file contains 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 from github ScalaTest master, not in any release: | |
// Here's a method that takes a general one. This method asks the passed EquaSet | |
// for its enclosing EquaSets, and stores it in variable path. It then "copies" | |
// the passed EquaSet into the new path, after which it can union with a newly | |
// created EquaSet at the same path and return it. | |
scala> def addBippy(set: EquaSets[String]#EquaSet): EquaSets[String]#EquaSet = { | |
| val path = set.enclosingEquaSets | |
| val relocated = set.copyInto(path) | |
| relocated union path.EquaSet("bippy") |
This file contains 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 gist shows how the compiler will deal with EquaSets as method parameters. | |
// In short, the compiler will let you do anything that is known to be OK. In | |
// particular, it won't let you union, intersect, or diff two EquaSets unless | |
// the full path dependent type is known and matches. | |
// | |
scala> import org.scalactic._ | |
scala> val trimmed = EquaSets[String](StringNormalizations.trimmed.toHashingEquality) | |
trimmed: org.scalactic.EquaSets[String] = org.scalactic.EquaSets@2c011b16 |
NewerOlder