From 45331283c1aa44c169f9a9ec1e249afef70b0100 Mon Sep 17 00:00:00 2001 From: programmingkidx Date: Tue, 16 Jan 2024 19:45:19 -0500 Subject: [PATCH] Fix issues with map type (#242) * Fix bug with size variable's value not being set when map is empty * Fix bug with convertToGoValue() that makes call to reflect.ValueOf() * foundation error test --- macos/foundation/foundation_test.go | 80 +++++++++++++++++++++++++++++ objc/type_convertion.go | 2 +- objc/type_convertion.m | 2 +- 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/macos/foundation/foundation_test.go b/macos/foundation/foundation_test.go index e5e86884..dea471f3 100644 --- a/macos/foundation/foundation_test.go +++ b/macos/foundation/foundation_test.go @@ -1,8 +1,12 @@ package foundation import ( + "fmt" "reflect" + "strings" "testing" + + "github.com/progrium/macdriver/objc" ) func TestFoundationValid(t *testing.T) {} @@ -35,3 +39,79 @@ func TestFoundationArray(t *testing.T) { t.Fatal("unexpected final slice from array") } } + + +// Test the (NS)Error type +func TestFoundationError(t *testing.T) { + t.Run("Empty Map", func(t *testing.T) { + + // declare the variables + var domain ErrorDomain = "My Domain" + code := -99 + userInfo := make(map[ErrorUserInfoKey]objc.IObject) + + // create the (NS)Error object + myError := Error_ErrorWithDomainCodeUserInfo(domain, code, userInfo) + + // check that length of map is zero + length := len(myError.UserInfo()) + if length != 0 { + t.Log("Error: Empty map length is not zero") + t.Fail() + } + }) + + t.Run("Map with items in it", func(t *testing.T) { + + // declare the variables + var domain ErrorDomain = "My Domain" + code := -99 + userInfo := make(map[ErrorUserInfoKey]objc.IObject) + userInfo["one"] = String_StringWithString("ONE") + userInfo["two"] = String_StringWithString("TWO") + userInfo["three"] = String_StringWithString("THREE") + userInfo["four"] = String_StringWithString("FOUR") + userInfo["five"] = String_StringWithString("FIVE") + + // create the (NS)Error object + myError := Error_ErrorWithDomainCodeUserInfo(domain, code, userInfo) + + // check each key with its value to see if they go together + for key, value := range myError.UserInfo() { + goKey := fmt.Sprintf("%s", reflect.ValueOf(key)) + goValue := string(value.Description()) + if strings.ToUpper(goKey) != goValue { + message := fmt.Sprintf("Failure detected: %s != %s", strings.ToUpper(goKey), goValue) + t.Log(message) + t.Fail() + } + } + }) + + t.Run("Check domain and code parameters", func(t *testing.T) { + + // declare the variables + var domain ErrorDomain = "My Domain" + code := -99 + userInfo := make(map[ErrorUserInfoKey]objc.IObject) + + // create the (NS)Error object + myError := Error_ErrorWithDomainCodeUserInfo(domain, code, userInfo) + + // check domain + checkDomain := myError.Domain() + if domain != checkDomain { + message := fmt.Sprintf("Expected domain value %s - actual %s", domain, checkDomain) + t.Log(message) + t.Fail() + } + + // check code + checkCode := myError.Code() + if code != checkCode { + message := fmt.Sprintf("Expected code value %d - actual %d", code, checkCode) + t.Log(message) + t.Fail() + } + }) +} diff --git a/objc/type_convertion.go b/objc/type_convertion.go index fcd026a4..31a69249 100644 --- a/objc/type_convertion.go +++ b/objc/type_convertion.go @@ -254,7 +254,7 @@ func convertToGoValue(p unsafe.Pointer, t reflect.Type) reflect.Value { return reflect.ValueOf(ToGoSlice(*(*unsafe.Pointer)(p), t).Interface()) } case reflect.Map: - return reflect.ValueOf(ToGoMap(*(*unsafe.Pointer)(p), t)) + return ToGoMap(*(*unsafe.Pointer)(p), t) case reflect.Struct: return reflect.NewAt(t, p).Elem() case reflect.Func: diff --git a/objc/type_convertion.m b/objc/type_convertion.m index 21f7ed79..2f3cd8b8 100644 --- a/objc/type_convertion.m +++ b/objc/type_convertion.m @@ -65,6 +65,7 @@ dict to_c_items(void* ptr) { dict c_dict; NSArray * keys = [result_ allKeys]; int size = [keys count]; + c_dict.len = size; if (size > 0) { void** key_data = malloc(size * sizeof(void*)); void** value_data = malloc(size * sizeof(void*)); @@ -76,7 +77,6 @@ dict to_c_items(void* ptr) { } c_dict.key_data = key_data; c_dict.value_data = value_data; - c_dict.len = size; } return c_dict; }