Skip to content

Commit

Permalink
Fix issues with map type (#242)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
programmingkidx authored Jan 17, 2024
1 parent e468a64 commit 4533128
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
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

0 comments on commit 4533128

Please sign in to comment.