@@ -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" ))
0 commit comments