Skip to content

Commit

Permalink
Fix print wrong escaped character, Fix #4
Browse files Browse the repository at this point in the history
  • Loading branch information
VerachadW committed Mar 22, 2017
1 parent e81e81c commit 177a241
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
17 changes: 13 additions & 4 deletions core/src/main/kotlin/com/taskworld/kraph/lang/GraphQLNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@ abstract internal class GraphQLNode {
fun getIndentString(level: Int) = " ".repeat(level)
fun getNewLineString(prettyFormat: Boolean) = if (prettyFormat) "\n" else "\\n"

private fun printEscaped(value: Any?, prettyFormat: Boolean) =
if (prettyFormat) {
"\"$value\""
} else {
"\\\"$value\\\""
}

fun Map<String, Any>.print(prettyFormat: Boolean) =
this.entries.foldIndexed("") { index, acc, entry ->
var string = acc + "${entry.key}: ${
when (entry.value) {
is String -> {
if (prettyFormat) {
"\"${entry.value}\""
} else {
"\\\"${entry.value}\\\""
printEscaped(entry.value, prettyFormat)
}
is List<*> -> {
val valueList = entry.value as List<*>
valueList.map {
printEscaped(it as String, prettyFormat)
}
}
else -> {
Expand Down
16 changes: 11 additions & 5 deletions core/src/test/kotlin/com/taskworld/kraph/test/GraphQLPrintSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.junit.runner.RunWith
import org.jetbrains.spek.api.dsl.on

//@RunWith(JUnitPlatform::class)
class GraphQLPrintSpek : Spek({

describe("Argument print function") {
given("id as argument and value as 1") {
val node = Argument(mapOf("id" to 1))
it("should print (id: 1)") {
assertThat(node.print(false, 0), equalTo("(id: 1)"))
on("print pretty") {
it("should print (id: 1)") {
assertThat(node.print(true, 0), equalTo("(id: 1)"))
}
}
on("print normal") {
it("should print (id: 1)") {
assertThat(node.print(false, 0), equalTo("(id: 1)"))
}
}
}
given("id and title as arguments and value as 1 and Kraph") {
Expand All @@ -36,7 +42,7 @@ class GraphQLPrintSpek : Spek({
given("an array of string as argument") {
val node = Argument(mapOf("titles" to listOf("title1", "title2")))
it("should print (titles: [\"title1\", \"title2\"]") {
assertThat(node.print(true, 0), equalTo("(titles: [\\\"title1\\\", \\\"title2\\\"])"))
assertThat(node.print(false, 0), equalTo("(titles: [\\\"title1\\\", \\\"title2\\\"])"))
}
}
}
Expand Down

0 comments on commit 177a241

Please sign in to comment.