ruote 2.1.11 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. data/CHANGELOG.txt +60 -0
  2. data/CREDITS.txt +22 -4
  3. data/LICENSE.txt +1 -1
  4. data/README.rdoc +6 -7
  5. data/Rakefile +58 -59
  6. data/TODO.txt +137 -65
  7. data/couch_url.txt +1 -0
  8. data/jruby_issue.txt +32 -0
  9. data/lib/ruote.rb +1 -1
  10. data/lib/ruote/context.rb +12 -10
  11. data/lib/ruote/engine.rb +280 -145
  12. data/lib/ruote/engine/process_error.rb +5 -5
  13. data/lib/ruote/engine/process_status.rb +47 -28
  14. data/lib/ruote/exp/command.rb +7 -10
  15. data/lib/ruote/exp/commanded.rb +2 -2
  16. data/lib/ruote/exp/condition.rb +130 -43
  17. data/lib/ruote/exp/fe_add_branches.rb +2 -2
  18. data/lib/ruote/exp/fe_apply.rb +1 -1
  19. data/lib/ruote/exp/fe_cancel_process.rb +3 -3
  20. data/lib/ruote/exp/fe_command.rb +3 -3
  21. data/lib/ruote/exp/fe_concurrence.rb +4 -4
  22. data/lib/ruote/exp/fe_concurrent_iterator.rb +17 -5
  23. data/lib/ruote/exp/fe_cron.rb +3 -3
  24. data/lib/ruote/exp/fe_cursor.rb +5 -5
  25. data/lib/ruote/exp/fe_define.rb +3 -3
  26. data/lib/ruote/exp/fe_echo.rb +3 -3
  27. data/lib/ruote/exp/fe_equals.rb +2 -2
  28. data/lib/ruote/exp/fe_error.rb +2 -2
  29. data/lib/ruote/exp/fe_filter.rb +519 -0
  30. data/lib/ruote/exp/fe_forget.rb +9 -2
  31. data/lib/ruote/exp/fe_given.rb +154 -0
  32. data/lib/ruote/exp/fe_if.rb +16 -13
  33. data/lib/ruote/exp/fe_inc.rb +3 -3
  34. data/lib/ruote/exp/fe_iterator.rb +4 -4
  35. data/lib/ruote/exp/fe_let.rb +75 -0
  36. data/lib/ruote/exp/fe_listen.rb +68 -12
  37. data/lib/ruote/exp/fe_lose.rb +110 -0
  38. data/lib/ruote/exp/fe_noop.rb +1 -1
  39. data/lib/ruote/exp/{fe_when.rb → fe_once.rb} +25 -21
  40. data/lib/ruote/exp/fe_participant.rb +14 -17
  41. data/lib/ruote/exp/fe_redo.rb +10 -6
  42. data/lib/ruote/exp/fe_ref.rb +1 -1
  43. data/lib/ruote/exp/fe_registerp.rb +112 -0
  44. data/lib/ruote/exp/fe_reserve.rb +3 -3
  45. data/lib/ruote/exp/fe_restore.rb +2 -2
  46. data/lib/ruote/exp/fe_save.rb +2 -2
  47. data/lib/ruote/exp/fe_sequence.rb +3 -4
  48. data/lib/ruote/exp/fe_set.rb +16 -7
  49. data/lib/ruote/exp/fe_subprocess.rb +23 -1
  50. data/lib/ruote/exp/fe_that.rb +92 -0
  51. data/lib/ruote/exp/fe_undo.rb +3 -3
  52. data/lib/ruote/exp/fe_unregisterp.rb +71 -0
  53. data/lib/ruote/exp/fe_wait.rb +2 -2
  54. data/lib/ruote/exp/flowexpression.rb +153 -78
  55. data/lib/ruote/exp/iterator.rb +2 -2
  56. data/lib/ruote/exp/merge.rb +2 -2
  57. data/lib/ruote/exp/ro_attributes.rb +14 -12
  58. data/lib/ruote/exp/ro_filters.rb +136 -0
  59. data/lib/ruote/exp/ro_persist.rb +51 -35
  60. data/lib/ruote/exp/ro_variables.rb +18 -27
  61. data/lib/ruote/fei.rb +73 -33
  62. data/lib/ruote/id/mnemo_wfid_generator.rb +1 -1
  63. data/lib/ruote/id/wfid_generator.rb +11 -4
  64. data/lib/ruote/log/default_history.rb +122 -0
  65. data/lib/ruote/log/pretty.rb +36 -8
  66. data/lib/ruote/log/storage_history.rb +37 -5
  67. data/lib/ruote/log/test_logger.rb +26 -24
  68. data/lib/ruote/log/wait_logger.rb +5 -3
  69. data/lib/ruote/part/block_participant.rb +22 -11
  70. data/lib/ruote/part/engine_participant.rb +6 -7
  71. data/lib/ruote/part/local_participant.rb +6 -12
  72. data/lib/ruote/part/no_op_participant.rb +4 -4
  73. data/lib/ruote/part/null_participant.rb +4 -4
  74. data/lib/ruote/part/smtp_participant.rb +4 -4
  75. data/lib/ruote/part/storage_participant.rb +40 -20
  76. data/lib/ruote/part/template.rb +4 -4
  77. data/lib/ruote/participant.rb +0 -1
  78. data/lib/ruote/{parser.rb → reader.rb} +30 -25
  79. data/lib/ruote/{parser → reader}/ruby_dsl.rb +28 -11
  80. data/lib/ruote/{parser → reader}/xml.rb +6 -5
  81. data/lib/ruote/receiver/base.rb +35 -13
  82. data/lib/ruote/storage/base.rb +20 -18
  83. data/lib/ruote/storage/composite_storage.rb +10 -10
  84. data/lib/ruote/storage/fs_storage.rb +17 -10
  85. data/lib/ruote/storage/hash_storage.rb +29 -18
  86. data/lib/ruote/svc/dispatch_pool.rb +41 -14
  87. data/lib/ruote/svc/dollar_sub.rb +50 -17
  88. data/lib/ruote/svc/error_handler.rb +19 -11
  89. data/lib/ruote/svc/expression_map.rb +4 -4
  90. data/lib/ruote/svc/participant_list.rb +105 -100
  91. data/lib/ruote/svc/tracker.rb +58 -18
  92. data/lib/ruote/svc/treechecker.rb +51 -24
  93. data/lib/ruote/tree_dot.rb +4 -4
  94. data/lib/ruote/util/filter.rb +440 -0
  95. data/lib/ruote/util/hashdot.rb +4 -4
  96. data/lib/ruote/util/look.rb +2 -6
  97. data/lib/ruote/util/lookup.rb +9 -7
  98. data/lib/ruote/util/misc.rb +40 -8
  99. data/lib/ruote/util/ometa.rb +1 -1
  100. data/lib/ruote/util/serializer.rb +4 -4
  101. data/lib/ruote/util/subprocess.rb +29 -9
  102. data/lib/ruote/util/time.rb +4 -4
  103. data/lib/ruote/util/tree.rb +3 -3
  104. data/lib/ruote/version.rb +2 -2
  105. data/lib/ruote/worker.rb +55 -32
  106. data/lib/ruote/workitem.rb +64 -11
  107. data/ruote.gemspec +31 -302
  108. data/test/bm/launch_bench.rb +37 -0
  109. data/test/functional/base.rb +60 -18
  110. data/test/functional/concurrent_base.rb +2 -2
  111. data/test/functional/ct_0_concurrence.rb +1 -1
  112. data/test/functional/ct_1_iterator.rb +1 -1
  113. data/test/functional/ct_2_cancel.rb +1 -1
  114. data/test/functional/eft_0_process_definition.rb +2 -2
  115. data/test/functional/eft_10_cancel_process.rb +1 -1
  116. data/test/functional/eft_11_wait.rb +19 -11
  117. data/test/functional/eft_12_listen.rb +79 -13
  118. data/test/functional/eft_13_iterator.rb +13 -10
  119. data/test/functional/eft_14_cursor.rb +98 -9
  120. data/test/functional/eft_15_loop.rb +6 -4
  121. data/test/functional/eft_16_if.rb +12 -0
  122. data/test/functional/eft_18_concurrent_iterator.rb +31 -32
  123. data/test/functional/eft_19_reserve.rb +4 -4
  124. data/test/functional/eft_1_echo.rb +9 -0
  125. data/test/functional/eft_20_save.rb +4 -4
  126. data/test/functional/{eft_28_when.rb → eft_28_once.rb} +33 -7
  127. data/test/functional/eft_30_ref.rb +17 -2
  128. data/test/functional/eft_31_registerp.rb +130 -0
  129. data/test/functional/eft_32_lose.rb +93 -0
  130. data/test/functional/eft_33_let.rb +31 -0
  131. data/test/functional/eft_34_given.rb +123 -0
  132. data/test/functional/eft_35_filter.rb +269 -0
  133. data/test/functional/eft_3_participant.rb +4 -6
  134. data/test/functional/eft_4_set.rb +16 -2
  135. data/test/functional/eft_5_subprocess.rb +2 -4
  136. data/test/functional/eft_6_concurrence.rb +29 -29
  137. data/test/functional/eft_8_undo.rb +39 -3
  138. data/test/functional/eft_9_redo.rb +94 -2
  139. data/test/functional/ft_10_dollar.rb +81 -2
  140. data/test/functional/ft_11_recursion.rb +13 -17
  141. data/test/functional/ft_12_launchitem.rb +9 -5
  142. data/test/functional/ft_13_variables.rb +7 -9
  143. data/test/functional/ft_14_re_apply.rb +6 -9
  144. data/test/functional/ft_15_timeout.rb +18 -18
  145. data/test/functional/ft_16_participant_params.rb +1 -3
  146. data/test/functional/ft_17_conditional.rb +25 -2
  147. data/test/functional/ft_18_kill.rb +65 -12
  148. data/test/functional/ft_1_process_status.rb +147 -71
  149. data/test/functional/ft_20_storage_participant.rb +0 -1
  150. data/test/functional/ft_21_forget.rb +82 -1
  151. data/test/functional/{ft_24_block_participants.rb → ft_24_block_participant.rb} +42 -11
  152. data/test/functional/ft_25_receiver.rb +47 -17
  153. data/test/functional/{ft_26_participant_timeout.rb → ft_26_participant_rtimeout.rb} +56 -19
  154. data/test/functional/ft_29_part_template.rb +6 -5
  155. data/test/functional/ft_2_errors.rb +21 -37
  156. data/test/functional/ft_30_smtp_participant.rb +1 -1
  157. data/test/functional/ft_31_part_blocking.rb +8 -6
  158. data/test/functional/ft_34_cursor_rewind.rb +13 -10
  159. data/test/functional/ft_35_add_service.rb +1 -1
  160. data/test/functional/ft_36_storage_history.rb +24 -1
  161. data/test/functional/ft_37_default_history.rb +109 -0
  162. data/test/functional/ft_38_participant_more.rb +10 -10
  163. data/test/functional/ft_39_wait_for.rb +12 -9
  164. data/test/functional/ft_3_participant_registration.rb +111 -32
  165. data/test/functional/ft_40_wait_logger.rb +2 -1
  166. data/test/functional/ft_41_participants.rb +30 -4
  167. data/test/functional/ft_43_participant_on_reply.rb +6 -23
  168. data/test/functional/ft_45_participant_accept.rb +4 -4
  169. data/test/functional/ft_46_launch_single.rb +36 -2
  170. data/test/functional/ft_47_wfid_generator.rb +54 -0
  171. data/test/functional/ft_48_lose.rb +112 -0
  172. data/test/functional/ft_49_engine_on_error.rb +201 -0
  173. data/test/functional/ft_4_cancel.rb +66 -6
  174. data/test/functional/ft_50_engine_config.rb +22 -0
  175. data/test/functional/ft_51_misc.rb +67 -0
  176. data/test/functional/ft_52_case.rb +134 -0
  177. data/test/functional/ft_53_engine_on_terminate.rb +95 -0
  178. data/test/functional/ft_54_patterns.rb +104 -0
  179. data/test/functional/{ft_37_engine_participant.rb → ft_55_engine_participant.rb} +4 -5
  180. data/test/functional/ft_56_filter_attribute.rb +259 -0
  181. data/test/functional/ft_5_on_error.rb +77 -30
  182. data/test/functional/ft_6_on_cancel.rb +66 -11
  183. data/test/functional/ft_7_tags.rb +94 -5
  184. data/test/functional/ft_8_participant_consumption.rb +36 -5
  185. data/test/functional/ft_9_subprocesses.rb +10 -10
  186. data/test/functional/rt_1_listen.rb +3 -3
  187. data/test/functional/{rt_3_when.rb → rt_3_once.rb} +4 -4
  188. data/test/functional/storage_helper.rb +15 -13
  189. data/test/functional/test.rb +1 -3
  190. data/test/test_helper.rb +0 -8
  191. data/test/unit/storage.rb +154 -10
  192. data/test/unit/{ut_0_ruby_parser.rb → ut_0_ruby_reader.rb} +61 -11
  193. data/test/unit/ut_11_lookup.rb +7 -0
  194. data/test/unit/ut_13_serializer.rb +1 -1
  195. data/test/unit/ut_15_util.rb +23 -0
  196. data/test/unit/{ut_16_parser.rb → ut_16_reader.rb} +11 -13
  197. data/test/unit/ut_1_fei.rb +57 -10
  198. data/test/unit/ut_20_composite_storage.rb +25 -11
  199. data/test/unit/ut_21_participant_list.rb +47 -0
  200. data/test/unit/ut_22_filter.rb +903 -0
  201. data/test/unit/ut_3_wait_logger.rb +2 -6
  202. data/test/unit/ut_6_condition.rb +164 -17
  203. data/test/unit/ut_7_workitem.rb +28 -0
  204. data/test/unit/ut_8_tree_to_dot.rb +1 -1
  205. data/test/unit/{ut_9_xml_parser.rb → ut_9_xml_reader.rb} +5 -5
  206. metadata +108 -84
  207. data/.gitignore +0 -4
  208. data/examples/barley.rb +0 -391
  209. data/examples/flickr_report.rb +0 -107
  210. data/examples/pong.rb +0 -37
  211. data/examples/ruote_quickstart.rb +0 -43
  212. data/examples/web_first_page.rb +0 -68
  213. data/lib/ruote/part/hash_participant.rb +0 -91
  214. data/test/README.rdoc +0 -15
  215. data/test/functional/crunner.sh +0 -19
  216. data/test/pdef.xml +0 -7
  217. data/test/unit/ut_2_wfidgen.rb +0 -21
