refactor: move reflect.Indirect test to _demo and use panic()
- Moved test from _cmptest/reflect_indirect/ to _demo/go/reflect-indirect/ - Refactored to use panic() for validation instead of fmt.Println - Added proper assertions for all test cases Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
This commit is contained in:
31
_demo/go/reflect-indirect/reflect-indirect.go
Normal file
31
_demo/go/reflect-indirect/reflect-indirect.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func main() {
|
||||
x := 42
|
||||
p := &x
|
||||
|
||||
// Test 1: Non-pointer value - should return same value
|
||||
v1 := reflect.Indirect(reflect.ValueOf(x))
|
||||
if !v1.IsValid() || v1.Interface() != 42 {
|
||||
panic("Non-pointer test failed: expected 42")
|
||||
}
|
||||
|
||||
// Test 2: Pointer - should dereference
|
||||
v2 := reflect.Indirect(reflect.ValueOf(p))
|
||||
if !v2.IsValid() || v2.Interface() != 42 {
|
||||
panic("Pointer dereference test failed: expected 42")
|
||||
}
|
||||
|
||||
// Test 3: Nil pointer - should return invalid Value
|
||||
var nilPtr *int
|
||||
v3 := reflect.Indirect(reflect.ValueOf(nilPtr))
|
||||
if v3.IsValid() {
|
||||
panic("Nil pointer test failed: expected invalid Value")
|
||||
}
|
||||
|
||||
println("PASS")
|
||||
}
|
||||
Reference in New Issue
Block a user