1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.core.technical.generator
21 
22 import eu.timepit.refined.auto._
23 import eu.timepit.refined.types.numeric.PosInt
24 import org.make.core.SlugHelper
25 import org.scalacheck.Gen
26 
27 import scala.io.Source
28 
29 object CustomGenerators {
30   object Color {
31     def gen: Gen[String] = Gen.listOfN(6, Gen.hexChar).map(_.mkString.toLowerCase.prependedAll("#"))
32   }
33 
34   object LoremIpsumGen {
35     private lazy val loremIpsumWords: Seq[String] = Source.fromResource("loremIpsum.csv").getLines().toSeq
36 
37     def word: Gen[String] = Gen.oneOf(loremIpsumWords)
38     def words: Gen[Seq[String]] = Gen.someOf(loremIpsumWords).map(_.toSeq)
39     def words(n: Int): Gen[Seq[String]] = Gen.listOfN(n, word)
40 
41     def sentence(maxLength: Option[PosInt] = None): Gen[String] =
42       Gen.atLeastOne(loremIpsumWords).map(_.toSeq).map { sen =>
43         val ret = sen.mkString("", " ", ".").capitalize
44         maxLength.collect {
45           case length if length < ret.length => ret.take(ret.take(length).lastIndexOf(" ")).appendedAll(".")
46         }.getOrElse(ret)
47       }
48     def sentences(n: Int, maxLength: Option[PosInt] = None): Gen[List[String]] = Gen.listOfN(n, sentence(maxLength))
49 
50     def slug(maxLength: Option[PosInt] = None): Gen[String] = sentence(maxLength).map(SlugHelper.apply)
51   }
52 
53   object ImageUrl {
54     def gen(width: Int, height: Int): Gen[String] =
55       Gen.chooseNum(0, 1084).map(picNum => s"https://i.picsum.photos/id/$picNum/$width/$height.jpg")
56   }
57 
58   object Mail {
59     def gen(prefix: Option[String] = None): Gen[String] = Gen.uuid.map { id =>
60       val tag: String = prefix.map(p => s"$p-$id").getOrElse(id.toString)
61       s"$tag@example.com"
62     }
63   }
64 
65   object PostalCode {
66     def gen: Gen[String] = Gen.listOfN(5, Gen.numChar).map(_.mkString)
67   }
68 
69   object FirstName {
70     private lazy val dictionary: Seq[String] = Source.fromResource("first_names.csv").getLines().toSeq
71 
72     lazy val gen: Gen[String] = {
73       Gen.oneOf(dictionary).map(_.trim.toLowerCase.capitalize)
74     }
75   }
76 
77   object URL {
78     def gen: Gen[String] = Gen.uuid.map(id => s"https://example.com/$id")
79   }
80 
81 }
Line Stmt Id Pos Tree Symbol Tests Code
31 3836 1036 - 1047 Select org.scalacheck.Gen.hexChar org.scalacheck.Gen.hexChar
31 5070 1021 - 1094 Apply org.scalacheck.Gen.map org.scalacheck.Gen.listOfN[Char](6, org.scalacheck.Gen.hexChar).map[String](((x$1: List[Char]) => scala.Predef.augmentString(x$1.mkString.toLowerCase()).prependedAll("#")))
31 1944 1053 - 1093 Apply scala.collection.StringOps.prependedAll scala.Predef.augmentString(x$1.mkString.toLowerCase()).prependedAll("#")
31 637 1033 - 1034 Literal <nosymbol> 6
37 4084 1261 - 1287 Apply org.scalacheck.Gen.oneOf org.scalacheck.Gen.oneOf[String](LoremIpsumGen.this.loremIpsumWords)
38 5486 1322 - 1362 Apply org.scalacheck.Gen.map org.scalacheck.Gen.someOf[String](LoremIpsumGen.this.loremIpsumWords).map[Seq[String]](((x$2: scala.collection.Seq[String]) => x$2.toSeq))
38 2297 1354 - 1361 Select scala.collection.IterableOnceOps.toSeq x$2.toSeq
39 1448 1405 - 1425 Apply org.scalacheck.Gen.listOfN org.scalacheck.Gen.listOfN[String](n, LoremIpsumGen.this.word)
39 3451 1420 - 1424 Select org.make.core.technical.generator.CustomGenerators.LoremIpsumGen.word LoremIpsumGen.this.word
42 4198 1499 - 1782 Apply org.scalacheck.Gen.map org.make.core.operation.operationofquestiontest org.scalacheck.Gen.atLeastOne[String](LoremIpsumGen.this.loremIpsumWords).map[Seq[String]](((x$3: scala.collection.Seq[String]) => x$3.toSeq)).map[String](((sen: Seq[String]) => { val ret: String = scala.Predef.augmentString(sen.mkString("", " ", ".")).capitalize; maxLength.collect[String](({ @SerialVersionUID(value = 0) final <synthetic> class $anonfun extends scala.runtime.AbstractPartialFunction[eu.timepit.refined.types.numeric.PosInt,String] with java.io.Serializable { def <init>(): <$anon: eu.timepit.refined.types.numeric.PosInt => String> = { $anonfun.super.<init>(); () }; final override def applyOrElse[A1 <: eu.timepit.refined.types.numeric.PosInt, B1 >: String](x1: A1, default: A1 => B1): B1 = ((x1.asInstanceOf[eu.timepit.refined.api.Refined[Int,eu.timepit.refined.numeric.Positive]]: eu.timepit.refined.types.numeric.PosInt): eu.timepit.refined.types.numeric.PosInt @unchecked) match { case (length @ _) if eu.timepit.refined.auto.autoUnwrap[eu.timepit.refined.api.Refined, Int, eu.timepit.refined.numeric.Positive](length)(api.this.RefType.refinedRefType).<(ret.length()) => scala.Predef.augmentString(scala.Predef.augmentString(ret).take(scala.Predef.augmentString(ret).take(eu.timepit.refined.auto.autoUnwrap[eu.timepit.refined.api.Refined, Int, eu.timepit.refined.numeric.Positive](length)(api.this.RefType.refinedRefType)).lastIndexOf(" "))).appendedAll(".") case (defaultCase$ @ _) => default.apply(x1) }; final def isDefinedAt(x1: eu.timepit.refined.types.numeric.PosInt): Boolean = ((x1.asInstanceOf[eu.timepit.refined.api.Refined[Int,eu.timepit.refined.numeric.Positive]]: eu.timepit.refined.types.numeric.PosInt): eu.timepit.refined.types.numeric.PosInt @unchecked) match { case (length @ _) if eu.timepit.refined.auto.autoUnwrap[eu.timepit.refined.api.Refined, Int, eu.timepit.refined.numeric.Positive](length)(api.this.RefType.refinedRefType).<(ret.length()) => true case (defaultCase$ @ _) => false } }; new $anonfun() }: PartialFunction[eu.timepit.refined.types.numeric.PosInt,String])).getOrElse[String](ret) }))
42 649 1535 - 1542 Select scala.collection.IterableOnceOps.toSeq org.make.core.operation.operationofquestiontest x$3.toSeq
43 1725 1575 - 1612 Select scala.collection.StringOps.capitalize org.make.core.operation.operationofquestiontest scala.Predef.augmentString(sen.mkString("", " ", ".")).capitalize
43 4018 1575 - 1601 Apply scala.collection.IterableOnceOps.mkString org.make.core.operation.operationofquestiontest sen.mkString("", " ", ".")
46 5082 1621 - 1774 Apply scala.Option.getOrElse org.make.core.operation.operationofquestiontest maxLength.collect[String](({ @SerialVersionUID(value = 0) final <synthetic> class $anonfun extends scala.runtime.AbstractPartialFunction[eu.timepit.refined.types.numeric.PosInt,String] with java.io.Serializable { def <init>(): <$anon: eu.timepit.refined.types.numeric.PosInt => String> = { $anonfun.super.<init>(); () }; final override def applyOrElse[A1 <: eu.timepit.refined.types.numeric.PosInt, B1 >: String](x1: A1, default: A1 => B1): B1 = ((x1.asInstanceOf[eu.timepit.refined.api.Refined[Int,eu.timepit.refined.numeric.Positive]]: eu.timepit.refined.types.numeric.PosInt): eu.timepit.refined.types.numeric.PosInt @unchecked) match { case (length @ _) if eu.timepit.refined.auto.autoUnwrap[eu.timepit.refined.api.Refined, Int, eu.timepit.refined.numeric.Positive](length)(api.this.RefType.refinedRefType).<(ret.length()) => scala.Predef.augmentString(scala.Predef.augmentString(ret).take(scala.Predef.augmentString(ret).take(eu.timepit.refined.auto.autoUnwrap[eu.timepit.refined.api.Refined, Int, eu.timepit.refined.numeric.Positive](length)(api.this.RefType.refinedRefType)).lastIndexOf(" "))).appendedAll(".") case (defaultCase$ @ _) => default.apply(x1) }; final def isDefinedAt(x1: eu.timepit.refined.types.numeric.PosInt): Boolean = ((x1.asInstanceOf[eu.timepit.refined.api.Refined[Int,eu.timepit.refined.numeric.Positive]]: eu.timepit.refined.types.numeric.PosInt): eu.timepit.refined.types.numeric.PosInt @unchecked) match { case (length @ _) if eu.timepit.refined.auto.autoUnwrap[eu.timepit.refined.api.Refined, Int, eu.timepit.refined.numeric.Positive](length)(api.this.RefType.refinedRefType).<(ret.length()) => true case (defaultCase$ @ _) => false } }; new $anonfun() }: PartialFunction[eu.timepit.refined.types.numeric.PosInt,String])).getOrElse[String](ret)
48 2310 1879 - 1898 Apply org.make.core.technical.generator.CustomGenerators.LoremIpsumGen.sentence LoremIpsumGen.this.sentence(maxLength)
48 5326 1864 - 1899 Apply org.scalacheck.Gen.listOfN org.scalacheck.Gen.listOfN[String](n, LoremIpsumGen.this.sentence(maxLength))
50 1459 1963 - 2004 Apply org.scalacheck.Gen.map org.make.core.operation.operationofquestiontest LoremIpsumGen.this.sentence(maxLength).map[String](((value: String) => org.make.core.SlugHelper.apply(value)))
50 3378 1987 - 2003 Apply org.make.core.SlugHelper.apply org.make.core.operation.operationofquestiontest org.make.core.SlugHelper.apply(value)
55 3971 2105 - 2109 Literal <nosymbol> org.make.core.operation.operationofquestiontest,org.make.api.technical.job.jobapitest 1084
55 5014 2101 - 2101 Select org.scalacheck.Gen.Choose.chooseInt org.make.core.operation.operationofquestiontest,org.make.api.technical.job.jobapitest Gen.this.Choose.chooseInt
55 1933 2101 - 2101 Select scala.math.Numeric.IntIsIntegral org.make.core.operation.operationofquestiontest,org.make.api.technical.job.jobapitest math.this.Numeric.IntIsIntegral
55 572 2102 - 2103 Literal <nosymbol> org.make.core.operation.operationofquestiontest,org.make.api.technical.job.jobapitest 0
55 3296 2088 - 2182 Apply org.scalacheck.Gen.map org.make.core.operation.operationofquestiontest,org.make.api.technical.job.jobapitest org.scalacheck.Gen.chooseNum[Int](0, 1084)(math.this.Numeric.IntIsIntegral, Gen.this.Choose.chooseInt).map[String](((picNum: Int) => ("https://i.picsum.photos/id/".+(picNum).+("/").+(width).+("/").+(height).+(".jpg"): String)))
59 3572 2262 - 2388 Apply org.scalacheck.Gen.map org.scalacheck.Gen.uuid.map[String](((id: java.util.UUID) => { val tag: String = prefix.map[String](((p: String) => ("".+(p).+("-").+(id): String))).getOrElse[String](id.toString()); ("".+(tag).+("@example.com"): String) }))
60 2319 2344 - 2355 Apply java.util.UUID.toString id.toString()
60 5471 2307 - 2356 Apply scala.Option.getOrElse prefix.map[String](((p: String) => ("".+(p).+("-").+(id): String))).getOrElse[String](id.toString())
66 3977 2475 - 2485 Select scala.collection.IterableOnceOps.mkString x$4.mkString
66 1568 2455 - 2456 Literal <nosymbol> 5
66 1878 2443 - 2486 Apply org.scalacheck.Gen.map org.scalacheck.Gen.listOfN[Char](5, org.scalacheck.Gen.numChar).map[String](((x$4: List[Char]) => x$4.mkString))
66 583 2458 - 2469 Select org.scalacheck.Gen.numChar org.scalacheck.Gen.numChar
78 5184 2767 - 2813 Apply org.scalacheck.Gen.map org.scalacheck.Gen.uuid.map[String](((id: java.util.UUID) => ("https://example.com/".+(id): String)))