-
Notifications
You must be signed in to change notification settings - Fork 0
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
2 changed files
with
56 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package reflect | ||
|
||
import "reflect" | ||
|
||
func TypeName(v any) string { | ||
return reflect.TypeOf(v).String() | ||
} |
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,49 @@ | ||
package reflect_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hjwalt/runway/reflect" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTypeof(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
// Test with an integer | ||
intType := reflect.TypeName(42) | ||
assert.Equal("int", intType) | ||
|
||
// Test with a string | ||
stringType := reflect.TypeName("hello") | ||
assert.Equal("string", stringType) | ||
|
||
// Test with a custom struct | ||
type Person struct { | ||
Name string | ||
Age int | ||
} | ||
personType := reflect.TypeName(Person{}) | ||
assert.Equal("reflect_test.Person", personType) | ||
|
||
// Test with a pointer | ||
pointerType := reflect.TypeName(&Person{}) | ||
assert.Equal("*reflect_test.Person", pointerType) | ||
|
||
// Test with a pointer var | ||
var personPointer *Person | ||
personPointerType := reflect.TypeName(personPointer) | ||
assert.Equal("*reflect_test.Person", personPointerType) | ||
|
||
// Test with a slice | ||
sliceType := reflect.TypeName([]int{}) | ||
assert.Equal("[]int", sliceType) | ||
|
||
// Test with a map | ||
mapType := reflect.TypeName(map[string]int{}) | ||
assert.Equal("map[string]int", mapType) | ||
|
||
// Test with a function | ||
funcType := reflect.TypeName(func() {}) | ||
assert.Equal("func()", funcType) | ||
} |