bbrowning-virtualbox 0.7.6.dev

Sign up to get free protection for your applications and to get access to all the features.
Files changed (281) hide show
  1. data/.gitignore +8 -0
  2. data/.yardopts +3 -0
  3. data/Gemfile +16 -0
  4. data/LICENSE +19 -0
  5. data/Rakefile +33 -0
  6. data/Readme.md +70 -0
  7. data/docs/GettingStarted.md +196 -0
  8. data/docs/WhatsNew.md +12 -0
  9. data/features/README.md +33 -0
  10. data/features/global.feature +19 -0
  11. data/features/global_extra_data.feature +27 -0
  12. data/features/step_definitions/abstract_model_steps.rb +39 -0
  13. data/features/step_definitions/extra_data_steps.rb +36 -0
  14. data/features/step_definitions/global_steps.rb +29 -0
  15. data/features/step_definitions/nat_engine_steps.rb +76 -0
  16. data/features/step_definitions/network_adapter_steps.rb +38 -0
  17. data/features/step_definitions/shared_folder_steps.rb +76 -0
  18. data/features/step_definitions/snapshot_steps.rb +74 -0
  19. data/features/step_definitions/storage_controller_steps.rb +16 -0
  20. data/features/step_definitions/virtualbox_steps.rb +17 -0
  21. data/features/step_definitions/vm_steps.rb +50 -0
  22. data/features/support/env.rb +61 -0
  23. data/features/support/helpers.rb +38 -0
  24. data/features/support/hooks.rb +30 -0
  25. data/features/support/ordered_hash.rb +49 -0
  26. data/features/support/vboxmanage.rb +191 -0
  27. data/features/version.feature +16 -0
  28. data/features/vm.feature +13 -0
  29. data/features/vm_bios.feature +29 -0
  30. data/features/vm_cpu.feature +29 -0
  31. data/features/vm_extra_data.feature +35 -0
  32. data/features/vm_hw_virt.feature +29 -0
  33. data/features/vm_nat_engine.feature +57 -0
  34. data/features/vm_network_adapters.feature +27 -0
  35. data/features/vm_shared_folders.feature +42 -0
  36. data/features/vm_snapshots.feature +29 -0
  37. data/features/vm_storage_controllers.feature +11 -0
  38. data/lib/virtualbox.rb +11 -0
  39. data/lib/virtualbox/abstract_model.rb +281 -0
  40. data/lib/virtualbox/abstract_model/attributable.rb +290 -0
  41. data/lib/virtualbox/abstract_model/dirty.rb +177 -0
  42. data/lib/virtualbox/abstract_model/interface_attributes.rb +98 -0
  43. data/lib/virtualbox/abstract_model/relatable.rb +332 -0
  44. data/lib/virtualbox/abstract_model/validatable.rb +167 -0
  45. data/lib/virtualbox/abstract_model/version_matcher.rb +35 -0
  46. data/lib/virtualbox/appliance.rb +62 -0
  47. data/lib/virtualbox/audio_adapter.rb +52 -0
  48. data/lib/virtualbox/bios.rb +50 -0
  49. data/lib/virtualbox/com.rb +23 -0
  50. data/lib/virtualbox/com/abstract_enum.rb +43 -0
  51. data/lib/virtualbox/com/abstract_implementer.rb +45 -0
  52. data/lib/virtualbox/com/abstract_interface.rb +167 -0
  53. data/lib/virtualbox/com/base_interface.rb +38 -0
  54. data/lib/virtualbox/com/ffi/interface.rb +150 -0
  55. data/lib/virtualbox/com/ffi/interfaces.rb +54 -0
  56. data/lib/virtualbox/com/ffi/util.rb +119 -0
  57. data/lib/virtualbox/com/ffi/vboxxpcomc.rb +31 -0
  58. data/lib/virtualbox/com/ffi_interface.rb +96 -0
  59. data/lib/virtualbox/com/implementer/base.rb +59 -0
  60. data/lib/virtualbox/com/implementer/ffi.rb +361 -0
  61. data/lib/virtualbox/com/implementer/mscom.rb +175 -0
  62. data/lib/virtualbox/com/implementer/nil.rb +10 -0
  63. data/lib/virtualbox/com/interface/3.1.x/access_mode.rb +11 -0
  64. data/lib/virtualbox/com/interface/3.1.x/appliance.rb +22 -0
  65. data/lib/virtualbox/com/interface/3.1.x/audio_adapter.rb +15 -0
  66. data/lib/virtualbox/com/interface/3.1.x/audio_controller_type.rb +11 -0
  67. data/lib/virtualbox/com/interface/3.1.x/audio_driver_type.rb +11 -0
  68. data/lib/virtualbox/com/interface/3.1.x/bios_boot_menu_mode.rb +11 -0
  69. data/lib/virtualbox/com/interface/3.1.x/bios_settings.rb +21 -0
  70. data/lib/virtualbox/com/interface/3.1.x/clipboard_mode.rb +11 -0
  71. data/lib/virtualbox/com/interface/3.1.x/console.rb +50 -0
  72. data/lib/virtualbox/com/interface/3.1.x/cpu_property_type.rb +11 -0
  73. data/lib/virtualbox/com/interface/3.1.x/device_type.rb +11 -0
  74. data/lib/virtualbox/com/interface/3.1.x/dhcp_server.rb +22 -0
  75. data/lib/virtualbox/com/interface/3.1.x/firmware_type.rb +11 -0
  76. data/lib/virtualbox/com/interface/3.1.x/guest_os_type.rb +23 -0
  77. data/lib/virtualbox/com/interface/3.1.x/host.rb +42 -0
  78. data/lib/virtualbox/com/interface/3.1.x/host_network_interface.rb +30 -0
  79. data/lib/virtualbox/com/interface/3.1.x/host_network_interface_medium_type.rb +11 -0
  80. data/lib/virtualbox/com/interface/3.1.x/host_network_interface_status.rb +11 -0
  81. data/lib/virtualbox/com/interface/3.1.x/host_network_interface_type.rb +11 -0
  82. data/lib/virtualbox/com/interface/3.1.x/host_usb_device.rb +13 -0
  83. data/lib/virtualbox/com/interface/3.1.x/host_usb_device_filter.rb +13 -0
  84. data/lib/virtualbox/com/interface/3.1.x/hw_virt_ex_property_type.rb +11 -0
  85. data/lib/virtualbox/com/interface/3.1.x/machine.rb +105 -0
  86. data/lib/virtualbox/com/interface/3.1.x/machine_state.rb +14 -0
  87. data/lib/virtualbox/com/interface/3.1.x/medium.rb +50 -0
  88. data/lib/virtualbox/com/interface/3.1.x/medium_attachment.rb +18 -0
  89. data/lib/virtualbox/com/interface/3.1.x/medium_format.rb +18 -0
  90. data/lib/virtualbox/com/interface/3.1.x/medium_state.rb +11 -0
  91. data/lib/virtualbox/com/interface/3.1.x/medium_type.rb +11 -0
  92. data/lib/virtualbox/com/interface/3.1.x/medium_variant.rb +11 -0
  93. data/lib/virtualbox/com/interface/3.1.x/network_adapter.rb +30 -0
  94. data/lib/virtualbox/com/interface/3.1.x/network_adapter_type.rb +11 -0
  95. data/lib/virtualbox/com/interface/3.1.x/network_attachment_type.rb +11 -0
  96. data/lib/virtualbox/com/interface/3.1.x/nsiexception.rb +23 -0
  97. data/lib/virtualbox/com/interface/3.1.x/nsisupports.rb +15 -0
  98. data/lib/virtualbox/com/interface/3.1.x/parallel_port.rb +17 -0
  99. data/lib/virtualbox/com/interface/3.1.x/port_mode.rb +11 -0
  100. data/lib/virtualbox/com/interface/3.1.x/progress.rb +63 -0
  101. data/lib/virtualbox/com/interface/3.1.x/serial_port.rb +19 -0
  102. data/lib/virtualbox/com/interface/3.1.x/session.rb +18 -0
  103. data/lib/virtualbox/com/interface/3.1.x/session_state.rb +11 -0
  104. data/lib/virtualbox/com/interface/3.1.x/session_type.rb +11 -0
  105. data/lib/virtualbox/com/interface/3.1.x/shared_folder.rb +17 -0
  106. data/lib/virtualbox/com/interface/3.1.x/snapshot.rb +20 -0
  107. data/lib/virtualbox/com/interface/3.1.x/storage_bus.rb +11 -0
  108. data/lib/virtualbox/com/interface/3.1.x/storage_controller.rb +23 -0
  109. data/lib/virtualbox/com/interface/3.1.x/storage_controller_type.rb +11 -0
  110. data/lib/virtualbox/com/interface/3.1.x/system_properties.rb +37 -0
  111. data/lib/virtualbox/com/interface/3.1.x/usb_controller.rb +20 -0
  112. data/lib/virtualbox/com/interface/3.1.x/usb_device.rb +24 -0
  113. data/lib/virtualbox/com/interface/3.1.x/usb_device_filter.rb +23 -0
  114. data/lib/virtualbox/com/interface/3.1.x/usb_device_filter_action.rb +11 -0
  115. data/lib/virtualbox/com/interface/3.1.x/usb_device_state.rb +11 -0
  116. data/lib/virtualbox/com/interface/3.1.x/virtual_box_error_info.rb +17 -0
  117. data/lib/virtualbox/com/interface/3.1.x/virtual_system_description.rb +19 -0
  118. data/lib/virtualbox/com/interface/3.1.x/virtual_system_description_type.rb +14 -0
  119. data/lib/virtualbox/com/interface/3.1.x/virtual_system_description_value_type.rb +11 -0
  120. data/lib/virtualbox/com/interface/3.1.x/virtualbox.rb +67 -0
  121. data/lib/virtualbox/com/interface/3.1.x/vrdp_auth_type.rb +11 -0
  122. data/lib/virtualbox/com/interface/3.1.x/vrdp_server.rb +19 -0
  123. data/lib/virtualbox/com/interface/3.2.x/access_mode.rb +11 -0
  124. data/lib/virtualbox/com/interface/3.2.x/appliance.rb +22 -0
  125. data/lib/virtualbox/com/interface/3.2.x/audio_adapter.rb +15 -0
  126. data/lib/virtualbox/com/interface/3.2.x/audio_controller_type.rb +11 -0
  127. data/lib/virtualbox/com/interface/3.2.x/audio_driver_type.rb +11 -0
  128. data/lib/virtualbox/com/interface/3.2.x/bios_boot_menu_mode.rb +11 -0
  129. data/lib/virtualbox/com/interface/3.2.x/bios_settings.rb +21 -0
  130. data/lib/virtualbox/com/interface/3.2.x/clipboard_mode.rb +11 -0
  131. data/lib/virtualbox/com/interface/3.2.x/console.rb +50 -0
  132. data/lib/virtualbox/com/interface/3.2.x/cpu_property_type.rb +11 -0
  133. data/lib/virtualbox/com/interface/3.2.x/device_type.rb +11 -0
  134. data/lib/virtualbox/com/interface/3.2.x/dhcp_server.rb +22 -0
  135. data/lib/virtualbox/com/interface/3.2.x/firmware_type.rb +11 -0
  136. data/lib/virtualbox/com/interface/3.2.x/guest.rb +13 -0
  137. data/lib/virtualbox/com/interface/3.2.x/guest_os_type.rb +33 -0
  138. data/lib/virtualbox/com/interface/3.2.x/host.rb +43 -0
  139. data/lib/virtualbox/com/interface/3.2.x/host_network_interface.rb +30 -0
  140. data/lib/virtualbox/com/interface/3.2.x/host_network_interface_medium_type.rb +11 -0
  141. data/lib/virtualbox/com/interface/3.2.x/host_network_interface_status.rb +11 -0
  142. data/lib/virtualbox/com/interface/3.2.x/host_network_interface_type.rb +11 -0
  143. data/lib/virtualbox/com/interface/3.2.x/host_usb_device.rb +13 -0
  144. data/lib/virtualbox/com/interface/3.2.x/host_usb_device_filter.rb +13 -0
  145. data/lib/virtualbox/com/interface/3.2.x/hw_virt_ex_property_type.rb +11 -0
  146. data/lib/virtualbox/com/interface/3.2.x/keyboard_hid_type.rb +11 -0
  147. data/lib/virtualbox/com/interface/3.2.x/machine.rb +118 -0
  148. data/lib/virtualbox/com/interface/3.2.x/machine_state.rb +14 -0
  149. data/lib/virtualbox/com/interface/3.2.x/medium.rb +51 -0
  150. data/lib/virtualbox/com/interface/3.2.x/medium_attachment.rb +18 -0
  151. data/lib/virtualbox/com/interface/3.2.x/medium_format.rb +18 -0
  152. data/lib/virtualbox/com/interface/3.2.x/medium_state.rb +11 -0
  153. data/lib/virtualbox/com/interface/3.2.x/medium_type.rb +11 -0
  154. data/lib/virtualbox/com/interface/3.2.x/medium_variant.rb +11 -0
  155. data/lib/virtualbox/com/interface/3.2.x/nat_alias_mode.rb +11 -0
  156. data/lib/virtualbox/com/interface/3.2.x/nat_engine.rb +27 -0
  157. data/lib/virtualbox/com/interface/3.2.x/nat_protocol.rb +11 -0
  158. data/lib/virtualbox/com/interface/3.2.x/network_adapter.rb +34 -0
  159. data/lib/virtualbox/com/interface/3.2.x/network_adapter_type.rb +11 -0
  160. data/lib/virtualbox/com/interface/3.2.x/network_attachment_type.rb +11 -0
  161. data/lib/virtualbox/com/interface/3.2.x/nsiexception.rb +23 -0
  162. data/lib/virtualbox/com/interface/3.2.x/nsisupports.rb +15 -0
  163. data/lib/virtualbox/com/interface/3.2.x/parallel_port.rb +17 -0
  164. data/lib/virtualbox/com/interface/3.2.x/pointing_hid_type.rb +11 -0
  165. data/lib/virtualbox/com/interface/3.2.x/port_mode.rb +11 -0
  166. data/lib/virtualbox/com/interface/3.2.x/progress.rb +63 -0
  167. data/lib/virtualbox/com/interface/3.2.x/serial_port.rb +19 -0
  168. data/lib/virtualbox/com/interface/3.2.x/session.rb +18 -0
  169. data/lib/virtualbox/com/interface/3.2.x/session_state.rb +11 -0
  170. data/lib/virtualbox/com/interface/3.2.x/session_type.rb +11 -0
  171. data/lib/virtualbox/com/interface/3.2.x/shared_folder.rb +17 -0
  172. data/lib/virtualbox/com/interface/3.2.x/snapshot.rb +20 -0
  173. data/lib/virtualbox/com/interface/3.2.x/storage_bus.rb +11 -0
  174. data/lib/virtualbox/com/interface/3.2.x/storage_controller.rb +24 -0
  175. data/lib/virtualbox/com/interface/3.2.x/storage_controller_type.rb +11 -0
  176. data/lib/virtualbox/com/interface/3.2.x/system_properties.rb +42 -0
  177. data/lib/virtualbox/com/interface/3.2.x/usb_controller.rb +21 -0
  178. data/lib/virtualbox/com/interface/3.2.x/usb_device.rb +24 -0
  179. data/lib/virtualbox/com/interface/3.2.x/usb_device_filter.rb +23 -0
  180. data/lib/virtualbox/com/interface/3.2.x/usb_device_filter_action.rb +11 -0
  181. data/lib/virtualbox/com/interface/3.2.x/usb_device_state.rb +11 -0
  182. data/lib/virtualbox/com/interface/3.2.x/virtual_box_error_info.rb +17 -0
  183. data/lib/virtualbox/com/interface/3.2.x/virtual_system_description.rb +19 -0
  184. data/lib/virtualbox/com/interface/3.2.x/virtual_system_description_type.rb +14 -0
  185. data/lib/virtualbox/com/interface/3.2.x/virtual_system_description_value_type.rb +11 -0
  186. data/lib/virtualbox/com/interface/3.2.x/virtualbox.rb +67 -0
  187. data/lib/virtualbox/com/interface/3.2.x/vrdp_auth_type.rb +11 -0
  188. data/lib/virtualbox/com/interface/3.2.x/vrdp_server.rb +21 -0
  189. data/lib/virtualbox/com/mscom_interface.rb +44 -0
  190. data/lib/virtualbox/com/nil_interface.rb +7 -0
  191. data/lib/virtualbox/com/util.rb +32 -0
  192. data/lib/virtualbox/cpu.rb +61 -0
  193. data/lib/virtualbox/dhcp_server.rb +89 -0
  194. data/lib/virtualbox/dvd.rb +27 -0
  195. data/lib/virtualbox/exceptions.rb +39 -0
  196. data/lib/virtualbox/ext/byte_normalizer.rb +17 -0
  197. data/lib/virtualbox/ext/glob_loader.rb +22 -0
  198. data/lib/virtualbox/ext/logger.rb +38 -0
  199. data/lib/virtualbox/ext/platform.rb +27 -0
  200. data/lib/virtualbox/ext/subclass_listing.rb +24 -0
  201. data/lib/virtualbox/extra_data.rb +127 -0
  202. data/lib/virtualbox/forwarded_port.rb +222 -0
  203. data/lib/virtualbox/global.rb +102 -0
  204. data/lib/virtualbox/guest_property.rb +116 -0
  205. data/lib/virtualbox/hard_drive.rb +246 -0
  206. data/lib/virtualbox/host.rb +71 -0
  207. data/lib/virtualbox/host_network_interface.rb +137 -0
  208. data/lib/virtualbox/hw_virtualization.rb +63 -0
  209. data/lib/virtualbox/lib.rb +84 -0
  210. data/lib/virtualbox/media.rb +20 -0
  211. data/lib/virtualbox/medium.rb +145 -0
  212. data/lib/virtualbox/medium_attachment.rb +61 -0
  213. data/lib/virtualbox/nat_engine.rb +71 -0
  214. data/lib/virtualbox/nat_forwarded_port.rb +171 -0
  215. data/lib/virtualbox/network_adapter.rb +166 -0
  216. data/lib/virtualbox/proxies/collection.rb +57 -0
  217. data/lib/virtualbox/shared_folder.rb +220 -0
  218. data/lib/virtualbox/snapshot.rb +185 -0
  219. data/lib/virtualbox/storage_controller.rb +160 -0
  220. data/lib/virtualbox/system_properties.rb +74 -0
  221. data/lib/virtualbox/usb_controller.rb +59 -0
  222. data/lib/virtualbox/usb_device_filter.rb +74 -0
  223. data/lib/virtualbox/version.rb +36 -0
  224. data/lib/virtualbox/virtual_system_description.rb +47 -0
  225. data/lib/virtualbox/vm.rb +684 -0
  226. data/lib/virtualbox/vrdp_server.rb +59 -0
  227. data/test/test_helper.rb +18 -0
  228. data/test/virtualbox/abstract_model/attributable_test.rb +269 -0
  229. data/test/virtualbox/abstract_model/dirty_test.rb +83 -0
  230. data/test/virtualbox/abstract_model/interface_attributes_test.rb +194 -0
  231. data/test/virtualbox/abstract_model/relatable_test.rb +348 -0
  232. data/test/virtualbox/abstract_model/validatable_test.rb +308 -0
  233. data/test/virtualbox/abstract_model/version_matcher_test.rb +41 -0
  234. data/test/virtualbox/abstract_model_test.rb +462 -0
  235. data/test/virtualbox/appliance_test.rb +159 -0
  236. data/test/virtualbox/audio_adapter_test.rb +83 -0
  237. data/test/virtualbox/bios_test.rb +83 -0
  238. data/test/virtualbox/com/abstract_enum_test.rb +49 -0
  239. data/test/virtualbox/com/abstract_implementer_test.rb +40 -0
  240. data/test/virtualbox/com/abstract_interface_test.rb +140 -0
  241. data/test/virtualbox/com/ffi/interface_test.rb +249 -0
  242. data/test/virtualbox/com/ffi/util_test.rb +108 -0
  243. data/test/virtualbox/com/ffi_interface_test.rb +42 -0
  244. data/test/virtualbox/com/implementer/base_test.rb +38 -0
  245. data/test/virtualbox/com/implementer/ffi_test.rb +527 -0
  246. data/test/virtualbox/com/implementer/mscom_test.rb +247 -0
  247. data/test/virtualbox/com/mscom_interface_test.rb +17 -0
  248. data/test/virtualbox/com/util_test.rb +17 -0
  249. data/test/virtualbox/cpu_test.rb +103 -0
  250. data/test/virtualbox/dhcp_server_test.rb +165 -0
  251. data/test/virtualbox/dvd_test.rb +28 -0
  252. data/test/virtualbox/ext/byte_normalizer_test.rb +34 -0
  253. data/test/virtualbox/ext/platform_test.rb +50 -0
  254. data/test/virtualbox/ext/subclass_listing_test.rb +25 -0
  255. data/test/virtualbox/extra_data_test.rb +155 -0
  256. data/test/virtualbox/forwarded_port_test.rb +286 -0
  257. data/test/virtualbox/global_test.rb +46 -0
  258. data/test/virtualbox/hard_drive_test.rb +141 -0
  259. data/test/virtualbox/host_network_interface_test.rb +254 -0
  260. data/test/virtualbox/host_test.rb +94 -0
  261. data/test/virtualbox/hw_virtualization_test.rb +103 -0
  262. data/test/virtualbox/lib_test.rb +93 -0
  263. data/test/virtualbox/medium_attachment_test.rb +147 -0
  264. data/test/virtualbox/medium_test.rb +192 -0
  265. data/test/virtualbox/nat_engine_test.rb +106 -0
  266. data/test/virtualbox/nat_forwarded_port_test.rb +222 -0
  267. data/test/virtualbox/network_adapter_test.rb +191 -0
  268. data/test/virtualbox/proxies/collection_test.rb +102 -0
  269. data/test/virtualbox/shared_folder_test.rb +219 -0
  270. data/test/virtualbox/snapshot_test.rb +231 -0
  271. data/test/virtualbox/storage_controller_test.rb +197 -0
  272. data/test/virtualbox/system_properties_test.rb +87 -0
  273. data/test/virtualbox/usb_controller_test.rb +112 -0
  274. data/test/virtualbox/usb_device_filter_test.rb +93 -0
  275. data/test/virtualbox/version_test.rb +59 -0
  276. data/test/virtualbox/virtual_system_description_test.rb +61 -0
  277. data/test/virtualbox/vm_test.rb +637 -0
  278. data/test/virtualbox/vrdp_server_test.rb +83 -0
  279. data/test/virtualbox_test.rb +11 -0
  280. data/virtualbox.gemspec +25 -0
  281. metadata +397 -0