@@ -15,10 +15,13 @@ class UtFeiTest < Test::Unit::TestCase
15
15
  def test_misc
16
16
 
17
17
  fei = Ruote::FlowExpressionId.new(
18
- 'expid' => '0_0', 'wfid' => '20101224-bababa', 'engine_id' => 'engine')
18
+ 'expid' => '0_0',
19
+ 'wfid' => '20101224-bababa',
20
+ 'subid' => '5dbf4ce1553453baa17c2213d239e5fa',
21
+ 'engine_id' => 'engine')
19
22
 
20
23
  assert_equal(
21
- '0_0!!20101224-bababa',
24
+ '0_0!5dbf4ce1553453baa17c2213d239e5fa!20101224-bababa',
22
25
  fei.to_storage_id)
23
26
 
24
27
  assert_equal(
@@ -26,7 +29,10 @@ class UtFeiTest < Test::Unit::TestCase
26
29
  fei.child_id)
27
30
 
28
31
  assert_equal(
29
- {"wfid"=>"20101224-bababa", "engine_id"=>"engine", "expid"=>"0_0"},
32
+ { 'expid' => '0_0',
33
+ 'wfid' => '20101224-bababa',
34
+ 'subid' => '5dbf4ce1553453baa17c2213d239e5fa',
35
+ 'engine_id' => 'engine' },
30
36
  fei.to_h)
