gir_ffi 0.15.2 → 0.15.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog.md +41 -0
  3. data/README.md +5 -6
  4. data/TODO.md +14 -35
  5. data/docs/Documentation.md +6 -0
  6. data/docs/Subclassing.md +38 -0
  7. data/lib/ffi-glib.rb +8 -3
  8. data/lib/ffi-glib/byte_array.rb +2 -1
  9. data/lib/ffi-glib/container_class_methods.rb +1 -3
  10. data/lib/ffi-glib/destroy_notify.rb +2 -2
  11. data/lib/ffi-glib/hash_table.rb +1 -2
  12. data/lib/ffi-glib/list.rb +11 -5
  13. data/lib/ffi-glib/list_methods.rb +12 -0
  14. data/lib/ffi-glib/s_list.rb +10 -4
  15. data/lib/ffi-gobject.rb +2 -5
  16. data/lib/ffi-gobject/object.rb +102 -92
  17. data/lib/ffi-gobject/object_class.rb +1 -1
  18. data/lib/ffi-gobject/ruby_closure.rb +1 -1
  19. data/lib/ffi-gobject/value.rb +108 -117
  20. data/lib/ffi-gobject_introspection/i_base_info.rb +35 -27
  21. data/lib/gir_ffi-base/gobject/lib.rb +4 -0
  22. data/lib/gir_ffi/array_element_convertor.rb +3 -4
  23. data/lib/gir_ffi/boxed_base.rb +13 -11
  24. data/lib/gir_ffi/builder.rb +3 -4
  25. data/lib/gir_ffi/builder_helper.rb +10 -2
  26. data/lib/gir_ffi/builders/base_argument_builder.rb +2 -2
  27. data/lib/gir_ffi/builders/base_method_builder.rb +1 -1
  28. data/lib/gir_ffi/builders/base_type_builder.rb +1 -1
  29. data/lib/gir_ffi/builders/callback_argument_builder.rb +2 -0
  30. data/lib/gir_ffi/builders/callback_builder.rb +25 -11
  31. data/lib/gir_ffi/builders/callback_return_value_builder.rb +4 -6
  32. data/lib/gir_ffi/builders/enum_builder.rb +9 -5
  33. data/lib/gir_ffi/builders/field_builder.rb +2 -1
  34. data/lib/gir_ffi/builders/function_builder.rb +1 -1
  35. data/lib/gir_ffi/builders/interface_builder.rb +3 -1
  36. data/lib/gir_ffi/builders/mapping_method_builder.rb +1 -1
  37. data/lib/gir_ffi/builders/marshalling_method_builder.rb +5 -2
  38. data/lib/gir_ffi/builders/method_template.rb +4 -4
  39. data/lib/gir_ffi/builders/module_builder.rb +4 -2
  40. data/lib/gir_ffi/builders/null_argument_builder.rb +0 -2
  41. data/lib/gir_ffi/builders/null_return_value_builder.rb +0 -2
  42. data/lib/gir_ffi/builders/object_builder.rb +18 -6
  43. data/lib/gir_ffi/builders/property_builder.rb +1 -2
  44. data/lib/gir_ffi/builders/signal_closure_builder.rb +3 -5
  45. data/lib/gir_ffi/builders/struct_builder.rb +34 -7
  46. data/lib/gir_ffi/builders/user_defined_builder.rb +8 -1
  47. data/lib/gir_ffi/builders/vfunc_argument_builder.rb +1 -1
  48. data/lib/gir_ffi/callback_base.rb +7 -8
  49. data/lib/gir_ffi/class_base.rb +1 -0
  50. data/lib/gir_ffi/core.rb +17 -2
  51. data/lib/gir_ffi/glib_error.rb +1 -2
  52. data/lib/gir_ffi/in_pointer.rb +10 -2
  53. data/lib/gir_ffi/interface_base.rb +5 -0
  54. data/lib/gir_ffi/object_base.rb +27 -0
  55. data/lib/gir_ffi/return_value_info.rb +1 -2
  56. data/lib/gir_ffi/struct_base.rb +1 -9
  57. data/lib/gir_ffi/struct_like_base.rb +9 -5
  58. data/lib/gir_ffi/type_map.rb +1 -0
  59. data/lib/gir_ffi/unintrospectable_type_info.rb +1 -1
  60. data/lib/gir_ffi/union_base.rb +1 -9
  61. data/lib/gir_ffi/user_defined_object_info.rb +3 -2
  62. data/lib/gir_ffi/version.rb +1 -1
  63. data/lib/gir_ffi/vfunc_implementation.rb +1 -0
  64. metadata +106 -143
  65. data/Gemfile +0 -14
  66. data/Rakefile +0 -9
  67. data/tasks/test.rake +0 -204
  68. data/test/base_test_helper.rb +0 -70
  69. data/test/ffi-glib/array_test.rb +0 -203
  70. data/test/ffi-glib/byte_array_test.rb +0 -28
  71. data/test/ffi-glib/bytes_test.rb +0 -61
  72. data/test/ffi-glib/closure_test.rb +0 -38
  73. data/test/ffi-glib/destroy_notify_test.rb +0 -18
  74. data/test/ffi-glib/hash_table_test.rb +0 -68
  75. data/test/ffi-glib/list_test.rb +0 -86
  76. data/test/ffi-glib/main_loop_test.rb +0 -53
  77. data/test/ffi-glib/ptr_array_test.rb +0 -112
  78. data/test/ffi-glib/ruby_closure_test.rb +0 -62
  79. data/test/ffi-glib/s_list_test.rb +0 -76
  80. data/test/ffi-glib/strv_test.rb +0 -60
  81. data/test/ffi-glib/variant_test.rb +0 -12
  82. data/test/ffi-gobject/gobject_test.rb +0 -76
  83. data/test/ffi-gobject/object_class_test.rb +0 -31
  84. data/test/ffi-gobject/object_test.rb +0 -122
  85. data/test/ffi-gobject/param_spec_test.rb +0 -32
  86. data/test/ffi-gobject/value_test.rb +0 -390
  87. data/test/ffi-gobject_introspection/gobject_type_init_test.rb +0 -26
  88. data/test/ffi-gobject_introspection/i_base_info_test.rb +0 -52
  89. data/test/ffi-gobject_introspection/i_constant_info_test.rb +0 -29
  90. data/test/ffi-gobject_introspection/i_enum_info_test.rb +0 -18
  91. data/test/ffi-gobject_introspection/i_function_info_test.rb +0 -6
  92. data/test/ffi-gobject_introspection/i_interface_info_test.rb +0 -23
  93. data/test/ffi-gobject_introspection/i_object_info_test.rb +0 -49
  94. data/test/ffi-gobject_introspection/i_property_info_test.rb +0 -47
  95. data/test/ffi-gobject_introspection/i_registered_type_info_test.rb +0 -27
  96. data/test/ffi-gobject_introspection/i_repository_test.rb +0 -81
  97. data/test/ffi-gobject_introspection/i_struct_info_test.rb +0 -23
  98. data/test/ffi-gobject_introspection/i_type_info_test.rb +0 -30
  99. data/test/ffi-gobject_introspection/i_union_info_test.rb +0 -17
  100. data/test/ffi-gobject_introspection/i_vfunc_info_test.rb +0 -41
  101. data/test/ffi-gobject_introspection/lib_test.rb +0 -13
  102. data/test/ffi-gobject_introspection/strv_test.rb +0 -46
  103. data/test/ffi-gobject_test.rb +0 -151
  104. data/test/gir_ffi/allocation_helper_test.rb +0 -36
  105. data/test/gir_ffi/arg_helper_test.rb +0 -139
  106. data/test/gir_ffi/boolean_test.rb +0 -43
  107. data/test/gir_ffi/boxed_base_test.rb +0 -65
  108. data/test/gir_ffi/builder_test.rb +0 -322
  109. data/test/gir_ffi/builders/argument_builder_test.rb +0 -651
  110. data/test/gir_ffi/builders/base_argument_builder_test.rb +0 -6
  111. data/test/gir_ffi/builders/callback_argument_builder_test.rb +0 -126
  112. data/test/gir_ffi/builders/callback_builder_test.rb +0 -117
  113. data/test/gir_ffi/builders/callback_return_value_builder_test.rb +0 -81
  114. data/test/gir_ffi/builders/constant_builder_test.rb +0 -6
  115. data/test/gir_ffi/builders/constructor_builder_test.rb +0 -37
  116. data/test/gir_ffi/builders/enum_builder_test.rb +0 -23
  117. data/test/gir_ffi/builders/field_builder_test.rb +0 -134
  118. data/test/gir_ffi/builders/function_builder_test.rb +0 -550
  119. data/test/gir_ffi/builders/initializer_builder_test.rb +0 -54
  120. data/test/gir_ffi/builders/interface_builder_test.rb +0 -32
  121. data/test/gir_ffi/builders/module_builder_test.rb +0 -43
  122. data/test/gir_ffi/builders/object_builder_test.rb +0 -98
  123. data/test/gir_ffi/builders/property_builder_test.rb +0 -158
  124. data/test/gir_ffi/builders/registered_type_builder_test.rb +0 -54
  125. data/test/gir_ffi/builders/return_value_builder_test.rb +0 -433
  126. data/test/gir_ffi/builders/signal_closure_builder_test.rb +0 -197
  127. data/test/gir_ffi/builders/struct_builder_test.rb +0 -93
  128. data/test/gir_ffi/builders/unintrospectable_boxed_builder_test.rb +0 -29
  129. data/test/gir_ffi/builders/unintrospectable_builder_test.rb +0 -84
  130. data/test/gir_ffi/builders/union_builder_test.rb +0 -28
  131. data/test/gir_ffi/builders/user_defined_builder_test.rb +0 -524
  132. data/test/gir_ffi/builders/vfunc_argument_builder_test.rb +0 -101
  133. data/test/gir_ffi/builders/vfunc_builder_test.rb +0 -241
  134. data/test/gir_ffi/callback_base_test.rb +0 -22
  135. data/test/gir_ffi/class_base_test.rb +0 -166
  136. data/test/gir_ffi/core_test.rb +0 -98
  137. data/test/gir_ffi/enum_base_test.rb +0 -43
  138. data/test/gir_ffi/error_type_info_test.rb +0 -50
  139. data/test/gir_ffi/ffi_ext/pointer_test.rb +0 -20
  140. data/test/gir_ffi/g_type_test.rb +0 -31
  141. data/test/gir_ffi/in_pointer_test.rb +0 -203
  142. data/test/gir_ffi/info_ext/i_callable_info_test.rb +0 -26
  143. data/test/gir_ffi/info_ext/i_callback_info_test.rb +0 -21
  144. data/test/gir_ffi/info_ext/i_field_info_test.rb +0 -37
  145. data/test/gir_ffi/info_ext/i_function_info_test.rb +0 -74
  146. data/test/gir_ffi/info_ext/i_signal_info_test.rb +0 -32
  147. data/test/gir_ffi/info_ext/i_type_info_test.rb +0 -711
  148. data/test/gir_ffi/info_ext/i_unresolved_info_test.rb +0 -19
  149. data/test/gir_ffi/info_ext/safe_constant_name_test.rb +0 -26
  150. data/test/gir_ffi/info_ext/safe_function_name_test.rb +0 -26
  151. data/test/gir_ffi/interface_base_test.rb +0 -20
  152. data/test/gir_ffi/method_stubber_test.rb +0 -65
  153. data/test/gir_ffi/object_base_test.rb +0 -64
  154. data/test/gir_ffi/object_store_test.rb +0 -41
  155. data/test/gir_ffi/receiver_argument_info_test.rb +0 -33
  156. data/test/gir_ffi/sized_array_test.rb +0 -206
  157. data/test/gir_ffi/struct_base_test.rb +0 -23
  158. data/test/gir_ffi/struct_like_base_test.rb +0 -167
  159. data/test/gir_ffi/type_map_test.rb +0 -17
  160. data/test/gir_ffi/unintrospectable_type_info_test.rb +0 -121
  161. data/test/gir_ffi/union_base_test.rb +0 -23
  162. data/test/gir_ffi/user_defined_object_info_test.rb +0 -119
  163. data/test/gir_ffi/user_defined_property_info_test.rb +0 -39
  164. data/test/gir_ffi/variable_name_generator_test.rb +0 -18
  165. data/test/gir_ffi/version_test.rb +0 -9
  166. data/test/gir_ffi/zero_terminated_test.rb +0 -108
  167. data/test/gir_ffi_test_helper.rb +0 -84
  168. data/test/integration/callback_exceptions_test.rb +0 -59
  169. data/test/integration/derived_classes_test.rb +0 -53
  170. data/test/integration/generated_everything_test.rb +0 -452
  171. data/test/integration/generated_gimarshallingtests_test.rb +0 -3096
  172. data/test/integration/generated_gio_test.rb +0 -116
  173. data/test/integration/generated_glib_test.rb +0 -10
  174. data/test/integration/generated_gobject_test.rb +0 -124
  175. data/test/integration/generated_gst_test.rb +0 -38
  176. data/test/integration/generated_gtk_source_test.rb +0 -32
  177. data/test/integration/generated_gtop_test.rb +0 -22
  178. data/test/integration/generated_pango_ft2_test.rb +0 -17
  179. data/test/integration/generated_pango_test.rb +0 -24
  180. data/test/integration/generated_regress_test.rb +0 -4062
  181. data/test/integration/generated_secret_test.rb +0 -14
  182. data/test/integration/generated_utility_test.rb +0 -174
  183. data/test/integration/generated_warnlib_test.rb +0 -43
  184. data/test/integration/method_lookup_test.rb +0 -34
  185. data/test/introspection_test_helper.rb +0 -93
  186. data/test/lib/Makefile.am +0 -110
  187. data/test/lib/autogen.sh +0 -4
  188. data/test/lib/configure.ac +0 -34
  189. data/test/lib/m4/jhflags.m4 +0 -21
  190. data/test/minitest/stats_plugin.rb +0 -30
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "gir_ffi_test_helper"
4
-
5
- describe GirFFI::InfoExt::IUnresolvedInfo do
6
- let(:info_class) do
7
- Class.new do
8
- include GirFFI::InfoExt::IUnresolvedInfo
9
- end
10
- end
11
-
12
- let(:unresolved_info) { info_class.new }
13
-
14
- describe "#to_ffi_type" do
15
- it "returns the most generic type" do
16
- _(unresolved_info.to_ffi_type).must_equal :pointer
17
- end
18
- end
19
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "gir_ffi_test_helper"
4
-
5
- describe GirFFI::InfoExt::SafeConstantName do
6
- let(:info_class) do
7
- Class.new do
8
- include GirFFI::InfoExt::SafeConstantName
9
- end
10
- end
11
- let(:info) { info_class.new }
12
-
13
- describe "#safe_name" do
14
- it "makes names starting with an underscore safe" do
15
- expect(info).to receive(:name).and_return "_foo"
16
-
17
- assert_equal "Private___foo", info.safe_name
18
- end
19
-
20
- it "makes names with dashes safe" do
21
- expect(info).to receive(:name).and_return "this-could-be-a-signal-name"
22
-
23
- _(info.safe_name).must_equal "This_could_be_a_signal_name"
24
- end
25
- end
26
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "gir_ffi_test_helper"
4
-
5
- describe GirFFI::InfoExt::SafeFunctionName do
6
- let(:info_class) do
7
- Class.new do
8
- include GirFFI::InfoExt::SafeFunctionName
9
- end
10
- end
11
- let(:info) { info_class.new }
12
-
13
- describe "#safe_name" do
14
- it "keeps lower case names lower case" do
15
- expect(info).to receive(:name).and_return "foo"
16
-
17
- assert_equal "foo", info.safe_name
18
- end
19
-
20
- it "returns a non-empty string if name is empty" do
21
- expect(info).to receive(:name).and_return ""
22
-
23
- assert_equal "_", info.safe_name
24
- end
25
- end
26
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "gir_ffi_test_helper"
4
-
5
- describe GirFFI::InterfaceBase do
6
- let(:interface) { Module.new { extend GirFFI::InterfaceBase } }
7
-
8
- describe "#wrap" do
9
- it "delegates conversion to the wrapped pointer" do
10
- expect(ptr = Object.new).to receive(:to_object).and_return "good-result"
11
- _(interface.wrap(ptr)).must_equal "good-result"
12
- end
13
- end
14
-
15
- describe ".to_ffi_type" do
16
- it "returns :pointer" do
17
- _(interface.to_ffi_type).must_equal :pointer
18
- end
19
- end
20
- end
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "gir_ffi_test_helper"
4
-
5
- describe GirFFI::MethodStubber do
6
- describe "#method_stub" do
7
- let(:stubber) { GirFFI::MethodStubber.new(method_info) }
8
- let(:result) { stubber.method_stub }
9
-
10
- describe "for a regular method" do
11
- let(:method_info) do
12
- get_method_introspection_data("Regress", "TestObj", "instance_method")
13
- end
14
-
15
- it "creates a method stub" do
16
- _(result).must_equal <<~STUB
17
- def instance_method *args, &block
18
- setup_and_call "instance_method", args, &block
19
- end
20
- STUB
21
- end
22
- end
23
-
24
- describe "for a static method" do
25
- let(:method_info) do
26
- get_method_introspection_data("Regress", "TestObj", "static_method")
27
- end
28
-
29
- it "creates a class method stub" do
30
- _(result).must_equal <<~STUB
31
- def self.static_method *args, &block
32
- setup_and_call "static_method", args, &block
33
- end
34
- STUB
35
- end
36
- end
37
-
38
- describe "for a module function" do
39
- let(:method_info) do
40
- get_introspection_data("Regress", "test_int")
41
- end
42
-
43
- it "creates a module method stub" do
44
- _(result).must_equal <<~STUB
45
- def self.test_int *args, &block
46
- setup_and_call "test_int", args, &block
47
- end
48
- STUB
49
- end
50
- end
51
-
52
- describe "for a method with an empty name" do
53
- let(:method_info) { get_method_introspection_data("Regress", "TestObj", "instance_method") }
54
-
55
- it "creates a method stub with a safe name that sets up the unsafe method" do
56
- allow(method_info).to receive(:name).and_return ""
57
- _(result).must_equal <<~STUB
58
- def _ *args, &block
59
- setup_and_call "", args, &block
60
- end
61
- STUB
62
- end
63
- end
64
- end
65
- end
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "gir_ffi_test_helper"
4
-
5
- GirFFI.setup :Regress
6
-
7
- describe GirFFI::ObjectBase do
8
- let(:derived_class) { Class.new GirFFI::ObjectBase }
9
-
10
- describe ".wrap" do
11
- it "delegates conversion to the wrapped pointer" do
12
- expect(ptr = Object.new).to receive(:to_object).and_return "good-result"
13
- _(derived_class.wrap(ptr)).must_equal "good-result"
14
- end
15
- end
16
-
17
- describe ".to_ffi_type" do
18
- it "returns itself" do
19
- _(derived_class.to_ffi_type).must_equal derived_class
20
- end
21
- end
22
-
23
- describe ".object_class" do
24
- it "returns an object of the class struct type" do
25
- _(Regress::TestObj.object_class).must_be_instance_of Regress::TestObjClass
26
- end
27
-
28
- it "caches its result" do
29
- first = Regress::TestObj.object_class
30
- second = Regress::TestObj.object_class
31
- _(second).must_be :eql?, first
32
- end
33
- end
34
-
35
- describe "#included_interfaces" do
36
- let(:base_class) { GIMarshallingTests::Object }
37
- let(:derived_class) { Class.new(base_class) }
38
-
39
- before do
40
- derived_class.class_eval { include GIMarshallingTests::Interface }
41
- end
42
-
43
- it "finds the included interface" do
44
- _(derived_class.included_interfaces).must_equal [GIMarshallingTests::Interface]
45
- end
46
- end
47
-
48
- describe "#registered_ancestors" do
49
- let(:base_class) { GIMarshallingTests::Object }
50
- let(:derived_class) { Class.new(base_class) }
51
-
52
- before do
53
- derived_class.class_eval { include GIMarshallingTests::Interface }
54
- end
55
-
56
- it "finds the ancestor classes and included interface" do
57
- _(derived_class.registered_ancestors)
58
- .must_equal [derived_class,
59
- GIMarshallingTests::Interface,
60
- GIMarshallingTests::Object,
61
- GObject::Object]
62
- end
63
- end
64
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "gir_ffi_test_helper"
4
-
5
- describe GirFFI::ObjectStore do
6
- let(:store) { GirFFI::ObjectStore.new }
7
-
8
- describe "#store" do
9
- it "returns a non-null pointer when storing objects" do
10
- obj = Object.new
11
- ptr = store.store obj
12
- _(ptr).wont_be :null?
13
- end
14
-
15
- it "returns a null pointer when storing nil" do
16
- ptr = store.store nil
17
- _(ptr).must_be :null?
18
- end
19
- end
20
-
21
- describe "#fetch" do
22
- it "returns the stored object when passed the key pointer" do
23
- obj = Object.new
24
- ptr = store.store obj
25
- result = store.fetch ptr
26
- _(result).must_equal obj
27
- end
28
-
29
- it "returns the nil object when passed a null pointer" do
30
- ptr = FFI::Pointer.new(0)
31
- result = store.fetch ptr
32
- _(result).must_be_nil
33
- end
34
-
35
- it "returns the pointer itself when passed an unknown pointer" do
36
- ptr = FFI::Pointer.new(42)
37
- result = store.fetch ptr
38
- _(result).must_equal ptr
39
- end
40
- end
41
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "gir_ffi_test_helper"
4
- require "gir_ffi/receiver_argument_info"
5
-
6
- describe GirFFI::ReceiverArgumentInfo do
7
- let(:dummy_type) { "foo" }
8
- let(:instance) { GirFFI::ReceiverArgumentInfo.new dummy_type }
9
-
10
- describe "#argument_type" do
11
- it "returns the argument type" do
12
- _(instance.argument_type).must_equal dummy_type
13
- end
14
- end
15
-
16
- describe "#direction" do
17
- it "returns the correct value" do
18
- _(instance.direction).must_equal :in
19
- end
20
- end
21
-
22
- describe "#ownership_transfer" do
23
- it "returns the correct value" do
24
- _(instance.ownership_transfer).must_equal :everything
25
- end
26
- end
27
-
28
- describe "#name" do
29
- it "returns the correct value" do
30
- _(instance.name).must_equal "_instance"
31
- end
32
- end
33
- end
@@ -1,206 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "gir_ffi_test_helper"
4
-
5
- describe GirFFI::SizedArray do
6
- describe "::wrap" do
7
- it "takes a type, size and pointer and returns a GirFFI::SizedArray wrapping them" do
8
- expect(ptr = Object.new).to receive(:null?).and_return false
9
- sarr = GirFFI::SizedArray.wrap :gint32, 3, ptr
10
- assert_instance_of GirFFI::SizedArray, sarr
11
- assert_equal ptr, sarr.to_ptr
12
- assert_equal 3, sarr.size
13
- assert_equal :gint32, sarr.element_type
14
- end
15
-
16
- it "returns nil if the wrapped pointer is null" do
17
- expect(ptr = Object.new).to receive(:null?).and_return true
18
- sarr = GirFFI::SizedArray.wrap :gint32, 3, ptr
19
- _(sarr).must_be_nil
20
- end
21
- end
22
-
23
- describe "#each" do
24
- it "yields each element" do
25
- ary = %w(one two three)
26
- ptrs = ary.map { |a| FFI::MemoryPointer.from_string(a) }
27
- ptrs << nil
28
- block = FFI::MemoryPointer.new(:pointer, ptrs.length)
29
- block.write_array_of_pointer ptrs
30
-
31
- sarr = GirFFI::SizedArray.new :utf8, 3, block
32
- arr = []
33
- sarr.each do |str|
34
- arr << str
35
- end
36
- assert_equal %w(one two three), arr
37
- end
38
- end
39
-
40
- describe "::from" do
41
- describe "from a Ruby array" do
42
- it "creates a GirFFI::SizedArray with the same elements" do
43
- arr = GirFFI::SizedArray.from :gint32, 3, [3, 2, 1]
44
- _(arr).must_be_instance_of GirFFI::SizedArray
45
- assert_equal [3, 2, 1], arr.to_a
46
- end
47
-
48
- it "raises an error if the array has the wrong number of elements" do
49
- _(proc { GirFFI::SizedArray.from :gint32, 4, [3, 2, 1] }).must_raise ArgumentError
50
- end
51
-
52
- it "uses the array's size if passed -1 as the size" do
53
- arr = GirFFI::SizedArray.from :gint32, -1, [3, 2, 1]
54
- _(arr.size).must_equal 3
55
- end
56
- end
57
-
58
- describe "from a GirFFI::SizedArray" do
59
- it "return its argument" do
60
- arr = GirFFI::SizedArray.from :gint32, 3, [3, 2, 1]
61
- arr2 = GirFFI::SizedArray.from :gint32, 3, arr
62
- assert_equal arr, arr2
63
- end
64
-
65
- it "raises an error if the argument has the wrong number of elements" do
66
- arr = GirFFI::SizedArray.from :gint32, 3, [3, 2, 1]
67
- _(proc { GirFFI::SizedArray.from :gint32, 4, arr }).must_raise ArgumentError
68
- end
69
- end
70
-
71
- it "returns nil when passed nil" do
72
- arr = GirFFI::SizedArray.from :gint32, 0, nil
73
- _(arr).must_be_nil
74
- end
75
-
76
- it "wraps its argument if given a pointer" do
77
- arr = GirFFI::SizedArray.from :gint32, 3, [3, 2, 1]
78
- arr2 = GirFFI::SizedArray.from :gint32, 3, arr.to_ptr
79
- assert_instance_of GirFFI::SizedArray, arr2
80
- refute arr2.equal? arr
81
- _(arr2.to_ptr).must_equal arr.to_ptr
82
- end
83
- end
84
-
85
- describe ".copy_from" do
86
- describe "from a Ruby array" do
87
- it "creates an unowned GirFFI::SizedArray with the same elements" do
88
- arr = GirFFI::SizedArray.copy_from :gint32, 3, [3, 2, 1]
89
- _(arr).must_be_instance_of GirFFI::SizedArray
90
- assert_equal [3, 2, 1], arr.to_a
91
- _(arr.to_ptr).wont_be :autorelease?
92
- end
93
-
94
- it "creates unowned copies of struct pointer elements" do
95
- struct = GIMarshallingTests::BoxedStruct.new
96
- struct.long_ = 2342
97
- _(struct.struct).must_be :owned?
98
-
99
- arr = GirFFI::SizedArray.copy_from([:pointer, GIMarshallingTests::BoxedStruct],
100
- 1,
101
- [struct])
102
- _(arr).must_be_instance_of GirFFI::SizedArray
103
- _(arr.to_ptr).wont_be :autorelease?
104
-
105
- struct_copy = arr.first
106
- _(struct_copy.long_).must_equal struct.long_
107
- _(struct_copy.to_ptr).wont_be :==, struct.to_ptr
108
- _(struct_copy.struct).wont_be :owned?
109
- end
110
-
111
- it "increases the ref count of object pointer elements" do
112
- obj = GIMarshallingTests::Object.new 42
113
- arr = GirFFI::SizedArray.copy_from([:pointer, GIMarshallingTests::Object],
114
- -1,
115
- [obj, nil])
116
- _(arr).must_be_instance_of GirFFI::SizedArray
117
- _(arr.to_ptr).wont_be :autorelease?
118
-
119
- _(arr.to_a).must_equal [obj, nil]
120
- _(object_ref_count(obj)).must_equal 2
121
- end
122
- end
123
-
124
- describe "from a GirFFI::SizedArray" do
125
- it "return an unowned copy of its argument" do
126
- arr = GirFFI::SizedArray.from :gint32, 3, [3, 2, 1]
127
- arr2 = GirFFI::SizedArray.copy_from :gint32, 3, arr
128
- _(arr.to_ptr).wont_be :==, arr2.to_ptr
129
- _(arr2.to_a).must_equal [3, 2, 1]
130
- _(arr2.to_ptr).wont_be :autorelease?
131
- end
132
- end
133
-
134
- it "returns nil when passed nil" do
135
- arr = GirFFI::SizedArray.copy_from :gint32, 0, nil
136
- _(arr).must_be_nil
137
- end
138
- end
139
-
140
- describe "#==" do
141
- it "returns true when comparing to an array with the same elements" do
142
- sized = GirFFI::SizedArray.from :int32, 3, [1, 2, 3]
143
-
144
- _(sized).must_be :==, [1, 2, 3]
145
- end
146
-
147
- it "returns false when comparing to an array with different elements" do
148
- sized = GirFFI::SizedArray.from :int32, 3, [1, 2, 3]
149
-
150
- _(sized).wont_be :==, [1, 2]
151
- end
152
-
153
- it "returns true when comparing to a sized array with the same elements" do
154
- sized = GirFFI::SizedArray.from :int32, 3, [1, 2, 3]
155
- other = GirFFI::SizedArray.from :int32, 3, [1, 2, 3]
156
-
157
- _(sized).must_be :==, other
158
- end
159
-
160
- it "returns false when comparing to a sized array with different elements" do
161
- sized = GirFFI::SizedArray.from :int32, 3, [1, 2, 3]
162
- other = GirFFI::SizedArray.from :int32, 2, [1, 2]
163
-
164
- _(sized).wont_be :==, other
165
- end
166
- end
167
-
168
- describe "#size_in_bytes" do
169
- it "returns the correct value" do
170
- sized = GirFFI::SizedArray.from :int32, 3, [1, 2, 3]
171
-
172
- _(sized.size_in_bytes).must_equal 12
173
- end
174
- end
175
-
176
- describe ".get_value_from_pointer" do
177
- it "returns just a pointer" do
178
- sized = GirFFI::SizedArray.from :int32, 3, [1, 2, 3]
179
- ptr = sized.to_ptr
180
- result = GirFFI::SizedArray.get_value_from_pointer(ptr, 0)
181
- _(result).must_be :==, ptr
182
- end
183
-
184
- it "offsets correctly" do
185
- sized = GirFFI::SizedArray.from :int32, 3, [1, 2, 3]
186
- ptr = sized.to_ptr
187
- next_ptr = GirFFI::SizedArray.get_value_from_pointer(ptr, 4)
188
- tail = GirFFI::SizedArray.from(:int32, 2, next_ptr)
189
- _(tail).must_be :==, [2, 3]
190
- end
191
- end
192
-
193
- describe ".copy_value_to_pointer" do
194
- it "copies data correctly" do
195
- sized = GirFFI::SizedArray.from :int32, 3, [1, 2, 3]
196
- target = FFI::MemoryPointer.new sized.size_in_bytes
197
- GirFFI::SizedArray.copy_value_to_pointer(sized, target)
198
- result = GirFFI::SizedArray.from :int32, 3, target
199
- _(result).must_be :==, [1, 2, 3]
200
- end
201
- end
202
-
203
- it "includes Enumerable" do
204
- _(GirFFI::SizedArray).must_include Enumerable
205
- end
206
- end