@@ -0,0 +1,348 @@
1
+ require File.expand_path("../../../test_helper", __FILE__)
2
+
3
+ class RelatableTest < Test::Unit::TestCase
4
+ class Relatee
5
+ def self.populate_relationship(caller, data)
6
+ "FOO"
7
+ end
8
+ end
9
+
10
+ class BarRelatee
11
+ def self.set_relationship(caller, old_value, new_value)
12
+ end
13
+ end
14
+
15
+ class EmptyRelatableModel
16
+ include VirtualBox::AbstractModel::Relatable
17
+ end
18
+
19
+ class RelatableModel < EmptyRelatableModel
20
+ relationship :foos, RelatableTest::Relatee
21
+ relationship :bars, RelatableTest::BarRelatee
22
+ end
23
+
24
+ setup do
25
+ @data = {}
26
+ end
27
+
28
+ context "class methods" do
29
+ should "read back relationships in order added" do
30
+ order = mock("order")
31
+ order_seq = sequence("order_seq")
32
+ order.expects(:foos).in_sequence(order_seq)
33
+ order.expects(:bars).in_sequence(order_seq)
34
+
35
+ RelatableModel.relationships.each do |name, options|
36
+ order.send(name)
37
+ end
38
+ end
39
+ end
40
+
41
+ context "setting a relationship" do
42
+ setup do
43
+ @model = RelatableModel.new
44
+ end
45
+
46
+ should "have a magic method relationship= which calls set_relationship" do
47
+ @model.expects(:set_relationship).with(:foos, "FOOS!")
48
+ @model.foos = "FOOS!"
49
+ end
50
+
51
+ should "raise a NonSettableRelationshipException if relationship can't be set" do
52
+ assert_raises(VirtualBox::Exceptions::NonSettableRelationshipException) {
53
+ @model.foos = "FOOS!"
54
+ }
55
+ end
56
+
57
+ should "call set_relationship on the relationship class" do
58
+ BarRelatee.expects(:populate_relationship).returns("foo")
59
+ @model.populate_relationships({})
60
+
61
+ BarRelatee.expects(:set_relationship).with(@model, "foo", "bars")
62
+ assert_nothing_raised { @model.bars = "bars" }
63
+ end
64
+
65
+ should "set the result of set_relationship as the new relationship data" do
66
+ BarRelatee.stubs(:set_relationship).returns("hello")
67
+ @model.bars = "zoo"
68
+ assert_equal "hello", @model.bars
69
+ end
70
+ end
71
+
72
+ context "subclasses" do
73
+ class SubRelatableModel < RelatableModel
74
+ relationship :bars, RelatableTest::Relatee
75
+ end
76
+
77
+ setup do
78
+ @relationships = SubRelatableModel.relationships
79
+ end
80
+
81
+ should "inherit relationships of parent" do
82
+ assert SubRelatableModel.has_relationship?(:foos)
83
+ assert SubRelatableModel.has_relationship?(:bars)
84
+ end
85
+
86
+ should "inherit options of relationships" do
87
+ assert_equal Relatee, SubRelatableModel.relationships_hash[:foos][:klass]
88
+ end
89
+ end
90
+
91
+ context "default callbacks" do
92
+ setup do
93
+ @model = RelatableModel.new
94
+ end
95
+
96
+ should "not raise an error if populate_relationship doesn't exist" do
97
+ assert !BarRelatee.respond_to?(:populate_relationship)
98
+ assert_nothing_raised { @model.populate_relationships(nil) }
99
+ end
100
+
101
+ should "not raise an error when saving relationships if the callback doesn't exist" do
102
+ assert !Relatee.respond_to?(:save_relationship)
103
+ assert_nothing_raised { @model.save_relationships }
104
+ end
105
+
106
+ should "not raise an error in destroying relationships if the callback doesn't exist" do
107
+ assert !Relatee.respond_to?(:destroy_relationship)
108
+ assert_nothing_raised { @model.destroy_relationships }
109
+ end
110
+ end
111
+
112
+ context "destroying" do
113
+ setup do
114
+ @model = RelatableModel.new
115
+ @model.populate_relationships({})
116
+ end
117
+
118
+ context "a single relationship" do
119
+ should "call destroy_relationship only for the given relationship" do
120
+ Relatee.expects(:destroy_relationship).once
121
+ BarRelatee.expects(:destroy_relationship).never
122
+ @model.destroy_relationship(:foos)
123
+ end
124
+
125
+ should "forward any args passed into destroy_relationship" do
126
+ Relatee.expects(:destroy_relationship).with(@model, anything, "HELLO").once
127
+ @model.destroy_relationship(:foos, "HELLO")
128
+ end
129
+
130
+ should "pass the data into destroy_relationship" do
131
+ Relatee.expects(:destroy_relationship).with(@model, "FOO").once
132
+ @model.destroy_relationship(:foos)
133
+ end
134
+
135
+ should "call read_relationship (to force the load if lazy)" do
136
+ Relatee.expects(:destroy_relationship).with(@model, "FOO").once
137
+ @model.expects(:read_relationship).with(:foos).once
138
+ @model.destroy_relationship(:foos)
139
+ end
140
+ end
141
+
142
+ context "all relationships" do
143
+ should "call destroy_relationship on the related class" do
144
+ Relatee.expects(:destroy_relationship).with(@model, anything).once
145
+ @model.destroy_relationships
146
+ end
147
+
148
+ should "forward any args passed into destroy relationships" do
149
+ Relatee.expects(:destroy_relationship).with(@model, anything, "HELLO").once
150
+ @model.destroy_relationships("HELLO")
151
+ end
152
+ end
153
+ end
154
+
155
+ context "lazy relationships" do
156
+ class LazyRelatableModel < EmptyRelatableModel
157
+ relationship :foos, RelatableTest::Relatee, :lazy => true
158
+ relationship :bars, RelatableTest::BarRelatee
159
+ end
160
+
161
+ setup do
162
+ @model = LazyRelatableModel.new
163
+ end
164
+
165
+ should "return true if a relationship is lazy, and false if not, when checking" do
166
+ assert @model.lazy_relationship?(:foos)
167
+ assert !@model.lazy_relationship?(:bars)
168
+ end
169
+
170
+ should "not be loaded by default" do
171
+ assert !@model.loaded_relationship?(:foos)
172
+ end
173
+
174
+ should "call `load_relationship` on initial load" do
175
+ @model.expects(:load_relationship).with(:foos).once
176
+ @model.foos
177
+ end
178
+
179
+ should "not call `load_relationship` for non lazy attributes" do
180
+ @model.expects(:load_relationship).never
181
+ @model.bars
182
+ end
183
+
184
+ should "mark a relationship as loaded on populate_relationship" do
185
+ @model.populate_relationship(:foos, {})
186
+ assert @model.loaded_relationship?(:foos)
187
+ end
188
+
189
+ should "not populate the lazy relationship right away" do
190
+ Relatee.expects(:populate_relationship).never
191
+ BarRelatee.expects(:populate_relationship).once
192
+ @model.populate_relationships({})
193
+ end
194
+ end
195
+
196
+ context "saving relationships" do
197
+ class RelatableWithLazyModel < RelatableModel
198
+ relationship :bazs, RelatableTest::Relatee, :lazy => true
199
+ relationship :vers, RelatableTest::Relatee, :version => "3.1"
200
+
201
+ def load_relationship(name)
202
+ populate_relationship(:bazs, "foo")
203
+ end
204
+ end
205
+
206
+ setup do
207
+ @model = RelatableWithLazyModel.new
208
+ VirtualBox.stubs(:version).returns("3.1.3")
209
+ end
210
+
211
+ should "call save_relationship for all relationships" do
212
+ @model.expects(:save_relationship).with(:foos).returns(true)
213
+ @model.expects(:save_relationship).with(:bars).returns(true)
214
+ @model.expects(:save_relationship).with(:bazs).returns(true)
215
+ @model.expects(:save_relationship).with(:vers).returns(true)
216
+ assert @model.save_relationships
217
+ end
218
+
219
+ should "not call save_relationship on non-loaded relations" do
220
+ Relatee.expects(:save_relationship).never
221
+ @model.save_relationship(:bazs)
222
+ end
223
+
224
+ should "not call save_relationship on relationships with mismatched versions" do
225
+ VirtualBox.stubs(:version).returns("3.2.4")
226
+ Relatee.expects(:save_relationship).never
227
+ @model.save_relationship(:vers)
228
+ end
229
+
230
+ should "call save_relationship on loaded lazy relationships" do
231
+ @model.load_relationship(:bazs)
232
+ Relatee.expects(:save_relationship).once
233
+ @model.save_relationship(:bazs)
234
+ end
235
+
236
+ should "call save_relationship on the related class" do
237
+ Relatee.expects(:save_relationship).with(@model, @model.foos).once.returns(:r)
238
+ assert_equal :r, @model.save_relationship(:foos)
239
+ end
240
+
241
+ should "forward parameters through" do
242
+ Relatee.expects(:save_relationship).with(@model, @model.foos, "YES").once
243
+ @model.save_relationship(:foos, "YES")
244
+ end
245
+ end
246
+
247
+ context "reading relationships" do
248
+ class VersionedRelatableModel < RelatableModel
249
+ relationship :ver, :Ver, :version => "3.1"
250
+ end
251
+
252
+ setup do
253
+ @model = VersionedRelatableModel.new
254
+ end
255
+
256
+ should "provide a read method for relationships" do
257
+ assert_nothing_raised { @model.foos }
258
+ end
259
+
260
+ should "raise an exception if invalid version for versioned relationships" do
261
+ VirtualBox.stubs(:version).returns("3.0.14")
262
+ assert_raises(VirtualBox::Exceptions::UnsupportedVersionException) {
263
+ @model.ver
264
+ }
265
+ end
266
+
267
+ should "not raise an exception if valid version for versioned relationship" do
268
+ VirtualBox.stubs(:version).returns("3.1.8")
269
+ assert_nothing_raised {
270
+ @model.ver
271
+ }
272
+ end
273
+ end
274
+
275
+ context "checking for relationships" do
276
+ setup do
277
+ @model = RelatableModel.new
278
+ end
279
+
280
+ should "have a class method as well" do
281
+ assert RelatableModel.has_relationship?(:foos)
282
+ assert !RelatableModel.has_relationship?(:bazs)
283
+ end
284
+
285
+ should "return true for existing relationships" do
286
+ assert @model.has_relationship?(:foos)
287
+ end
288
+
289
+ should "return false for nonexistent relationships" do
290
+ assert !@model.has_relationship?(:bazs)
291
+ end
292
+ end
293
+
294
+ context "determining the class of relationships" do
295
+ class ClassRelatableModel < EmptyRelatableModel
296
+ relationship :foo, RelatableTest::Relatee
297
+ relationship :bar, "RelatableTest::BarRelatee"
298
+ end
299
+
300
+ setup do
301
+ @model = ClassRelatableModel.new
302
+ end
303
+
304
+ should "just return the class for Class types" do
305
+ assert_equal Relatee, @model.relationship_class(:foo)
306
+ end
307
+
308
+ should "turn string into class" do
309
+ assert_equal BarRelatee, @model.relationship_class(:bar)
310
+ end
311
+ end
312
+
313
+ context "populating relationships" do
314
+ class PopulatingRelatableModel < RelatableModel
315
+ relationship :bazs, RelatableTest::Relatee, :version => "3.1"
316
+ end
317
+
318
+ setup do
319
+ @model = PopulatingRelatableModel.new
320
+ VirtualBox.stubs(:version).returns("3.1.4")
321
+ end
322
+
323
+ should "be able to populate a single relationship" do
324
+ Relatee.expects(:populate_relationship).with(@model, @data).once
325
+ @model.populate_relationship(:foos, @data)
326
+ end
327
+
328
+ should "not populate versioned relationships if version mismatch" do
329
+ VirtualBox.stubs(:version).returns("3.0.4")
330
+ Relatee.expects(:populate_relationship).never
331
+ @model.populate_relationship(:bazs, @data)
332
+ end
333
+
334
+ should "call populate_relationship on the related class" do
335
+ populate_seq = sequence("populate_seq")
336
+ @model.expects(:populate_relationship).with(:foos, @data).once.in_sequence(populate_seq)
337
+ @model.expects(:populate_relationship).with(:bars, @data).once.in_sequence(populate_seq)
338
+ @model.expects(:populate_relationship).with(:bazs, @data).once.in_sequence(populate_seq)
339
+ @model.populate_relationships(@data)
340
+ end
341
+
342
+ should "properly save returned value as the value for the relationship" do
343
+ Relatee.expects(:populate_relationship).twice.returns("HEY")
344
+ @model.populate_relationships(@data)
345
+ assert_equal "HEY", @model.foos
346
+ end
347
+ end
348
+ end
@@ -0,0 +1,308 @@
1
+ require File.expand_path("../../../test_helper", __FILE__)
2
+
3
+ class ValidatableTest < Test::Unit::TestCase
4
+ class ValidatableModel
5
+ include VirtualBox::AbstractModel::Validatable
6
+
7
+ attr_accessor :foos
8
+ attr_accessor :bars
9
+ end
10
+
11
+ context "errors" do
12
+ setup do
13
+ @model = ValidatableModel.new
14
+ end
15
+
16
+ should "have no errors by default" do
17
+ assert @model.errors.empty?
18
+ end
19
+
20
+ should "be able to add errors" do
21
+ @model.add_error(:foo, "is blank")
22
+ assert !@model.errors.empty?
23
+ assert !@model.errors[:foo].nil?
24
+ assert_equal "is blank", @model.errors[:foo].first
25
+ end
26
+
27
+ should "be able to add multiple errors" do
28
+ @model.add_error(:foo, "foo")
29
+ @model.add_error(:foo, "bar")
30
+ assert !@model.errors.empty?
31
+ assert !@model.errors[:foo].nil?
32
+ assert_equal 2, @model.errors[:foo].length
33
+ end
34
+
35
+ should "be able to clear errors" do
36
+ @model.add_error(:foo, "foo")
37
+ assert !@model.errors.empty?
38
+ @model.clear_errors
39
+ assert @model.errors.empty?
40
+ end
41
+
42
+ should "be able to get full error messages" do
43
+ @model.add_error(:foo, "should be bar.")
44
+ assert_equal ['Foo should be bar.'], @model.full_error_messages
45
+ end
46
+
47
+ should "be able to get the errors on a specific field" do
48
+ @model.add_error(:foo, "an error.")
49
+ assert @model.errors_on(:bar).nil?
50
+ assert @model.errors_on(:foo)
51
+ end
52
+ end
53
+
54
+ context "validity" do
55
+ setup do
56
+ @model = ValidatableModel.new
57
+ end
58
+
59
+ should "call validate on valid?" do
60
+ @model.expects(:validate)
61
+ assert @model.valid?
62
+ end
63
+
64
+ should "be valid if there are no errors" do
65
+ assert @model.valid?
66
+ end
67
+
68
+ should "be invalid if there are any errors" do
69
+ @model.add_error(:foo, "foo")
70
+ assert !@model.valid?
71
+ end
72
+
73
+ should "have a validate method by default which returns true" do
74
+ assert @model.validate
75
+ end
76
+ end
77
+
78
+ context "extracting options hash from multi-param functions" do
79
+ setup do
80
+ @model = ValidatableModel.new
81
+ end
82
+
83
+ should "modify the initial fields array" do
84
+ fields = [{}]
85
+ @model.__validates_extract_options(fields, {})
86
+ assert fields.empty?
87
+ end
88
+
89
+ should "not modify the initial fields array if not a hash at the end" do
90
+ fields = [:foo]
91
+ @model.__validates_extract_options(fields, {})
92
+ assert_equal [:foo], fields
93
+ end
94
+
95
+ should "return the defaults hash merged with the given options" do
96
+ fields = [{ :foo => :bar }]
97
+ result = @model.__validates_extract_options(fields, { :foo => 0, :bar => :baz })
98
+ assert_equal({ :foo => :bar, :bar => :baz }, result)
99
+ end
100
+ end
101
+
102
+ context "specific validations" do
103
+ setup do
104
+ @model = ValidatableModel.new
105
+ end
106
+
107
+ context "validates_presence_of" do
108
+ setup do
109
+ @model.foos = "foo"
110
+ @model.bars = "bar"
111
+ end
112
+
113
+ should "not add an error if not blank" do
114
+ @model.validates_presence_of(:foos)
115
+ assert @model.valid?
116
+ end
117
+
118
+ should "add an error if blank field" do
119
+ @model.foos = ""
120
+ @model.validates_presence_of(:foos)
121
+ assert !@model.valid?
122
+ end
123
+
124
+ should "add an error for a nil field" do
125
+ @model.foos = nil
126
+ @model.validates_presence_of(:foos)
127
+ assert !@model.valid?
128
+ end
129
+
130
+ should "validate multiple fields" do
131
+ @model.bars = nil
132
+ @model.validates_presence_of(:foos, :bars)
133
+
134
+ assert !@model.valid?
135
+ assert @model.errors[:bars]
136
+ end
137
+
138
+ should "return false on invalid" do
139
+ @model.bars = nil
140
+ assert !@model.validates_presence_of(:bars)
141
+ end
142
+
143
+ should "return true on valid" do
144
+ @model.bars = "foo"
145
+ assert @model.validates_presence_of(:bars)
146
+ end
147
+
148
+ should "return false if any are invalid on multiple fields" do
149
+ @model.bars = nil
150
+ assert !@model.validates_presence_of(:foos, :bars)
151
+ end
152
+
153
+ should "return true if all fields are valid" do
154
+ @model.foos = "foo"
155
+ @model.bars = "bar"
156
+ assert @model.validates_presence_of(:foos, :bars)
157
+ end
158
+
159
+ should "add error message if invalid" do
160
+ @model.foos = nil
161
+ assert !@model.validates_presence_of(:foos)
162
+ assert_equal "can't be blank.", @model.errors[:foos].first
163
+ end
164
+
165
+ should "use custom error message if given" do
166
+ @model.foos = nil
167
+ assert !@model.validates_presence_of(:foos, :message => "can't be nil.")
168
+ assert_equal "can't be nil.", @model.errors[:foos].first
169
+ end
170
+ end
171
+
172
+ context "validates_format_of" do
173
+ setup do
174
+ @model.foos = "foo"
175
+ @model.bars = "bar"
176
+ end
177
+
178
+ should "not add an error if formatted properly" do
179
+ @model.validates_format_of(:foos, :with => /^foo$/)
180
+ assert @model.valid?
181
+ end
182
+
183
+ should "add an error if not formatted properly" do
184
+ @model.validates_format_of(:bars, :with => /^foo$/)
185
+ assert !@model.valid?
186
+ end
187
+
188
+ should "not add an error for a nil field" do
189
+ @model.foos = nil
190
+ @model.validates_format_of(:foos, :with => /^foo$/)
191
+ assert @model.valid?
192
+ end
193
+
194
+ should "validate multiple fields" do
195
+ @model.validates_format_of(:foos, :bars, :with => /^foo$/)
196
+ assert !@model.valid?
197
+ assert @model.errors[:bars]
198
+ end
199
+
200
+ should "return false on invalid" do
201
+ assert !@model.validates_format_of(:bars, :with => /^foo$/)
202
+ end
203
+
204
+ should "return true on valid" do
205
+ assert @model.validates_format_of(:foos, :with => /^foo$/)
206
+ end
207
+
208
+ should "return false if any are invalid on multiple fields" do
209
+ assert !@model.validates_format_of(:foos, :bars, :with => /^foo$/)
210
+ end
211
+
212
+ should "return true if all fields are valid" do
213
+ assert @model.validates_format_of(:foos, :bars, :with => /^\w+$/)
214
+ end
215
+ end
216
+
217
+ context "validates_numericality_of" do
218
+ setup do
219
+ @model.foos = 1
220
+ @model.bars = "1"
221
+ end
222
+
223
+ should "not add an error if a number" do
224
+ @model.validates_numericality_of(:foos)
225
+ assert @model.valid?
226
+ end
227
+
228
+ should "add an error if not a number" do
229
+ @model.validates_numericality_of(:bars)
230
+ assert !@model.valid?
231
+ end
232
+
233
+ should "not add an error for a nil field" do
234
+ @model.foos = nil
235
+ @model.validates_numericality_of(:foos)
236
+ assert @model.valid?
237
+ end
238
+
239
+ should "validate multiple fields" do
240
+ @model.validates_numericality_of(:foos, :bars)
241
+ assert !@model.valid?
242
+ assert @model.errors[:bars]
243
+ end
244
+
245
+ should "return false on invalid" do
246
+ assert !@model.validates_numericality_of(:bars)
247
+ end
248
+
249
+ should "return true on valid" do
250
+ assert @model.validates_numericality_of(:foos)
251
+ end
252
+
253
+ should "return false if any are invalid on multiple fields" do
254
+ assert !@model.validates_numericality_of(:foos, :bars)
255
+ end
256
+
257
+ should "return true if all fields are valid" do
258
+ @model.bars = 2
259
+ assert @model.validates_numericality_of(:foos, :bars)
260
+ end
261
+ end
262
+
263
+ context "validates_inclusion_of" do
264
+ setup do
265
+ @model.foos = 1
266
+ @model.bars = 2
267
+ end
268
+
269
+ should "not add an error if value is included" do
270
+ @model.validates_inclusion_of(:foos, :in => [1])
271
+ assert @model.valid?
272
+ end
273
+
274
+ should "add an error if value is not included" do
275
+ @model.validates_inclusion_of(:bars, :in => [1])
276
+ assert !@model.valid?
277
+ end
278
+
279
+ should "not add an error for a nil field" do
280
+ @model.foos = nil
281
+ @model.validates_inclusion_of(:foos, :in => [1,2])
282
+ assert @model.valid?
283
+ end
284
+
285
+ should "validate multiple fields" do
286
+ @model.validates_inclusion_of(:foos, :bars, :in => [1,3])
287
+ assert !@model.valid?
288
+ assert @model.errors[:bars]
289
+ end
290
+
291
+ should "return false on invalid" do
292
+ assert !@model.validates_inclusion_of(:bars, :in => [1])
293
+ end
294
+
295
+ should "return true on valid" do
296
+ assert @model.validates_inclusion_of(:foos, :in => [1])
297
+ end
298
+
299
+ should "return false if any are invalid on multiple fields" do
300
+ assert !@model.validates_inclusion_of(:foos, :bars, :in => [1,3])
301
+ end
302
+
303
+ should "return true if all fields are valid" do
304
+ assert @model.validates_inclusion_of(:foos, :bars, :in => [1,2])
305
+ end
306
+ end
307
+ end
308
+ end