Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrating from the org.json Library to Gson #248

Merged
merged 15 commits into from
Jan 29, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.takahirom.roborazzi

import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import java.io.File
import java.io.FileReader

Expand All @@ -22,11 +23,11 @@ sealed interface CaptureResult {
get() = null

override fun toJson(): JsonObject {
val json = JsonObject()
json.addProperty("type", "recorded")
json.addProperty("golden_file_path", goldenFile.absolutePath)
json.addProperty("timestamp", timestampNs)
return json
val recorded = object {
val golden_file_path = goldenFile.absolutePath
val timestamp = timestampNs
sanao1006 marked this conversation as resolved.
Show resolved Hide resolved
}
return JsonParser.parseString(Gson().toJson(recorded)).asJsonObject
}
}

Expand All @@ -37,13 +38,13 @@ sealed interface CaptureResult {
override val timestampNs: Long,
) : CaptureResult {
override fun toJson(): JsonObject {
val json = JsonObject()
json.addProperty("type", "added")
json.addProperty("compare_file_path", compareFile.absolutePath)
json.addProperty("actual_file_path", actualFile.absolutePath)
json.addProperty("golden_file_path", goldenFile.absolutePath)
json.addProperty("timestamp", timestampNs)
return json
val added = object {
val compare_file_path: String = compareFile.absolutePath
val actual_file_path: String = actualFile.absolutePath
val golden_file_path: String = goldenFile.absolutePath
val timestamp: Long = timestampNs
}
return JsonParser.parseString(Gson().toJson(added)).asJsonObject
}
}

Expand All @@ -54,13 +55,13 @@ sealed interface CaptureResult {
override val timestampNs: Long
) : CaptureResult {
override fun toJson(): JsonObject {
val json = JsonObject()
json.addProperty("type", "changed")
json.addProperty("compare_file_path", compareFile.absolutePath)
json.addProperty("actual_file_path", actualFile.absolutePath)
json.addProperty("golden_file_path", goldenFile.absolutePath)
json.addProperty("timestamp", timestampNs)
return json
val changed = object {
val compare_file_path: String = compareFile.absolutePath
val actual_file_path: String = actualFile.absolutePath
val golden_file_path: String = goldenFile.absolutePath
val timestamp: Long = timestampNs
}
return JsonParser.parseString(Gson().toJson(changed)).asJsonObject
}
}

Expand All @@ -74,11 +75,11 @@ sealed interface CaptureResult {
get() = null

override fun toJson(): JsonObject {
val json = JsonObject()
json.addProperty("type", "unchanged")
json.addProperty("golden_file_path", goldenFile.absolutePath)
json.addProperty("timestamp", timestampNs)
return json
val unChanged = object {
val golden_file_path: String = goldenFile.absolutePath
val timestamp: Long = timestampNs
}
return JsonParser.parseString(Gson().toJson(unChanged)).asJsonObject
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package com.github.takahirom.roborazzi

import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import java.io.File
import java.io.FileReader

data class CaptureResults(
val summary: ResultSummary,
val resultSummary: ResultSummary,
val captureResults: List<CaptureResult>
) {
fun toJson(): JsonObject {
val json = JsonObject()
json.add("summary", summary.toJson())
val resultsArray = JsonArray()
captureResults.forEach { result ->
resultsArray.add(result.toJson())
val captureResultsObject = object {
val summary = resultSummary
val results = captureResults.map { it.toJson() }
sanao1006 marked this conversation as resolved.
Show resolved Hide resolved
}
json.add("results", resultsArray)
return json
return JsonParser.parseString(Gson().toJson(captureResultsObject)).asJsonObject
}

fun toHtml(reportDirectoryPath: String): String {
Expand Down Expand Up @@ -80,7 +77,7 @@ data class CaptureResults(
}
}
return buildString {
append(summary.toHtml())
append(resultSummary.toHtml())
append(buildTable("Recorded images", "recorded", recordedImages))
append(buildTable("Added images", "added", addedImages))
append(buildTable("Changed images", "changed", changedImages))
Expand All @@ -106,7 +103,7 @@ data class CaptureResults(

fun from(results: List<CaptureResult>): CaptureResults {
return CaptureResults(
summary = ResultSummary(
resultSummary = ResultSummary(
total = results.size,
recorded = results.count { it is CaptureResult.Recorded },
added = results.count { it is CaptureResult.Added },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.takahirom.roborazzi

import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser

data class ResultSummary(
val total: Int,
Expand All @@ -10,13 +12,7 @@ data class ResultSummary(
val unchanged: Int
) {
fun toJson(): JsonObject {
val json = JsonObject()
json.addProperty("total", total)
json.addProperty("recorded", recorded)
json.addProperty("added", added)
json.addProperty("changed", changed)
json.addProperty("unchanged", unchanged)
return json
return JsonParser.parseString(Gson().toJson(this)).asJsonObject
}

fun toHtml(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class CaptureResultTest {
""".trimIndent()
val jsonObject = JsonParser.parseString(jsonString).asJsonObject
val compareReportResult = CaptureResults.fromJson(jsonObject)
val summary = compareReportResult.summary
val summary = compareReportResult.resultSummary
val captureResults = compareReportResult.captureResults

// Test summary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,17 @@ dependencies {
val recordedFile =
testProjectDir.root.resolve("app/build/test-results/roborazzi/results-summary.json")
val resutls = CaptureResults.fromJsonFile(recordedFile.absolutePath)
assert(resutls.summary.recorded == recorded) {
"Expected count: $recorded, actual count: ${resutls.summary.recorded} summary:${resutls.summary}"
assert(resutls.resultSummary.recorded == recorded) {
"Expected count: $recorded, actual count: ${resutls.resultSummary.recorded} summary:${resutls.resultSummary}"
}
assert(resutls.summary.added == added) {
"Expected count: $added, actual count: ${resutls.summary.added} summary:${resutls.summary}"
assert(resutls.resultSummary.added == added) {
"Expected count: $added, actual count: ${resutls.resultSummary.added} summary:${resutls.resultSummary}"
}
assert(resutls.summary.changed == changed) {
"Expected count: $changed, actual count: ${resutls.summary.changed} summary:${resutls.summary}"
assert(resutls.resultSummary.changed == changed) {
"Expected count: $changed, actual count: ${resutls.resultSummary.changed} summary:${resutls.resultSummary}"
}
assert(resutls.summary.unchanged == unchanged) {
"Expected count: $unchanged, actual count: ${resutls.summary.unchanged} summary:${resutls.summary}"
assert(resutls.resultSummary.unchanged == unchanged) {
"Expected count: $unchanged, actual count: ${resutls.resultSummary.unchanged} summary:${resutls.resultSummary}"
}
}

Expand Down