Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

matchTo does not work on Option #17

Closed
francisdb opened this issue Sep 20, 2019 · 2 comments · Fixed by #92
Closed

matchTo does not work on Option #17

francisdb opened this issue Sep 20, 2019 · 2 comments · Fixed by #92

Comments

@francisdb
Copy link

Option("test") should equal(Some("test"))

Option("test") should matchTo(Some("test"))
overloaded method value should with alternatives:
[error]   (endWithWord: org.scalatest.words.EndWithWord)(implicit ev: Option[String] <:< String)TaskSnapshotFormatSpec.this.ResultOfEndWithWordForString <and>
[error]   (startWithWord: org.scalatest.words.StartWithWord)(implicit ev: Option[String] <:< String)TaskSnapshotFormatSpec.this.ResultOfStartWithWordForString <and>
[error]   (includeWord: org.scalatest.words.IncludeWord)(implicit ev: Option[String] <:< String)TaskSnapshotFormatSpec.this.ResultOfIncludeWordForString <and>
[error]   (notExist: org.scalatest.words.ResultOfNotExist)(implicit existence: org.scalatest.enablers.Existence[Option[String]])org.scalatest.Assertion <and>
[error]   (existWord: org.scalatest.words.ExistWord)(implicit existence: org.scalatest.enablers.Existence[Option[String]])org.scalatest.Assertion <and>
[error]   (containWord: org.scalatest.words.ContainWord)org.scalatest.words.ResultOfContainWord[Option[String]] <and>
[error]   (haveWord: org.scalatest.words.HaveWord)TaskSnapshotFormatSpec.this.ResultOfHaveWordForExtent[Option[String]] <and>
[error]   (beWord: org.scalatest.words.BeWord)TaskSnapshotFormatSpec.this.ResultOfBeWordForAny[Option[String]] <and>
[error]   (inv: org.scalactic.TripleEqualsSupport.TripleEqualsInvocationOnSpread[Option[String]])(implicit ev: Numeric[Option[String]])org.scalatest.Assertion <and>
[error]   [U](inv: org.scalactic.TripleEqualsSupport.TripleEqualsInvocation[U])(implicit constraint: org.scalactic.CanEqual[Option[String],U])org.scalatest.Assertion <and>
[error]   (notWord: org.scalatest.words.NotWord)org.scalatest.words.ResultOfNotWordForAny[Option[String]] <and>
[error]   [TYPECLASS1[_], TYPECLASS2[_]](rightMatcherFactory2: org.scalatest.matchers.MatcherFactory2[Option[String],TYPECLASS1,TYPECLASS2])(implicit typeClass1: TYPECLASS1[Option[String]], implicit typeClass2: TYPECLASS2[Option[String]])org.scalatest.Assertion <and>
[error]   [TYPECLASS1[_]](rightMatcherFactory1: org.scalatest.matchers.MatcherFactory1[Option[String],TYPECLASS1])(implicit typeClass1: TYPECLASS1[Option[String]])org.scalatest.Assertion <and>
[error]   (rightMatcherX1: org.scalatest.matchers.Matcher[Option[String]])org.scalatest.Assertion
[error]  cannot be applied to (com.softwaremill.diffx.scalatest.DiffMatcher.DiffForMatcher[Some[String]])
[error]     Option("test") should matchTo(Some("test"))
@ghostbuster91
Copy link
Collaborator

ghostbuster91 commented Sep 20, 2019

This is because the types don’t quite match up. You have Diff instances in scope for String and Option[String] but the right value you are comparing is of type Some[Int] . As a workaround I will suggest to re-type Some[String] as Option[String] .

A proper way to handle such cases would be to make Diff type class contravariant.

@francisdb
Copy link
Author

Another option for ScalaTest is using OptionValues

class TaskSnapshotFormatSpec extends FlatSpec with Matchers with OptionValues{
...
fooOpt.value should matchTo(fooExpected)
...
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants