Skip to content

Commit aa4de33

Browse files
bakeemawaytoysjbgi
authored andcommitted
Added working tests coverage for the StringBuffer and StringBuilder semigroup implementations. Added the Semigroup tests to the list of Scalacheck test suite.
1 parent a8e979f commit aa4de33

2 files changed

Lines changed: 37 additions & 26 deletions

File tree

props-core-scalacheck/src/test/scala/fj/CheckSemigroup.scala

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,36 @@ object CheckIOSemigroup extends Properties("ioSemigroup") {
7070

7171
}
7272

73-
object CheckStringBuilder extends Properties("stringBuilderSemigroup") {
74-
75-
val s: Semigroup[java.lang.StringBuilder] = Semigroup.stringBuilderSemigroup
73+
/**
74+
* A [[Properties]] implementation for testing [[Semigroup]] implementations that
75+
* apply to mutable builder style classes such as [[java.lang.StringBuilder]] and
76+
* [[java.lang.StringBuffer]].
77+
*
78+
* @param s a Semigroup implementation to test.
79+
* @param desc a description of the Semigroup implementation under test.
80+
* @param conversion a function that converts a value of type T to a new instance of type S.
81+
* @tparam T the type that the builder constructs.
82+
* @tparam S the type to which the Semigroup applies.
83+
*/
84+
case class CheckMutableBuilder[S, T](s: Semigroup[S], desc: String, conversion: T => S)(implicit val arbitrary: Arbitrary[T]) extends Properties(desc) {
7685

77-
implicit def toStringBuilder(str: String): java.lang.StringBuilder = new java.lang.StringBuilder(str)
86+
implicit def toBuilder(t: T): S = conversion(t)
7887

79-
property("sum(x,y)") = forAll((x: String, y: String, z: String) =>
88+
property("sum(x,y)") = forAll((x: T, y: T, z: T) =>
8089
s.sum(s.sum(x, y), z).toString == s.sum(x, s.sum(y, z)).toString
8190
)
8291

83-
property("sum()") = forAll((x: String, y: String, z: String) => {
92+
property("sum()") = forAll((x: T, y: T, z: T) => {
8493
val sf = s.sum()
85-
sf.f(sf.f(x).f(y)).f(z).toString == sf.f(x).f(sf.f(y).f(z.toString))
94+
sf.f(sf.f(x).f(y)).f(z).toString == sf.f(x).f(sf.f(y).f(z)).toString
8695
})
8796

88-
property("dual()") = forAll((x: String, y: String, z: String) => {
97+
property("dual()") = forAll((x: T, y: T, z: T) => {
8998
val sd = s.dual()
90-
sd.sum(sd.sum(x, y), z).toString == sd.sum(x, sd.sum(y, z).toString)
99+
sd.sum(sd.sum(x, y), z).toString == sd.sum(x, sd.sum(y, z)).toString
91100
})
92101

93-
property("sum(x)") = forAll((x: String, y: String) =>
102+
property("sum(x)") = forAll((x: T, y: T) =>
94103
s.sum(x).f(y).toString == s.sum(x, y).toString)
95104

96105
}
@@ -193,8 +202,8 @@ object CheckSemigroup extends Properties("Semigroup") {
193202
include(SemigroupProperties(Semigroup.streamSemigroup[Int](), "streamSemigroup"))
194203

195204
include(SemigroupProperties(Semigroup.stringSemigroup, "stringSemigroup"))
196-
include(SemigroupProperties(Semigroup.stringBufferSemigroup, "stringBufferSemigroup"))
197-
include(SemigroupProperties(Semigroup.stringBuilderSemigroup, "stringBuilderSemigroup"))
205+
include(CheckMutableBuilder(Semigroup.stringBufferSemigroup, "stringBufferSemigroup", (s: String) => new java.lang.StringBuffer(s)))
206+
include(CheckMutableBuilder(Semigroup.stringBuilderSemigroup, "stringBuilderSemigroup", (s: String) => new java.lang.StringBuilder(s)))
198207

199208

200209
include(SemigroupProperties(Semigroup.unitSemigroup, "unitSemigroup"))
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package fj
22

33
object Tests {
4-
def tests = List (
4+
def tests = List(
55
CheckP2.properties,
6+
CheckSemigroup.properties,
67
fj.data.CheckArray.properties,
78
fj.data.CheckIO.properties,
89
fj.data.CheckIteratee.properties,
@@ -19,26 +20,27 @@ object Tests {
1920

2021
def main(args: Array[String]) {
2122
run(tests)
22-
// System.exit(0)
23+
// System.exit(0)
2324
}
2425

25-
import org.scalacheck.Prop
26-
import org.scalacheck.Test
2726
import org.scalacheck.Test.check
27+
import org.scalacheck.{Prop, Test}
2828

2929
def run(tests: List[(String, Prop)]) =
3030
tests foreach { case (name, p) => {
31-
val c = check(new Test.Parameters.Default { override val maxSize = 20 }, p)
32-
c.status match {
33-
case Test.Passed => println("Passed " + name)
34-
case Test.Proved(_) => println("Proved " + name)
35-
case f @ Test.Failed(_, _) => sys.error(name + ": " + f)
36-
case Test.Exhausted => println("Exhausted " + name)
37-
case f @ Test.PropException(_, e, _) => {
38-
e.printStackTrace
39-
sys.error(name + ": " + f)
40-
}
31+
val c = check(new Test.Parameters.Default {
32+
override val maxSize = 20
33+
}, p)
34+
c.status match {
35+
case Test.Passed => println("Passed " + name)
36+
case Test.Proved(_) => println("Proved " + name)
37+
case f@Test.Failed(_, _) => sys.error(name + ": " + f)
38+
case Test.Exhausted => println("Exhausted " + name)
39+
case f@Test.PropException(_, e, _) => {
40+
e.printStackTrace
41+
sys.error(name + ": " + f)
4142
}
4243
}
4344
}
45+
}
4446
}

0 commit comments

Comments
 (0)