ruote-maestrodev 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (265) hide show
  1. data/CHANGELOG.txt +290 -0
  2. data/CREDITS.txt +99 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.rdoc +88 -0
  5. data/Rakefile +108 -0
  6. data/TODO.txt +488 -0
  7. data/lib/ruote.rb +7 -0
  8. data/lib/ruote/context.rb +194 -0
  9. data/lib/ruote/engine.rb +1062 -0
  10. data/lib/ruote/engine/process_error.rb +122 -0
  11. data/lib/ruote/engine/process_status.rb +448 -0
  12. data/lib/ruote/exp/command.rb +87 -0
  13. data/lib/ruote/exp/commanded.rb +69 -0
  14. data/lib/ruote/exp/condition.rb +227 -0
  15. data/lib/ruote/exp/fe_add_branches.rb +138 -0
  16. data/lib/ruote/exp/fe_apply.rb +154 -0
  17. data/lib/ruote/exp/fe_cancel_process.rb +78 -0
  18. data/lib/ruote/exp/fe_command.rb +156 -0
  19. data/lib/ruote/exp/fe_concurrence.rb +321 -0
  20. data/lib/ruote/exp/fe_concurrent_iterator.rb +219 -0
  21. data/lib/ruote/exp/fe_cron.rb +141 -0
  22. data/lib/ruote/exp/fe_cursor.rb +324 -0
  23. data/lib/ruote/exp/fe_define.rb +112 -0
  24. data/lib/ruote/exp/fe_echo.rb +60 -0
  25. data/lib/ruote/exp/fe_equals.rb +115 -0
  26. data/lib/ruote/exp/fe_error.rb +82 -0
  27. data/lib/ruote/exp/fe_filter.rb +648 -0
  28. data/lib/ruote/exp/fe_forget.rb +88 -0
  29. data/lib/ruote/exp/fe_given.rb +154 -0
  30. data/lib/ruote/exp/fe_if.rb +127 -0
  31. data/lib/ruote/exp/fe_inc.rb +205 -0
  32. data/lib/ruote/exp/fe_iterator.rb +234 -0
  33. data/lib/ruote/exp/fe_let.rb +75 -0
  34. data/lib/ruote/exp/fe_listen.rb +304 -0
  35. data/lib/ruote/exp/fe_lose.rb +110 -0
  36. data/lib/ruote/exp/fe_noop.rb +45 -0
  37. data/lib/ruote/exp/fe_once.rb +215 -0
  38. data/lib/ruote/exp/fe_participant.rb +287 -0
  39. data/lib/ruote/exp/fe_read.rb +69 -0
  40. data/lib/ruote/exp/fe_redo.rb +82 -0
  41. data/lib/ruote/exp/fe_ref.rb +152 -0
  42. data/lib/ruote/exp/fe_registerp.rb +110 -0
  43. data/lib/ruote/exp/fe_reserve.rb +126 -0
  44. data/lib/ruote/exp/fe_restore.rb +102 -0
  45. data/lib/ruote/exp/fe_save.rb +72 -0
  46. data/lib/ruote/exp/fe_sequence.rb +59 -0
  47. data/lib/ruote/exp/fe_set.rb +154 -0
  48. data/lib/ruote/exp/fe_subprocess.rb +211 -0
  49. data/lib/ruote/exp/fe_that.rb +92 -0
  50. data/lib/ruote/exp/fe_undo.rb +67 -0
  51. data/lib/ruote/exp/fe_unregisterp.rb +69 -0
  52. data/lib/ruote/exp/fe_wait.rb +95 -0
  53. data/lib/ruote/exp/flowexpression.rb +886 -0
  54. data/lib/ruote/exp/iterator.rb +81 -0
  55. data/lib/ruote/exp/merge.rb +118 -0
  56. data/lib/ruote/exp/ro_attributes.rb +212 -0
  57. data/lib/ruote/exp/ro_filters.rb +136 -0
  58. data/lib/ruote/exp/ro_persist.rb +154 -0
  59. data/lib/ruote/exp/ro_variables.rb +189 -0
  60. data/lib/ruote/exp/ro_vf.rb +68 -0
  61. data/lib/ruote/fei.rb +260 -0
  62. data/lib/ruote/id/mnemo_wfid_generator.rb +43 -0
  63. data/lib/ruote/id/wfid_generator.rb +81 -0
  64. data/lib/ruote/log/default_history.rb +122 -0
  65. data/lib/ruote/log/pretty.rb +176 -0
  66. data/lib/ruote/log/storage_history.rb +159 -0
  67. data/lib/ruote/log/test_logger.rb +208 -0
  68. data/lib/ruote/log/wait_logger.rb +64 -0
  69. data/lib/ruote/part/block_participant.rb +137 -0
  70. data/lib/ruote/part/code_participant.rb +81 -0
  71. data/lib/ruote/part/engine_participant.rb +189 -0
  72. data/lib/ruote/part/local_participant.rb +138 -0
  73. data/lib/ruote/part/no_op_participant.rb +60 -0
  74. data/lib/ruote/part/null_participant.rb +54 -0
  75. data/lib/ruote/part/rev_participant.rb +169 -0
  76. data/lib/ruote/part/smtp_participant.rb +116 -0
  77. data/lib/ruote/part/storage_participant.rb +392 -0
  78. data/lib/ruote/part/template.rb +84 -0
  79. data/lib/ruote/participant.rb +7 -0
  80. data/lib/ruote/reader.rb +278 -0
  81. data/lib/ruote/reader/json.rb +49 -0
  82. data/lib/ruote/reader/radial.rb +290 -0
  83. data/lib/ruote/reader/ruby_dsl.rb +186 -0
  84. data/lib/ruote/reader/xml.rb +99 -0
  85. data/lib/ruote/receiver/base.rb +212 -0
  86. data/lib/ruote/storage/base.rb +364 -0
  87. data/lib/ruote/storage/composite_storage.rb +121 -0
  88. data/lib/ruote/storage/fs_storage.rb +139 -0
  89. data/lib/ruote/storage/hash_storage.rb +211 -0
  90. data/lib/ruote/svc/dispatch_pool.rb +158 -0
  91. data/lib/ruote/svc/dollar_sub.rb +298 -0
  92. data/lib/ruote/svc/error_handler.rb +138 -0
  93. data/lib/ruote/svc/expression_map.rb +97 -0
  94. data/lib/ruote/svc/participant_list.rb +397 -0
  95. data/lib/ruote/svc/tracker.rb +172 -0
  96. data/lib/ruote/svc/treechecker.rb +141 -0
  97. data/lib/ruote/tree_dot.rb +85 -0
  98. data/lib/ruote/util/filter.rb +525 -0
  99. data/lib/ruote/util/hashdot.rb +79 -0
  100. data/lib/ruote/util/look.rb +128 -0
  101. data/lib/ruote/util/lookup.rb +127 -0
  102. data/lib/ruote/util/misc.rb +167 -0
  103. data/lib/ruote/util/ometa.rb +71 -0
  104. data/lib/ruote/util/serializer.rb +103 -0
  105. data/lib/ruote/util/subprocess.rb +88 -0
  106. data/lib/ruote/util/time.rb +100 -0
  107. data/lib/ruote/util/tree.rb +58 -0
  108. data/lib/ruote/version.rb +29 -0
  109. data/lib/ruote/worker.rb +386 -0
  110. data/lib/ruote/workitem.rb +394 -0
  111. data/phil.txt +14 -0
  112. data/ruote.gemspec +44 -0
  113. data/test/bm/ci.rb +55 -0
  114. data/test/bm/ici.rb +71 -0
  115. data/test/bm/juuman.rb +54 -0
  116. data/test/bm/launch_bench.rb +37 -0
  117. data/test/bm/load_26c.rb +97 -0
  118. data/test/bm/mega.rb +64 -0
  119. data/test/bm/seq_thousand.rb +31 -0
  120. data/test/bm/t.rb +35 -0
  121. data/test/functional/base.rb +247 -0
  122. data/test/functional/concurrent_base.rb +98 -0
  123. data/test/functional/crunner.rb +31 -0
  124. data/test/functional/ct_0_concurrence.rb +65 -0
  125. data/test/functional/ct_1_iterator.rb +67 -0
  126. data/test/functional/ct_2_cancel.rb +81 -0
  127. data/test/functional/eft_0_process_definition.rb +65 -0
  128. data/test/functional/eft_10_cancel_process.rb +46 -0
  129. data/test/functional/eft_11_wait.rb +109 -0
  130. data/test/functional/eft_12_listen.rb +500 -0
  131. data/test/functional/eft_13_iterator.rb +342 -0
  132. data/test/functional/eft_14_cursor.rb +456 -0
  133. data/test/functional/eft_15_loop.rb +69 -0
  134. data/test/functional/eft_16_if.rb +183 -0
  135. data/test/functional/eft_17_equals.rb +55 -0
  136. data/test/functional/eft_18_concurrent_iterator.rb +410 -0
  137. data/test/functional/eft_19_reserve.rb +136 -0
  138. data/test/functional/eft_1_echo.rb +68 -0
  139. data/test/functional/eft_20_save.rb +116 -0
  140. data/test/functional/eft_21_restore.rb +61 -0
  141. data/test/functional/eft_22_noop.rb +28 -0
  142. data/test/functional/eft_23_apply.rb +168 -0
  143. data/test/functional/eft_24_add_branches.rb +98 -0
  144. data/test/functional/eft_25_command.rb +28 -0
  145. data/test/functional/eft_26_error.rb +77 -0
  146. data/test/functional/eft_27_inc.rb +280 -0
  147. data/test/functional/eft_28_once.rb +135 -0
  148. data/test/functional/eft_29_cron.rb +64 -0
  149. data/test/functional/eft_2_sequence.rb +58 -0
  150. data/test/functional/eft_30_ref.rb +155 -0
  151. data/test/functional/eft_31_registerp.rb +130 -0
  152. data/test/functional/eft_32_lose.rb +93 -0
  153. data/test/functional/eft_33_let.rb +31 -0
  154. data/test/functional/eft_34_given.rb +123 -0
  155. data/test/functional/eft_35_filter.rb +375 -0
  156. data/test/functional/eft_36_read.rb +95 -0
  157. data/test/functional/eft_3_participant.rb +149 -0
  158. data/test/functional/eft_4_set.rb +296 -0
  159. data/test/functional/eft_5_subprocess.rb +163 -0
  160. data/test/functional/eft_6_concurrence.rb +304 -0
  161. data/test/functional/eft_7_forget.rb +61 -0
  162. data/test/functional/eft_8_undo.rb +114 -0
  163. data/test/functional/eft_9_redo.rb +138 -0
  164. data/test/functional/ft_0_worker.rb +65 -0
  165. data/test/functional/ft_10_dollar.rb +304 -0
  166. data/test/functional/ft_11_recursion.rb +109 -0
  167. data/test/functional/ft_12_launchitem.rb +43 -0
  168. data/test/functional/ft_13_variables.rb +151 -0
  169. data/test/functional/ft_14_re_apply.rb +324 -0
  170. data/test/functional/ft_15_timeout.rb +226 -0
  171. data/test/functional/ft_16_participant_params.rb +98 -0
  172. data/test/functional/ft_17_conditional.rb +102 -0
  173. data/test/functional/ft_18_kill.rb +138 -0
  174. data/test/functional/ft_19_participant_code.rb +67 -0
  175. data/test/functional/ft_1_process_status.rb +796 -0
  176. data/test/functional/ft_20_storage_participant.rb +543 -0
  177. data/test/functional/ft_21_forget.rb +153 -0
  178. data/test/functional/ft_22_process_definitions.rb +90 -0
  179. data/test/functional/ft_23_load_defs.rb +79 -0
  180. data/test/functional/ft_24_block_participant.rb +235 -0
  181. data/test/functional/ft_25_receiver.rb +207 -0
  182. data/test/functional/ft_26_participant_rtimeout.rb +179 -0
  183. data/test/functional/ft_27_var_indirection.rb +128 -0
  184. data/test/functional/ft_28_null_noop_participants.rb +51 -0
  185. data/test/functional/ft_29_part_template.rb +60 -0
  186. data/test/functional/ft_2_errors.rb +380 -0
  187. data/test/functional/ft_30_smtp_participant.rb +122 -0
  188. data/test/functional/ft_31_part_blocking.rb +72 -0
  189. data/test/functional/ft_33_participant_subprocess_priority.rb +32 -0
  190. data/test/functional/ft_34_cursor_rewind.rb +101 -0
  191. data/test/functional/ft_35_add_service.rb +56 -0
  192. data/test/functional/ft_36_storage_history.rb +150 -0
  193. data/test/functional/ft_37_default_history.rb +109 -0
  194. data/test/functional/ft_38_participant_more.rb +193 -0
  195. data/test/functional/ft_39_wait_for.rb +136 -0
  196. data/test/functional/ft_3_participant_registration.rb +574 -0
  197. data/test/functional/ft_40_wait_logger.rb +62 -0
  198. data/test/functional/ft_41_participants.rb +91 -0
  199. data/test/functional/ft_42_storage_copy.rb +71 -0
  200. data/test/functional/ft_43_participant_on_reply.rb +87 -0
  201. data/test/functional/ft_44_var_participant.rb +35 -0
  202. data/test/functional/ft_45_participant_accept.rb +64 -0
  203. data/test/functional/ft_46_launch_single.rb +83 -0
  204. data/test/functional/ft_47_wfid_generator.rb +54 -0
  205. data/test/functional/ft_48_lose.rb +112 -0
  206. data/test/functional/ft_49_engine_on_error.rb +201 -0
  207. data/test/functional/ft_4_cancel.rb +132 -0
  208. data/test/functional/ft_50_engine_config.rb +22 -0
  209. data/test/functional/ft_51_misc.rb +67 -0
  210. data/test/functional/ft_52_case.rb +134 -0
  211. data/test/functional/ft_53_engine_on_terminate.rb +95 -0
  212. data/test/functional/ft_54_patterns.rb +104 -0
  213. data/test/functional/ft_55_engine_participant.rb +303 -0
  214. data/test/functional/ft_56_filter_attribute.rb +259 -0
  215. data/test/functional/ft_57_rev_participant.rb +252 -0
  216. data/test/functional/ft_58_workitem.rb +69 -0
  217. data/test/functional/ft_59_pause.rb +343 -0
  218. data/test/functional/ft_5_on_error.rb +384 -0
  219. data/test/functional/ft_60_code_participant.rb +45 -0
  220. data/test/functional/ft_61_trailing_fields.rb +34 -0
  221. data/test/functional/ft_62_exp_name_and_dollar_substitution.rb +35 -0
  222. data/test/functional/ft_6_on_cancel.rb +221 -0
  223. data/test/functional/ft_7_tags.rb +177 -0
  224. data/test/functional/ft_8_participant_consumption.rb +124 -0
  225. data/test/functional/ft_9_subprocesses.rb +146 -0
  226. data/test/functional/restart_base.rb +34 -0
  227. data/test/functional/rt_0_wait.rb +55 -0
  228. data/test/functional/rt_1_listen.rb +56 -0
  229. data/test/functional/rt_2_errors.rb +56 -0
  230. data/test/functional/rt_3_once.rb +70 -0
  231. data/test/functional/rt_4_cron.rb +64 -0
  232. data/test/functional/rt_5_timeout.rb +60 -0
  233. data/test/functional/rtest.rb +8 -0
  234. data/test/functional/storage_helper.rb +93 -0
  235. data/test/functional/test.rb +44 -0
  236. data/test/functional/vertical.rb +46 -0
  237. data/test/path_helper.rb +15 -0
  238. data/test/test.rb +13 -0
  239. data/test/test_helper.rb +28 -0
  240. data/test/unit/storage.rb +428 -0
  241. data/test/unit/storages.rb +37 -0
  242. data/test/unit/test.rb +28 -0
  243. data/test/unit/ut_0_ruby_reader.rb +223 -0
  244. data/test/unit/ut_11_lookup.rb +122 -0
  245. data/test/unit/ut_13_serializer.rb +65 -0
  246. data/test/unit/ut_14_is_uri.rb +28 -0
  247. data/test/unit/ut_15_util.rb +57 -0
  248. data/test/unit/ut_16_reader.rb +225 -0
  249. data/test/unit/ut_18_engine.rb +47 -0
  250. data/test/unit/ut_19_part_template.rb +86 -0
  251. data/test/unit/ut_1_fei.rb +165 -0
  252. data/test/unit/ut_20_composite_storage.rb +74 -0
  253. data/test/unit/ut_21_svc_participant_list.rb +46 -0
  254. data/test/unit/ut_22_filter.rb +1094 -0
  255. data/test/unit/ut_23_svc_tracker.rb +48 -0
  256. data/test/unit/ut_24_radial_reader.rb +332 -0
  257. data/test/unit/ut_25_merge.rb +113 -0
  258. data/test/unit/ut_3_wait_logger.rb +39 -0
  259. data/test/unit/ut_4_expmap.rb +20 -0
  260. data/test/unit/ut_5_tree.rb +54 -0
  261. data/test/unit/ut_6_condition.rb +303 -0
  262. data/test/unit/ut_7_workitem.rb +99 -0
  263. data/test/unit/ut_8_tree_to_dot.rb +72 -0
  264. data/test/unit/ut_9_xml_reader.rb +61 -0
  265. metadata +504 -0
