state_machines_activerecord_rspec 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a5dd59fd91c12e0ee672103e015d626de7a2b9f
4
+ data.tar.gz: 15288ad636ec6172b671e5453b7269b5e750776b
5
+ SHA512:
6
+ metadata.gz: b1b941927c8cde7c25ae912701b04c16a2e193c6b39c0202a3c8d3e6548ed720112fca1434c0ae1f8f6cd10f725fb356bc16292475591d381bf71fadafe67fa7
7
+ data.tar.gz: aafa122635131f309db7b9fce920475ae04843a24b285c25abdd9af703107095e570caab8b1f502cbd4c01025a03508441fcd9e95e0dc900708505ca553fe71b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p392@state_machines_activerecord_rspec"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.15.5 ()" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ if [[ $- == *i* ]] # check for interactive shells
29
+ then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
30
+ else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
31
+ fi
32
+ else
33
+ # If the environment file has not yet been created, use the RVM CLI to select.
34
+ rvm --create use "$environment_id" || {
35
+ echo "Failed to create RVM environment '${environment_id}'."
36
+ return 1
37
+ }
38
+ fi
39
+
40
+ # If you use bundler, this might be useful to you:
41
+ # if [[ -s Gemfile ]] && {
42
+ # ! builtin command -v bundle >/dev/null ||
43
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
44
+ # }
45
+ # then
46
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
47
+ # gem install bundler
48
+ # fi
49
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
50
+ # then
51
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
52
+ # fi
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0](https://github.com/chrise86/state_machines_activerecord_rspec/tree/v0.1.0)
2
+
3
+ - Initial release.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in state_machines_activerecord_rspec.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'pry'
8
+ end
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 modocache
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # state_machines_activerecord_rspec
2
+
3
+ Custom matchers for [state-machines/state_machines-activerecord](https://github.com/state-machines/state_machines-activerecord) **forked from [modocache/state_machine_rspec](https://github.com/modocache/state_machine_rspec)**.
4
+
5
+ ## Matchers
6
+
7
+ ### `have_state` & `reject_state`
8
+
9
+ ```ruby
10
+ describe Vehicle do
11
+ it { should have_states :parked, :idling, :stalled, :first_gear,
12
+ :second_gear, :third_gear }
13
+ it { should reject_state :flying }
14
+
15
+ it { should have_states :active, :off, on: :alarm_state }
16
+ it { should have_state :active, on: :alarm_state, value: 1 }
17
+ it { should reject_states :broken, :ringing, on: :alarm_state }
18
+ end
19
+ ```
20
+
21
+ ### `handle_event` & `reject_event`
22
+
23
+ ```ruby
24
+ describe Vehicle do
25
+ it { should handle_events :shift_down, :crash, when: :third_gear }
26
+ it { should handle_events :enable_alarm, :disable_alarm,
27
+ when: :active, on: :alarm_state }
28
+ it { should reject_events :park, :ignite, :idle, :shift_up, :repair,
29
+ when: :third_gear }
30
+ end
31
+ ```
32
+
33
+
34
+ ## Installation
35
+
36
+ Add these lines to your application's Gemfile:
37
+
38
+ ```ruby
39
+ group :test do
40
+ gem 'state_machines_activerecord_rspec'
41
+ end
42
+ ```
43
+
44
+ And include the matchers in `spec/spec_helper.rb`:
45
+
46
+ ```ruby
47
+ RSpec.configure do |config|
48
+ config.include StateMachinesActiverecordRspec::Matchers
49
+ end
50
+ ```
51
+
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,35 @@
1
+ require 'matchers/events/matcher'
2
+
3
+ module StateMachinesActiverecordRspec
4
+ module Matchers
5
+ def handle_events(value, *values)
6
+ HandleEventMatcher.new(values.unshift(value))
7
+ end
8
+ alias_method :handle_event, :handle_events
9
+
10
+ class HandleEventMatcher < StateMachinesActiverecordRspec::Matchers::Events::Matcher
11
+ def matches_events?(events)
12
+ !invalid_events?
13
+ end
14
+
15
+ def description
16
+ message = super
17
+ message << " on #{state_machine_scope.inspect}" if state_machine_scope
18
+ "handle #{message}"
19
+ end
20
+
21
+ private
22
+
23
+ def invalid_events?
24
+ invalid_events = @introspector.invalid_events(@events)
25
+ unless invalid_events.empty?
26
+ @failure_message = "Expected to be able to handle events: " +
27
+ "#{invalid_events.join(', ')} in state: " +
28
+ "#{@introspector.current_state_value}"
29
+ end
30
+
31
+ !invalid_events.empty?
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,71 @@
1
+ require 'active_support/core_ext/array/extract_options'
2
+
3
+ module StateMachinesActiverecordRspec
4
+ module Matchers
5
+ module Events
6
+ class Matcher
7
+ attr_reader :failure_message
8
+
9
+ def initialize(events)
10
+ @options = events.extract_options!
11
+ @events = events
12
+ end
13
+
14
+ def matches?(subject)
15
+ @subject = subject
16
+ @introspector = StateMachineIntrospector.new(@subject,
17
+ @options.fetch(:on, nil))
18
+ enter_when_state
19
+ return false if undefined_events?
20
+ return false unless matches_events?(@events)
21
+ @failure_message.nil?
22
+ end
23
+
24
+ def matches_events?(events)
25
+ raise NotImplementedError,
26
+ "subclasses of #{self.class} must override matches_events?"
27
+ end
28
+
29
+ def description
30
+ message = @events.map{ |event| event.inspect }.join(', ')
31
+ message << " when #{state_name.inspect}" if state_name
32
+ message
33
+ end
34
+
35
+ protected
36
+
37
+ def state_machine_scope
38
+ @options.fetch(:on, nil)
39
+ end
40
+
41
+ private
42
+
43
+ def enter_when_state
44
+ if state_name
45
+ unless when_state = @introspector.state(state_name)
46
+ raise StateMachineIntrospectorError,
47
+ "#{@subject.class} does not define state: #{state_name}"
48
+ end
49
+
50
+ @subject.send("#{@introspector.state_machine_attribute}=",
51
+ when_state.value)
52
+ end
53
+ end
54
+
55
+ def state_name
56
+ @options.fetch(:when, nil)
57
+ end
58
+
59
+ def undefined_events?
60
+ undefined_events = @introspector.undefined_events(@events)
61
+ unless undefined_events.empty?
62
+ @failure_message = "state_machine: #{@introspector.state_machine_attribute} " +
63
+ "does not define events: #{undefined_events.join(', ')}"
64
+ end
65
+
66
+ !undefined_events.empty?
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,35 @@
1
+ require 'matchers/events/matcher'
2
+
3
+ module StateMachinesActiverecordRspec
4
+ module Matchers
5
+ def reject_events(value, *values)
6
+ RejectEventMatcher.new(values.unshift(value))
7
+ end
8
+ alias_method :reject_event, :reject_events
9
+
10
+ class RejectEventMatcher < StateMachinesActiverecordRspec::Matchers::Events::Matcher
11
+ def matches_events?(events)
12
+ !valid_events?
13
+ end
14
+
15
+ def description
16
+ message = super
17
+ message << " on #{state_machine_scope.inspect}" if state_machine_scope
18
+ "reject #{message}"
19
+ end
20
+
21
+ private
22
+
23
+ def valid_events?
24
+ valid_events = @introspector.valid_events(@events)
25
+ unless valid_events.empty?
26
+ @failure_message = "Did not expect to be able to handle events: " +
27
+ "#{valid_events.join(', ')} in state: " +
28
+ "#{@introspector.current_state_value}"
29
+ end
30
+
31
+ !valid_events.empty?
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,46 @@
1
+ require 'matchers/states/matcher'
2
+
3
+ module StateMachinesActiverecordRspec
4
+ module Matchers
5
+ def have_states(state, *states)
6
+ HaveStateMatcher.new(states.unshift(state))
7
+ end
8
+ alias_method :have_state, :have_states
9
+
10
+ class HaveStateMatcher < StateMachinesActiverecordRspec::Matchers::States::Matcher
11
+ def matches_states?(states)
12
+ return false if undefined_states?
13
+ return false if incorrect_value?
14
+ @failure_message.nil?
15
+ end
16
+
17
+ def description
18
+ message = super
19
+ message << " == #{state_value.inspect}" if state_value
20
+ message << " on #{state_machine_scope.inspect}" if state_machine_scope
21
+ "have #{message}"
22
+ end
23
+
24
+ private
25
+
26
+ def undefined_states?
27
+ undefined_states = @introspector.undefined_states(@states)
28
+ unless undefined_states.empty?
29
+ @failure_message = "Expected #{@introspector.state_machine_attribute} " +
30
+ "to allow states: #{undefined_states.join(', ')}"
31
+ end
32
+
33
+ !undefined_states.empty?
34
+ end
35
+
36
+ def incorrect_value?
37
+ if state_value && @introspector.state(@states.first).value != state_value
38
+ @failure_message = "Expected #{@states.first} to have value #{state_value}"
39
+ true
40
+ end
41
+
42
+ false
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,56 @@
1
+ require 'active_support/core_ext/array/extract_options'
2
+
3
+ module StateMachinesActiverecordRspec
4
+ module Matchers
5
+ module States
6
+ class Matcher
7
+ attr_reader :failure_message
8
+
9
+ def initialize(states)
10
+ @options = states.extract_options!
11
+ @states = states
12
+ end
13
+
14
+ def description
15
+ @states.map{ |event| event.inspect }.join(', ')
16
+ end
17
+
18
+ def matches?(subject)
19
+ raise_if_multiple_values
20
+
21
+ @subject = subject
22
+ @introspector = StateMachineIntrospector.new(@subject,
23
+ state_machine_scope)
24
+
25
+ return false unless matches_states?(@states)
26
+ @failure_message.nil?
27
+ end
28
+
29
+ def matches_states?(states)
30
+ raise NotImplementedError,
31
+ "subclasses of #{self.class} must override matches_states?"
32
+ end
33
+
34
+ protected
35
+
36
+ def state_machine_scope
37
+ @options.fetch(:on, nil)
38
+ end
39
+
40
+ def state_value
41
+ @options.fetch(:value, nil)
42
+ end
43
+
44
+ private
45
+
46
+ def raise_if_multiple_values
47
+ if @states.count > 1 && state_value
48
+ raise ArgumentError, 'cannot make value assertions on ' +
49
+ 'multiple states at once'
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+
@@ -0,0 +1,34 @@
1
+ require 'matchers/states/matcher'
2
+
3
+ module StateMachinesActiverecordRspec
4
+ module Matchers
5
+ def reject_states(state, *states)
6
+ RejectStateMatcher.new(states.unshift(state))
7
+ end
8
+ alias_method :reject_state, :reject_states
9
+
10
+ class RejectStateMatcher < StateMachinesActiverecordRspec::Matchers::States::Matcher
11
+ def matches_states?(states)
12
+ no_defined_states?
13
+ end
14
+
15
+ def description
16
+ message = super
17
+ message << " on #{state_machine_scope.inspect}" if state_machine_scope
18
+ "not have #{message}"
19
+ end
20
+
21
+ private
22
+
23
+ def no_defined_states?
24
+ defined_states = @introspector.defined_states(@states)
25
+ unless defined_states.empty?
26
+ @failure_message = "Did not expect #{@introspector.state_machine_attribute} " +
27
+ "to allow states: #{defined_states.join(', ')}"
28
+ end
29
+
30
+ defined_states.empty?
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,70 @@
1
+ class StateMachineIntrospector
2
+ def initialize(subject, state_machine_name=nil)
3
+ @subject = subject
4
+ @state_machine_name = state_machine_name
5
+ end
6
+
7
+ def state_machine_attribute
8
+ state_machine.attribute
9
+ end
10
+
11
+ def current_state_value
12
+ @subject.send(state_machine_attribute)
13
+ end
14
+
15
+ def state(name)
16
+ state = state_machine.states.find { |s| s.name == name }
17
+ end
18
+
19
+ def undefined_states(states)
20
+ states.reject { |s| state_defined? s }
21
+ end
22
+
23
+ def defined_states(states)
24
+ states.select { |s| state_defined? s }
25
+ end
26
+
27
+ def undefined_events(events)
28
+ events.reject { |e| event_defined? e }
29
+ end
30
+
31
+ def valid_events(events)
32
+ events.select { |e| valid_event? e }
33
+ end
34
+
35
+ def invalid_events(events)
36
+ events.reject { |e| valid_event? e }
37
+ end
38
+
39
+ private
40
+
41
+ def state_machine
42
+ if @state_machine_name
43
+ unless machine = @subject.class.state_machines[@state_machine_name]
44
+ raise StateMachineIntrospectorError,
45
+ "#{@subject.class} does not have a state machine defined " +
46
+ "on #{@state_machine_name}"
47
+ end
48
+ else
49
+ machine = @subject.class.state_machine
50
+ end
51
+
52
+ machine
53
+ end
54
+
55
+ def state_defined?(state_name)
56
+ state(state_name)
57
+ end
58
+
59
+ def event_defined?(event)
60
+ @subject.respond_to? "can_#{event}?"
61
+ end
62
+
63
+ def valid_event?(event)
64
+ @subject.send("can_#{event}?")
65
+ end
66
+
67
+ end
68
+
69
+ class StateMachineIntrospectorError < StandardError
70
+ end
@@ -0,0 +1,3 @@
1
+ module StateMachinesActiverecordRspec
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'state_machines_activerecord_rspec/version'
2
+ require 'state_machines_activerecord_rspec/state_machine_introspector'
3
+ require 'matchers/events/handle_event'
4
+ require 'matchers/events/reject_event'
5
+ require 'matchers/states/have_state'
6
+ require 'matchers/states/reject_state'
7
+
8
+ module StateMachinesActiverecordRspec
9
+ module Matchers
10
+ end
11
+ end