Skip to content

Commit

Permalink
Merge pull request #423 from iRevive/tests-cleanup
Browse files Browse the repository at this point in the history
Tests: reuse existing `Arbitrary`, `Gen`, and `Cogen` instances
  • Loading branch information
iRevive authored Jan 11, 2024
2 parents d7f2f53 + 94bb5d5 commit cc1f812
Show file tree
Hide file tree
Showing 36 changed files with 641 additions and 630 deletions.
9 changes: 6 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ lazy val `core-metrics` = crossProject(JVMPlatform, JSPlatform, NativePlatform)
lazy val `core-trace` = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("core/trace"))
.dependsOn(`core-common`)
.dependsOn(`core-common` % "compile->compile;test->test")
.settings(scalaReflectDependency)
.settings(munitDependencies)
.settings(
Expand Down Expand Up @@ -157,7 +157,7 @@ lazy val `sdk-common` = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.enablePlugins(BuildInfoPlugin)
.enablePlugins(NoPublishPlugin)
.in(file("sdk/common"))
.dependsOn(`core-common` % "test->test", semconv)
.dependsOn(`core-common` % "compile->compile;test->test", semconv)
.settings(
name := "otel4s-sdk-common",
startYear := Some(2023),
Expand All @@ -180,7 +180,10 @@ lazy val `sdk-trace` = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.enablePlugins(NoPublishPlugin)
.in(file("sdk/trace"))
.dependsOn(`sdk-common`, `core-trace`)
.dependsOn(
`sdk-common` % "compile->compile;test->test",
`core-trace` % "compile->compile;test->test"
)
.settings(
name := "otel4s-sdk-trace",
startYear := Some(2023),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ package org.typelevel.otel4s
import cats.kernel.laws.discipline.HashTests
import cats.kernel.laws.discipline.MonoidTests
import munit.DisciplineSuite
import org.typelevel.otel4s.arbitrary._
import org.typelevel.otel4s.scalacheck.Arbitraries._
import org.typelevel.otel4s.scalacheck.Cogens._

class AttributesLawTests extends DisciplineSuite {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ package org.typelevel.otel4s

import cats.Show
import munit.ScalaCheckSuite
import org.scalacheck.Arbitrary
import org.scalacheck.Gen
import org.scalacheck.Prop.forAll
import org.typelevel.otel4s.arbitrary.attribute
import org.typelevel.otel4s.arbitrary.attributes
import org.typelevel.otel4s.scalacheck.Gens

class AttributesProps extends ScalaCheckSuite {

private val listOfAttributes = Gen.listOf(Arbitrary.arbitrary[Attribute[_]])
private val listOfAttributes = Gen.listOf(Gens.attribute)

property("Attributes#size is equal to the number of unique keys") {
forAll(listOfAttributes) { attributes =>
Expand Down Expand Up @@ -150,7 +148,7 @@ class AttributesProps extends ScalaCheckSuite {
}

property("Show[Attributes]") {
forAll(attributes.arbitrary) { attributes =>
forAll(Gens.attributes) { attributes =>
val expected = attributes.toList
.map(Show[Attribute[_]].show)
.mkString("Attributes(", ", ", ")")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Typelevel
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,25 +14,20 @@
* limitations under the License.
*/

package org.typelevel.otel4s
package sdk
package trace
package org.typelevel.otel4s.scalacheck

import org.scalacheck.Arbitrary
import org.typelevel.otel4s.sdk.trace.samplers.SamplingDecision
import org.typelevel.otel4s.Attribute
import org.typelevel.otel4s.Attributes

object Arbitraries {
trait Arbitraries {

implicit val attributeArbitrary: Arbitrary[Attribute[_]] =
Arbitrary(Gens.attribute)

implicit val attributesArbitrary: Arbitrary[Attributes] =
Arbitrary(Gens.attributes)

implicit val resourceArbitrary: Arbitrary[Resource] =
Arbitrary(Gens.resource)

implicit val samplingDecisionArbitrary: Arbitrary[SamplingDecision] =
Arbitrary(Gens.samplingDecision)

}

object Arbitraries extends Arbitraries
Original file line number Diff line number Diff line change
Expand Up @@ -14,77 +14,16 @@
* limitations under the License.
*/

package org.typelevel.otel4s
package org.typelevel.otel4s.scalacheck

import org.scalacheck.Arbitrary
import org.scalacheck.Cogen
import org.scalacheck.Gen
import org.scalacheck.Gen.listOf
import org.scalacheck.Gen.nonEmptyListOf
import org.scalacheck.rng.Seed
import org.typelevel.otel4s.Attribute
import org.typelevel.otel4s.AttributeKey
import org.typelevel.otel4s.AttributeType
import org.typelevel.otel4s.Attributes

object arbitrary extends ArbitraryInstances
trait ArbitraryInstances {

val nonEmptyString: Gen[String] =
Arbitrary.arbitrary[String].suchThat(_.nonEmpty)

implicit val booleanAttribute: Gen[Attribute[Boolean]] = for {
b <- Arbitrary.arbitrary[Boolean]
k <- nonEmptyString
} yield Attribute(k, b)

implicit val stringAttribute: Gen[Attribute[String]] = for {
s <- nonEmptyString
k <- nonEmptyString
} yield Attribute(k, s)

implicit val longAttribute: Gen[Attribute[Long]] = for {
l <- Arbitrary.arbitrary[Long]
k <- nonEmptyString
} yield Attribute(k, l)

implicit val doubleAttribute: Gen[Attribute[Double]] = for {
d <- Arbitrary.arbitrary[Double]
k <- nonEmptyString
} yield Attribute(k, d)

implicit val stringListAttribute: Gen[Attribute[List[String]]] = for {
l <- nonEmptyListOf(nonEmptyString)
k <- nonEmptyString
} yield Attribute(k, l)

implicit val booleanListAttribute: Gen[Attribute[List[Boolean]]] = for {
l <- nonEmptyListOf(Arbitrary.arbitrary[Boolean])
k <- nonEmptyString
} yield Attribute(k, l)

implicit val longListAttribute: Gen[Attribute[List[Long]]] = for {
l <- nonEmptyListOf(Arbitrary.arbitrary[Long])
k <- nonEmptyString
} yield Attribute(k, l)

implicit val doubleListAttribute: Gen[Attribute[List[Double]]] = for {
l <- nonEmptyListOf(Arbitrary.arbitrary[Double])
k <- nonEmptyString
} yield Attribute(k, l)

implicit val attribute: Arbitrary[Attribute[_]] = Arbitrary(
Gen.oneOf(
booleanAttribute,
stringAttribute,
longAttribute,
doubleAttribute,
stringListAttribute,
booleanListAttribute,
longListAttribute,
doubleListAttribute
)
)

implicit val attributes: Arbitrary[Attributes] = Arbitrary(
listOf(attribute.arbitrary).map(Attributes(_: _*))
)
trait Cogens {

implicit val attributeTypeCogen: Cogen[AttributeType[_]] =
Cogen[String].contramap(_.toString)
Expand Down Expand Up @@ -121,4 +60,7 @@ trait ArbitraryInstances {

implicit val attributesCogen: Cogen[Attributes] =
Cogen[List[Attribute[_]]].contramap(_.toList)

}

object Cogens extends Cogens
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.typelevel.otel4s
package scalacheck

import org.scalacheck.Arbitrary
import org.scalacheck.Gen
import org.typelevel.otel4s.Attribute.KeySelect

trait Gens {

val nonEmptyString: Gen[String] =
Gen.alphaNumStr.suchThat(_.nonEmpty)

val attribute: Gen[Attribute[_]] = {
implicit val stringArb: Arbitrary[String] =
Arbitrary(nonEmptyString)

implicit def listArb[A: Arbitrary]: Arbitrary[List[A]] =
Arbitrary(Gen.nonEmptyListOf(Arbitrary.arbitrary[A]))

def attribute[A: KeySelect: Arbitrary]: Gen[Attribute[A]] =
for {
key <- nonEmptyString
value <- Arbitrary.arbitrary[A]
} yield Attribute(key, value)

val string: Gen[Attribute[String]] = attribute[String]
val boolean: Gen[Attribute[Boolean]] = attribute[Boolean]
val long: Gen[Attribute[Long]] = attribute[Long]
val double: Gen[Attribute[Double]] = attribute[Double]

val stringList: Gen[Attribute[List[String]]] = attribute[List[String]]
val booleanList: Gen[Attribute[List[Boolean]]] = attribute[List[Boolean]]
val longList: Gen[Attribute[List[Long]]] = attribute[List[Long]]
val doubleList: Gen[Attribute[List[Double]]] = attribute[List[Double]]

Gen.oneOf(
boolean,
string,
long,
double,
stringList,
booleanList,
longList,
doubleList
)
}

val attributes: Gen[Attributes] =
for {
attributes <- Gen.listOf(attribute)
} yield Attributes.fromSpecific(attributes)

}

object Gens extends Gens
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,16 @@ import cats.Show
import cats.kernel.laws.discipline.HashTests
import cats.syntax.show._
import munit._
import org.scalacheck.Arbitrary
import org.scalacheck.Cogen
import org.scalacheck.Gen
import org.scalacheck.Prop
import org.typelevel.otel4s.trace.SpanContext.SpanId
import org.typelevel.otel4s.trace.SpanContext.TraceId
import org.typelevel.otel4s.trace.scalacheck.Arbitraries._
import org.typelevel.otel4s.trace.scalacheck.Cogens._
import org.typelevel.otel4s.trace.scalacheck.Gens
import scodec.bits.ByteVector

class SpanContextSuite extends DisciplineSuite {

private val traceIdGen: Gen[ByteVector] =
for {
hi <- Gen.long
lo <- Gen.long.suchThat(_ != 0)
} yield SpanContext.TraceId.fromLongs(hi, lo)

private val spanIdGen: Gen[ByteVector] =
for {
value <- Gen.long.suchThat(_ != 0)
} yield SpanContext.SpanId.fromLong(value)

private val spanContextGen: Gen[SpanContext] =
for {
traceId <- traceIdGen
spanId <- spanIdGen
traceFlags <- Gen.oneOf(TraceFlags.Sampled, TraceFlags.Default)
remote <- Gen.oneOf(true, false)
} yield SpanContext(traceId, spanId, traceFlags, TraceState.empty, remote)

private implicit val spanContextArbitrary: Arbitrary[SpanContext] =
Arbitrary(spanContextGen)

private implicit val spanContextCogen: Cogen[SpanContext] =
Cogen[(String, String, Byte, Map[String, String], Boolean, Boolean)]
.contramap(c =>
(
c.traceIdHex,
c.spanIdHex,
c.traceFlags.toByte,
c.traceState.asMap,
c.isValid,
c.isRemote
)
)

private val ValidTraceIdHex =
"00000000000000000000000000000061"

Expand Down Expand Up @@ -146,7 +111,7 @@ class SpanContextSuite extends DisciplineSuite {
}

test("Show[SpanContext]") {
Prop.forAll(spanContextGen) { ctx =>
Prop.forAll(Gens.spanContext) { ctx =>
val expected = show"SpanContext{" +
show"traceId=${ctx.traceIdHex}, " +
show"spanId=${ctx.spanIdHex}, " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,17 @@ package org.typelevel.otel4s.trace
import cats.Show
import cats.kernel.laws.discipline.HashTests
import munit._
import org.scalacheck.Arbitrary
import org.scalacheck.Cogen
import org.scalacheck.Gen
import org.scalacheck.Prop
import org.typelevel.otel4s.trace.scalacheck.Arbitraries._
import org.typelevel.otel4s.trace.scalacheck.Cogens._
import org.typelevel.otel4s.trace.scalacheck.Gens

class SpanKindSuite extends DisciplineSuite {

private val spanKindGen: Gen[SpanKind] =
Gen.oneOf(
SpanKind.Internal,
SpanKind.Server,
SpanKind.Client,
SpanKind.Producer,
SpanKind.Consumer
)

private implicit val spanKindArbitrary: Arbitrary[SpanKind] =
Arbitrary(spanKindGen)

private implicit val spanKindCogen: Cogen[SpanKind] =
Cogen[String].contramap(_.toString)

checkAll("SpanKind.HashLaws", HashTests[SpanKind].hash)

property("Show[SpanKind]") {
Prop.forAll(spanKindGen) { spanKind =>
Prop.forAll(Gens.spanKind) { spanKind =>
val expected = spanKind match {
case SpanKind.Internal => "Internal"
case SpanKind.Server => "Server"
Expand Down
Loading

0 comments on commit cc1f812

Please sign in to comment.