Skip to content

Commit c0c9ad3

Browse files
authored
Feature/fixing sampler macro (#79)
* Checking types of known fields * Adding a test for the edge case * Fixing edge case for known fields * Removing implicit requirements where they are not used
1 parent e053dd8 commit c0c9ad3

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

util-samplers/src/main/scala/com/outworkers/util/empty/EmptyGenerators.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait EmptyGenerators {
1515
*/
1616
def void[T : Empty]: T = implicitly[Empty[T]].sample
1717

18-
def voidOpt[T : Empty]: Option[T] = Option.empty[T]
18+
def voidOpt[T]: Option[T] = Option.empty[T]
1919
def voidMap[K : Empty, V: Empty]: Map[K, V] = Map.empty[K, V]
2020

2121
/**
@@ -26,8 +26,7 @@ trait EmptyGenerators {
2626
* @return A Collection of "size" elements with type T.
2727
*/
2828
def void[M[X] <: TraversableOnce[X], T](size: Int = 0)(
29-
implicit cbf: CanBuildFrom[Nothing, T, M[T]],
30-
proof: Empty[T]
29+
implicit cbf: CanBuildFrom[Nothing, T, M[T]]
3130
): M[T] = cbf().result()
3231

3332
def oneOf[T](list: Seq[T]): T = Gen.oneOf(list).sample.get

util-samplers/src/main/scala/com/outworkers/util/samplers/SamplerMacro.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class SamplerMacro(val c: blackbox.Context) extends AnnotationToolkit with Black
230230
accessor match {
231231
case MapType(col) => col.default
232232
case OptionType(opt) => accessor.name match {
233-
case KnownField(derived) => {
233+
case KnownField(derived) if accessor.paramType =:= typeOf[String] => {
234234

235235
val fillOptionsImp = c.inferImplicitValue(fillOptions, silent = true)
236236

@@ -248,7 +248,7 @@ class SamplerMacro(val c: blackbox.Context) extends AnnotationToolkit with Black
248248
}
249249

250250
case _ => accessor.name match {
251-
case KnownField(derived) => {
251+
case KnownField(derived) if accessor.paramType =:= typeOf[String] => {
252252

253253
q"$prefix.gen[$derived].value"
254254
}

util-samplers/src/test/scala/com/outworkers/util/samplers/GeneratorsTest.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class GeneratorsTest extends FlatSpec with Matchers with GeneratorDrivenProperty
7878
sample shouldEqual sample
7979
}
8080

81+
it should "skip dictionary lookups for non stringly typed fields" in {
82+
val sample = gen[EdgeCase]
83+
Console.println(sample)
84+
}
85+
8186

8287
it should "automatically generate a sampler for a nested Enumeration inside a CaseClass" in {
8388
val sample = gen[IndexedSeq[String]]

util-samplers/src/test/scala/org/outworkers/domain/test/domain.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ case class NestedOptions(
4747
collections: Option[CollectionSample]
4848
)
4949

50+
case class CustomEmailWrapper(email: String)
51+
52+
case class EdgeCase(
53+
id: UUID,
54+
email: CustomEmailWrapper
55+
)
56+
5057
import java.util.UUID
5158

5259
import org.outworkers.domain.test._

0 commit comments

Comments
 (0)