Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with map type #242

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions macos/foundation/foundation_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package foundation

import (
"fmt"
"reflect"
"strings"
"testing"

"github.com/progrium/macdriver/objc"
)

func TestFoundationValid(t *testing.T) {}
Expand Down Expand Up @@ -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()
}
})
}
2 changes: 1 addition & 1 deletion objc/type_convertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion objc/type_convertion.m
Original file line number Diff line number Diff line change
Expand Up @@ -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*));
Expand All @@ -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;
}
Expand Down
Loading