Skip to content

Commit

Permalink
Add unit test for GDObject handling notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
gilzoide committed Oct 12, 2023
1 parent 31e0b58 commit 35b05e6
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions test/unit_tests/GDObject.gd
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
extends RefCounted

static var ObjectiveC = Engine.get_singleton("ObjectiveC")
const NOTIFICATION_NAME = "my cool notification"
const NOTIFICATION_USER_INFO = { hello = "world" }

var my_property = 5
var notification_called = false


func test_property() -> bool:
var objcObject = ObjectiveC.wrap_object(self)
assert(objcObject.my_property == my_property)
assert(objcObject.invoke("my_property") == my_property)
var self_objc = ObjectiveC.wrap_object(self)
assert(self_objc.my_property == my_property)
assert(self_objc.invoke("my_property") == my_property)
return true


func test_method() -> bool:
var objcObject = ObjectiveC.wrap_object(self)
assert(objcObject.invoke("intToBool:", 1) == true)
assert(objcObject.invoke("intToBool:", 0) == false)
var self_objc = ObjectiveC.wrap_object(self)
assert(self_objc.invoke("intToBool:", 1) == true)
assert(self_objc.invoke("intToBool:", 0) == false)
return true


func test_notification() -> bool:
var self_objc = ObjectiveC.wrap_object(self)
var NSNotificationCenter = ObjectiveC.NSNotificationCenter.defaultCenter
NSNotificationCenter.invoke("addObserver:selector:name:object:", self_objc, "handleNotification:", NOTIFICATION_NAME, null)
NSNotificationCenter.invoke("postNotificationName:object:userInfo:", NOTIFICATION_NAME, null, NOTIFICATION_USER_INFO)
NSNotificationCenter.invoke("removeObserver:", self_objc)
assert(notification_called, "Notification was not called")
return true


func intToBool(value: int) -> bool:
return bool(value)


func handleNotification(notification):
assert(notification.name == NOTIFICATION_NAME)
assert(notification.userInfo.to_dictionary() == NOTIFICATION_USER_INFO)
notification_called = true


func methodSignatureForSelector(sel: String) -> String:
match sel:
"my_property":
return "i@:"
"intToBool:":
return "B@:i"
"handleNotification:":
return "v@:@"
_:
return ""

0 comments on commit 35b05e6

Please sign in to comment.