This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
198 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.kweb | ||
|
||
import com.gargoylesoftware.htmlunit.html.HtmlPage | ||
import io.kweb.dom.element.creation.tags.h1 | ||
import io.kweb.dom.element.new | ||
import org.amshove.kluent.shouldEqual | ||
import org.jetbrains.spek.api.Spek | ||
import org.jetbrains.spek.api.dsl.* | ||
|
||
object KwebSpec : Spek({ | ||
given("a Kweb instance listening on port 12243") { | ||
on("creating a kweb server") { | ||
val kweb = Kweb(port = 12243) { | ||
doc.body.new { | ||
h1().text("Lorum Ipsum") | ||
} | ||
} | ||
|
||
|
||
useWebClient { webClient -> | ||
|
||
|
||
val page = webClient.getPage<HtmlPage>("http://localhost:12243/") | ||
|
||
webClient.waitForBackgroundJavaScript(10000) | ||
|
||
page.getElementsByTagName("h1").let { h1Elements -> | ||
it("should contain one H1 element containing the appropriate text") { | ||
h1Elements.size shouldEqual 1 | ||
h1Elements.first().let { h1Element -> | ||
h1Element.textContent shouldEqual "Lorum Ipsum" | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
kweb.close() | ||
} | ||
|
||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,86 @@ | ||
package io.kweb.state | ||
|
||
import io.kotlintest.matchers.shouldBe | ||
import io.kotlintest.specs.FreeSpec | ||
import org.amshove.kluent.shouldEqual | ||
import org.jetbrains.spek.api.Spek | ||
import org.jetbrains.spek.api.dsl.* | ||
|
||
/** | ||
* Created by ian on 6/18/17. | ||
*/ | ||
class BindableSpec : FreeSpec() { | ||
init { | ||
"A Bindable should initialize correctly" { | ||
val sw = ReadOnlyBindable("Test") | ||
sw.value shouldBe "Test" | ||
object BindableSpec : Spek({ | ||
describe("ReadOnlyBindable") { | ||
val readOnlyBindable = ReadOnlyBindable("Test") | ||
on("retrieving its value") { | ||
val value = readOnlyBindable.value | ||
it("should match the value it was initialized with") { | ||
readOnlyBindable.value shouldEqual "Test" | ||
} | ||
} | ||
} | ||
|
||
"A Bindable should notify a listener of a change" { | ||
val sw = Bindable("Foo") | ||
var old : String? = null | ||
var new : String? = null | ||
sw.addListener { o, n -> | ||
old shouldBe null | ||
new shouldBe null | ||
describe("A simple string bindable") { | ||
val bindable = Bindable("Foo") | ||
context("adding a listener that modifies two vars") { | ||
var old: String? = null | ||
var new: String? = null | ||
val handle = bindable.addListener { o, n -> | ||
old shouldEqual null | ||
new shouldEqual null | ||
old = o | ||
new = n | ||
} | ||
sw.value = "Bar" | ||
old shouldBe "Foo" | ||
new shouldBe "Bar" | ||
} | ||
"A removed listener shouldn't be called" { | ||
val sw = Bindable("Foo") | ||
var old : String? = null | ||
var new : String? = null | ||
val listenerHandler = sw.addListener { o, n -> | ||
old shouldBe null | ||
new shouldBe null | ||
old = o | ||
new = n | ||
on("modifying the value") { | ||
bindable.value = "Bar" | ||
it("should call the listener, modifying the vars accordingly") { | ||
old shouldEqual "Foo" | ||
new shouldEqual "Bar" | ||
} | ||
} | ||
on("removing the listener and modifying the value again") { | ||
bindable.removeListener(handle) | ||
bindable.value = "FooBar" | ||
it("shouldn't call the listener again") { | ||
old shouldEqual "Foo" | ||
new shouldEqual "Bar" | ||
} | ||
|
||
} | ||
sw.removeListener(listenerHandler) | ||
sw.value = "Bar" | ||
old shouldBe null | ||
new shouldBe null | ||
} | ||
context("creating a one-way mapping") { | ||
val mappedBindable = bindable.map { it.length } | ||
on("modifying the original bindable") { | ||
bindable.value = "elephant" | ||
it("should be mapped correctly") { | ||
mappedBindable.value shouldEqual 8 | ||
} | ||
|
||
"A read-only mapped watcher should work" { | ||
val sw = Bindable("Foo") | ||
val mapped = sw.map {it -> it.length} | ||
mapped.value shouldBe 3 | ||
sw.value = "Hello" | ||
mapped.value shouldBe 5 | ||
} | ||
} | ||
/* | ||
"A bi-directional mapped watcher should work" { | ||
data class Foo(var bar : Int) | ||
val sw = Bindable(Foo(12)) | ||
val mapped = sw.map( | ||
object : ReversableFunction<> | ||
|
||
{it.bar.toString()}, {n, o -> n.copy(bar = o.toInt())} | ||
} | ||
describe("a string bindable") { | ||
val lowerCaseBindable = Bindable("foo") | ||
context("creating a two-way mapping") { | ||
val upperCaseBindable = lowerCaseBindable.map(object : ReversableFunction<String, String> { | ||
override fun map(from: String) = from.toUpperCase() | ||
|
||
override fun unmap(original: String, change: String) = change.toLowerCase() | ||
|
||
) | ||
sw.value shouldBe Foo(12) | ||
mapped.value = "143" | ||
sw.value shouldBe Foo(143) | ||
} */ | ||
}) | ||
on("modifying the original bindable") { | ||
lowerCaseBindable.value = "one" | ||
it("should be mapped correctly") { | ||
val value = upperCaseBindable.value | ||
value shouldEqual "ONE" | ||
} | ||
} | ||
on("modifying the mappedBindable") { | ||
upperCaseBindable.value = "TWO" | ||
it("should be unmapped correctly") { | ||
val value = lowerCaseBindable.value | ||
value shouldEqual "two" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) |
Oops, something went wrong.