31
37
  end
32
38
 
@@ -100,19 +106,60 @@ class UtFeiTest < Test::Unit::TestCase
100
106
  assert_equal(
101
107
  { 'engine_id' => 'engine',
102
108
  'expid' => '0_0_1',
103
- 'sub_wfid' => '',
109
+ 'subid' => '5dbf4ce1553453baa17c2213d239e5fa',
104
110
  'wfid' => '20100224-fake' },
105
- Ruote::FlowExpressionId.extract_h('0_0_1!!20100224-fake'))
111
+ Ruote::FlowExpressionId.extract_h(
112
+ '0_0_1!5dbf4ce1553453baa17c2213d239e5fa!20100224-fake'))
106
113
  end
107
114
 
108
115
  def test_extract
116
+
109
117
  assert_equal(
110
118
  Ruote::FlowExpressionId.new(
111
- { 'engine_id' => 'engine',
112
- 'expid' => '0_0_1',
113
- 'sub_wfid' => '',
114
- 'wfid' => '20100224-fake' }),
115
- Ruote::FlowExpressionId.extract('0_0_1!!20100224-fake'))
119
+ 'engine_id' => 'engine',
120
+ 'expid' => '0_0_1',
121
+ 'subid' => '5dbf4ce1553453baa17c2213d239e5fa',
122
+ 'wfid' => '20100224-fake'),
123
+ Ruote::FlowExpressionId.extract(
124
+ '0_0_1!5dbf4ce1553453baa17c2213d239e5fa!20100224-fake'))
125
+ end
126
+
127
+ def test_subid_backward_compatibility__subid
128
+
129
+ fei = Ruote::FlowExpressionId.new(
130
+ 'engine_id' => 'engine',
131
+ 'expid' => '0_0_1',
132
+ 'subid' => 'd7ca677379e2a1f4933402b9196cf2a1',
133
+ 'wfid' => '20100224-fake')
134
+
135
+ assert_equal 'd7ca677379e2a1f4933402b9196cf2a1', fei.sub_wfid
136
+ assert_equal 'd7ca677379e2a1f4933402b9196cf2a1', fei.subid
137
+ assert_equal 'd7ca677379e2a1f4933402b9196cf2a1', fei.h['subid']
138
+ assert_nil fei.h['sub_wfid']
139
+ end
140
+
141
+ def test_subid_backward_compatibility__sub_wfid
142
+
143
+ fei = Ruote::FlowExpressionId.new(
144
+ 'engine_id' => 'engine',
145
+ 'expid' => '0_0_1',
146
+ 'sub_wfid' => 'd7ca677379e2a1f4933402b9196cf2a1',
147
+ 'wfid' => '20100224-fake')
148
+
149
+ assert_equal 'd7ca677379e2a1f4933402b9196cf2a1', fei.sub_wfid
150
+ assert_equal 'd7ca677379e2a1f4933402b9196cf2a1', fei.subid
151
+ assert_equal 'd7ca677379e2a1f4933402b9196cf2a1', fei.h['subid']
152
+ assert_nil fei.h['sub_wfid']
153
+ end
154
+
155
+ def test_generate_subid
156
+
157
+ n = 21_000
158
+ h = '0_0'
159
+
160
+ ids = n.times.collect { Ruote.generate_subid(h) }
161
+
162
+ assert_equal n, ids.uniq.size, "subid generation seems weak"
116
163
  end
