From 4e5a2efd498f22ae0efd7d68313990d42fda0826 Mon Sep 17 00:00:00 2001 From: [email protected] Date: Sat, 13 Jan 2024 23:06:53 -0500 Subject: [PATCH] NSError tests --- macos/foundation/foundation_test.go | 81 +++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/macos/foundation/foundation_test.go b/macos/foundation/foundation_test.go index e5e8688..140988d 100644 --- a/macos/foundation/foundation_test.go +++ b/macos/foundation/foundation_test.go @@ -1,12 +1,16 @@ package foundation import ( + "fmt" "reflect" + "strings" "testing" + "github.com/progrium/macdriver/objc" ) func TestFoundationValid(t *testing.T) {} +/* func TestFoundationDictionary(t *testing.T) { s := String_StringWithString("bar") d1 := Dictionary_DictionaryWithObjectsAndKeys(s, "foo", "value", "key", nil) @@ -20,6 +24,7 @@ func TestFoundationDictionary(t *testing.T) { t.Fatal("unexpected final map from dictionary") } } + func TestFoundationArray(t *testing.T) { s := String_StringWithString("one") @@ -35,3 +40,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.36.1