Skip to content

Commit

Permalink
Update Kotlin and library dependcies (#18)
Browse files Browse the repository at this point in the history
* Update .gitignore

* Update dependecies

* Add secondary constructor for DataEntry.NonDecimalNUmber

* Add Test for DataEntry classes

* Fix failed UTs
  • Loading branch information
VerachadW authored Oct 26, 2017
1 parent 539559f commit 58374f6
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
.gradle

build
out
gradle.properties
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
ext {
kotlin_version = '1.1.1'
kotlin_version = '1.1.51'
}
repositories {
mavenCentral()
Expand Down
7 changes: 4 additions & 3 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ buildscript {

bintray_plugin_version = '0.3.4'

spek_version = '1.1.1'
hamkrest_version = '1.2.2.0'
spek_version = '1.1.5'
hamkrest_version = '1.4.2.2'
junit_version = '4.12'
junit_platform_version = '1.0.0-M4'
junit_platform_version = '1.0.0'

}
repositories {
Expand Down Expand Up @@ -64,6 +64,7 @@ dependencies {
testCompile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testCompile "junit:junit:$junit_version"
testCompile "com.natpryce:hamkrest:$hamkrest_version"
testCompile "org.junit.platform:junit-platform-runner:1.0.0"
testCompile ("org.jetbrains.spek:spek-api:$spek_version") {
exclude group: 'org.jetbrains.kotlin'
}
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/kotlin/me/lazmaid/kraph/lang/DataEntry.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package me.lazmaid.kraph.lang

import javax.xml.crypto.Data

/**
* Created by vwongsawangt on 8/6/2017 AD.
*/

internal sealed class DataEntry {
abstract fun print(prettyFormat: Boolean): String

class NonDecimalNumberData(val value: Long) : DataEntry() {
class NonDecimalNumberData(private val value: Long) : DataEntry() {
constructor(value: Int) : this(value.toLong())

override fun print(prettyFormat: Boolean) = value.toString()
}

class DecimalNumberData(val value: Double) : DataEntry() {
class DecimalNumberData(private val value: Double) : DataEntry() {
override fun print(prettyFormat: Boolean) = value.toString()
}

class BooleanData(val value: Boolean) : DataEntry() {
class BooleanData(private val value: Boolean) : DataEntry() {
override fun print(prettyFormat: Boolean) = value.toString()
}

class StringData(val value: String) : DataEntry() {
class StringData(private val value: String) : DataEntry() {
override fun print(prettyFormat: Boolean) =
if (prettyFormat) {
"\"$value\""
Expand All @@ -30,7 +30,7 @@ internal sealed class DataEntry {
}
}

class ArrayData(val values: List<DataEntry>) : DataEntry() {
class ArrayData(private val values: List<DataEntry>) : DataEntry() {
override fun print(prettyFormat: Boolean) =
values.foldIndexed("[") { index, acc, item ->
var newAcc = acc + item.print(prettyFormat)
Expand All @@ -43,7 +43,7 @@ internal sealed class DataEntry {
}
}

class ObjectData(val values: List<Pair<String, DataEntry>>) : DataEntry() {
class ObjectData(private val values: List<Pair<String, DataEntry>>) : DataEntry() {
override fun print(prettyFormat: Boolean) =
values.foldIndexed("{") { index, acc, (k, v) ->
var newAcc = acc + "${k.wrappedWithQuotes(prettyFormat)}: ${v.print(prettyFormat)}"
Expand Down
120 changes: 120 additions & 0 deletions core/src/test/kotlin/me/lazmaid/kraph/test/DataEntrySpek.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package me.lazmaid.kraph.test

import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import com.natpryce.hamkrest.isA
import me.lazmaid.kraph.lang.DataEntry
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

class DataEntrySpek : Spek({
describe("NonDecimalNumberDataEntry class") {
given("value as 5 and print pretty is false") {
val entry = DataEntry.NonDecimalNumberData(5)
val valueString = entry.print(false)
it("should print \"5\"") {
assertThat(valueString, isA(equalTo("5")))
}
}
given("value as 5 and print pretty is true") {
val entry = DataEntry.NonDecimalNumberData(5)
val valueString = entry.print(true)
it("should print \"5\"") {
assertThat(valueString, isA(equalTo("5")))
}
}
}

describe("DecimalNumberDataEntry class") {
given("value as 5.0 and print pretty is false") {
val entry = DataEntry.DecimalNumberData(5.0)
val valueString = entry.print(false)
it("should print \"5.0\"") {
assertThat(valueString, isA(equalTo("5.0")))
}
}
given("value as 5.0 and print pretty is true") {
val entry = DataEntry.DecimalNumberData(5.0)
val valueString = entry.print(true)
it("should print \"5.0\"") {
assertThat(valueString, isA(equalTo("5.0")))
}
}
}

describe("BooleanDataEntry class") {
given("value as true and print pretty is false") {
val entry = DataEntry.BooleanData(true)
val valueString = entry.print(false)
it("should print \"true\"") {
assertThat(valueString, isA(equalTo("true")))
}
}
given("value as true and print pretty is true") {
val entry = DataEntry.BooleanData(true)
val valueString = entry.print(true)
it("should print \"true\"") {
assertThat(valueString, isA(equalTo("true")))
}
}
}

describe("StringDataEntry class") {
given("value as 'Hello World' and print pretty is false") {
val entry = DataEntry.StringData("Hello World")
val valueString = entry.print(false)
it("should print \\\"Hello World\\\"") {
assertThat(valueString, isA(equalTo("\\\"Hello World\\\"")))
}
}
given("value as true and print pretty is true") {
val entry = DataEntry.StringData("Hello World")
val valueString = entry.print(true)
it("should print \"Hello World\"") {
assertThat(valueString, isA(equalTo("\"Hello World\"")))
}
}
}

describe("ArrayDataEntry class") {
given("value as [1,2,3] and print pretty is false") {
val values = listOf(1, 2, 3).map { DataEntry.NonDecimalNumberData(it) }
val entry = DataEntry.ArrayData(values)
val valueString = entry.print(false)
it("should print [1, 2, 3]") {
assertThat(valueString, isA(equalTo("[1, 2, 3]")))
}
}
given("vale as [1,2,3] and print pretty is true") {
val values = listOf(1, 2, 3).map { DataEntry.NonDecimalNumberData(it) }
val entry = DataEntry.ArrayData(values)
val valueString = entry.print(true)
it("should print [1, 2, 3]") {
assertThat(valueString, isA(equalTo("[1, 2, 3]")))
}
}
}

describe("ObjectDataEntry class") {
given("with key-values and print pretty as false") {
val values = listOf("name" to DataEntry.StringData("John Doe"),
"age" to DataEntry.NonDecimalNumberData(18))
val entry = DataEntry.ObjectData(values)
val valueString = entry.print(false)
it("should print {\\\"name\\\": \\\"John Doe\\\", \\\"age\\\": 18}") {
assertThat(valueString, isA(equalTo("{\\\"name\\\": \\\"John Doe\\\", \\\"age\\\": 18}")))
}
}
given("with key-values and print pretty as true") {
val values = listOf("name" to DataEntry.StringData("John Doe"),
"age" to DataEntry.NonDecimalNumberData(18))
val entry = DataEntry.ObjectData(values)
val valueString = entry.print(true)
it("should print {\"name\": \"John Doe\", \"age\": 18}") {
assertThat(valueString, isA(equalTo("{\"name\": \"John Doe\", \"age\": 18}")))
}
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ class GraphQLPrintSpek : Spek({
assertThat(node.print(true, 0), equalTo("(users: [{\"name\": \"user1\", \"email\": \"[email protected]\"}, {\"name\": \"user2\", \"email\": \"[email protected]\"}])"))
}
}
on("print pretty") {
on("print normal") {
it("should print (users: [{\\\"name\\\": \\\"user1\\\", \\\"email\\\": \\\"[email protected]\\\"}, {\\\"name\": \\\"user2\\\", \\\"email\\\": \\\"[email protected]\\\"}])") {
assertThat(node.print(false, 0), equalTo("(users: [{\\\"name\\\": \\\"user1\\\", \\\"email\": \\\"[email protected]\\\"}, {\\\"name\\\": \\\"user2\\\", \\\"email\\\": \\\"[email protected]\\\"}])"))
assertThat(node.print(false, 0), equalTo("(users: [{\\\"name\\\": \\\"user1\\\", \\\"email\\\": \\\"[email protected]\\\"}, {\\\"name\\\": \\\"user2\\\", \\\"email\\\": \\\"[email protected]\\\"}])"))
}
}
}
Expand Down

0 comments on commit 58374f6

Please sign in to comment.