@@ -0,0 +1,74 @@
1
+
2
+ #
3
+ # testing ruote
4
+ #
5
+ # Sun Mar 14 21:25:52 JST 2010
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), '..', 'test_helper.rb')
9
+
10
+ require 'ruote'
11
+ require 'ruote/storage/composite_storage'
12
+
13
+
14
+ class UtCompositeStorageTest < Test::Unit::TestCase
15
+
16
+ def setup
17
+
18
+ @msgs = Ruote::HashStorage.new({})
19
+ @default = Ruote::HashStorage.new({})
20
+ @cs = Ruote::CompositeStorage.new(@default, 'msgs' => @msgs)
21
+ end
22
+
23
+ def test_initial
24
+
25
+ @cs.put('action' => 'terminate', 'type' => 'msgs', '_id' => 'xxx')
26
+ @cs.put_msg('terminate', 'type' => 'msgs')
27
+ @cs.put_schedule('at', {}, Time.now + 10, 'action' => 'reply')
28
+
29
+ assert_equal 0, @default.h['msgs'].size
30
+ assert_equal 1, @default.h['schedules'].size
31
+ assert_equal 2, @cs.get_msgs.size
32
+ assert_equal 2, @msgs.get_msgs.size
33
+ assert_equal 0, @msgs.h['schedules'].size
34
+ end
35
+
36
+ def test_delete
37
+
38
+ @cs.put('action' => 'terminate', 'type' => 'msgs', '_id' => 'xxx')
39
+
40
+ msg = @cs.get_many('msgs').first
41
+
42
+ r = @cs.delete(msg)
43
+
44
+ assert_nil r
45
+ assert_equal 0, @default.h['msgs'].size
46
+ end
47
+
48
+ class TracingStorage
49
+ attr_reader :trace
50
+ def initialize
51
+ @trace = []
52
+ end
53
+ def method_missing(m, *args)
54
+ @trace << [ m, *args ]
55
+ end
56
+ end
57
+
58
+ def test_special_methods
59
+
60
+ default = TracingStorage.new
61
+
62
+ cs = Ruote::CompositeStorage.new(default, {})
63
+
64
+ cs.delete_schedule('x') # schedule id
65
+ cs.reserve('type' => 'schedules', '_id' => 'nada')
66
+
67
+ assert_equal([
68
+ [ :delete_schedule, 'x' ],
69
+ [ :reserve, { 'type' => 'schedules', '_id' => 'nada' } ]
70
+ ],
71
+ default.trace)
72
+ end
73
+ end
74
+
@@ -0,0 +1,46 @@
1
+
2
+ #
3
+ # testing ruote
4
+ #
5
+ # Wed Jan 26 09:21:06 JST 2011
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), %w[ .. test_helper.rb ])
9
+
10
+ require 'ostruct'
11
+ require 'ruote'
12
+ require 'ruote/svc/participant_list'
13
+
14
+
15
+ class UtSvcParticipantListTest < Test::Unit::TestCase
16
+
17
+ class FakeStorage
18
+ def initialize
19
+ @count = -1
20
+ end
21
+ def put(doc)
22
+ @count = @count + 1
23
+ return true if @count == 0
24
+ nil
25
+ end
26
+ def get_configuration(whatever)
27
+ { 'list' => [] }
28
+ end
29
+ end
30
+
31
+ # Fighting issue #20 found by 'sandbox'
32
+ #
33
+ # https://github.com/jmettraux/ruote/issues#issue/20
34
+ #
35
+ def test_register_participant_fail_and_retry
36
+
37
+ con = OpenStruct.new(:storage => FakeStorage.new)
38
+
39
+ pl = Ruote::ParticipantList.new(con)
40
+
41
+ pl.register('toto', Ruote::NullParticipant, { :hello => :world }, nil)
42
+
43
+ assert true
44
+ end
45
+ end
46
+
@@ -0,0 +1,1094 @@
1
+
2
+ #
3
+ # testing ruote
4
+ #
5
+ # Sun Jan 30 21:08:14 JST 2011
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), '..', 'test_helper.rb')
9
+
10
+ require_json
11
+ require 'rufus/json'
12
+ require 'ruote/util/filter'
13
+
14
+
15
+ class UtFilterTest < Test::Unit::TestCase
16
+
17
+ def test_missing_field
18
+
19
+ assert_raise ArgumentError do
20
+ Ruote.filter([ { 'type' => 'string' } ], {})
21
+ end
22
+ end
23
+
24
+ def test_not_a_filter
25
+
26
+ assert_raise ArgumentError do
27
+ Ruote.filter('nada', {})
28
+ end
29
+ end
30
+
31
+ #
32
+ # transformations
33
+
34
+ def assert_filter(result, filter, hash)
35
+
36
+ assert_equal(result, Ruote.filter(Rufus::Json.dup(filter), hash))
37
+ end
38
+
39
+ def test_remove
40
+
41
+ assert_filter(
42
+ {},
43
+ [ { 'field' => 'x', 'remove' => true } ],
44
+ { 'x' => 'y' })
45
+ assert_filter(
46
+ {},
47
+ [ { 'field' => '/.+/', 'remove' => true } ],
48
+ { 'x' => 'y', 'z' => 'a' })
49
+
50
+ assert_filter(
51
+ { 'x' => {} },
52
+ [ { 'field' => 'x.y', 'remove' => true } ],
53
+ { 'x' => { 'y' => 'z' } })
54
+ end
55
+
56
+ def test_default
57
+
58
+ assert_filter(
59
+ { 'x' => 1 },
60
+ [ { 'field' => 'x', 'default' => 1 } ],
61
+ {})
62
+ assert_filter(
63
+ { 'x' => 1 },
64
+ [ { 'field' => 'x', 'default' => 1 } ],
65
+ { 'x' => nil })
66
+
67
+ assert_filter(
68
+ {},
69
+ [ { 'field' => '/.+/', 'default' => 1 } ],
70
+ {})
71
+ assert_filter(
72
+ { 'x' => 1 },
73
+ [ { 'field' => '/.+/', 'default' => 1 } ],
74
+ { 'x' => nil })
75
+
76
+ assert_filter(
77
+ { 'x' => 2 },
78
+ [ { 'field' => 'x', 'default' => 1 } ],
79
+ { 'x' => 2 })
80
+
81
+ assert_filter(
82
+ { 'x' => { 'y' => 1 } },
83
+ [ { 'field' => 'x.y', 'default' => 1 } ],
84
+ { 'x' => {} })
85
+
86
+ assert_filter(
87
+ { 'x' => { 'y' => 2 } },
88
+ [ { 'field' => 'x.y', 'default' => 1 } ],
89
+ { 'x' => { 'y' => 2 } })
90
+
91
+ assert_filter(
92
+ { 'x' => { 'y' => 1 } },
93
+ [ { 'field' => 'x', 'default' => {} },
94
+ { 'field' => 'x.y', 'default' => 1 } ],
95
+ {})
96
+ end
97
+
98
+ def test_or
99
+
100
+ assert_filter(
101
+ { 'x' => 'y' },
102
+ [ { 'field' => 'x', 'type' => 'string', 'or' => 'z' } ],
103
+ { 'x' => 'y' })
104
+ assert_filter(
105
+ { 'x' => 'z' },
106
+ [ { 'field' => 'x', 'type' => 'string', 'or' => 'z' } ],
107
+ { 'x' => 2 })
108
+
109
+ assert_filter(
110
+ { 'x' => 'z' },
111
+ [ { 'field' => '/.+/', 'type' => 'string', 'or' => 'z' } ],
112
+ { 'x' => 2 })
113
+ end
114
+
115
+ def test_nil_or
116
+
117
+ assert_filter(
118
+ { 'x' => 'y' },
119
+ [ { 'field' => 'x', 'or' => 'z' } ],
120
+ { 'x' => 'y' })
121
+ assert_filter(
122
+ { 'x' => 'z' },
123
+ [ { 'field' => 'x', 'or' => 'z' } ],
124
+ {})
125
+ assert_filter(
126
+ { 'x' => 'z' },
127
+ [ { 'field' => 'x', 'or' => 'z' } ],
128
+ { 'x' => nil })
129
+
130
+ assert_filter(
131
+ {},
132
+ [ { 'field' => '/.+/', 'or' => 'z' } ],
133
+ {})
134
+ assert_filter(
135
+ { 'x' => 'z' },
136
+ [ { 'field' => '/.+/', 'or' => 'z' } ],
137
+ { 'x' => nil })
138
+ end
139
+
140
+ def test_and
141
+
142
+ assert_filter(
143
+ { 'x' => 'z' },
144
+ [ { 'field' => 'x', 'type' => 'string', 'and' => 'z' } ],
145
+ { 'x' => 'y' })
146
+ assert_filter(
147
+ { 'x' => 1 },
148
+ [ { 'field' => 'x', 'type' => 'string', 'and' => 'z' } ],
149
+ { 'x' => 1 })
150
+
151
+ assert_filter(
152
+ { 'x' => 1, 'z' => 1 },
153
+ [ { 'field' => '/.+/', 'type' => 'string', 'and' => 1 } ],
154
+ { 'x' => 'y', 'z' => 'a' })
155
+ assert_filter(
156
+ { 'x' => 1, 'z' => 2 },
157
+ [ { 'field' => '/.+/', 'type' => 'string', 'and' => 1 } ],
158
+ { 'x' => 'y', 'z' => 2 })
159
+ end
160
+
161
+ def test_set
162
+
163
+ assert_filter(
164
+ { 'x' => 'z' },
165
+ [ { 'field' => 'x', 'set' => 'z' } ],
166
+ { 'x' => 'y' })
167
+ assert_filter(
168
+ { 'x' => 'z' },
169
+ [ { 'field' => 'x', 'set' => 'z' } ],
170
+ {})
171
+
172
+ assert_filter(
173
+ { 'x' => 'z' },
174
+ [ { 'field' => '/.+/', 'set' => 'z' } ],
175
+ { 'x' => 'y' })
176
+ assert_filter(
177
+ {},
178
+ [ { 'field' => '/.+/', 'set' => 'z' } ],
179
+ {})
180
+ end
181
+
182
+ def test_set_multiple_fields
183
+
184
+ assert_filter(
185
+ { 'x' => 'A', 'y' => 'A', 'z' => 'A' },
186
+ [ { 'field' => 'x,y,z', 'set' => 'A' } ],
187
+ {})
188
+ assert_filter(
189
+ { 'x' => 'A', 'y' => 'A', 'z' => 'A' },
190
+ [ { 'fields' => %w[ x y z ], 'set' => 'A' } ],
191
+ {})
192
+ end
193
+
194
+ def test_copy
195
+
196
+ assert_filter(
197
+ { 'x' => 'y', 'z' => 'y' },
198
+ [ { 'field' => 'x', 'copy_to' => 'z' } ],
199
+ { 'x' => 'y' })
200
+ assert_filter(
201
+ { 'x' => 'y', 'z' => 'y' },
202
+ [ { 'field' => 'z', 'copy_from' => 'x' } ],
203
+ { 'x' => 'y' })
204
+ end
205
+
206
+ def test_copy_and_regex
207
+
208
+ assert_filter(
209
+ { 'a' => %w[ x y ], 'b0' => 'x', 'b1' => 'y' },
210
+ [ { 'field' => '/a\.(.+)/', 'copy_to' => 'b\1' } ],
211
+ { 'a' => %w[ x y ]})
212
+ assert_filter(
213
+ { 'a' => %w[ x y ], 'b0' => 'x', 'b1' => 'y' },
214
+ [ { 'field' => '/a!(.+)/', 'copy_to' => 'b\1' } ],
215
+ { 'a' => %w[ x y ]})
216
+
217
+ assert_filter(
218
+ { 'a' => 7, 'c' => 7, 'source' => [ 7 ] },
219
+ [ { 'field' => '/^.$/', 'copy_from' => 'source.0' } ],
220
+ { 'a' => 'b', 'c' => 'd', 'source' => [ 7 ] })
221
+ end
222
+
223
+ def test_move
224
+
225
+ assert_filter(
226
+ { 'z' => 'y' },
227
+ [ { 'field' => 'x', 'move_to' => 'z' } ],
228
+ { 'x' => 'y' })
229
+ assert_filter(
230
+ { 'z' => 'y' },
231
+ [ { 'field' => 'z', 'move_from' => 'x' } ],
232
+ { 'x' => 'y' })
233
+ end
234
+
235
+ def test_move_and_regex
236
+
237
+ assert_filter(
238
+ { 'Z' => 'a' },
239
+ [ { 'field' => '/.+/', 'move_to' => 'Z' } ],
240
+ { 'x' => 'y', 'z' => 'a' })
241
+ assert_filter(
242
+ { 'prefix_x' => 'y', 'prefix_z' => 'a' },
243
+ [ { 'field' => '/(.+)/', 'move_to' => 'prefix_\1' } ],
244
+ { 'x' => 'y', 'z' => 'a' })
245
+
246
+ assert_filter(
247
+ { 'h0' => {}, 'h1' => { 'a' => 'b', 'c' => 'd' } },
248
+ [ { 'field' => '/^h0!(.+)/', 'move_to' => 'h1.\1' } ],
249
+ { 'h0' => { 'a' => 'b', 'c' => 'd' }, 'h1' => {} })
250
+ end
251
+
252
+ def test_merge_from
253
+
254
+ assert_filter(
255
+ { 'x' => { 'a' => 'A', 'b' => 2, 'c' => 'C' }, 'y' => { 'a' => 'A', 'c' => 'C' } },
256
+ [ { 'field' => 'x', 'merge_from' => 'y' } ],
257
+ { 'x' => { 'a' => 1, 'b' => 2 }, 'y' => { 'a' => 'A', 'c' => 'C' } })
258
+ end
259
+
260
+ def test_merge_to
261
+
262
+ assert_filter(
263
+ { 'x' => { 'a' => 1, 'b' => 2 }, 'y' => { 'a' => 1, 'b' => 2, 'c' => 'C' } },
264
+ [ { 'field' => 'x', 'merge_to' => 'y' } ],
265
+ { 'x' => { 'a' => 1, 'b' => 2 }, 'y' => { 'a' => 'A', 'c' => 'C' } })
266
+
267
+ assert_filter(
268
+ { 'x' => { 'a' => 1, 'b' => 2 }, 'y' => 2 },
269
+ [ { 'field' => 'x', 'mg_to' => 'y' } ],
270
+ { 'x' => { 'a' => 1, 'b' => 2 }, 'y' => 2 })
271
+ end
272
+
273
+ def test_merge_to__non_hash
274
+
275
+ assert_filter(
276
+ { 'x' => { 'a' => 1, 'y' => 2 }, 'y' => 2 },
277
+ [ { 'field' => 'y', 'mg_to' => 'x' } ],
278
+ { 'x' => { 'a' => 1 }, 'y' => 2 })
279
+
280
+ assert_filter(
281
+ { 'x' => { 'a' => 1 }, 'y' => 2 },
282
+ [ { 'field' => 'x', 'mg_to' => 'y' } ],
283
+ { 'x' => { 'a' => 1 }, 'y' => 2 })
284
+ end
285
+
286
+ def test_merge_to__array # push too
287
+
288
+ assert_filter(
289
+ { 'x' => [ 'a', 'b', 2 ], 'y' => 2 },
290
+ [ { 'field' => 'y', 'mg_to' => 'x' } ],
291
+ { 'x' => [ 'a', 'b' ], 'y' => 2 })
292
+ assert_filter(
293
+ { 'x' => [ 'a', 'b', 2 ], 'y' => 2 },
294
+ [ { 'field' => 'y', 'push_to' => 'x' } ],
295
+ { 'x' => [ 'a', 'b' ], 'y' => 2 })
296
+ assert_filter(
297
+ { 'x' => [ 'a', 'b', 2 ], 'y' => 2 },
298
+ [ { 'field' => 'y', 'pu_to' => 'x' } ],
299
+ { 'x' => [ 'a', 'b' ], 'y' => 2 })
300
+ end
301
+
302
+ def test_merge_from__non_hash
303
+
304
+ assert_filter(
305
+ { 'x' => { 'a' => 1, 'y' => 2 }, 'y' => 2 },
306
+ [ { 'field' => 'x', 'merge_from' => 'y' } ],
307
+ { 'x' => { 'a' => 1 }, 'y' => 2 })
308
+ end
309
+
310
+ def test_merge_from__array
311
+
312
+ assert_filter(
313
+ { 'x' => [ 'a', 'b', 2 ], 'y' => 2 },
314
+ [ { 'field' => 'x', 'merge_from' => 'y' } ],
315
+ { 'x' => [ 'a', 'b' ], 'y' => 2 })
316
+ assert_filter(
317
+ { 'x' => [ 'a', 'b', 2 ], 'y' => 2 },
318
+ [ { 'field' => 'x', 'push_from' => 'y' } ],
319
+ { 'x' => [ 'a', 'b' ], 'y' => 2 })
320
+ assert_filter(
321
+ { 'x' => [ 'a', 'b', 2 ], 'y' => 2 },
322
+ [ { 'field' => 'x', 'pu_from' => 'y' } ],
323
+ { 'x' => [ 'a', 'b' ], 'y' => 2 })
324
+ end
325
+
326
+ def test_merge_dot
327
+
328
+ assert_filter(
329
+ { 'x' => { 'a' => 1, 'b' => 2 }, 'a' => 1, 'b' => 2 },
330
+ [ { 'field' => 'x', 'merge_to' => '.' } ],
331
+ { 'x' => { 'a' => 1, 'b' => 2 } })
332
+
333
+ assert_filter(
334
+ { 'x' => { 'a' => 1, 'b' => 2, 'x' => { 'a' => 1, 'b' => 2 } } },
335
+ [ { 'field' => 'x', 'merge_from' => '.' } ],
336
+ { 'x' => { 'a' => 1, 'b' => 2 } })
337
+ end
338
+
339
+ def test_migrate
340
+
341
+ assert_filter(
342
+ { 'x' => { 'a' => 'A', 'b' => 2, 'c' => 'C' } },
343
+ [ { 'field' => 'x', 'migrate_from' => 'y' } ],
344
+ { 'x' => { 'a' => 1, 'b' => 2 }, 'y' => { 'a' => 'A', 'c' => 'C' } })
345
+
346
+ assert_filter(
347
+ { 'y' => { 'a' => 1, 'b' => 2, 'c' => 'C' } },
348
+ [ { 'field' => 'x', 'migrate_to' => 'y' } ],
349
+ { 'x' => { 'a' => 1, 'b' => 2 }, 'y' => { 'a' => 'A', 'c' => 'C' } })
350
+
351
+ assert_filter(
352
+ { 'x' => { 'a' => 1, 'b' => 2 }, 'y' => 2 },
353
+ [ { 'field' => 'x', 'migrate_to' => 'y' } ],
354
+ { 'x' => { 'a' => 1, 'b' => 2 }, 'y' => 2 })
355
+ end
356
+
357
+ def test_migrate_dot
358
+
359
+ assert_filter(
360
+ { 'a' => 1, 'b' => 2 },
361
+ [ { 'field' => 'x', 'mi_to' => '.' } ],
362
+ { 'x' => { 'a' => 1, 'b' => 2 } })
363
+
364
+ assert_filter(
365
+ { 'x' => { 'a' => 1, 'b' => 2, 'x' => { 'a' => 1, 'b' => 2 } } },
366
+ [ { 'field' => 'x', 'mi_from' => '.' } ],
367
+ { 'x' => { 'a' => 1, 'b' => 2 } })
368
+ end
369
+
370
+ def test_tilde
371
+
372
+ assert_filter(
373
+ { 'x' => 'a', 'y' => 'a' },
374
+ [ { 'field' => 'x', 'set' => 'b' },
375
+ { 'field' => 'x', 'copy_from' => '~.x' },
376
+ { 'field' => 'y', 'copy_from' => '~.x' } ],
377
+ { 'x' => 'a' })
378
+ end
379
+
380
+ def test_tilde_restoring
381
+
382
+ assert_filter(
383
+ { 'private_x' => 'a' },
384
+ [ { 'field' => '/^private_/', 'del' => true },
385
+ { 'field' => '/^~~.private_/', 'merge_to' => '.' } ],
386
+ { 'private_x' => 'a' })
387
+ end
388
+
389
+ def test_restore
390
+
391
+ assert_filter(
392
+ { 'x' => 'a', 'y' => 'a' },
393
+ [ { 'field' => 'x', 'set' => 'X' },
394
+ { 'field' => 'y', 'set' => 'Y' },
395
+ { 'field' => '/^.$/', 'restore' => true } ],
396
+ { 'x' => 'a', 'y' => 'a' })
397
+ end
398
+
399
+ def test_restore_with_a_given_prefix
400
+
401
+ assert_filter(
402
+ { 'x' => 'a', 'y' => 'a' },
403
+ [ { 'field' => 'A', 'set' => {} },
404
+ { 'field' => '.', 'merge_to' => 'A' },
405
+ { 'field' => 'x', 'set' => 'X' },
406
+ { 'field' => 'y', 'set' => 'Y' },
407
+ { 'field' => '/^[a-z]$/', 'restore_from' => 'A' },
408
+ { 'field' => 'A', 'delete' => true } ],
409
+ { 'x' => 'a', 'y' => 'a' })
410
+ end
411
+
412
+ def test_really_restore
413
+
414
+ assert_filter(
415
+ { 'x' => 'a', 'y' => 'a', 'z' => 'good' },
416
+ [ { 'field' => '/./', 'del' => true },
417
+ { 'field' => 'y', 'set' => 'bad' },
418
+ { 'field' => 'z', 'set' => 'good' },
419
+ { 'field' => '/./', 'restore' => true } ],
420
+ { 'x' => 'a', 'y' => 'a' })
421
+ end
422
+
423
+ def test_cumulation_or
424
+
425
+ assert_filter(
426
+ { 'x' => { 'a' => 2 } },
427
+ [ { 'field' => 'x', 't' => 'hash', 'has' => 'a', 'or' => { 'a' => 2 } } ],
428
+ { 'x' => %w[ a b c ] })
429
+ end
430
+
431
+ #
432
+ # validations
433
+
434
+ def assert_valid(filter, hash)
435
+
436
+ begin
437
+ Ruote.filter(Rufus::Json.dup(filter), hash)
438
+ assert true
439
+ rescue Ruote::ValidationError => ve
440
+ puts
441
+ puts
442
+ p ve
443
+ p ve.deviations
444
+ puts
445
+ puts 'hash:'
446
+ p hash
447
+ puts 'filter:'
448
+ filter.each { |l| p l }
449
+ puts
450
+ assert false
451
+ end
452
+ end
453
+
454
+ def assert_not_valid(filter, hash, opts={ :deviations => 1 })
455
+
456
+ error = nil
457
+
458
+ begin
459
+ Ruote.filter(Rufus::Json.dup(filter), hash)
460
+ rescue => error
461
+ end
462
+
463
+ p error.deviations if opts[:verbose]
464
+
465
+ assert_not_nil(
466
+ error, "ValidationError was not raised")
467
+ assert_equal(
468
+ opts[:deviations], error.deviations.size, "deviation count doesn't match")
469
+
470
+ @deviations = error.deviations
471
+ end
472
+
473
+ def test_presence
474
+
475
+ assert_valid(
476
+ [ { 'field' => 'x' } ], { 'x' => 'deux' })
477
+ assert_not_valid(
478
+ [ { 'field' => 'x' } ], { 'y' => 'trois' })
479
+
480
+ assert_valid(
481
+ [ { 'field' => '/x|y/' } ], { 'x' => 'deux' })
482
+ assert_not_valid(
483
+ [ { 'field' => '/x|y/' } ], { 'z' => 'quatre' })
484
+ end
485
+
486
+ def test_this_or_that_field
487
+
488
+ assert_valid(
489
+ [ { 'field' => 'x|y', 'type' => 'string' } ], { 'x' => 'deux' })
490
+ assert_valid(
491
+ [ { 'field' => 'x|y', 'type' => 'string' } ], { 'y' => 'trois' })
492
+
493
+
494
+ assert_valid(
495
+ [ { 'field' => 'x|y'} ], { 'x' => 'deux' })
496
+
497
+ assert_not_valid(
498
+ [ { 'field' => 'x|y', 'type' => 'string' } ], { 'z' => 'quatre' })
499
+ assert_not_valid(
500
+ [ { 'field' => 'x|y' } ], { 'z' => 'quatre' })
501
+ end
502
+
503
+ def test_type
504
+
505
+ assert_valid(
506
+ [ { 'field' => 'x', 'type' => 'string' } ], { 'x' => 'deux' })
507
+ assert_valid(
508
+ [ { 'field' => 'x', 'type' => 'number' } ], { 'x' => 1 })
509
+ assert_valid(
510
+ [ { 'field' => 'x', 'type' => 'number' } ], { 'x' => 1.0 })
511
+ assert_valid(
512
+ [ { 'field' => 'x', 'type' => 'object' } ], { 'x' => {} })
513
+ assert_valid(
514
+ [ { 'field' => 'x', 'type' => 'hash' } ], { 'x' => {} })
515
+ assert_valid(
516
+ [ { 'field' => 'x', 'type' => 'array' } ], { 'x' => [] })
517
+ assert_valid(
518
+ [ { 'field' => 'x', 'type' => 'boolean' } ], { 'x' => true })
519
+ assert_valid(
520
+ [ { 'field' => 'x', 'type' => 'boolean' } ], { 'x' => false })
521
+ assert_valid(
522
+ [ { 'field' => 'x', 'type' => 'bool' } ], { 'x' => true })
523
+ assert_valid(
524
+ [ { 'field' => 'x', 'type' => 'bool' } ], { 'x' => false })
525
+ assert_valid(
526
+ [ { 'field' => 'x', 'type' => 'null' } ], { 'x' => nil })
527
+ assert_valid(
528
+ [ { 'field' => 'x', 'type' => 'null' } ], {})
529
+ assert_valid(
530
+ [ { 'field' => 'x', 'type' => 'nil' } ], {})
531
+
532
+ assert_not_valid(
533
+ [ { 'field' => 'x', 'type' => 'string' } ], { 'x' => 2 })
534
+ assert_not_valid(
535
+ [ { 'field' => 'x', 'type' => 'number' } ], { 'x' => 'one' })
536
+ assert_not_valid(
537
+ [ { 'field' => 'x', 'type' => 'number' } ], { 'x' => '1.0' })
538
+ assert_not_valid(
539
+ [ { 'field' => 'x', 'type' => 'object' } ], { 'x' => [] })
540
+ assert_not_valid(
541
+ [ { 'field' => 'x', 'type' => 'hash' } ], { 'x' => [] })
542
+ assert_not_valid(
543
+ [ { 'field' => 'x', 'type' => 'array' } ], { 'x' => {} })
544
+ assert_not_valid(
545
+ [ { 'field' => 'x', 'type' => 'boolean' } ], { 'x' => 'true' })
546
+ assert_not_valid(
547
+ [ { 'field' => 'x', 'type' => 'boolean' } ], { 'x' => 'false' })
548
+ assert_not_valid(
549
+ [ { 'field' => 'x', 'type' => 'bool' } ], { 'x' => 'true' })
550
+ assert_not_valid(
551
+ [ { 'field' => 'x', 'type' => 'bool' } ], { 'x' => 'true' })
552
+ assert_not_valid(
553
+ [ { 'field' => 'x', 'type' => 'null' } ], { 'x' => false })
554
+ assert_not_valid(
555
+ [ { 'field' => 'x', 'type' => 'null' } ], { 'x' => 1 })
556
+
557
+ assert_not_valid(
558
+ [ { 'field' => 'x', 'type' => 'string' } ], {})
559
+ end
560
+
561
+ def test_type_deep
562
+
563
+ assert_valid(
564
+ [ { 'field' => 'x.y', 'type' => 'string' } ], { 'x' => { 'y' => 'z' } })
565
+
566
+ assert_not_valid(
567
+ [ { 'field' => 'x.y', 'type' => 'string' } ], { 'x' => { 'y' => 1 } })
568
+ end
569
+
570
+ def test_type_union
571
+
572
+ assert_valid(
573
+ [ { 'field' => 'x', 'type' => 'string,number' } ], { 'x' => 'a' })
574
+ assert_valid(
575
+ [ { 'field' => 'x', 'type' => 'string,number' } ], { 'x' => 1 })
576
+ assert_valid(
577
+ [ { 'field' => 'x', 'type' => [ 'string', 'number' ] } ], { 'x' => 'a' })
578
+ assert_valid(
579
+ [ { 'field' => 'x', 'type' => [ 'string', 'number' ] } ], { 'x' => 1 })
580
+ end
581
+
582
+ def test_type_and_null
583
+
584
+ assert_valid(
585
+ [ { 'field' => 'x', 'type' => 'string,null' } ], {})
586
+ assert_valid(
587
+ [ { 'field' => 'x', 'type' => 'string,null' } ], { 'x' => nil })
588
+ assert_valid(
589
+ [ { 'field' => 'x', 'type' => 'string,null' } ], { 'x' => 'x' })
590
+ end
591
+
592
+ def test_type_array
593
+
594
+ assert_valid(
595
+ [ { 'field' => 'x', 'type' => 'array<string>' } ],
596
+ { 'x' => %w[ a b c ] })
597
+ assert_valid(
598
+ [ { 'field' => 'x', 'type' => 'array<string,number>' } ],
599
+ { 'x' => [ 'a', 1 ] })
600
+
601
+ assert_not_valid(
602
+ [ { 'field' => 'x', 'type' => 'array<string>' } ],
603
+ { 'x' => [ 'a', 1 ] })
604
+ assert_not_valid(
605
+ [ { 'field' => 'x', 'type' => 'array<string,number>' } ],
606
+ { 'x' => [ 'a', true ] })
607
+ end
608
+
609
+ def test_type_array_deep
610
+
611
+ assert_valid(
612
+ [ { 'field' => 'x', 'type' => 'array<array<string>>' } ],
613
+ { 'x' => [ %w[ a b ], %w[ c d ] ] })
614
+
615
+ assert_not_valid(
616
+ [ { 'field' => 'x', 'type' => 'array<array<string>>' } ],
617
+ { 'x' => [ %w[ a b ], [ 2, 3 ] ] })
618
+ end
619
+
620
+ def test_type_hash
621
+
622
+ assert_valid(
623
+ [ { 'field' => 'x', 'type' => 'hash<string>' } ],
624
+ { 'x' => { 'a' => 'b', 'c' => 'd' } })
625
+ assert_valid(
626
+ [ { 'field' => 'x', 'type' => 'hash<string,number>' } ],
627
+ { 'x' => { 'a' => 'b', 'c' => 0 } })
628
+
629
+ assert_not_valid(
630
+ [ { 'field' => 'x', 'type' => 'hash<string>' } ],
631
+ { 'x' => { 'a' => 'b', 'c' => 0 } })
632
+ assert_not_valid(
633
+ [ { 'field' => 'x', 'type' => 'hash<string,number>' } ],
634
+ { 'x' => { 'a' => 'b', 'c' => true } })
635
+ end
636
+
637
+ def test_type_and_regex
638
+
639
+ assert_valid(
640
+ [ { 'field' => '/./', 'type' => 'string' } ],
641
+ { 'x' => 'y', 'z' => 'a' })
642
+
643
+ assert_not_valid(
644
+ [ { 'field' => '/./', 'type' => 'string' } ],
645
+ { 'x' => 'y', 'z' => 1 })
646
+
647
+ assert_not_valid(
648
+ [ { 'field' => '/./', 'type' => 'string' } ],
649
+ { 'x' => 1, 'z' => 1 },
650
+ :deviations => 2)
651
+ end
652
+
653
+ def test_match
654
+
655
+ assert_valid(
656
+ [ { 'field' => 'x', 'match' => 'to' } ],
657
+ { 'x' => 'toto' })
658
+ assert_valid(
659
+ [ { 'field' => 'x', 'match' => '1' } ],
660
+ { 'x' => 1.0 })
661
+
662
+ assert_not_valid(
663
+ [ { 'field' => 'x', 'match' => 'to' } ],
664
+ { 'x' => 'tutu' })
665
+ assert_not_valid(
666
+ [ { 'field' => 'x', 'match' => '1' } ],
667
+ { 'x' => 2.0 })
668
+ end
669
+
670
+ def test_smatch
671
+
672
+ assert_valid(
673
+ [ { 'field' => 'x', 'smatch' => 'to' } ],
674
+ { 'x' => 'toto' })
675
+ assert_not_valid(
676
+ [ { 'field' => 'x', 'smatch' => '1' } ],
677
+ { 'x' => 1.0 })
678
+
679
+ assert_not_valid(
680
+ [ { 'field' => 'x', 'smatch' => 'to' } ],
681
+ { 'x' => 'tutu' })
682
+ assert_not_valid(
683
+ [ { 'field' => 'x', 'smatch' => '1' } ],
684
+ { 'x' => 2.0 })
685
+ end
686
+
687
+ def test_is
688
+
689
+ assert_valid(
690
+ [ { 'field' => 'x', 'is' => true } ],
691
+ { 'x' => true })
692
+ assert_valid(
693
+ [ { 'field' => 'x', 'is' => (1..3).to_a } ],
694
+ { 'x' => [ 1, 2, 3 ] })
695
+
696
+ assert_not_valid(
697
+ [ { 'field' => 'x', 'is' => false } ],
698
+ { 'x' => true })
699
+ assert_not_valid(
700
+ [ { 'field' => 'x', 'is' => true } ],
701
+ { 'x' => 1 })
702
+ assert_not_valid(
703
+ [ { 'field' => 'x', 'is' => (1..3).to_a } ],
704
+ { 'x' => [ 3, 2, 1 ] })
705
+ end
706
+
707
+ def test_size
708
+
709
+ assert_valid(
710
+ [ { 'field' => 'x', 'size' => 4 } ],
711
+ { 'x' => 'toto' })
712
+ assert_valid(
713
+ [ { 'field' => 'x', 'size' => '4' } ],
714
+ { 'x' => 'toto' })
715
+ assert_valid(
716
+ [ { 'field' => 'x', 'size' => 4 } ],
717
+ { 'x' => %w[ a b c d ] })
718
+ assert_valid(
719
+ [ { 'field' => 'x', 'size' => 2 } ],
720
+ { 'x' => { 'a' => 'b', 'c' => 'd' } })
721
+
722
+ assert_not_valid(
723
+ [ { 'field' => 'x', 'size' => 2 } ],
724
+ {})
725
+ assert_not_valid(
726
+ [ { 'field' => 'x', 'size' => 2 } ],
727
+ { 'x' => 3 })
728
+ end
729
+
730
+ def test_size_range
731
+
732
+ assert_valid(
733
+ [ { 'field' => 'x', 'size' => [ 2, 3 ] } ],
734
+ { 'x' => %w[ a b ] })
735
+ assert_valid(
736
+ [ { 'field' => 'x', 'size' => [ 2, 3 ] } ],
737
+ { 'x' => %w[ a b c ] })
738
+
739
+ assert_not_valid(
740
+ [ { 'field' => 'x', 'size' => [ 2, 3 ] } ],
741
+ { 'x' => %w[ a ] })
742
+ assert_not_valid(
743
+ [ { 'field' => 'x', 'size' => [ 2, 3 ] } ],
744
+ { 'x' => %w[ a b c d ] })
745
+ end
746
+
747
+ def test_size_open_range
748
+
749
+ assert_valid(
750
+ [ { 'field' => 'x', 'size' => [ 2 ] } ],
751
+ { 'x' => %w[ a b ] })
752
+ assert_valid(
753
+ [ { 'field' => 'x', 'size' => [ 2, nil ] } ],
754
+ { 'x' => %w[ a b ] })
755
+ assert_valid(
756
+ [ { 'field' => 'x', 'size' => ",3" } ],
757
+ { 'x' => %w[ a b c ] })
758
+
759
+ assert_not_valid(
760
+ [ { 'field' => 'x', 'size' => "2," } ],
761
+ { 'x' => %w[ a ] })
762
+ assert_not_valid(
763
+ [ { 'field' => 'x', 'size' => [ nil, 3 ] } ],
764
+ { 'x' => %w[ a b c d ] })
765
+ end
766
+
767
+ def test_empty
768
+
769
+ assert_valid(
770
+ [ { 'field' => 'x', 'empty' => true } ],
771
+ { 'x' => %w[] })
772
+ assert_valid(
773
+ [ { 'field' => 'x', 'empty' => true } ],
774
+ { 'x' => {} })
775
+ assert_valid(
776
+ [ { 'field' => 'x', 'empty' => true } ],
777
+ { 'x' => '' })
778
+ assert_valid(
779
+ [ { 'field' => 'x', 'empty' => false } ],
780
+ { 'x' => [ 1 ] })
781
+
782
+ assert_not_valid(
783
+ [ { 'field' => 'x', 'empty' => true } ],
784
+ { 'x' => 'deux' })
785
+ assert_not_valid(
786
+ [ { 'field' => 'x', 'empty' => true } ],
787
+ { 'x' => %w[ a b ] })
788
+ assert_not_valid(
789
+ [ { 'field' => 'x', 'empty' => true } ],
790
+ { 'x' => { 'a' => 'b' } })
791
+ assert_not_valid(
792
+ [ { 'field' => 'x', 'empty' => false } ],
793
+ { 'x' => {} })
794
+ end
795
+
796
+ def test_string_and_empty
797
+
798
+ assert_valid(
799
+ [ { 'field' => 'x', 'empty' => true, 'type' => 'string' } ],
800
+ { 'x' => '' })
801
+ assert_valid(
802
+ [ { 'field' => 'x', 'empty' => false, 'type' => 'string' } ],
803
+ { 'x' => 'yyy' })
804
+
805
+ assert_not_valid(
806
+ [ { 'field' => 'x', 'empty' => true, 'type' => 'string' } ],
807
+ { 'x' => {} })
808
+ end
809
+
810
+ def test_in
811
+
812
+ assert_valid(
813
+ [ { 'field' => 'x', 'in' => %w[ alpha bravo ] } ],
814
+ { 'x' => 'alpha' })
815
+ assert_valid(
816
+ [ { 'field' => 'x', 'in' => "alpha, bravo" } ],
817
+ { 'x' => 'alpha' })
818
+ assert_valid(
819
+ [ { 'field' => 'x', 'in' => [ true ] } ],
820
+ { 'x' => true })
821
+
822
+ assert_not_valid(
823
+ [ { 'field' => 'x', 'in' => %w[ alpha bravo ] } ],
824
+ { 'x' => 'charly' })
825
+ assert_not_valid(
826
+ [ { 'field' => 'x', 'in' => "alpha, bravo" } ],
827
+ { 'x' => 'charly' })
828
+ assert_not_valid(
829
+ [ { 'field' => 'x', 'in' => [ false ] } ],
830
+ { 'x' => true })
831
+ end
832
+
833
+ def test_has__keys
834
+
835
+ assert_valid(
836
+ [ { 'field' => '.', 'has' => 'x' } ],
837
+ { 'x' => 'alpha' })
838
+ assert_valid(
839
+ [ { 'field' => 'x', 'has' => 'a' } ],
840
+ { 'x' => { 'a' => 1 } })
841
+ assert_valid(
842
+ [ { 'field' => 'x', 'has' => 'a, b' } ],
843
+ { 'x' => { 'a' => 1, 'b' => 2 } })
844
+ assert_valid(
845
+ [ { 'field' => 'x', 'has' => %w[ a b ] } ],
846
+ { 'x' => { 'a' => 1, 'b' => 2 } })
847
+
848
+ assert_not_valid(
849
+ [ { 'field' => '.', 'has' => 'x' } ],
850
+ {})
851
+ assert_not_valid(
852
+ [ { 'field' => 'x', 'has' => 'b' } ],
853
+ { 'x' => { 'a' => 1 } })
854
+ assert_not_valid(
855
+ [ { 'field' => 'x', 'has' => 'a, c' } ],
856
+ { 'x' => { 'a' => 1, 'b' => 2 } })
857
+ assert_not_valid(
858
+ [ { 'field' => 'x', 'has' => %w[ a c ] } ],
859
+ { 'x' => { 'a' => 1, 'b' => 2 } })
860
+ end
861
+
862
+ def test_has__elts
863
+
864
+ assert_valid(
865
+ [ { 'field' => 'x', 'has' => 'a' } ],
866
+ { 'x' => %w[ a b c ] })
867
+ assert_valid(
868
+ [ { 'field' => 'x', 'has' => 'a, b' } ],
869
+ { 'x' => %w[ a b c ] })
870
+ assert_valid(
871
+ [ { 'field' => 'x', 'has' => %w[ a b ] } ],
872
+ { 'x' => %w[ a b c ] })
873
+
874
+ assert_not_valid(
875
+ [ { 'field' => 'x', 'has' => 'd' } ],
876
+ { 'x' => %w[ a b c ] })
877
+ assert_not_valid(
878
+ [ { 'field' => 'x', 'has' => 'a, d' } ],
879
+ { 'x' => %w[ a b c ] })
880
+ assert_not_valid(
881
+ [ { 'field' => 'x', 'has' => %w[ a d ] } ],
882
+ { 'x' => %w[ a b c ] })
883
+ end
884
+
885
+ def test_includes
886
+
887
+ assert_valid(
888
+ [ { 'field' => 'x', 'includes' => 'a' } ],
889
+ { 'x' => %w[ a b c ] })
890
+
891
+ assert_not_valid(
892
+ [ { 'field' => 'x', 'includes' => 'z' } ],
893
+ { 'x' => %w[ a b c ] })
894
+ assert_not_valid(
895
+ [ { 'field' => 'x', 'includes' => 'z' } ],
896
+ { 'x' => 'abcz' })
897
+ end
898
+
899
+ def test_includes__value
900
+
901
+ assert_valid(
902
+ [ { 'field' => 'x', 'includes' => 1 } ],
903
+ { 'x' => { 'a' => 1 } })
904
+
905
+ assert_not_valid(
906
+ [ { 'field' => 'x', 'includes' => '1' } ],
907
+ { 'x' => { 'a' => 1 } })
908
+ end
909
+
910
+ def test_valid
911
+
912
+ # 'valid' can be used in conjunction with the dollar notation
913
+ #
914
+ # field => 'x', 'valid' => '${v:accept}'
915
+
916
+ assert_valid(
917
+ [ { 'field' => 'x', 'valid' => true } ],
918
+ {})
919
+ assert_valid(
920
+ [ { 'field' => 'x', 'valid' => 'true' } ],
921
+ {})
922
+
923
+ assert_not_valid(
924
+ [ { 'field' => 'x', 'valid' => false } ],
925
+ {})
926
+ assert_not_valid(
927
+ [ { 'field' => 'x', 'valid' => 'false' } ],
928
+ {})
929
+ assert_not_valid(
930
+ [ { 'field' => 'x', 'valid' => 'nada' } ],
931
+ {})
932
+ end
933
+
934
+ def test_cumulation
935
+
936
+ assert_valid(
937
+ [ { 'field' => 'x', 't' => 'array', 'has' => 'a' } ],
938
+ { 'x' => %w[ a b c ] })
939
+
940
+ assert_not_valid(
941
+ [ { 'field' => 'x', 't' => 'hash', 'has' => 'a' } ],
942
+ { 'x' => %w[ a b c ] })
943
+ end
944
+
945
+ def test_multiple_validations
946
+
947
+ assert_not_valid(
948
+ [
949
+ { 'field' => 'x', 't' => 'array', 'has' => 1 },
950
+ { 'field' => 'y', 't' => 'string' }
951
+ ],
952
+ {
953
+ 'x' => %w[ a b c ],
954
+ 'y' => true
955
+ },
956
+ :deviations => 2)
957
+
958
+ assert_equal [
959
+ [ { "has" => 1, "field" => "x", "t" => "array"}, "x", [ "a", "b", "c" ] ],
960
+ [ { "field" => "y", "t" => "string" }, "y", true ]
961
+ ], @deviations
962
+ # not super happy with this @breaks thing
963
+ end
964
+
965
+ # when :no_raise => true and the validation fails, an array is returned
966
+ # listing the 'deviations'
967
+ #
968
+ def test_no_raise
969
+
970
+ r = Ruote.filter(
971
+ [ { 'field' => 'x', 't' => 'hash', 'has' => 'a' } ],
972
+ { 'x' => %w[ a b c ] },
973
+ :no_raise => true)
974
+
975
+ assert_equal(
976
+ [ [ { "has" => "a", "field" => "x", "t" => "hash"}, "x", [ "a", "b", "c" ] ] ],
977
+ r)
978
+ end
979
+
980
+ # Data passes through the filter untouched.
981
+ #
982
+ def test_no_raise_empty_result
983
+
984
+ r = Ruote.filter(
985
+ [ { 'field' => 'x', 't' => 'array', 'has' => 'a' } ],
986
+ { 'x' => %w[ a b c ] },
987
+ :no_raise => true)
988
+
989
+ assert_equal(
990
+ { 'x' => %w[ a b c ] },
991
+ r)
992
+ end
993
+
994
+ #
995
+ # flatten_keys tests
996
+
997
+ def test_flatten_keys
998
+
999
+ assert_equal(
1000
+ [
1001
+ 'a',
1002
+ 'c',
1003
+ 'c.d',
1004
+ 'c.f',
1005
+ 'c.f.l',
1006
+ 'c.f.n',
1007
+ 'c.f.n.0',
1008
+ 'c.f.n.1',
1009
+ 'c.f.n.2',
1010
+ 'c.g',
1011
+ 'c.g.0',
1012
+ 'c.g.0.i',
1013
+ 'c.g.1',
1014
+ 'h',
1015
+ 'h.0',
1016
+ 'h.1',
1017
+ 'h.2'
1018
+ ],
1019
+ Ruote.flatten_keys({
1020
+ 'a' => 'b',
1021
+ 'c' => {
1022
+ 'd' => 'e',
1023
+ 'f' => {
1024
+ 'l' => 'm',
1025
+ 'n' => [ 1, 2, 3 ]
1026
+ },
1027
+ 'g' => [
1028
+ { 'i' => 'j' },
1029
+ 'k'
1030
+ ]
1031
+ },
1032
+ 'h' => [
1033
+ 1, 2, 3
1034
+ ]
1035
+ }))
1036
+ end
1037
+
1038
+ def test_top_level_or
1039
+
1040
+ filter = [
1041
+ { 'field' => 'x', 'type' => 'string' },
1042
+ { 'field' => 'y', 'type' => 'number' },
1043
+ 'or',
1044
+ { 'field' => 'z', 'type' => 'bool' }
1045
+ ]
1046
+
1047
+ assert_valid(filter, { 'x' => 'whatever', 'y' => 1 })
1048
+ assert_valid(filter, { 'z' => true })
1049
+ assert_valid(filter, { 'x' => -9, 'z' => false })
1050
+ assert_valid(filter, { 'x' => 'ah', 'y' => 2 })
1051
+
1052
+ assert_not_valid(filter, { 'x' => 'whatever', 'y' => 'notanumber' })
1053
+ assert_not_valid(filter, { 'z' => 'notabool' })
1054
+ assert_not_valid(filter, { 'x' => 'x', 'z' => 'nada' })
1055
+
1056
+ filter = [
1057
+ [ { 'field' => 'x', 'type' => 'string' },
1058
+ { 'field' => 'y', 'type' => 'number' } ],
1059
+ [ { 'field' => 'z', 'type' => 'bool' } ]
1060
+ ]
1061
+
1062
+ assert_valid(filter, { 'x' => 'whatever', 'y' => 1 })
1063
+ assert_valid(filter, { 'z' => true })
1064
+ assert_valid(filter, { 'x' => -9, 'z' => false })
1065
+ assert_valid(filter, { 'x' => 'ah', 'y' => 2 })
1066
+ end
1067
+
1068
+ def test_top_level_or_and_pipe_field
1069
+
1070
+ filter = [
1071
+ { 'field' => 'x|params.x', 'type' => 'string' },
1072
+ 'or',
1073
+ { 'field' => 'y|params.y', 'type' => 'bool' }
1074
+ ]
1075
+
1076
+ assert_valid(filter, { 'x' => 'out' })
1077
+ assert_valid(filter, { 'params' => { 'x' => 'in' } })
1078
+ assert_valid(filter, { 'y' => true })
1079
+ assert_valid(filter, { 'params' => { 'y' => false } })
1080
+
1081
+ assert_not_valid(filter, { 'y' => 'out' })
1082
+ assert_not_valid(filter, { 'params' => { 'y' => 'in' } })
1083
+ assert_not_valid(filter, {})
1084
+ end
1085
+
1086
+ def test_misc_neutralization
1087
+
1088
+ assert_valid(
1089
+ [ { :field => 'x', :type => :string } ], { 'x' => 'deux' })
1090
+ assert_valid(
1091
+ [ { :field => 'x', 'smatch' => /de/ } ], { 'x' => 'deux' })
1092
+ end
1093
+ end
1094
+