Skip to content

Commit

Permalink
improve main_test
Browse files Browse the repository at this point in the history
  • Loading branch information
vistart committed Aug 14, 2023
1 parent 8a6ca85 commit 614ffdb
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"math/rand"
"reflect"
"strconv"
"sync"
"testing"
Expand Down Expand Up @@ -1139,3 +1140,50 @@ func TestOneDoneMultiNotified(t *testing.T) {
log.Println("finished.")
})
}

func TestTypeConversion(t *testing.T) {
t.Run("scalar type", func(t *testing.T) {
var a any = 6
value1, ok := a.(int)
assert.True(t, ok)
assert.Equal(t, 6, value1)
value2, ok := a.(int64)
assert.False(t, ok)
assert.Zero(t, value2)
})
t.Run("struct", func(t *testing.T) {
type typeA struct {
FieldA int
FieldB int64
}
var a any = typeA{1, 2}
type typeB struct {
FieldA int
FieldB int64
}
value1, ok := a.(typeB)
assert.False(t, ok)
assert.Equal(t, typeB{0, 0}, value1)

var b any = typeB{1, 2}
value2, ok := b.(typeB)
assert.True(t, ok)
assert.Equal(t, typeB{1, 2}, value2)

var c1 any = typeA{1, 2}
var c2 any = typeB{1, 2}

assert.NotEqual(t, reflect.TypeOf(c1), reflect.TypeOf(c2))
})
}

func TestChannel(t *testing.T) {
t.Run("close when blocking", func(t *testing.T) {
chan1 := make(chan struct{})
go func() {
<-chan1
}()
time.Sleep(time.Millisecond * 100)
close(chan1)
})
}

0 comments on commit 614ffdb

Please sign in to comment.