Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cast args when applying mockFunctionValDef in scala 3 #529

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private[clazz] object MockMaker:
Select.unique(
Apply(
Select.unique(Ref(mockFunctionValDef.symbol), "apply"),
args.flatten.collect { case t: Term => t }
args.flatten.collect { case t: Term => Select.unique(t, "asInstanceOf") }
),
"asInstanceOf"
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2011-2015 ScalaMock Contributors (https://github.com/paulbutcher/ScalaMock/graphs/contributors)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package org.scalamock.test.scalatest

import org.scalamock.scalatest.MockFactory
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

trait Bar
case class Baz(s: String) extends Bar

trait Foo {
def p[T <: Bar](gen: Seq[T], t: Seq[T] => Seq[String]): Seq[String] = t(gen)
def q[T <: Bar](gen: Seq[T]): Seq[String] = gen.map(_.toString)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method (q) was already absolutely fine, it was only the above method p that caused compilation failures

Copy link
Contributor

@goshacodes goshacodes Sep 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked one more time.
Lets add some more simple methods in this trait and also I would change names of method and trait so they would correspond to what we are trying to achieve.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, should have time tomorrow

Copy link
Contributor Author

@hughsimpson hughsimpson Sep 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah thinking about it would be good to have some

def overloaded(gen: Seq[C1], t: Seq[C1] => Seq[String]): Seq[String] = t(gen)
def overloaded(gen: Seq[C2], t: Seq[C2] => Seq[String]): Seq[String] = t(gen)

stuff wouldn't it...

}

class TypeParamOnFunctionArgToMethod extends AnyFlatSpec with Matchers with MockFactory {

"TypeParamOnFunctionArgToMethod suite" should "permit mocking a method that takes a function w/ input parameterised by fn type param" in {
val mockedTrait = mock[Foo]
(mockedTrait.p[Baz](_:Seq[Baz], _: Seq[?] => Seq[String])).expects(Seq(Baz("one")), *).returning(Seq("one"))
(mockedTrait.q[Baz](_:Seq[Baz])).expects(Seq(Baz("one"))).returning(Seq("one"))

mockedTrait.p(Seq(Baz("one")), (_:Seq[Baz]).map(_.toString)) shouldBe Seq("one")
mockedTrait.q(Seq(Baz("one"))) shouldBe Seq("one")
}
}
Loading