Skip to content

Commit

Permalink
Add property tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dboris committed Oct 4, 2024
1 parent e828bbb commit 9ace407
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions runtime/runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,10 @@ module Property = struct
open Object
open Define

(** Get the value of a property. *)
let get ~typ = get_property ~typ: (Objc_t.value_typ typ)

(** Set the value of a property. *)
let set ~typ = set_property ~typ: (Objc_t.value_typ typ)

(** Getter for non-object values. *)
Expand Down Expand Up @@ -441,15 +443,15 @@ module Property = struct
in
method_spec ~cmd ~typ: (typ @-> returning void) ~imp ~enc

(** Definition of a property getter and (optionally) setter. *)
(** Definition of a property getter and (by default) setter. *)
let define :
type a.
?assign:bool ->
?copy:bool ->
?readonly:bool ->
string ->
a Objc_t.t ->
Define.method_spec' list
method_spec' list
= fun
?(assign = false)
?(copy = false)
Expand Down
29 changes: 29 additions & 0 deletions test/test-camlkit-base/test_objc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,33 @@ let test_value_accessors ~name x () =
~typ: (returning int)
in
A.check A.int "set value and get same value" x v
;;

let test_value_property ~name x () =
let ivars = [Ivar.define "myVar" Objc_t.int]
and methods = Property.define "myVar" Objc_t.int
in
let self = _new_ (Class.define name ~ivars ~methods) in
self |> Property.set ~typ: Objc_t.int "myVar" x;
let v =
self |> Property.get ~typ: Objc_t.int "myVar"
in
A.check A.int "set property value and get same value" x v
;;

let test_object_property ~name x () =
let ivars = [Ivar.define "myVar" Objc_t.id]
and methods = Property.define "myVar" Objc_t.id
in
let self = _new_ (Class.define name ~ivars ~methods) in
self |> Property.set ~typ: Objc_t.id "myVar" x;
let v =
self |> Property.get ~typ: Objc_t.id "myVar"
in
A.check A.string "set property value and get same value"
(NSString._UTF8String x)
(NSString._UTF8String v)
;;

let test_object_accessors ~name x () =
let ivars = [Ivar.define "myVar" Objc_t.id]
Expand Down Expand Up @@ -290,6 +317,8 @@ let suite =
; "value accessors", `Quick, test_value_accessors ~name:"MyClass7" 12
; "object accessors", `Quick, test_object_accessors ~name:"MyClass8" (new_string "Hello")
; "set and get ivar via kvc", `Quick, test_kvc ~class_name:"MyClass9" "Test"
; "value property", `Quick, test_value_property ~name:"MyClass10" 42
; "object property", `Quick, test_object_property ~name:"MyClass11" (new_string "Hello")
; "get selector name as string", `Quick, test_string_of_selector
; "test block", `Quick, test_block
; "test msg_send_super", `Quick, test_msg_send_super
Expand Down

0 comments on commit 9ace407

Please sign in to comment.