cassandra_object 0.6.0.pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/cassandra_object/associations/one_to_many.rb +136 -0
- data/lib/cassandra_object/associations/one_to_one.rb +77 -0
- data/lib/cassandra_object/associations.rb +35 -0
- data/lib/cassandra_object/attributes.rb +93 -0
- data/lib/cassandra_object/base.rb +104 -0
- data/lib/cassandra_object/callbacks.rb +10 -0
- data/lib/cassandra_object/collection.rb +8 -0
- data/lib/cassandra_object/cursor.rb +86 -0
- data/lib/cassandra_object/dirty.rb +27 -0
- data/lib/cassandra_object/identity/abstract_key_factory.rb +36 -0
- data/lib/cassandra_object/identity/key.rb +20 -0
- data/lib/cassandra_object/identity/natural_key_factory.rb +51 -0
- data/lib/cassandra_object/identity/uuid_key_factory.rb +37 -0
- data/lib/cassandra_object/identity.rb +61 -0
- data/lib/cassandra_object/indexes.rb +129 -0
- data/lib/cassandra_object/legacy_callbacks.rb +33 -0
- data/lib/cassandra_object/migrations.rb +72 -0
- data/lib/cassandra_object/mocking.rb +15 -0
- data/lib/cassandra_object/persistence.rb +193 -0
- data/lib/cassandra_object/serialization.rb +6 -0
- data/lib/cassandra_object/type_registration.rb +7 -0
- data/lib/cassandra_object/types.rb +128 -0
- data/lib/cassandra_object/validation.rb +58 -0
- data/lib/cassandra_object.rb +30 -0
- data/vendor/active_support_shims.rb +4 -0
- data/vendor/activemodel/CHANGELOG +13 -0
- data/vendor/activemodel/CHANGES +12 -0
- data/vendor/activemodel/MIT-LICENSE +21 -0
- data/vendor/activemodel/README +21 -0
- data/vendor/activemodel/Rakefile +52 -0
- data/vendor/activemodel/activemodel.gemspec +19 -0
- data/vendor/activemodel/examples/validations.rb +29 -0
- data/vendor/activemodel/lib/active_model/attribute_methods.rb +291 -0
- data/vendor/activemodel/lib/active_model/callbacks.rb +91 -0
- data/vendor/activemodel/lib/active_model/conversion.rb +8 -0
- data/vendor/activemodel/lib/active_model/deprecated_error_methods.rb +33 -0
- data/vendor/activemodel/lib/active_model/dirty.rb +126 -0
- data/vendor/activemodel/lib/active_model/errors.rb +162 -0
- data/vendor/activemodel/lib/active_model/lint.rb +91 -0
- data/vendor/activemodel/lib/active_model/locale/en.yml +27 -0
- data/vendor/activemodel/lib/active_model/naming.rb +45 -0
- data/vendor/activemodel/lib/active_model/observing.rb +191 -0
- data/vendor/activemodel/lib/active_model/railtie.rb +2 -0
- data/vendor/activemodel/lib/active_model/serialization.rb +30 -0
- data/vendor/activemodel/lib/active_model/serializers/json.rb +96 -0
- data/vendor/activemodel/lib/active_model/serializers/xml.rb +204 -0
- data/vendor/activemodel/lib/active_model/state_machine/event.rb +62 -0
- data/vendor/activemodel/lib/active_model/state_machine/machine.rb +75 -0
- data/vendor/activemodel/lib/active_model/state_machine/state.rb +47 -0
- data/vendor/activemodel/lib/active_model/state_machine/state_transition.rb +40 -0
- data/vendor/activemodel/lib/active_model/state_machine.rb +70 -0
- data/vendor/activemodel/lib/active_model/test_case.rb +18 -0
- data/vendor/activemodel/lib/active_model/translation.rb +44 -0
- data/vendor/activemodel/lib/active_model/validations/acceptance.rb +55 -0
- data/vendor/activemodel/lib/active_model/validations/confirmation.rb +47 -0
- data/vendor/activemodel/lib/active_model/validations/exclusion.rb +42 -0
- data/vendor/activemodel/lib/active_model/validations/format.rb +64 -0
- data/vendor/activemodel/lib/active_model/validations/inclusion.rb +42 -0
- data/vendor/activemodel/lib/active_model/validations/length.rb +117 -0
- data/vendor/activemodel/lib/active_model/validations/numericality.rb +111 -0
- data/vendor/activemodel/lib/active_model/validations/presence.rb +42 -0
- data/vendor/activemodel/lib/active_model/validations/with.rb +59 -0
- data/vendor/activemodel/lib/active_model/validations.rb +120 -0
- data/vendor/activemodel/lib/active_model/validator.rb +110 -0
- data/vendor/activemodel/lib/active_model/version.rb +9 -0
- data/vendor/activemodel/lib/active_model.rb +61 -0
- data/vendor/activemodel/test/cases/attribute_methods_test.rb +46 -0
- data/vendor/activemodel/test/cases/callbacks_test.rb +70 -0
- data/vendor/activemodel/test/cases/helper.rb +23 -0
- data/vendor/activemodel/test/cases/lint_test.rb +28 -0
- data/vendor/activemodel/test/cases/naming_test.rb +28 -0
- data/vendor/activemodel/test/cases/observing_test.rb +133 -0
- data/vendor/activemodel/test/cases/serializeration/json_serialization_test.rb +83 -0
- data/vendor/activemodel/test/cases/serializeration/xml_serialization_test.rb +110 -0
- data/vendor/activemodel/test/cases/state_machine/event_test.rb +49 -0
- data/vendor/activemodel/test/cases/state_machine/machine_test.rb +43 -0
- data/vendor/activemodel/test/cases/state_machine/state_test.rb +72 -0
- data/vendor/activemodel/test/cases/state_machine/state_transition_test.rb +84 -0
- data/vendor/activemodel/test/cases/state_machine_test.rb +312 -0
- data/vendor/activemodel/test/cases/tests_database.rb +37 -0
- data/vendor/activemodel/test/cases/translation_test.rb +45 -0
- data/vendor/activemodel/test/cases/validations/acceptance_validation_test.rb +71 -0
- data/vendor/activemodel/test/cases/validations/conditional_validation_test.rb +141 -0
- data/vendor/activemodel/test/cases/validations/confirmation_validation_test.rb +58 -0
- data/vendor/activemodel/test/cases/validations/exclusion_validation_test.rb +47 -0
- data/vendor/activemodel/test/cases/validations/format_validation_test.rb +118 -0
- data/vendor/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb +175 -0
- data/vendor/activemodel/test/cases/validations/i18n_validation_test.rb +527 -0
- data/vendor/activemodel/test/cases/validations/inclusion_validation_test.rb +71 -0
- data/vendor/activemodel/test/cases/validations/length_validation_test.rb +437 -0
- data/vendor/activemodel/test/cases/validations/numericality_validation_test.rb +180 -0
- data/vendor/activemodel/test/cases/validations/presence_validation_test.rb +70 -0
- data/vendor/activemodel/test/cases/validations/with_validation_test.rb +166 -0
- data/vendor/activemodel/test/cases/validations_test.rb +215 -0
- data/vendor/activemodel/test/config.rb +3 -0
- data/vendor/activemodel/test/fixtures/topics.yml +41 -0
- data/vendor/activemodel/test/models/contact.rb +7 -0
- data/vendor/activemodel/test/models/custom_reader.rb +17 -0
- data/vendor/activemodel/test/models/developer.rb +6 -0
- data/vendor/activemodel/test/models/person.rb +9 -0
- data/vendor/activemodel/test/models/reply.rb +34 -0
- data/vendor/activemodel/test/models/topic.rb +9 -0
- data/vendor/activemodel/test/models/track_back.rb +4 -0
- data/vendor/activemodel/test/schema.rb +14 -0
- data/vendor/activesupport/lib/active_support/autoload.rb +48 -0
- data/vendor/activesupport/lib/active_support/concern.rb +25 -0
- data/vendor/activesupport/lib/active_support/core_ext/array/wrap.rb +20 -0
- data/vendor/activesupport/lib/active_support/core_ext/object/blank.rb +58 -0
- data/vendor/activesupport/lib/active_support/core_ext/object/tap.rb +6 -0
- data/vendor/activesupport/lib/active_support/dependency_module.rb +17 -0
- data/vendor/activesupport/lib/active_support/i18n.rb +2 -0
- data/vendor/activesupport/lib/active_support/locale/en.yml +33 -0
- metadata +230 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
require 'cases/helper'
|
|
2
|
+
require 'models/contact'
|
|
3
|
+
require 'active_support/core_ext/object/instance_variables'
|
|
4
|
+
|
|
5
|
+
class Contact
|
|
6
|
+
extend ActiveModel::Naming
|
|
7
|
+
include ActiveModel::Serializers::JSON
|
|
8
|
+
|
|
9
|
+
def attributes
|
|
10
|
+
instance_values
|
|
11
|
+
end unless method_defined?(:attributes)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class JsonSerializationTest < ActiveModel::TestCase
|
|
15
|
+
def setup
|
|
16
|
+
@contact = Contact.new
|
|
17
|
+
@contact.name = 'Konata Izumi'
|
|
18
|
+
@contact.age = 16
|
|
19
|
+
@contact.created_at = Time.utc(2006, 8, 1)
|
|
20
|
+
@contact.awesome = true
|
|
21
|
+
@contact.preferences = { 'shows' => 'anime' }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
test "should include root in json" do
|
|
25
|
+
begin
|
|
26
|
+
Contact.include_root_in_json = true
|
|
27
|
+
json = @contact.to_json
|
|
28
|
+
|
|
29
|
+
assert_match %r{^\{"contact":\{}, json
|
|
30
|
+
assert_match %r{"name":"Konata Izumi"}, json
|
|
31
|
+
assert_match %r{"age":16}, json
|
|
32
|
+
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
|
|
33
|
+
assert_match %r{"awesome":true}, json
|
|
34
|
+
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
|
35
|
+
ensure
|
|
36
|
+
Contact.include_root_in_json = false
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
test "should encode all encodable attributes" do
|
|
41
|
+
json = @contact.to_json
|
|
42
|
+
|
|
43
|
+
assert_match %r{"name":"Konata Izumi"}, json
|
|
44
|
+
assert_match %r{"age":16}, json
|
|
45
|
+
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
|
|
46
|
+
assert_match %r{"awesome":true}, json
|
|
47
|
+
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
test "should allow attribute filtering with only" do
|
|
51
|
+
json = @contact.to_json(:only => [:name, :age])
|
|
52
|
+
|
|
53
|
+
assert_match %r{"name":"Konata Izumi"}, json
|
|
54
|
+
assert_match %r{"age":16}, json
|
|
55
|
+
assert_no_match %r{"awesome":true}, json
|
|
56
|
+
assert !json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
|
|
57
|
+
assert_no_match %r{"preferences":\{"shows":"anime"\}}, json
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
test "should allow attribute filtering with except" do
|
|
61
|
+
json = @contact.to_json(:except => [:name, :age])
|
|
62
|
+
|
|
63
|
+
assert_no_match %r{"name":"Konata Izumi"}, json
|
|
64
|
+
assert_no_match %r{"age":16}, json
|
|
65
|
+
assert_match %r{"awesome":true}, json
|
|
66
|
+
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
|
|
67
|
+
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
test "methds are called on object" do
|
|
71
|
+
# Define methods on fixture.
|
|
72
|
+
def @contact.label; "Has cheezburger"; end
|
|
73
|
+
def @contact.favorite_quote; "Constraints are liberating"; end
|
|
74
|
+
|
|
75
|
+
# Single method.
|
|
76
|
+
assert_match %r{"label":"Has cheezburger"}, @contact.to_json(:only => :name, :methods => :label)
|
|
77
|
+
|
|
78
|
+
# Both methods.
|
|
79
|
+
methods_json = @contact.to_json(:only => :name, :methods => [:label, :favorite_quote])
|
|
80
|
+
assert_match %r{"label":"Has cheezburger"}, methods_json
|
|
81
|
+
assert_match %r{"favorite_quote":"Constraints are liberating"}, methods_json
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
require 'cases/helper'
|
|
2
|
+
require 'models/contact'
|
|
3
|
+
require 'active_support/core_ext/object/instance_variables'
|
|
4
|
+
|
|
5
|
+
class Contact
|
|
6
|
+
extend ActiveModel::Naming
|
|
7
|
+
include ActiveModel::Serializers::Xml
|
|
8
|
+
|
|
9
|
+
def attributes
|
|
10
|
+
instance_values
|
|
11
|
+
end unless method_defined?(:attributes)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
module Admin
|
|
15
|
+
class Contact < ::Contact
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class XmlSerializationTest < ActiveModel::TestCase
|
|
20
|
+
def setup
|
|
21
|
+
@contact = Contact.new
|
|
22
|
+
@contact.name = 'aaron stack'
|
|
23
|
+
@contact.age = 25
|
|
24
|
+
@contact.created_at = Time.utc(2006, 8, 1)
|
|
25
|
+
@contact.awesome = false
|
|
26
|
+
@contact.preferences = { :gem => 'ruby' }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
test "should serialize default root" do
|
|
30
|
+
@xml = @contact.to_xml
|
|
31
|
+
assert_match %r{^<contact>}, @xml
|
|
32
|
+
assert_match %r{</contact>$}, @xml
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
test "should serialize namespaced root" do
|
|
36
|
+
@xml = Admin::Contact.new(@contact.attributes).to_xml
|
|
37
|
+
assert_match %r{^<admin-contact>}, @xml
|
|
38
|
+
assert_match %r{</admin-contact>$}, @xml
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
test "should serialize default root with namespace" do
|
|
42
|
+
@xml = @contact.to_xml :namespace => "http://xml.rubyonrails.org/contact"
|
|
43
|
+
assert_match %r{^<contact xmlns="http://xml.rubyonrails.org/contact">}, @xml
|
|
44
|
+
assert_match %r{</contact>$}, @xml
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
test "should serialize custom root" do
|
|
48
|
+
@xml = @contact.to_xml :root => 'xml_contact'
|
|
49
|
+
assert_match %r{^<xml-contact>}, @xml
|
|
50
|
+
assert_match %r{</xml-contact>$}, @xml
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
test "should allow undasherized tags" do
|
|
54
|
+
@xml = @contact.to_xml :root => 'xml_contact', :dasherize => false
|
|
55
|
+
assert_match %r{^<xml_contact>}, @xml
|
|
56
|
+
assert_match %r{</xml_contact>$}, @xml
|
|
57
|
+
assert_match %r{<created_at}, @xml
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
test "should allow camelized tags" do
|
|
61
|
+
@xml = @contact.to_xml :root => 'xml_contact', :camelize => true
|
|
62
|
+
assert_match %r{^<XmlContact>}, @xml
|
|
63
|
+
assert_match %r{</XmlContact>$}, @xml
|
|
64
|
+
assert_match %r{<CreatedAt}, @xml
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
test "should allow skipped types" do
|
|
68
|
+
@xml = @contact.to_xml :skip_types => true
|
|
69
|
+
assert %r{<age>25</age>}.match(@xml)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
test "should include yielded additions" do
|
|
73
|
+
@xml = @contact.to_xml do |xml|
|
|
74
|
+
xml.creator "David"
|
|
75
|
+
end
|
|
76
|
+
assert_match %r{<creator>David</creator>}, @xml
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
test "should serialize string" do
|
|
80
|
+
assert_match %r{<name>aaron stack</name>}, @contact.to_xml
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
test "should serialize integer" do
|
|
84
|
+
assert_match %r{<age type="integer">25</age>}, @contact.to_xml
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
test "should serialize datetime" do
|
|
88
|
+
assert_match %r{<created-at type=\"datetime\">2006-08-01T00:00:00Z</created-at>}, @contact.to_xml
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
test "should serialize boolean" do
|
|
92
|
+
assert_match %r{<awesome type=\"boolean\">false</awesome>}, @contact.to_xml
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
test "should serialize yaml" do
|
|
96
|
+
assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @contact.to_xml
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
test "should call proc on object" do
|
|
100
|
+
proc = Proc.new { |options| options[:builder].tag!('nationality', 'unknown') }
|
|
101
|
+
xml = @contact.to_xml(:procs => [ proc ])
|
|
102
|
+
assert_match %r{<nationality>unknown</nationality>}, xml
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
test 'should supply serializable to second proc argument' do
|
|
106
|
+
proc = Proc.new { |options, record| options[:builder].tag!('name-reverse', record.name.reverse) }
|
|
107
|
+
xml = @contact.to_xml(:procs => [ proc ])
|
|
108
|
+
assert_match %r{<name-reverse>kcats noraa</name-reverse>}, xml
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require 'cases/helper'
|
|
2
|
+
|
|
3
|
+
class EventTest < ActiveModel::TestCase
|
|
4
|
+
def setup
|
|
5
|
+
@state_name = :close_order
|
|
6
|
+
@success = :success_callback
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def new_event
|
|
10
|
+
@event = ActiveModel::StateMachine::Event.new(nil, @state_name, {:success => @success}) do
|
|
11
|
+
transitions :to => :closed, :from => [:open, :received]
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
test 'should set the name' do
|
|
16
|
+
assert_equal @state_name, new_event.name
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
test 'should set the success option' do
|
|
20
|
+
assert_equal @success, new_event.success
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test 'should create StateTransitions' do
|
|
24
|
+
ActiveModel::StateMachine::StateTransition.expects(:new).with(:to => :closed, :from => :open)
|
|
25
|
+
ActiveModel::StateMachine::StateTransition.expects(:new).with(:to => :closed, :from => :received)
|
|
26
|
+
new_event
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class EventBeingFiredTest < ActiveModel::TestCase
|
|
31
|
+
test 'should raise an AASM::InvalidTransition error if the transitions are empty' do
|
|
32
|
+
event = ActiveModel::StateMachine::Event.new(nil, :event)
|
|
33
|
+
|
|
34
|
+
assert_raise ActiveModel::StateMachine::InvalidTransition do
|
|
35
|
+
event.fire(nil)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
test 'should return the state of the first matching transition it finds' do
|
|
40
|
+
event = ActiveModel::StateMachine::Event.new(nil, :event) do
|
|
41
|
+
transitions :to => :closed, :from => [:open, :received]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
obj = stub
|
|
45
|
+
obj.stubs(:current_state).returns(:open)
|
|
46
|
+
|
|
47
|
+
assert_equal :closed, event.fire(obj)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require 'cases/helper'
|
|
2
|
+
|
|
3
|
+
class MachineTestSubject
|
|
4
|
+
include ActiveModel::StateMachine
|
|
5
|
+
|
|
6
|
+
state_machine do
|
|
7
|
+
state :open
|
|
8
|
+
state :closed
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
state_machine :initial => :foo do
|
|
12
|
+
event :shutdown do
|
|
13
|
+
transitions :from => :open, :to => :closed
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
event :timeout do
|
|
17
|
+
transitions :from => :open, :to => :closed
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
state_machine :extra, :initial => :bar do
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
class StateMachineMachineTest < ActiveModel::TestCase
|
|
26
|
+
test "allows reuse of existing machines" do
|
|
27
|
+
assert_equal 2, MachineTestSubject.state_machines.size
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
test "sets #initial_state from :initial option" do
|
|
31
|
+
assert_equal :bar, MachineTestSubject.state_machine(:extra).initial_state
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
test "accesses non-default state machine" do
|
|
35
|
+
assert_kind_of ActiveModel::StateMachine::Machine, MachineTestSubject.state_machine(:extra)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
test "finds events for given state" do
|
|
39
|
+
events = MachineTestSubject.state_machine.events_for(:open)
|
|
40
|
+
assert events.include?(:shutdown)
|
|
41
|
+
assert events.include?(:timeout)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
require 'cases/helper'
|
|
2
|
+
|
|
3
|
+
class StateTestSubject
|
|
4
|
+
include ActiveModel::StateMachine
|
|
5
|
+
|
|
6
|
+
state_machine do
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class StateTest < ActiveModel::TestCase
|
|
11
|
+
def setup
|
|
12
|
+
@state_name = :astate
|
|
13
|
+
@machine = StateTestSubject.state_machine
|
|
14
|
+
@options = { :crazy_custom_key => 'key', :machine => @machine }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def new_state(options={})
|
|
18
|
+
ActiveModel::StateMachine::State.new(@state_name, @options.merge(options))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
test 'sets the name' do
|
|
22
|
+
assert_equal :astate, new_state.name
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
test 'sets the display_name from name' do
|
|
26
|
+
assert_equal "Astate", new_state.display_name
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
test 'sets the display_name from options' do
|
|
30
|
+
assert_equal "A State", new_state(:display => "A State").display_name
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
test 'sets the options and expose them as options' do
|
|
34
|
+
@options.delete(:machine)
|
|
35
|
+
assert_equal @options, new_state.options
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
test 'equals a symbol of the same name' do
|
|
39
|
+
assert_equal new_state, :astate
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
test 'equals a State of the same name' do
|
|
43
|
+
assert_equal new_state, new_state
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
test 'should send a message to the record for an action if the action is present as a symbol' do
|
|
47
|
+
state = new_state(:entering => :foo)
|
|
48
|
+
|
|
49
|
+
record = stub
|
|
50
|
+
record.expects(:foo)
|
|
51
|
+
|
|
52
|
+
state.call_action(:entering, record)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
test 'should send a message to the record for an action if the action is present as a string' do
|
|
56
|
+
state = new_state(:entering => 'foo')
|
|
57
|
+
|
|
58
|
+
record = stub
|
|
59
|
+
record.expects(:foo)
|
|
60
|
+
|
|
61
|
+
state.call_action(:entering, record)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
test 'should call a proc, passing in the record for an action if the action is present' do
|
|
65
|
+
state = new_state(:entering => Proc.new {|r| r.foobar})
|
|
66
|
+
|
|
67
|
+
record = stub
|
|
68
|
+
record.expects(:foobar)
|
|
69
|
+
|
|
70
|
+
state.call_action(:entering, record)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
require 'cases/helper'
|
|
2
|
+
|
|
3
|
+
class StateTransitionTest < ActiveModel::TestCase
|
|
4
|
+
test 'should set from, to, and opts attr readers' do
|
|
5
|
+
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
|
6
|
+
st = ActiveModel::StateMachine::StateTransition.new(opts)
|
|
7
|
+
|
|
8
|
+
assert_equal opts[:from], st.from
|
|
9
|
+
assert_equal opts[:to], st.to
|
|
10
|
+
assert_equal opts, st.options
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
test 'should pass equality check if from and to are the same' do
|
|
14
|
+
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
|
15
|
+
st = ActiveModel::StateMachine::StateTransition.new(opts)
|
|
16
|
+
|
|
17
|
+
obj = stub
|
|
18
|
+
obj.stubs(:from).returns(opts[:from])
|
|
19
|
+
obj.stubs(:to).returns(opts[:to])
|
|
20
|
+
|
|
21
|
+
assert_equal st, obj
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
test 'should fail equality check if from are not the same' do
|
|
25
|
+
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
|
26
|
+
st = ActiveModel::StateMachine::StateTransition.new(opts)
|
|
27
|
+
|
|
28
|
+
obj = stub
|
|
29
|
+
obj.stubs(:from).returns('blah')
|
|
30
|
+
obj.stubs(:to).returns(opts[:to])
|
|
31
|
+
|
|
32
|
+
assert_not_equal st, obj
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
test 'should fail equality check if to are not the same' do
|
|
36
|
+
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
|
37
|
+
st = ActiveModel::StateMachine::StateTransition.new(opts)
|
|
38
|
+
|
|
39
|
+
obj = stub
|
|
40
|
+
obj.stubs(:from).returns(opts[:from])
|
|
41
|
+
obj.stubs(:to).returns('blah')
|
|
42
|
+
|
|
43
|
+
assert_not_equal st, obj
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
class StateTransitionGuardCheckTest < ActiveModel::TestCase
|
|
48
|
+
test 'should return true of there is no guard' do
|
|
49
|
+
opts = {:from => 'foo', :to => 'bar'}
|
|
50
|
+
st = ActiveModel::StateMachine::StateTransition.new(opts)
|
|
51
|
+
|
|
52
|
+
assert st.perform(nil)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
test 'should call the method on the object if guard is a symbol' do
|
|
56
|
+
opts = {:from => 'foo', :to => 'bar', :guard => :test_guard}
|
|
57
|
+
st = ActiveModel::StateMachine::StateTransition.new(opts)
|
|
58
|
+
|
|
59
|
+
obj = stub
|
|
60
|
+
obj.expects(:test_guard)
|
|
61
|
+
|
|
62
|
+
st.perform(obj)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
test 'should call the method on the object if guard is a string' do
|
|
66
|
+
opts = {:from => 'foo', :to => 'bar', :guard => 'test_guard'}
|
|
67
|
+
st = ActiveModel::StateMachine::StateTransition.new(opts)
|
|
68
|
+
|
|
69
|
+
obj = stub
|
|
70
|
+
obj.expects(:test_guard)
|
|
71
|
+
|
|
72
|
+
st.perform(obj)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
test 'should call the proc passing the object if the guard is a proc' do
|
|
76
|
+
opts = {:from => 'foo', :to => 'bar', :guard => Proc.new {|o| o.test_guard}}
|
|
77
|
+
st = ActiveModel::StateMachine::StateTransition.new(opts)
|
|
78
|
+
|
|
79
|
+
obj = stub
|
|
80
|
+
obj.expects(:test_guard)
|
|
81
|
+
|
|
82
|
+
st.perform(obj)
|
|
83
|
+
end
|
|
84
|
+
end
|