How to use build method of io.kotest.assertions.json.schema.JsonSchema class

Best Kotest code snippet using io.kotest.assertions.json.schema.JsonSchema.build

build.gradle.kts

Source:build.gradle.kts Github

copy

Full Screen

...275tasks.register("backupYarnLock") {276 dependsOn(":kotlinNpmInstall")277 doLast {278 copy {279 from("$rootDir/build/js/yarn.lock")280 rename { "yarn.lock.bak" }281 into(rootDir)282 }283 }284 inputs.file("$rootDir/build/js/yarn.lock").withPropertyName("inputFile")285 outputs.file("$rootDir/yarn.lock.bak").withPropertyName("outputFile")286}287val restoreYarnLock = tasks.register("restoreYarnLock") {288 doLast {289 copy {290 from("$rootDir/yarn.lock.bak")291 rename { "yarn.lock" }292 into("$rootDir/build/js")293 }294 }295 inputs.file("$rootDir/yarn.lock.bak").withPropertyName("inputFile")296 outputs.file("$rootDir/build/js/yarn.lock").withPropertyName("outputFile")297}298tasks.named("kotlinNpmInstall").configure {299 dependsOn(restoreYarnLock)300}301tasks.register("validateYarnLock") {302 dependsOn(":kotlinNpmInstall")303 doLast {304 val expected = file("$rootDir/yarn.lock.bak").readText()305 val actual = file("$rootDir/build/js/yarn.lock").readText()306 if (expected != actual) {307 throw AssertionError(308 "Generated yarn.lock differs from the one in the repository. " +309 "It can happen because someone has updated a dependency and haven't run `./gradlew :backupYarnLock --refresh-dependencies` " +310 "afterwards."311 )312 }313 }314 inputs.files("$rootDir/yarn.lock.bak", "$rootDir/build/js/yarn.lock").withPropertyName("inputFiles")315}316allprojects {317 tasks.withType<org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask> {318 args += "--ignore-scripts"319 }320}321/**********************************************************************************************************************/...

Full Screen

Full Screen

parse.kt

Source:parse.kt Github

copy

Full Screen