117
164
  end
118
165
 
@@ -13,22 +13,36 @@ require 'ruote/storage/composite_storage'
13
13
 
14
14
  class UtCompositeStorageTest < Test::Unit::TestCase
15
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
+
16
23
  def test_initial
17
24
 
18
- msgs = Ruote::HashStorage.new({})
19
- default = Ruote::HashStorage.new({})
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')
20
39
 
21
- cs = Ruote::CompositeStorage.new(default, 'msgs' => msgs)
40
+ msg = @cs.get_many('msgs').first
22
41
 
23
- cs.put('action' => 'terminate', 'type' => 'msgs', '_id' => 'xxx')
24
- cs.put_msg('terminate', 'type' => 'msgs')
25
- cs.put_schedule('at', {}, Time.now + 10, 'action' => 'reply')
42
+ r = @cs.delete(msg)
26
43
 
27
- assert_equal 0, default.h['msgs'].size
28
- assert_equal 1, default.h['schedules'].size
29
- assert_equal 2, cs.get_msgs.size
30
- assert_equal 2, msgs.get_msgs.size
31
- assert_equal 0, msgs.h['schedules'].size
44
+ assert_nil r
45
+ assert_equal 0, @default.h['msgs'].size
32
46
  end
33
47
  end
34
48
 
@@ -0,0 +1,47 @@
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
+ #require File.join(File.dirname(__FILE__), %w[ .. functional storage_helper.rb ])
10
+
11
+ require 'ostruct'
12
+ require 'ruote'
13
+ require 'ruote/svc/participant_list'
14
+
15
+
16
+ class UtEngineTest < Test::Unit::TestCase
17
+
18
+ class FakeStorage
19
+ def initialize
20
+ @count = -1
21
+ end
22
+ def put(doc)
23
+ @count = @count + 1
24
+ return true if @count == 0
25
+ nil
26
+ end
27
+ def get_configuration(whatever)
28
+ { 'list' => [] }
29
+ end
30
+ end
31
+
32
+ # Fighting issue #20 found by 'sandbox'
33
+ #
34
+ # https://github.com/jmettraux/ruote/issues#issue/20
35
+ #
36
+ def test_register_participant_fail_and_retry
37
+
38
+ con = OpenStruct.new(:storage => FakeStorage.new)
39
+
40
+ pl = Ruote::ParticipantList.new(con)
41
+
42
+ pl.register('toto', Ruote::NullParticipant, { :hello => :world }, nil)
43
+
44
+ assert true
45
+ end
46
+ end
47
+
@@ -0,0 +1,903 @@
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
+ Ruote.filter(Rufus::Json.dup(filter), hash)
437
+ assert true
438
+ end
439
+
440
+ def assert_not_valid(filter, hash, deviations=1)
441
+
442
+ error = nil
443
+
444
+ begin
445
+ Ruote.filter(Rufus::Json.dup(filter), hash)
446
+ rescue => error
447
+ end
448
+
449
+ assert_not_nil(
450
+ error, "ValidationError was not raised")
451
+ assert_equal(
452
+ deviations, error.deviations.size, "deviation count doesn't match")
453
+
454
+ @deviations = error.deviations
455
+ end
456
+
457
+ def test_type
458
+
459
+ assert_valid(
460
+ [ { 'field' => 'x', 'type' => 'string' } ], { 'x' => 'deux' })
461
+ assert_valid(
462
+ [ { 'field' => 'x', 'type' => 'number' } ], { 'x' => 1 })
463
+ assert_valid(
464
+ [ { 'field' => 'x', 'type' => 'number' } ], { 'x' => 1.0 })
465
+ assert_valid(
466
+ [ { 'field' => 'x', 'type' => 'object' } ], { 'x' => {} })
467
+ assert_valid(
468
+ [ { 'field' => 'x', 'type' => 'hash' } ], { 'x' => {} })
469
+ assert_valid(
470
+ [ { 'field' => 'x', 'type' => 'array' } ], { 'x' => [] })
471
+ assert_valid(
472
+ [ { 'field' => 'x', 'type' => 'boolean' } ], { 'x' => true })
473
+ assert_valid(
474
+ [ { 'field' => 'x', 'type' => 'boolean' } ], { 'x' => false })
475
+ assert_valid(
476
+ [ { 'field' => 'x', 'type' => 'bool' } ], { 'x' => true })
477
+ assert_valid(
478
+ [ { 'field' => 'x', 'type' => 'bool' } ], { 'x' => false })
479
+ assert_valid(
480
+ [ { 'field' => 'x', 'type' => 'null' } ], { 'x' => nil })
481
+ assert_valid(
482
+ [ { 'field' => 'x', 'type' => 'null' } ], {})
483
+ assert_valid(
484
+ [ { 'field' => 'x', 'type' => 'nil' } ], {})
485
+
486
+ assert_not_valid(
487
+ [ { 'field' => 'x', 'type' => 'string' } ], { 'x' => 2 })
488
+ assert_not_valid(
489
+ [ { 'field' => 'x', 'type' => 'number' } ], { 'x' => 'one' })
490
+ assert_not_valid(
491
+ [ { 'field' => 'x', 'type' => 'number' } ], { 'x' => '1.0' })
492
+ assert_not_valid(
493
+ [ { 'field' => 'x', 'type' => 'object' } ], { 'x' => [] })
494
+ assert_not_valid(
495
+ [ { 'field' => 'x', 'type' => 'hash' } ], { 'x' => [] })
496
+ assert_not_valid(
497
+ [ { 'field' => 'x', 'type' => 'array' } ], { 'x' => {} })
498
+ assert_not_valid(
499
+ [ { 'field' => 'x', 'type' => 'boolean' } ], { 'x' => 'true' })
500
+ assert_not_valid(
501
+ [ { 'field' => 'x', 'type' => 'boolean' } ], { 'x' => 'false' })
502
+ assert_not_valid(
503
+ [ { 'field' => 'x', 'type' => 'bool' } ], { 'x' => 'true' })
504
+ assert_not_valid(
505
+ [ { 'field' => 'x', 'type' => 'bool' } ], { 'x' => 'true' })
506
+ assert_not_valid(
507
+ [ { 'field' => 'x', 'type' => 'null' } ], { 'x' => false })
508
+ assert_not_valid(
509
+ [ { 'field' => 'x', 'type' => 'null' } ], { 'x' => 1 })
510
+ end
511
+
512
+ def test_type_deep
513
+
514
+ assert_valid(
515
+ [ { 'field' => 'x.y', 'type' => 'string' } ], { 'x' => { 'y' => 'z' } })
516
+
517
+ assert_not_valid(
518
+ [ { 'field' => 'x.y', 'type' => 'string' } ], { 'x' => { 'y' => 1 } })
519
+ end
520
+
521
+ def test_type_union
522
+
523
+ assert_valid(
524
+ [ { 'field' => 'x', 'type' => 'string,number' } ], { 'x' => 'a' })
525
+ assert_valid(
526
+ [ { 'field' => 'x', 'type' => 'string,number' } ], { 'x' => 1 })
527
+ assert_valid(
528
+ [ { 'field' => 'x', 'type' => [ 'string', 'number' ] } ], { 'x' => 'a' })
529
+ assert_valid(
530
+ [ { 'field' => 'x', 'type' => [ 'string', 'number' ] } ], { 'x' => 1 })
531
+ end
532
+
533
+ def test_type_and_null
534
+
535
+ assert_valid(
536
+ [ { 'field' => 'x', 'type' => 'string,null' } ], {})
537
+ assert_valid(
538
+ [ { 'field' => 'x', 'type' => 'string,null' } ], { 'x' => nil })
539
+ assert_valid(
540
+ [ { 'field' => 'x', 'type' => 'string,null' } ], { 'x' => 'x' })
541
+ end
542
+
543
+ def test_type_array
544
+
545
+ assert_valid(
546
+ [ { 'field' => 'x', 'type' => 'array<string>' } ],
547
+ { 'x' => %w[ a b c ] })
548
+ assert_valid(
549
+ [ { 'field' => 'x', 'type' => 'array<string,number>' } ],
550
+ { 'x' => [ 'a', 1 ] })
551
+
552
+ assert_not_valid(
553
+ [ { 'field' => 'x', 'type' => 'array<string>' } ],
554
+ { 'x' => [ 'a', 1 ] })
555
+ assert_not_valid(
556
+ [ { 'field' => 'x', 'type' => 'array<string,number>' } ],
557
+ { 'x' => [ 'a', true ] })
558
+ end
559
+
560
+ def test_type_array_deep
561
+
562
+ assert_valid(
563
+ [ { 'field' => 'x', 'type' => 'array<array<string>>' } ],
564
+ { 'x' => [ %w[ a b ], %w[ c d ] ] })
565
+
566
+ assert_not_valid(
567
+ [ { 'field' => 'x', 'type' => 'array<array<string>>' } ],
568
+ { 'x' => [ %w[ a b ], [ 2, 3 ] ] })
569
+ end
570
+
571
+ def test_type_hash
572
+
573
+ assert_valid(
574
+ [ { 'field' => 'x', 'type' => 'hash<string>' } ],
575
+ { 'x' => { 'a' => 'b', 'c' => 'd' } })
576
+ assert_valid(
577
+ [ { 'field' => 'x', 'type' => 'hash<string,number>' } ],
578
+ { 'x' => { 'a' => 'b', 'c' => 0 } })
579
+
580
+ assert_not_valid(
581
+ [ { 'field' => 'x', 'type' => 'hash<string>' } ],
582
+ { 'x' => { 'a' => 'b', 'c' => 0 } })
583
+ assert_not_valid(
584
+ [ { 'field' => 'x', 'type' => 'hash<string,number>' } ],
585
+ { 'x' => { 'a' => 'b', 'c' => true } })
586
+ end
587
+
588
+ def test_type_and_regex
589
+
590
+ assert_valid(
591
+ [ { 'field' => '/./', 'type' => 'string' } ],
592
+ { 'x' => 'y', 'z' => 'a' })
593
+
594
+ assert_not_valid(
595
+ [ { 'field' => '/./', 'type' => 'string' } ],
596
+ { 'x' => 'y', 'z' => 1 })
597
+
598
+ assert_not_valid(
599
+ [ { 'field' => '/./', 'type' => 'string' } ],
600
+ { 'x' => 1, 'z' => 1 },
601
+ 2)
602
+ end
603
+
604
+ def test_match
605
+
606
+ assert_valid(
607
+ [ { 'field' => 'x', 'match' => 'to' } ],
608
+ { 'x' => 'toto' })
609
+ assert_valid(
610
+ [ { 'field' => 'x', 'match' => '1' } ],
611
+ { 'x' => 1.0 })
612
+
613
+ assert_not_valid(
614
+ [ { 'field' => 'x', 'match' => 'to' } ],
615
+ { 'x' => 'tutu' })
616
+ assert_not_valid(
617
+ [ { 'field' => 'x', 'match' => '1' } ],
618
+ { 'x' => 2.0 })
619
+ end
620
+
621
+ def test_smatch
622
+
623
+ assert_valid(
624
+ [ { 'field' => 'x', 'smatch' => 'to' } ],
625
+ { 'x' => 'toto' })
626
+ assert_not_valid(
627
+ [ { 'field' => 'x', 'smatch' => '1' } ],
628
+ { 'x' => 1.0 })
629
+
630
+ assert_not_valid(
631
+ [ { 'field' => 'x', 'smatch' => 'to' } ],
632
+ { 'x' => 'tutu' })
633
+ assert_not_valid(
634
+ [ { 'field' => 'x', 'smatch' => '1' } ],
635
+ { 'x' => 2.0 })
636
+ end
637
+
638
+ def test_size
639
+
640
+ assert_valid(
641
+ [ { 'field' => 'x', 'size' => 4 } ],
642
+ { 'x' => 'toto' })
643
+ assert_valid(
644
+ [ { 'field' => 'x', 'size' => '4' } ],
645
+ { 'x' => 'toto' })
646
+ assert_valid(
647
+ [ { 'field' => 'x', 'size' => 4 } ],
648
+ { 'x' => %w[ a b c d ] })
649
+ assert_valid(
650
+ [ { 'field' => 'x', 'size' => 2 } ],
651
+ { 'x' => { 'a' => 'b', 'c' => 'd' } })
652
+
653
+ assert_not_valid(
654
+ [ { 'field' => 'x', 'size' => 2 } ],
655
+ {})
656
+ assert_not_valid(
657
+ [ { 'field' => 'x', 'size' => 2 } ],
658
+ { 'x' => 3 })
659
+ end
660
+
661
+ def test_size_range
662
+
663
+ assert_valid(
664
+ [ { 'field' => 'x', 'size' => [ 2, 3 ] } ],
665
+ { 'x' => %w[ a b ] })
666
+ assert_valid(
667
+ [ { 'field' => 'x', 'size' => [ 2, 3 ] } ],
668
+ { 'x' => %w[ a b c ] })
669
+
670
+ assert_not_valid(
671
+ [ { 'field' => 'x', 'size' => [ 2, 3 ] } ],
672
+ { 'x' => %w[ a ] })
673
+ assert_not_valid(
674
+ [ { 'field' => 'x', 'size' => [ 2, 3 ] } ],
675
+ { 'x' => %w[ a b c d ] })
676
+ end
677
+
678
+ def test_size_open_range
679
+
680
+ assert_valid(
681
+ [ { 'field' => 'x', 'size' => [ 2 ] } ],
682
+ { 'x' => %w[ a b ] })
683
+ assert_valid(
684
+ [ { 'field' => 'x', 'size' => [ 2, nil ] } ],
685
+ { 'x' => %w[ a b ] })
686
+ assert_valid(
687
+ [ { 'field' => 'x', 'size' => ",3" } ],
688
+ { 'x' => %w[ a b c ] })
689
+
690
+ assert_not_valid(
691
+ [ { 'field' => 'x', 'size' => "2," } ],
692
+ { 'x' => %w[ a ] })
693
+ assert_not_valid(
694
+ [ { 'field' => 'x', 'size' => [ nil, 3 ] } ],
695
+ { 'x' => %w[ a b c d ] })
696
+ end
697
+
698
+ def test_empty
699
+
700
+ assert_valid(
701
+ [ { 'field' => 'x', 'empty' => true } ],
702
+ { 'x' => %w[] })
703
+ assert_valid(
704
+ [ { 'field' => 'x', 'empty' => true } ],
705
+ { 'x' => {} })
706
+ assert_valid(
707
+ [ { 'field' => 'x', 'empty' => true } ],
708
+ { 'x' => '' })
709
+
710
+ assert_not_valid(
711
+ [ { 'field' => 'x', 'empty' => true } ],
712
+ { 'x' => 'deux' })
713
+ assert_not_valid(
714
+ [ { 'field' => 'x', 'empty' => true } ],
715
+ { 'x' => %w[ a b ] })
716
+ assert_not_valid(
717
+ [ { 'field' => 'x', 'empty' => true } ],
718
+ { 'x' => { 'a' => 'b' } })
719
+ end
720
+
721
+ def test_in
722
+
723
+ assert_valid(
724
+ [ { 'field' => 'x', 'in' => %w[ alpha bravo ] } ],
725
+ { 'x' => 'alpha' })
726
+ assert_valid(
727
+ [ { 'field' => 'x', 'in' => "alpha, bravo" } ],
728
+ { 'x' => 'alpha' })
729
+
730
+ assert_not_valid(
731
+ [ { 'field' => 'x', 'in' => %w[ alpha bravo ] } ],
732
+ { 'x' => 'charly' })
733
+ assert_not_valid(
734
+ [ { 'field' => 'x', 'in' => "alpha, bravo" } ],
735
+ { 'x' => 'charly' })
736
+ end
737
+
738
+ def test_has__keys
739
+
740
+ assert_valid(
741
+ [ { 'field' => '.', 'has' => 'x' } ],
742
+ { 'x' => 'alpha' })
743
+ assert_valid(
744
+ [ { 'field' => 'x', 'has' => 'a' } ],
745
+ { 'x' => { 'a' => 1 } })
746
+ assert_valid(
747
+ [ { 'field' => 'x', 'has' => 'a, b' } ],
748
+ { 'x' => { 'a' => 1, 'b' => 2 } })
749
+ assert_valid(
750
+ [ { 'field' => 'x', 'has' => %w[ a b ] } ],
751
+ { 'x' => { 'a' => 1, 'b' => 2 } })
752
+
753
+ assert_not_valid(
754
+ [ { 'field' => '.', 'has' => 'x' } ],
755
+ {})
756
+ assert_not_valid(
757
+ [ { 'field' => 'x', 'has' => 'b' } ],
758
+ { 'x' => { 'a' => 1 } })
759
+ assert_not_valid(
760
+ [ { 'field' => 'x', 'has' => 'a, c' } ],
761
+ { 'x' => { 'a' => 1, 'b' => 2 } })
762
+ assert_not_valid(
763
+ [ { 'field' => 'x', 'has' => %w[ a c ] } ],
764
+ { 'x' => { 'a' => 1, 'b' => 2 } })
765
+ end
766
+
767
+ def test_has__elts
768
+
769
+ assert_valid(
770
+ [ { 'field' => 'x', 'has' => 'a' } ],
771
+ { 'x' => %w[ a b c ] })
772
+ assert_valid(
773
+ [ { 'field' => 'x', 'has' => 'a, b' } ],
774
+ { 'x' => %w[ a b c ] })
775
+ assert_valid(
776
+ [ { 'field' => 'x', 'has' => %w[ a b ] } ],
777
+ { 'x' => %w[ a b c ] })
778
+
779
+ assert_not_valid(
780
+ [ { 'field' => 'x', 'has' => 'd' } ],
781
+ { 'x' => %w[ a b c ] })
782
+ assert_not_valid(
783
+ [ { 'field' => 'x', 'has' => 'a, d' } ],
784
+ { 'x' => %w[ a b c ] })
785
+ assert_not_valid(
786
+ [ { 'field' => 'x', 'has' => %w[ a d ] } ],
787
+ { 'x' => %w[ a b c ] })
788
+ end
789
+
790
+ def test_valid
791
+
792
+ # 'valid' can be used in conjunction with the dollar notation
793
+ #
794
+ # field => 'x', 'valid' => '${v:accept}'
795
+
796
+ assert_valid(
797
+ [ { 'field' => 'x', 'valid' => true } ],
798
+ {})
799
+ assert_valid(
800
+ [ { 'field' => 'x', 'valid' => 'true' } ],
801
+ {})
802
+
803
+ assert_not_valid(
804
+ [ { 'field' => 'x', 'valid' => false } ],
805
+ {})
806
+ assert_not_valid(
807
+ [ { 'field' => 'x', 'valid' => 'false' } ],
808
+ {})
809
+ assert_not_valid(
810
+ [ { 'field' => 'x', 'valid' => 'nada' } ],
811
+ {})
812
+ end
813
+
814
+ def test_cumulation
815
+
816
+ assert_valid(
817
+ [ { 'field' => 'x', 't' => 'array', 'has' => 'a' } ],
818
+ { 'x' => %w[ a b c ] })
819
+
820
+ assert_not_valid(
821
+ [ { 'field' => 'x', 't' => 'hash', 'has' => 'a' } ],
822
+ { 'x' => %w[ a b c ] })
823
+ end
824
+
825
+ def test_multiple_validations
826
+
827
+ assert_not_valid(
828
+ [
829
+ { 'field' => 'x', 't' => 'array', 'has' => 1 },
830
+ { 'field' => 'y', 't' => 'string' }
831
+ ],
832
+ {
833
+ 'x' => %w[ a b c ],
834
+ 'y' => true
835
+ },
836
+ 2)
837
+
838
+ assert_equal [
839
+ [ { "has" => 1, "field" => "x", "t" => "array"}, "x", [ "a", "b", "c" ] ],
840
+ [ { "field" => "y", "t" => "string" }, "y", true ]
841
+ ], @deviations
842
+ # not super happy with this @breaks thing
843
+ end
844
+
845
+ # when :no_raise => true and the validation fails, an array is returned
846
+ # listing the 'deviations'
847
+ #
848
+ def test_no_raise
849
+
850
+ r = Ruote.filter(
851
+ [ { 'field' => 'x', 't' => 'hash', 'has' => 'a' } ],
852
+ { 'x' => %w[ a b c ] },
853
+ :no_raise => true)
854
+
855
+ assert_equal(
856
+ [ [ { "has" => "a", "field" => "x", "t" => "hash"}, "x", [ "a", "b", "c" ] ] ], r)
857
+ end
858
+
859
+ #
860
+ # flatten_keys tests
861
+
862
+ def test_flatten_keys
863
+
864
+ assert_equal(
865
+ [
866
+ 'a',
867
+ 'c',
868
+ 'c.d',
869
+ 'c.f',
870
+ 'c.f.l',
871
+ 'c.f.n',
872
+ 'c.f.n.0',
873
+ 'c.f.n.1',
874
+ 'c.f.n.2',
875
+ 'c.g',
876
+ 'c.g.0',
877
+ 'c.g.0.i',
878
+ 'c.g.1',
879
+ 'h',
880
+ 'h.0',
881
+ 'h.1',
882
+ 'h.2'
883
+ ],
884
+ Ruote.flatten_keys({
885
+ 'a' => 'b',
886
+ 'c' => {
887
+ 'd' => 'e',
888
+ 'f' => {
889
+ 'l' => 'm',
890
+ 'n' => [ 1, 2, 3 ]
891
+ },
892
+ 'g' => [
893
+ { 'i' => 'j' },
894
+ 'k'
895
+ ]
896
+ },
897
+ 'h' => [
898
+ 1, 2, 3
899
+ ]
900
+ }))
901
+ end
902
+ end
903
+