All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clouway.testing.matchers.json.JsonMatchers.kt Maven / Gradle / Ivy

There is a newer version: 0.0.8
Show newest version
package com.clouway.testing.matchers.json

import com.google.gson.GsonBuilder
import com.google.gson.JsonParser
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher

/**
 * @author Stanimir Iliev 
 */

object JsonMatchers {
  fun isSameJsonAs(example: String): Matcher {
    return object : TypeSafeMatcher() {
      var value = ""
      val parser = JsonParser()
      val gson = GsonBuilder().setPrettyPrinting().create()

      override fun matchesSafely(value: String): Boolean {
        this.value = value

        return parser.parse(example) == parser.parse(value)
      }

      override fun describeTo(description: Description) {
        description.appendValue(gson.toJson((parser.parse(example))))
      }

      override fun describeMismatchSafely(item: String, mismatchDescription: Description) {
        mismatchDescription.appendText("was ").appendValue(gson.toJson((parser.parse(item))))
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy