Skip to content

Commit 59a9c33

Browse files
committed
1. 修改stateSwitch on和off的返回值
2. 完善stateswitch的测试 3. 将stateswitch的name属性作为构造函数
1 parent d278f8b commit 59a9c33

File tree

9 files changed

+77
-89
lines changed

9 files changed

+77
-89
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"a":"b"}

wechaty-puppet/src/main/kotlin/Puppet.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ abstract class Puppet : EventEmitter {
6161
/**
6262
*
6363
*/
64-
6564
constructor(puppetOptions: PuppetOptions) {
6665

6766
count.addAndGet(1)

wechaty-puppet/src/main/kotlin/io/github/wechaty/eventEmitter/EventEmitter.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ open class EventEmitter : EventEmitterInterface {
8484
}
8585

8686
map.put(event, wrapListener)
87-
8887
}
8988

9089
override fun removeAllListeners(event:Event): Boolean {

wechaty-puppet/src/main/kotlin/io/github/wechaty/memorycard/backend/StorageFile.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import java.io.File
1313
import java.io.IOException
1414
import java.lang.Exception
1515

16-
// 本身是不存储数据的
17-
// 存储了持久化的文件名和以及用什么方式存储
1816
class StorageFile(val name: String, var options: StorageBackendOptions) : StorageBackend(name, options) {
1917

2018
private var absFileName: String

wechaty-puppet/src/main/kotlin/io/github/wechaty/memorycard/backend/StorageS3.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class StorageS3(val name: String, var options: StorageBackendOptions) : StorageB
2121
val basicAWSCredentials = BasicAWSCredentials(_options.accessKeyId, _options.secretAccessKey)
2222
this.s3 = AmazonS3ClientBuilder.standard().withCredentials(AWSStaticCredentialsProvider(basicAWSCredentials))
2323
.withRegion(_options.region).build()
24-
2524
}
2625

2726
override fun save(payload: MemoryCardPayload) {

wechaty-puppet/src/main/kotlin/io/github/wechaty/status/StateSwitch.kt

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ import java.util.concurrent.atomic.AtomicInteger
1212
import javax.swing.plaf.nimbus.State
1313

1414
var COUNTER = AtomicInteger()
15-
val nop: () -> Unit = {}
1615
val resolver: () -> Unit = {}
17-
class StateSwitch: EventEmitter(){
16+
class StateSwitch(var name :String = "#${COUNTER.addAndGet(1)}"): EventEmitter(){
1817

1918
@Volatile
2019
private var onoff:Boolean = false
@@ -28,8 +27,8 @@ class StateSwitch: EventEmitter(){
2827
private lateinit var onResolver : () -> Unit
2928
private lateinit var offResolver : () -> Unit
3029

31-
private var name :String = "#${COUNTER.addAndGet(1)}"
32-
30+
// private var name :String = "#${COUNTER.addAndGet(1)}"
31+
private val VERSION: String = "0.0.0"
3332
private lateinit var onQueue: ArrayBlockingQueue<() -> Unit>
3433
private lateinit var offQueue: ArrayBlockingQueue<() -> Unit>
3534

@@ -42,36 +41,21 @@ class StateSwitch: EventEmitter(){
4241
offQueue = ArrayBlockingQueue(1)
4342
this.offQueue.put(resolver)
4443

45-
// runBlocking {
46-
// onPromise = {
47-
// onResolver = nop
48-
// }
49-
// onPromise()
50-
// }
5144
}
5245

5346
// 传入on的状态只能是on或者说pending
54-
fun on(state: StateEnum): String {
47+
fun on(state: StateEnum): StateEnum {
5548

5649
log.debug("statusSwitch $name on ${state.name} <- ${this.on()}")
5750

5851
if (state == StateEnum.OFF) {
59-
throw Exception("the parameter state shouldn't be off")
52+
throw Exception("the parameter state shouldn't be OFF")
6053
}
6154
onoff = true
6255
pending = (state == StateEnum.PENDING)
6356

6457
emit(EventEnum.ON, state.name)
6558

66-
// if (this.offResolver === nop) {
67-
// runBlocking {
68-
// offPromise = {
69-
// offResolver = nop
70-
// }
71-
// offPromise()
72-
// }
73-
// }
74-
7559
if (this.offQueue.isEmpty()) {
7660
this.offQueue.put(resolver)
7761
}
@@ -83,37 +67,29 @@ class StateSwitch: EventEmitter(){
8367

8468
}
8569
// get the current on state
86-
fun on(): String {
70+
fun on(): StateEnum {
8771
val on =
8872
if (this.onoff == true)
8973
if (this.pending == true)
90-
"pending"
74+
StateEnum.PENDING
9175
else
92-
"true"
76+
StateEnum.ON
9377
else
94-
"false"
78+
StateEnum.OFF
9579
log.info("StateSwitch, <{}> on() is {}", this.name, on)
9680
return on
9781
}
9882

99-
fun off(state: StateEnum): String {
83+
fun off(state: StateEnum): StateEnum {
10084
log.info("StateSwitch, <{}> off({}) <- ({})", this.name, state, this.off())
10185
if (state == StateEnum.ON) {
102-
throw Exception("the parameter state shouldn't be on")
86+
throw Exception("the parameter state shouldn't be ON")
10387
}
10488

10589
this.onoff = false
10690
this.pending = (state == StateEnum.PENDING)
10791
this.emit(StateEnum.OFF, state)
10892

109-
// if (this.onResolver === nop) {
110-
// runBlocking {
111-
// onPromise = {
112-
// onResolver = nop
113-
// }
114-
// onPromise()
115-
// }
116-
// }
11793
if (this.onQueue.isEmpty()) {
11894
this.onQueue.put(resolver)
11995
}
@@ -127,15 +103,15 @@ class StateSwitch: EventEmitter(){
127103
*
128104
* @return 返回当前off的状态
129105
*/
130-
fun off(): String {
106+
fun off(): StateEnum {
131107
val off =
132108
if (!this.onoff)
133109
if (this.pending)
134-
"pending"
110+
StateEnum.PENDING
135111
else
136-
"true"
112+
StateEnum.OFF
137113
else
138-
"false"
114+
StateEnum.ON
139115
log.info("StateSwitch, <{}> off() is {}", this.name, off)
140116
return off
141117
}
@@ -157,10 +133,6 @@ class StateSwitch: EventEmitter(){
157133
// 当前状态为on
158134
// 或者说当前状态为off, 但是允许变换状态
159135
// his.onPromise
160-
// coroutineScope {
161-
// val job = launch { onPromise }
162-
// job.join()
163-
// }
164136
CoroutineScope(Dispatchers.Default).launch {
165137
onQueue.take()
166138
}
@@ -173,11 +145,6 @@ class StateSwitch: EventEmitter(){
173145
}
174146
// 当前状态为off,或者说当前状态为on, 但是允许变换状态
175147
// 执行状态改变时执行的函数
176-
// this.offPromise
177-
// coroutineScope {
178-
// val job = launch { offPromise }
179-
// job.join()
180-
// }
181148
CoroutineScope(Dispatchers.Default).launch {
182149
offQueue.take()
183150
}
@@ -207,6 +174,9 @@ class StateSwitch: EventEmitter(){
207174
})
208175
}
209176

177+
fun version(): String {
178+
return this.VERSION
179+
}
210180
companion object {
211181
private val log = LoggerFactory.getLogger(StateSwitch::class.java)
212182
}

wechaty-puppet/src/test/kotlin/io/github/wechaty/memorycard/MemoryCardTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,4 @@ class MemoryCardTest {
166166
println(it)
167167
}
168168
}
169-
170169
}

wechaty-puppet/src/test/kotlin/io/github/wechaty/status/StateSwitchTes.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

wechaty-puppet/src/test/kotlin/io/github/wechaty/status/StateSwitchTest.kt

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,101 @@ package io.github.wechaty.status
22

33
import io.github.wechaty.StateEnum
44
import io.github.wechaty.eventEmitter.Listener
5+
import io.github.wechaty.io.github.wechaty.status.StateSwitchListener
56
import org.junit.Test
67
import org.junit.Assert.*;
78
class StateSwitchTest {
89

910
@Test
1011
fun testOn() {
1112
val stateSwitch = StateSwitch()
12-
assertEquals("default is not on", "false", stateSwitch.on())
13+
assertEquals("default is not on", StateEnum.OFF, stateSwitch.on())
1314

1415
stateSwitch.on(StateEnum.PENDING)
15-
assertEquals("should be state pending", "pending", stateSwitch.on())
16+
assertEquals("should be state pending", StateEnum.PENDING, stateSwitch.on())
1617
stateSwitch.on(StateEnum.ON)
17-
assertEquals("should be state true", "true", stateSwitch.on())
18-
assertEquals("should not off", "false", stateSwitch.off())
18+
assertEquals("should be state true", StateEnum.ON, stateSwitch.on())
19+
assertEquals("should not off", StateEnum.ON, stateSwitch.off())
1920
stateSwitch.off(StateEnum.OFF)
20-
assertEquals("should not on after off()", "false", stateSwitch.on())
21+
assertEquals("should not on after off()", StateEnum.OFF, stateSwitch.off())
2122

2223
}
2324

2425
@Test
2526
fun testOff() {
2627
val stateSwitch = StateSwitch()
27-
stateSwitch.off(StateEnum.PENDING)
28-
println(stateSwitch.off())
2928

30-
stateSwitch.ready(StateEnum.OFF)
31-
println(stateSwitch.on())
29+
assertEquals("default is OFF", StateEnum.OFF, stateSwitch.off())
30+
stateSwitch.off(StateEnum.PENDING)
31+
assertEquals("should be state PENDING", StateEnum.PENDING, stateSwitch.off())
3232

3333
stateSwitch.off(StateEnum.OFF)
34-
println(stateSwitch.on())
34+
assertEquals("should be state OFF", StateEnum.OFF, stateSwitch.off())
35+
assertEquals("should not ON", StateEnum.OFF, stateSwitch.on())
36+
37+
stateSwitch.on(StateEnum.ON)
38+
assertEquals("should not OFF after on()", StateEnum.ON, stateSwitch.on())
39+
}
40+
41+
@Test
42+
fun testPending() {
43+
val stateSwitch = StateSwitch()
44+
assertEquals("default is not PENDING", StateEnum.OFF, stateSwitch.off())
45+
stateSwitch.on(StateEnum.PENDING)
46+
assertEquals("should in PENDING state", StateEnum.PENDING, stateSwitch.on())
47+
48+
stateSwitch.on(StateEnum.ON)
49+
assertEquals("should not in pending state", StateEnum.ON, stateSwitch.on())
50+
51+
stateSwitch.off(StateEnum.PENDING)
52+
assertEquals("should in PENDING state", StateEnum.PENDING, stateSwitch.off())
3553
}
3654

3755
@Test
38-
fun name() {
56+
fun testName() {
57+
var CLIENT_NAME = "StateSwitchTest"
58+
val stateSwitch = StateSwitch(CLIENT_NAME)
59+
assertEquals("should get the same client name as init", CLIENT_NAME, stateSwitch.name)
60+
}
3961

62+
@Test
63+
fun testVersion() {
64+
val stateSwitch = StateSwitch()
65+
println(stateSwitch.name)
66+
assertNotNull("should get version", stateSwitch.version())
4067
}
68+
69+
4170
@Test
4271
fun ready() {
4372
val stateSwitch = StateSwitch()
73+
println("刚开始:" + stateSwitch.off())
4474
stateSwitch.on(StateEnum.PENDING)
4575
println("调用on(pending):" + stateSwitch.on())
46-
println("调用on(pending):" + stateSwitch.off())
4776
stateSwitch.ready(StateEnum.ON)
4877
println("调用ready(on)之后:" + stateSwitch.on())
4978
stateSwitch.on(StateEnum.ON)
5079
println("调用on(on):" + stateSwitch.on())
80+
81+
stateSwitch.off(StateEnum.PENDING)
82+
println("调用off(pending):" + stateSwitch.off())
83+
stateSwitch.off(StateEnum.OFF)
84+
println("调用off(off):" + stateSwitch.off())
5185
}
5286

5387
@Test
54-
fun add() {
88+
fun addEventListener() {
5589
val stateSwitch = StateSwitch()
90+
stateSwitch.addEventListener(StateEnum.ON, object : StateSwitchListener {
91+
override fun handler(state: StateEnum) {
92+
println(state.name)
93+
}
94+
});
95+
96+
stateSwitch.removeEventListener(StateEnum.OFF, object : StateSwitchListener {
97+
override fun handler(state: StateEnum) {
98+
println(state.name)
99+
}
100+
})
56101
}
57102
}

0 commit comments

Comments
 (0)