Skip to content

Commit

Permalink
Fix escaped string array (#6)
Browse files Browse the repository at this point in the history
* Update Spek version

* Fix print wrong escaped character, Fix #4

* Rewrite the GraphQLPrintSpek for preetty print test

* Bump hotfix version

* Update build status link
  • Loading branch information
Verachad Wongsawangtham authored Mar 24, 2017
1 parent 747ca09 commit ff19228
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Kraph [![Build Status](https://travis-ci.org/taskworld/kraph.svg?branch=master)](https://travis-ci.org/taskworld/kraph) [ ![Download](https://api.bintray.com/packages/tw/maven/kraph/images/download.svg) ](https://bintray.com/tw/maven/kraph/_latestVersion)
# Kraph [![Build Status](https://travis-ci.org/VerachadW/kraph.svg?branch=master)](https://travis-ci.org/VerachadW/kraph) [ ![Download](https://api.bintray.com/packages/tw/maven/kraph/images/download.svg) ](https://bintray.com/tw/maven/kraph/_latestVersion)
In short, this is a GraphQL request JSON body builder for Kotlin. It will generate the JSON string for request body that work with GraphQL Server. For example, we have the GraphQL query for list all the notes.
```
query {
Expand Down
30 changes: 23 additions & 7 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
buildscript {
ext {
publish_version = '0.4.0'
publish_version = '0.4.1'

bintray_plugin_version = '0.3.4'

spek_version = '1.0.89'
spek_version = '1.1.0-beta4'
hamkrest_version = '1.2.2.0'
junit_version = '4.12'
junit_platform_version = '1.0.0-M2'
junit_platform_version = '1.0.0-M3'

}
repositories {
Expand All @@ -17,20 +17,30 @@ buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.junit.platform:junit-platform-gradle-plugin:$junit_platform_version"
classpath "org.junit.platform:junit-platform-runner:$junit_platform_version"
// classpath "org.junit.platform:junit-platform-runner:$junit_platform_version"
classpath "com.novoda:bintray-release:$bintray_plugin_version"
}
}

apply plugin: 'kotlin'
apply plugin: 'com.novoda.bintray-release'
apply plugin: 'org.junit.platform.gradle.plugin'

junitPlatform {
filters {
engines {
include 'spek'
}
}
}

archivesBaseName = 'kraph'
version = "v.$publish_version"

repositories {
jcenter()
mavenCentral()
maven { url "http://dl.bintray.com/jetbrains/spek" }
}

sourceSets {
Expand All @@ -51,9 +61,15 @@ publish {

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testCompile "junit:junit:$junit_version"
testCompile "com.natpryce:hamkrest:$hamkrest_version"
testCompile "org.jetbrains.spek:spek-api:$spek_version"
testRuntime "org.jetbrains.spek:spek-junit-platform-engine:$spek_version"
testCompile "org.junit.platform:junit-platform-runner:$junit_platform_version"
testCompile ("org.jetbrains.spek:spek-api:$spek_version") {
exclude group: 'org.jetbrains.kotlin'
}
testRuntime ("org.jetbrains.spek:spek-junit-platform-engine:$spek_version") {
exclude group: 'org.junit.platform'
exclude group: 'org.jetbrains.kotlin'
}
// testCompile "org.junit.platform:junit-platform-runner:$junit_platform_version"
}
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
3 changes: 0 additions & 3 deletions core/src/test/kotlin/com/taskworld/kraph/test/BuilderSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ 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.platform.runner.JUnitPlatform
import org.junit.runner.RunWith

@RunWith(JUnitPlatform::class)
class BuilderSpek : Spek({
describe("Kraph Query DSL Builder") {
given("sample query") {
Expand Down
180 changes: 137 additions & 43 deletions core/src/test/kotlin/com/taskworld/kraph/test/GraphQLPrintSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,80 @@ 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.platform.runner.JUnitPlatform
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") {
val node = Argument(mapOf("id" to 1, "title" to "Kraph"))
it("should print (id: 1, title: \\\"Kraph\\\")") {
assertThat(node.print(false, 0), equalTo("(id: 1, title: \\\"Kraph\\\")"))
on("print pretty") {
it("should print (id: 1, title: \"Kraph\")") {
assertThat(node.print(true, 0), equalTo("(id: 1, title: \"Kraph\")"))
}
}
on("print normal") {
it("should print (id: 1, title: \\\"Kraph\\\")") {
assertThat(node.print(false, 0), equalTo("(id: 1, title: \\\"Kraph\\\")"))
}
}
}
given("id and title as arguments and value as 1 and Kraph with pretty format enabled") {
val node = Argument(mapOf("id" to 1, "title" to "Kraph"))
it("should print (id: 1, title: \"Kraph\")") {
assertThat(node.print(true, 0), equalTo("(id: 1, title: \"Kraph\")"))
given("an array of string as argument") {
val node = Argument(mapOf("titles" to listOf("title1", "title2")))
on("print pretty") {
it("should print (titles: [\"title1\", \"title2\"]") {
assertThat(node.print(true, 0), equalTo("(titles: [\"title1\", \"title2\"])"))
}
}
on("print normal") {
it("should print (titles: [\\\"title1\\\", \\\"title2\\\"]") {
assertThat(node.print(false, 0), equalTo("(titles: [\\\"title1\\\", \\\"title2\\\"])"))
}
}
}
}
describe("SelectionSet print function") {
given("two fields; id and title") {
val fields = listOf(Field("id"), Field("title"))
val node = SelectionSet(fields)
it("should print {id title}") {
assertThat(node.print(false, 0), equalTo("{\\nid\\ntitle\\n}"))
on("print pretty") {
it("should print {\n id\n title\n}") {
assertThat(node.print(true, 0), equalTo("{\n id\n title\n}"))
}
}
on("print normal") {
it("should print {id title}") {
assertThat(node.print(false, 0), equalTo("{\\nid\\ntitle\\n}"))
}
}
}
given("three fields; id, title, and assignee which contains name and email") {
val assigneeSet = SelectionSet(listOf(Field("name"), Field("email")))
val assigneeField = Field("assignee", selectionSet = assigneeSet)
val fields = listOf(Field("id"), Field("title"), assigneeField)
val node = SelectionSet(fields)
it("should print {id title assignee {name email}}") {
assertThat(node.print(false, 0), equalTo("{\\nid\\ntitle\\nassignee {\\nname\\nemail\\n}\\n}"))
on("print pretty") {
it("should print {\n id\n title\n assignee {\n name\n email\n }\n}") {
assertThat(node.print(true, 0), equalTo("{\n id\n title\n assignee {\n name\n email\n }\n}"))
}
}
}
given("three fields; id, title, and assignee which contains name and email with pretty format enabled") {
val assigneeSet = SelectionSet(listOf(Field("name"), Field("email")))
val assigneeField = Field("assignee", selectionSet = assigneeSet)
val fields = listOf(Field("id"), Field("title"), assigneeField)
val node = SelectionSet(fields)
it("should print {id title assignee {name email}} with pretty format") {
assertThat(node.print(true, 0), equalTo("{\n id\n title\n assignee {\n name\n email\n }\n}"))
on("print normal") {
it("should print {\\nid\\ntitle\\nassignee {\\nname\\nemail\\n}\\n}") {
assertThat(node.print(false, 0), equalTo("{\\nid\\ntitle\\nassignee {\\nname\\nemail\\n}\\n}"))
}
}
}
}
Expand All @@ -67,75 +91,145 @@ class GraphQLPrintSpek : Spek({
val argNode = InputArgument(mapOf("email" to "[email protected]", "password" to "abcd1234"))
val setNode = SelectionSet(listOf(Field("id"), Field("token")))
val node = Mutation("registerUser", argNode, setNode)
it("should print registerUser(input: {email: \\\"[email protected]\\\", password: \\\"abcd1234\\\"}){ id token }") {
assertThat(node.print(false, 0), equalTo("registerUser(input: { email: \\\"[email protected]\\\", password: \\\"abcd1234\\\" }) {\\nid\\ntoken\\n}"))
on("print pretty") {
it("should print registerUser(input: { email: \"[email protected]\", password: \"abcd1234\" }){\n id\n token\n}") {
assertThat(node.print(true, 0), equalTo("registerUser(input: { email: \"[email protected]\", password: \"abcd1234\" }) {\n id\n token\n}"))
}
}
on("print normal") {
it("should print registerUser(input: { email: \\\"[email protected]\\\", password: \\\"abcd1234\\\" }){ id token }") {
assertThat(node.print(false, 0), equalTo("registerUser(input: { email: \\\"[email protected]\\\", password: \\\"abcd1234\\\" }) {\\nid\\ntoken\\n}"))
}
}
}
}
describe("Field print function") {
given("name id") {
val node = Field("id")
it("should print id") {
assertThat(node.print(false, 0), equalTo("id"))
on("print pretty") {
it("should print id") {
assertThat(node.print(true, 0), equalTo("id"))
}
}
on("print normal") {
it("should print id") {
assertThat(node.print(false, 0), equalTo("id"))
}
}
}
given("name avatarSize and size argument with value as 20") {
val argNode = Argument(mapOf("size" to 20))
val node = Field("avatarSize", arguments = argNode)
it("should print avatarSize(size: 20)") {
assertThat(node.print(false, 0), equalTo("avatarSize(size: 20)"))
on("print pretty") {
it("should print avatarSize(size: 20)") {
assertThat(node.print(true, 0), equalTo("avatarSize(size: 20)"))
}
}
on("print normal") {
it("should print avatarSize(size: 20)") {
assertThat(node.print(false, 0), equalTo("avatarSize(size: 20)"))
}
}
}
given("name assignee that contains name and email") {
val setNode = SelectionSet(listOf(Field("name"), Field("email")))
val node = Field("assignee", selectionSet = setNode)
it("should print assignee { name email }") {
assertThat(node.print(false, 0), equalTo("assignee {\\nname\\nemail\\n}"))
on("print pretty") {
it("should print assignee {\n name\n email\n}") {
assertThat(node.print(true, 0), equalTo("assignee {\n name\n email\n}"))
}
}
on("print normal") {
it("should print assignee {\\nname\\nemail\\n}") {
assertThat(node.print(false, 0), equalTo("assignee {\\nname\\nemail\\n}"))
}
}
}
given("name user and id argument with value as 10 and contains name and email") {
val argNode = Argument(mapOf("id" to 10))
val setNode = SelectionSet(listOf(Field("name"), Field("email")))
val node = Field("user", argNode, setNode)
it("should print user(id: 10){ name email }") {
assertThat(node.print(false, 0), equalTo("user(id: 10) {\\nname\\nemail\\n}"))
on("print pretty") {
it("should print user(id: 10) {\n name\n email\n") {
assertThat(node.print(true, 0), equalTo("user(id: 10) {\n name\n email\n}"))
}
}
on("print normal") {
it("should print user(id: 10) {\\nname\\nemail\\n}") {
assertThat(node.print(false, 0), equalTo("user(id: 10) {\\nname\\nemail\\n}"))
}
}
}
}
describe("Operation print function") {
given("query type and field named id") {
val node = Operation(OperationType.QUERY, SelectionSet(listOf(Field("id"))))
it("should print query { id }") {
assertThat(node.print(false, 0), equalTo("query {\\nid\\n}"))
on("print pretty") {
it("should print query {\n id\n}") {
assertThat(node.print(true, 0), equalTo("query {\n id\n}"))
}
}
on("print normal") {
it("should print query {\\nid\\n}") {
assertThat(node.print(false, 0), equalTo("query {\\nid\\n}"))
}
}
}
given("query type with name \"getTask\" and field id") {
val node = Operation(OperationType.QUERY, name = "getTask", selectionSet = SelectionSet(listOf(Field("id"))))
it("should print query getTask { id }") {
assertThat(node.print(false, 0), equalTo("query getTask {\\nid\\n}"))
on("print pretty") {
it("should print query getTask {\n id\n}") {
assertThat(node.print(true, 0), equalTo("query getTask {\n id\n}"))
}
}
on("print normal") {
it("should print query getTask {\\nid\\n}") {
assertThat(node.print(false, 0), equalTo("query getTask {\\nid\\n}"))
}
}
}
given("query type with name \"getTask\" and id(1234) as argument and field title") {
val argNode = Argument(mapOf("id" to 1234))
val node = Operation(OperationType.QUERY, name = "getTask", arguments = argNode, selectionSet= SelectionSet(listOf(Field("title"))))
it("should print query getTask(id: 1234) { title }") {
assertThat(node.print(false, 0), equalTo("query getTask(id: 1234) {\\ntitle\\n}"))
on("print pretty") {
it("should print query getTask(id: 1234) {\n title\n}") {
assertThat(node.print(true, 0), equalTo("query getTask(id: 1234) {\n title\n}"))
}
}
on("print normal") {
it("should print query getTask(id: 1234) {\\ntitle\\n}") {
assertThat(node.print(false, 0), equalTo("query getTask(id: 1234) {\\ntitle\\n}"))
}
}
}
}
describe("Document print function") {
given("document with simple query") {
val queryNode = Operation(OperationType.QUERY, selectionSet = SelectionSet(listOf(Field("id"))))
val node = Document(queryNode)
it("should print document {\"query\":\"query { id }\", \"variables\": null, \"operationName\": null}") {
assertThat(node.print(false, 0), equalTo("{\"query\": \"query {\\nid\\n}\", \"variables\": null, \"operationName\": null}"))
on("print pretty") {
it("should print document {\"query\":\"query { id }\", \"variables\": null, \"operationName\": null}") {
assertThat(node.print(true, 0), equalTo("{\"query\": \"query {\n id\n}\", \"variables\": null, \"operationName\": null}"))
}
}
on("print normal") {
it("should print document {\"query\":\"query { id }\", \"variables\": null, \"operationName\": null}") {
assertThat(node.print(false, 0), equalTo("{\"query\": \"query {\\nid\\n}\", \"variables\": null, \"operationName\": null}"))
}
}
}
given("document with query named getAllTasks") {
val queryNode = Operation(OperationType.QUERY, name = "getAllTasks", selectionSet = SelectionSet(listOf(Field("id"))))
val node = Document(queryNode)
it("should print document {\"query\":\"query getAllTasks { id }\", \"variables\": null, \"operationName\": \"getAllTasks\"}") {
assertThat(node.print(false, 0), equalTo("{\"query\": \"query getAllTasks {\\nid\\n}\", \"variables\": null, \"operationName\": \"getAllTasks\"}"))
on("print pretty") {
it("should print document {\"query\":\"query getAllTasks { id }\", \"variables\": null, \"operationName\": \"getAllTasks\"}") {
assertThat(node.print(true, 0), equalTo("{\"query\": \"query getAllTasks {\n id\n}\", \"variables\": null, \"operationName\": \"getAllTasks\"}"))
}
}
on("print normal") {
it("should print document {\"query\":\"query getAllTasks { id }\", \"variables\": null, \"operationName\": \"getAllTasks\"}") {
assertThat(node.print(false, 0), equalTo("{\"query\": \"query getAllTasks {\\nid\\n}\", \"variables\": null, \"operationName\": \"getAllTasks\"}"))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ 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.platform.runner.JUnitPlatform
import org.junit.runner.RunWith

/**
* Created by VerachadW on 10/3/2016 AD.
*/
@RunWith(JUnitPlatform::class)
class RelayPrintSpek : Spek({
describe("Relay InputArgument print function") {
given("id as argument and value as 1") {
Expand Down

0 comments on commit ff19228

Please sign in to comment.