...18import kotlinx.serialization.DeserializationStrategy19import kotlinx.serialization.KSerializer20import kotlinx.serialization.decodeFromString21import kotlinx.serialization.descriptors.SerialDescriptor22import kotlinx.serialization.descriptors.buildClassSerialDescriptor23import kotlinx.serialization.descriptors.element24import kotlinx.serialization.encoding.CompositeDecoder25import kotlinx.serialization.encoding.Decoder26import kotlinx.serialization.encoding.Encoder27import kotlinx.serialization.encoding.decodeStructure28import kotlinx.serialization.json.Json29import kotlinx.serialization.json.JsonContentPolymorphicSerializer30import kotlinx.serialization.json.JsonElement31import kotlinx.serialization.json.JsonNull32import kotlinx.serialization.json.JsonObject33import kotlinx.serialization.json.jsonObject34import kotlinx.serialization.json.jsonPrimitive35private val schemaJsonConfig = Json {36 ignoreUnknownKeys = true37 classDiscriminator = "type"38}39/**40 * Parses a subset of JSON Schema into [JsonSchemaElement] which can be used to verify a json document with41 * [shouldMatchSchema]42 */43@ExperimentalKotest44fun parseSchema(jsonSchema: String): JsonSchema =45 JsonSchema(root = schemaJsonConfig.decodeFromString(SchemaDeserializer, jsonSchema))46@ExperimentalKotest47internal object SchemaDeserializer : JsonContentPolymorphicSerializer<JsonSchemaElement>(JsonSchemaElement::class) {48 override fun selectDeserializer(element: JsonElement): DeserializationStrategy<out JsonSchemaElement> {49 return when (val type = element.jsonObject.get("type")?.jsonPrimitive?.content) {50 "array" -> JsonSchema.JsonArray.serializer()51 "object" -> JsonSchema.JsonObject.serializer()52 "string" -> JsonSchemaStringSerializer53 "integer" -> JsonSchemaIntegerSerializer54 "number" -> JsonSchemaNumberSerializer55 "boolean" -> JsonSchema.JsonBoolean.serializer()56 "null" -> JsonSchema.Null.serializer()57 else -> error("Unknown type: $type")58 }59 }60}61private infix fun <T> Matcher<T>?.and(other: Matcher<T>) =62 if (this != null) this and other else other63@ExperimentalKotest64internal object JsonSchemaStringSerializer : KSerializer<JsonSchema.JsonString> {65 override fun deserialize(decoder: Decoder): JsonSchema.JsonString =66 decoder.decodeStructure(descriptor) {67 var matcher: Matcher<String>? = null68 while (true) {69 when (val index = decodeElementIndex(descriptor)) {70 1 -> matcher = matcher and haveMinLength(decodeIntElement(descriptor, index))71 2 -> matcher = matcher and haveMaxLength(decodeIntElement(descriptor, index))72 3 -> matcher = matcher and match(decodeStringElement(descriptor, index).toRegex())73 // Formats: https://json-schema.org/understanding-json-schema/reference/string.html#built-in-formats74 // TODO: Map formats to matchers75 4 -> println("Formats are currently not supported")76 CompositeDecoder.DECODE_DONE -> break77 }78 }79 JsonSchema.JsonString(matcher)80 }81 override val descriptor = buildClassSerialDescriptor("JsonSchema.JsonString") {82 element<String>("type")83 element<Int>("minLength", isOptional = true)84 element<Int>("maxLength", isOptional = true)85 element<String>("pattern", isOptional = true)86 element<String>("format", isOptional = true)87 }88 override fun serialize(encoder: Encoder, value: JsonSchema.JsonString) {89 TODO("Serialization of JsonSchema not supported atm")90 }91}92@ExperimentalKotest93internal object JsonSchemaIntegerSerializer : KSerializer<JsonSchema.JsonInteger> {94 override fun deserialize(decoder: Decoder): JsonSchema.JsonInteger =95 decoder.decodeStructure(descriptor) {96 var matcher: Matcher<Long>? = null97 while (true) {98 when (val index = decodeElementIndex(descriptor)) {99 1 -> matcher = matcher and beMultipleOf(decodeLongElement(descriptor, index))100 2 -> matcher = matcher and beGreaterThanOrEqualTo(decodeLongElement(descriptor, index))101 3 -> matcher = matcher and beGreaterThan(decodeLongElement(descriptor, index))102 4 -> matcher = matcher and beLessThanOrEqualTo(decodeLongElement(descriptor, index))103 5 -> matcher = matcher and beLessThan(decodeLongElement(descriptor, index))104 CompositeDecoder.DECODE_DONE -> break105 }106 }107 JsonSchema.JsonInteger(matcher)108 }109 override val descriptor = buildClassSerialDescriptor("JsonSchema.JsonInteger") {110 element<String>("type")111 element<Long>("multipleOf", isOptional = true)112 element<Long>("minimum", isOptional = true)113 element<Long>("exclusiveMinimum", isOptional = true)114 element<Long>("maximum", isOptional = true)115 element<Long>("exclusiveMaximum", isOptional = true)116 }117 override fun serialize(encoder: Encoder, value: JsonSchema.JsonInteger) {118 TODO("Not yet implemented")119 }120}121@ExperimentalKotest122internal object JsonSchemaNumberSerializer : KSerializer<JsonSchema.JsonDecimal> {123 override fun deserialize(decoder: Decoder): JsonSchema.JsonDecimal =124 decoder.decodeStructure(descriptor) {125 var matcher: Matcher<Double>? = null126 while (true) {127 when (val index = decodeElementIndex(descriptor)) {128 1 -> matcher = matcher and beMultipleOf(decodeDoubleElement(descriptor, index))129 2 -> matcher = matcher and beGreaterThanOrEqualTo(decodeDoubleElement(descriptor, index))130 3 -> matcher = matcher and beGreaterThan(decodeDoubleElement(descriptor, index))131 4 -> matcher = matcher and beLessThanOrEqualTo(decodeDoubleElement(descriptor, index))132 5 -> matcher = matcher and beLessThan(decodeDoubleElement(descriptor, index))133 CompositeDecoder.DECODE_DONE -> break134 }135 }136 JsonSchema.JsonDecimal(matcher)137 }138 override val descriptor = buildClassSerialDescriptor("JsonSchema.JsonDecimal") {139 element<String>("type")140 element<Double>("multipleOf", isOptional = true)141 element<Double>("minimum", isOptional = true)142 element<Double>("exclusiveMinimum", isOptional = true)143 element<Double>("maximum", isOptional = true)144 element<Double>("exclusiveMaximum", isOptional = true)145 }146 override fun serialize(encoder: Encoder, value: JsonSchema.JsonDecimal) {147 TODO("Not yet implemented")148 }149}...

Full Screen

Full Screen

builder.kt

Source:builder.kt Github

copy

Full Screen

...67 ) {68 properties[name] = JsonSchema.Builder.elementBuilder()69 if (required) requiredProperties.add(name)70 }71 fun build() = JsonObject(72 additionalProperties = additionalProperties,73 minProperties = minProperties,74 maxProperties = maxProperties,75 properties = properties,76 requiredProperties = requiredProperties.toTypedArray()77 )78 }79 @Serializable80 data class JsonObject(81 /**82 * Controls whether this node allows additional properties to be defined or not.83 * By default, additional properties are _allowed_84 *85 * https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties86 */87 val additionalProperties: Boolean = true,88 val minProperties: Int = 0,89 val maxProperties: Int? = null,90 val properties: Map<String, JsonSchemaElement>,91 /**92 * https://json-schema.org/understanding-json-schema/reference/object.html#required-properties93 */94 val requiredProperties: Array<String> = emptyArray(),95 ) : JsonSchemaElement {96 operator fun get(name: String) = properties.get(name)97 override fun typeName() = "object"98 }99 data class JsonString(override val matcher: Matcher<String>? = null) : JsonSchemaElement, ValueNode<String> {100 override fun typeName() = "string"101 }102 data class JsonInteger(103 override val matcher: Matcher<Long>? = null104 ) : JsonSchemaElement, JsonNumber, ValueNode<Long> {105 override fun typeName() = "integer"106 }107 data class JsonDecimal(108 override val matcher: Matcher<Double>? = null109 ) : JsonSchemaElement, JsonNumber, ValueNode<Double> {110 override fun typeName() = "number"111 }112 @Serializable113 object JsonBoolean : JsonSchemaElement, ValueNode<Boolean> {114 override fun typeName() = "boolean"115 }116 @Serializable117 object Null : JsonSchemaElement {118 override fun typeName() = "null"119 }120}121/**122 * Creates a [JsonSchema.JsonString] node, which is a leaf node that will hold a [String] value.123 * Optionally, you can provide a [matcherBuilder] that constructs a [Matcher] which the node will be tested with.124 */125@ExperimentalKotest126fun JsonSchema.Builder.string(matcherBuilder: () -> Matcher<String>? = { null }) =127 JsonSchema.JsonString(matcherBuilder())128/**129 * Creates a [JsonSchema.JsonInteger] node, which is a leaf node that will hold a [Long] value.130 * Optionally, you can provide a [matcherBuilder] that constructs a [Matcher] which the node will be tested with.131 */132@ExperimentalKotest133fun JsonSchema.Builder.integer(matcherBuilder: () -> Matcher<Long>? = { null }) =134 JsonSchema.JsonInteger(matcherBuilder())135/**136 * Creates a [JsonSchema.JsonDecimal] node, which is a leaf node that will hold a [Double] value.137 * Optionally, you can provide a [matcherBuilder] that constructs a [Matcher] which the node will be tested with.138 */139@ExperimentalKotest140fun JsonSchema.Builder.number(matcherBuilder: () -> Matcher<Double>? = { null }) =141 JsonSchema.JsonDecimal(matcherBuilder())142/**143 * Alias for [number]144 *145 * Creates a [JsonSchema.JsonDecimal] node, which is a leaf node that will hold a [Double] value.146 * Optionally, you can provide a [matcherBuilder] that constructs a [Matcher] which the node will be tested with.147 */148@ExperimentalKotest149fun JsonSchema.Builder.decimal(matcherBuilder: () -> Matcher<Double>? = { null }) =150 JsonSchema.JsonDecimal(matcherBuilder())151/**152 * Creates a [JsonSchema.JsonBoolean] node, which is a leaf node that will hold a [Boolean] value.153 * It supports no further configuration. The actual value must always be either true or false.154 */155@ExperimentalKotest156fun JsonSchema.Builder.boolean() =157 JsonSchema.JsonBoolean158/**159 * Creates a [JsonSchema.Null] node, which is a leaf node that must always be null, if present.160 */161@ExperimentalKotest162fun JsonSchema.Builder.`null`() =163 JsonSchema.Null164/**165 * Creates a [JsonSchema.JsonObject] node. Expand on the object configuration using the [dsl] which lets you specify166 * properties using [withProperty]167 *168 * Example:169 * ```kotlin170 * val personSchema = jsonSchema {171 * obj {172 * withProperty("name") { string() }173 * withProperty("age") { number() }174 * }175 * }176 * ```177 */178@ExperimentalKotest179fun JsonSchema.Builder.obj(dsl: JsonSchema.JsonObjectBuilder.() -> Unit = {}) =180 JsonSchema.JsonObjectBuilder().apply(dsl).build()181/**182 * Defines a [JsonSchema.JsonArray] node where the elements are of the type provided by [typeBuilder].183 */184@ExperimentalKotest185fun JsonSchema.Builder.array(typeBuilder: () -> JsonSchemaElement) =186 JsonSchema.JsonArray(typeBuilder())187@ExperimentalKotest188fun jsonSchema(189 rootBuilder: JsonSchema.Builder.() -> JsonSchemaElement190): JsonSchema =191 JsonSchema(192 JsonSchema.Builder.rootBuilder()193 )...

Full Screen

Full Screen

build

Using AI Code Generation

copy

Full Screen

1val schema = JsonSchema.build {2 "properties" to {3 "name" to {4 }5 "age" to {6 }7 }8}9val schema = JsonSchema.build {10 "properties" to {11 "name" to {12 }13 "age" to {14 }15 }16}17val schema = JsonSchema.build {18 "properties" to {19 "name" to {20 }21 "age" to {22 }23 }24}25val schema = JsonSchema.build {26 "properties" to {27 "name" to {28 }29 "age" to {30 }31 }32}33val schema = JsonSchema.build {34 "properties" to {35 "name" to {36 }37 "age" to {38 }39 }40}41val schema = JsonSchema.build {42 "properties" to {43 "name" to {44 }45 "age" to {46 }47 }48}49val schema = JsonSchema.build {50 "properties" to {51 "name" to {52 }53 "age" to {

Full Screen

Full Screen

build

Using AI Code Generation

copy

Full Screen

1val schema = JsonSchema.build {2"properties" {3"firstName" {4}5"age" {6}7}8}9{10}11json should matchJsonSchema(schema)12{13}14json should matchJsonSchema("schema.json")15{16}17val schema = JsonSchema.build {18"properties" {19"firstName" {20}21"age" {22}23}24}25{26}27json should matchJsonSchema(schema)28val schema = JsonSchema.build {29"properties" {30"firstName" {31}32"age" {33}34}35}36{37}38json should matchJsonSchema(schema)39{40}41json should matchJsonSchema("schema.json")42{43}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful