Skip to content

Commit

Permalink
Allow to generate primitives as well for a given AST (#249)
Browse files Browse the repository at this point in the history
Co-authored-by: Willem Veelenturf <[email protected]>
  • Loading branch information
ZzAve and wilmveel authored Aug 7, 2024
1 parent b986912 commit cac45bd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ fun AST.generate(type: String, random: Random = Random.Default): JsonElement = R
isDictionary = false
).let { generate(it, random) }

fun AST.generate(type: Reference, random: Random = Random.Default): JsonElement = resolveReference(type)
.let { if (type.isIterable) generateIterator(it, random) else generateObject(it, random) }
fun AST.generate(type: Reference, random: Random = Random.Default): JsonElement =
generateReference(type, random)

private fun AST.resolveReference(type: Reference) = filterIsInstance<Definition>()
.find { it.identifier.value == type.value }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package community.flock.wirespec.generator
import arrow.core.getOrElse
import community.flock.wirespec.compiler.core.WirespecSpec
import community.flock.wirespec.compiler.core.parse
import community.flock.wirespec.compiler.core.parse.Field.Reference.Primitive
import community.flock.wirespec.compiler.core.parse.Field.Reference.Primitive.Type
import community.flock.wirespec.compiler.utils.noLogger
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonPrimitive
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -54,6 +57,15 @@ class GeneratorTest {
assertEquals(expect, res.toString())
}

@Test
fun generatePrimitive() {
val ast = parser(src)
val random = Random(0L)
val res = ast.generate(Primitive(Type.String, false, false), random)
val expect = "ZKN8V5p8ktkmmMX"
assertEquals(expect, res.jsonPrimitive.content)
}

@Test
fun shouldBeEqualWhenSameAttributes() {
val ast = parser(src)
Expand Down

0 comments on commit cac45bd

Please sign in to comment.