From ffcad3cdad77e8439cce9c4d01c3923ab2e4cbee Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Fri, 1 Jul 2016 23:03:03 +0100 Subject: [PATCH] Make CrossVersion & ModuleId more commonly equal Two instance of functions defined in the same way don't equal themselves, but the same instance of a function does. So by using a val idStringFun for full and binary, and making Binary and Full case classes there are much better chances that ModuleId's defined in the same way will be equal. --- .../sbt/librarymanagement/CrossVersion.scala | 15 +++++++++++++-- .../src/test/scala/CrossVersionTest.scala | 9 +++++++++ .../src/test/scala/ModuleIdTest.scala | 17 +++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 librarymanagement/src/test/scala/ModuleIdTest.scala diff --git a/librarymanagement/src/main/scala/sbt/librarymanagement/CrossVersion.scala b/librarymanagement/src/main/scala/sbt/librarymanagement/CrossVersion.scala index d3cf58d6a..d9869e030 100644 --- a/librarymanagement/src/main/scala/sbt/librarymanagement/CrossVersion.scala +++ b/librarymanagement/src/main/scala/sbt/librarymanagement/CrossVersion.scala @@ -26,6 +26,11 @@ object CrossVersion { */ final class Binary(val remapVersion: String => String) extends CrossVersion { override def toString = "Binary" + override def hashCode = remapVersion.## + override def equals(that: Any) = that match { + case that: Binary => this.remapVersion == that.remapVersion + case _ => false + } } /** @@ -35,6 +40,11 @@ object CrossVersion { */ final class Full(val remapVersion: String => String) extends CrossVersion { override def toString = "Full" + override def hashCode = remapVersion.## + override def equals(that: Any) = that match { + case that: Full => this.remapVersion == that.remapVersion + case _ => false + } } private val disabledTag = implicitly[FastTypeTag[Disabled.type]] @@ -75,7 +85,7 @@ object CrossVersion { } /** Cross-versions a module with the full version (typically the full Scala version). */ - def full: CrossVersion = new Full(idFun) + def full: CrossVersion = new Full(idStringFun) /** * Cross-versions a module with the result of applying `remapVersion` to the full version @@ -84,7 +94,7 @@ object CrossVersion { def fullMapped(remapVersion: String => String): CrossVersion = new Full(remapVersion) /** Cross-versions a module with the binary version (typically the binary Scala version). */ - def binary: CrossVersion = new Binary(idFun) + def binary: CrossVersion = new Binary(idStringFun) /** * Cross-versions a module with the result of applying `remapVersion` to the binary version @@ -93,6 +103,7 @@ object CrossVersion { def binaryMapped(remapVersion: String => String): CrossVersion = new Binary(remapVersion) private[this] def idFun[T]: T => T = x => x + private[this] val idStringFun = idFun[String] private[sbt] def append(s: String): Option[String => String] = Some(x => crossName(x, s)) diff --git a/librarymanagement/src/test/scala/CrossVersionTest.scala b/librarymanagement/src/test/scala/CrossVersionTest.scala index 3f8a608ea..f70343e73 100644 --- a/librarymanagement/src/test/scala/CrossVersionTest.scala +++ b/librarymanagement/src/test/scala/CrossVersionTest.scala @@ -115,4 +115,13 @@ class CrossVersionTest extends UnitSpec { it should "return binary Scala version for 2.10.1 as 2.10" in { CrossVersion.binaryScalaVersion("2.10.1") shouldBe "2.10" } + it should "return disabled cross version as equal to a copy" in { + CrossVersion.Disabled shouldBe CrossVersion.Disabled + } + it should "return full cross version as equal to a copy" in { + CrossVersion.full shouldBe CrossVersion.full + } + it should "return binary cross version as equal to a copy" in { + CrossVersion.binary shouldBe CrossVersion.binary + } } diff --git a/librarymanagement/src/test/scala/ModuleIdTest.scala b/librarymanagement/src/test/scala/ModuleIdTest.scala new file mode 100644 index 000000000..8b1d7d7b7 --- /dev/null +++ b/librarymanagement/src/test/scala/ModuleIdTest.scala @@ -0,0 +1,17 @@ +package sbt.librarymanagement + +import sbt.internal.util.UnitSpec + +class ModuleIdTest extends UnitSpec { + "Module Id" should "return cross-disabled module id as equal to a copy" in { + ModuleID("com.acme", "foo", "1") shouldBe ModuleID("com.acme", "foo", "1") + } + it should "return cross-full module id as equal to a copy" in { + (ModuleID("com.acme", "foo", "1") cross CrossVersion.full) shouldBe + (ModuleID("com.acme", "foo", "1") cross CrossVersion.full) + } + it should "return cross-binary module id as equal to a copy" in { + (ModuleID("com.acme", "foo", "1") cross CrossVersion.binary) shouldBe + (ModuleID("com.acme", "foo", "1") cross CrossVersion.binary) + } +}