Skip to content

Commit

Permalink
Nested object variable naming (#60)
Browse files Browse the repository at this point in the history
* Add initial unit tests

* Fix for nested variable naming

* version
  • Loading branch information
andycate authored Feb 18, 2019
1 parent 5f7aa88 commit 5096e6b
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies {
}

group = 'org.team5499'
version = '0.4.3' /* Change this when deploying a new version */
version = '0.5.0' /* Change this when deploying a new version */

task sourcesJar(type: Jar) {
from sourceSets.main.allJava
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/org/team5499/dashboard/Dashboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,17 @@ object Dashboard {
null
})

Spark.awaitInitialization()
SocketHandler.startBroadcastThread() // start broadcasting data
}

fun stop() {
SocketHandler.stopBroadcastThread()
SocketHandler.awaitStop()
Spark.stop()
Spark.awaitStop()
}

fun setVariable(key: String, value: Any) {
variableUpdates.put(key, value)
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/kotlin/org/team5499/dashboard/DashboardVar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@ class DashboardVar<T>(var initValue: T) {

var isInit = false

fun getDashName(thisRef: Any?, property: KProperty<*>): String {
val packageLength = thisRef!!::class.java.getPackage().name.length
return "${thisRef::class.qualifiedName!!.substring(packageLength + 1)}.${property.name}"
}

operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
if (!isInit) {
Dashboard.setVariable(property.name, initValue as Any)
Dashboard.setVariable(getDashName(thisRef, property), initValue as Any)
isInit = true
println(getDashName(thisRef, property))
return initValue
}
return Dashboard.getVariable(property.name)
return Dashboard.getVariable(getDashName(thisRef, property))
}

operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
isInit = true
Dashboard.setVariable(property.name, value as Any)
Dashboard.setVariable(getDashName(thisRef, property), value as Any)
}
}
9 changes: 9 additions & 0 deletions src/main/kotlin/org/team5499/dashboard/SocketHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ class SocketHandler {
public fun startBroadcastThread() {
broadcastThread.start()
}

public fun stopBroadcastThread() {
broadcastThread.stop()
}

public fun awaitStop() {
broadcastThread.join()
}

public fun broadcastJSONMinusSession(json: JSONObject, session: Session) {
for (s in sessions) {
if (!s.equals(session)) {
Expand Down
98 changes: 98 additions & 0 deletions src/test/kotlin/tests/NestedVariableTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package tests

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.fail

import org.team5499.dashboard.Dashboard
import org.team5499.dashboard.DashboardVar
import org.team5499.dashboard.DashboardException

class NestedVariableTest {

companion object {
@BeforeAll
@JvmStatic
fun initDashboard() {
Dashboard.start(this, "playgroundConf.json")
Constants.initConstants()
}

@AfterAll
@JvmStatic
fun tearDownDashboard() {
Dashboard.stop()
}
}

@Test
fun checkForOuterVar() {
try {
assertEquals(Dashboard.getDouble("Constants.PROP"), 1.0)
} catch (de: DashboardException) {
fail<Any>("Constants.PROP not found")
}
}

@Test
fun checkForFirstVar() {
try {
assertEquals(Dashboard.getDouble("Constants.First.PROP"), 2.0)
} catch (de: DashboardException) {
fail<Any>("Constants.First.PROP not found")
}
}

@Test
fun checkForSecondVar() {
try {
assertEquals(Dashboard.getDouble("Constants.Second.PROP"), 3.0)
} catch (de: DashboardException) {
fail<Any>("Constants.Second.PROP not found")
}
}

@Test
fun checkForOuterNestVar() {
try {
assertEquals(Dashboard.getDouble("Constants.Nest.PROP"), 4.0)
} catch (de: DashboardException) {
fail<Any>("Constants.Nest.PROP not found")
}
}

@Test
fun checkForInnerNestVar() {
try {
assertEquals(Dashboard.getDouble("Constants.Nest.Inner.PROP"), 5.0)
} catch (de: DashboardException) {
fail<Any>("Constants.Nest.Inner.PROP not found")
}
}
}

object Constants {

public fun initConstants() {
DashboardVar.initClassProps(Constants::class)
}

public var PROP by DashboardVar(1.0)

object First {
public var PROP by DashboardVar(2.0)
}

object Second {
public var PROP by DashboardVar(3.0)
}

object Nest {
public var PROP by DashboardVar(4.0)
object Inner {
public var PROP by DashboardVar(5.0)
}
}
}

0 comments on commit 5096e6b

Please sign in to comment.