Improve event unit test

This commit is contained in:
Drahoslav 2018-06-06 17:51:04 +02:00
parent 4ae1cfd964
commit 250b0836f8

View File

@ -15,35 +15,35 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}
func TestRisingEdgeEvent(t *testing.T) {
func TestEvent(t *testing.T) {
src := Pin(3)
src.Mode(Output)
src.Low()
pin := Pin(2)
pin.Mode(Input)
pin.PullDown()
pin.Detect(RiseEdge)
timeout := time.After(time.Second)
loop:
for {
t.Run("rising edge", func(t *testing.T) {
pin.Detect(RiseEdge)
src.Low()
for i := 0; ; i++ {
src.High()
time.Sleep(time.Second / 5)
time.Sleep(time.Second / 10)
if pin.EdgeDetected() {
t.Log("edge rised")
} else {
t.Errorf("Rise event should be detected")
}
select {
case <-timeout:
break loop
default:
if i == 5 {
break
}
src.Low()
}
time.Sleep(time.Second / 10)
if pin.EdgeDetected() {
t.Error("Rise should not be detected, no change since last call")
}
@ -52,38 +52,30 @@ loop:
if pin.EdgeDetected() {
t.Error("Rise should not be detected, events disabled")
}
}
func TestFallingEdgeEvent(t *testing.T) {
src := Pin(3)
src.Mode(Output)
})
t.Run("falling edge", func(t *testing.T) {
pin.Detect(FallEdge)
src.High()
pin := Pin(2)
pin.Mode(Input)
pin.PullDown()
pin.Detect(FallEdge)
timeout := time.After(time.Second)
loop:
for {
for i := 0; ; i++ {
src.Low()
time.Sleep(time.Second / 5)
time.Sleep(time.Second / 10)
if pin.EdgeDetected() {
t.Log("edge fallen")
} else {
t.Errorf("Fall event should be detected")
}
select {
case <-timeout:
break loop
default:
if i == 5 {
break
}
src.High()
}
time.Sleep(time.Second / 10)
if pin.EdgeDetected() {
t.Error("Fall should not be detected, no change since last call")
}
@ -92,4 +84,37 @@ loop:
if pin.EdgeDetected() {
t.Error("Fall should not be detected, events disabled")
}
})
t.Run("both edges", func(t *testing.T) {
pin.Detect(AnyEdge)
src.Low()
for i := 0; i < 5; i++ {
src.High()
if pin.EdgeDetected() {
t.Log("edge detected")
} else {
t.Errorf("Rise event shoud be detected")
}
src.Low()
if pin.EdgeDetected() {
t.Log("edge detected")
} else {
t.Errorf("Fall edge should be detected")
}
}
pin.Detect(NoEdge)
src.High()
src.Low()
if pin.EdgeDetected() {
t.Errorf("No edge should be detected, events disabled")
}
})
}