package main
import (
"fmt"
"sync"
"time"
)
import (
"context"
set "container/list" lock "sync" )
func helperFunction() string {
return "Helper"
}
type HelperStruct struct {
Value int
}
func NewHelperStruct(value int) *HelperStruct {
return &HelperStruct{Value: value}
}
func nestedFunction() string {
return "Nested"
}
func useParentStruct() *HelperStruct {
return NewHelperStruct(100)
}
func otherHelperFunction() int {
return 42
}
func callOwnHelper() int {
return otherHelperFunction() }
var (
localHelperFunc = helperFunction
LocalHelperStruct = HelperStruct
)
import (
_ "net/http/pprof" )
func main() {
fmt.Println("=== Testing Import Resolution ===\n")
fmt.Println("1. Standard library imports:")
var mu sync.Mutex
mu.Lock()
mu.Unlock()
fmt.Println(" sync.Mutex created and used ✓")
duration := time.Second
fmt.Printf(" time.Duration used: %v ✓\n", duration)
fmt.Println("\n2. Aliased imports:")
ctx := context.Background()
fmt.Printf(" context.Background() via import: %T ✓\n", ctx)
list := set.New()
list.PushBack(1)
fmt.Println(" list used as 'set' alias ✓")
var lockMu lock.Mutex
lockMu.Lock()
lockMu.Unlock()
fmt.Println(" sync.Mutex used as 'lock' alias ✓")
fmt.Println("\n3. Local function resolution:")
result := helperFunction()
fmt.Printf(" helperFunction() returns: '%s' (expected: 'Helper') ✓\n", result)
helper := NewHelperStruct(10)
fmt.Printf(" HelperStruct created with value: %d ✓\n", helper.Value)
fmt.Println("\n4. Nested function calls:")
nestedResult := nestedFunction()
fmt.Printf(" nestedFunction() returns: '%s' (expected: 'Nested') ✓\n", nestedResult)
parentStruct := useParentStruct()
fmt.Printf(" useParentStruct() returns struct with value: %d ✓\n", parentStruct.Value)
fmt.Println("\n5. Scope resolution with conflicting names:")
localResult := helperFunction()
fmt.Printf(" helperFunction() returns: '%s' (expected: 'Helper') ✓\n", localResult)
otherResult := callOwnHelper()
fmt.Printf(" callOwnHelper() returns: %d (expected: 42) ✓\n", otherResult)
fmt.Println("\n6. Local aliases (dot import equivalent):")
aliasResult := localHelperFunc()
fmt.Printf(" localHelperFunc() returns: '%s' (expected: 'Helper') ✓\n", aliasResult)
aliasStruct := LocalHelperStruct{Value: 99}
fmt.Printf(" LocalHelperStruct created with value: %d ✓\n", aliasStruct.Value)
fmt.Println("\n=== All import tests completed ===")
}
func TestStandardImports() error {
var mu sync.Mutex
mu.Lock()
defer mu.Unlock()
ctx := context.Background()
if ctx == nil {
return fmt.Errorf("context should not be nil")
}
return nil
}
func TestAliasedImports() error {
list := set.New() if list == nil {
return fmt.Errorf("aliased list should not be nil")
}
var lockMu lock.Mutex lockMu.Lock()
defer lockMu.Unlock()
return nil
}
func TestLocalResolution() error {
result := helperFunction()
if result != "Helper" {
return fmt.Errorf("expected 'Helper', got '%s'", result)
}
helper := NewHelperStruct(20)
if helper.Value != 20 {
return fmt.Errorf("expected value 20, got %d", helper.Value)
}
nested := nestedFunction()
if nested != "Nested" {
return fmt.Errorf("expected 'Nested', got '%s'", nested)
}
return nil
}
func TestScopeResolution() error {
result := callOwnHelper()
if result != 42 {
return fmt.Errorf("expected 42, got %d", result)
}
return nil
}
func TestNestedResolution() error {
result := helperFunction()
if result != "Helper" {
return fmt.Errorf("nested test: expected 'Helper', got '%s'", result)
}
return nil
}
func RunAllTests() {
tests := []struct {
name string
test func() error
}{
{"Standard Imports", TestStandardImports},
{"Aliased Imports", TestAliasedImports},
{"Local Resolution", TestLocalResolution},
{"Scope Resolution", TestScopeResolution},
{"Nested Resolution", TestNestedResolution},
}
fmt.Println("\n=== Running Tests ===")
allPassed := true
for _, test := range tests {
if err := test.test(); err != nil {
fmt.Printf("❌ %s: %v\n", test.name, err)
allPassed = false
} else {
fmt.Printf("✅ %s: passed\n", test.name)
}
}
if allPassed {
fmt.Println("\n🎉 All tests passed!")
} else {
fmt.Println("\n❌ Some tests failed!")
}
}
func init() {
go func() {
time.Sleep(100 * time.Millisecond) RunAllTests()
}()
}