state_jacket 0.0.4 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in state_jacket.gemspec
4
- gemspec
3
+ group :development, :test do
4
+ gem "micro_test"
5
+ gem "pry"
6
+ gem "pry-stack_explorer"
7
+ end
data/README.md CHANGED
@@ -128,3 +128,13 @@ turnstyle.break
128
128
  turnstyle.open # => RuntimeError: Can't transition from error to :open
129
129
  turnstyle.close # => RuntimeError: Can't transition from error to :closed
130
130
  ```
131
+
132
+ ## Running the Tests
133
+
134
+ ```
135
+ $ gem install state_jacket
136
+ $ gem unpack state_jacket
137
+ $ cd state_jacket-[VERSION]
138
+ $ bundle
139
+ $ bundle exec mt
140
+ ```
@@ -10,17 +10,17 @@ module StateJacket
10
10
 
11
11
  def add(state)
12
12
  if state.is_a?(Hash)
13
- self[state.keys.first.to_sym] = state.values.first.map(&:to_sym)
13
+ self[state.keys.first.to_s] = state.values.first.map(&:to_s)
14
14
  else
15
- self[state] = nil
15
+ self[state.to_s] = nil
16
16
  end
17
17
  end
18
18
 
19
19
  def can_transition?(from_to)
20
- from = from_to.keys.first.to_sym
20
+ from = from_to.keys.first.to_s
21
21
  to = from_to.values.first
22
22
  to = [to] unless to.is_a?(Array)
23
- to = to.map(&:to_sym)
23
+ to = to.map(&:to_s)
24
24
  transitions = self[from] || []
25
25
  (to & transitions).length == to.length
26
26
  end
@@ -32,7 +32,7 @@ module StateJacket
32
32
  end
33
33
 
34
34
  def transitioner?(state)
35
- transitioners.include?(state.to_sym)
35
+ transitioners.include?(state.to_s)
36
36
  end
37
37
 
38
38
  def terminators
@@ -42,7 +42,7 @@ module StateJacket
42
42
  end
43
43
 
44
44
  def terminator?(state)
45
- terminators.include?(state.to_sym)
45
+ terminators.include?(state.to_s)
46
46
  end
47
47
 
48
48
  def lock
@@ -56,7 +56,7 @@ module StateJacket
56
56
  end
57
57
 
58
58
  def supports_state?(state)
59
- keys.include?(state.to_sym)
59
+ keys.include?(state.to_s)
60
60
  end
61
61
 
62
62
  end
@@ -1,3 +1,3 @@
1
1
  module StateJacket
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.7"
3
3
  end
data/state_jacket.gemspec CHANGED
@@ -9,10 +9,14 @@ Gem::Specification.new do |gem|
9
9
  gem.authors = ["Nathan Hopkins"]
10
10
  gem.email = ["natehop@gmail.com"]
11
11
  gem.summary = "Intuitively define state machine like states and transitions."
12
- gem.homepage = ""
12
+ gem.homepage = "https://github.com/hopsoft/state_jacket"
13
13
 
14
14
  gem.files = `git ls-files`.split($/)
15
15
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
16
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
17
  gem.require_paths = ["lib"]
18
+
19
+ gem.add_development_dependency "micro_test"
20
+ gem.add_development_dependency "pry"
21
+ gem.add_development_dependency "pry-stack_explorer"
18
22
  end
@@ -0,0 +1,109 @@
1
+ require "micro_test"
2
+ require_relative "../lib/state_jacket/catalog"
3
+
4
+ class CatalogTest < MicroTest::Test
5
+ before do
6
+ @catalog = StateJacket::Catalog.new
7
+ end
8
+
9
+ test "add state" do
10
+ @catalog.add :start
11
+ assert @catalog.has_key?("start")
12
+ end
13
+
14
+ test "terminators" do
15
+ @catalog.add :start
16
+ assert @catalog.terminators.include?("start")
17
+ end
18
+
19
+ test "transitioners" do
20
+ @catalog.add :start => [:finish]
21
+ assert @catalog.transitioners == ["start"]
22
+ end
23
+
24
+ test "can transition" do
25
+ @catalog.add :start => [:finish]
26
+ assert @catalog.can_transition?(:start => :finish)
27
+ end
28
+
29
+ test "supports state" do
30
+ @catalog.add :start => [:finish]
31
+ assert @catalog.supports_state?(:start)
32
+ end
33
+
34
+ test "lock failure" do
35
+ @catalog.add :start => [:finish]
36
+ error = nil
37
+ begin
38
+ @catalog.lock
39
+ rescue Exception => ex
40
+ error = ex
41
+ end
42
+ assert error.message.start_with?("Invalid StateJacket::Catalog!")
43
+ end
44
+
45
+ test "lock success" do
46
+ @catalog.add :start => [:finish]
47
+ @catalog.add :finish
48
+ error = nil
49
+ begin
50
+ @catalog.lock
51
+ rescue Exception => ex
52
+ error = ex
53
+ end
54
+ assert error.nil?
55
+ end
56
+
57
+ test "symbol state" do
58
+ @catalog.add :start => [:finish]
59
+ @catalog.add :finish
60
+ assert @catalog.keys.include?("start")
61
+ assert @catalog.can_transition?(:start => :finish)
62
+ end
63
+
64
+ test "string state" do
65
+ @catalog.add "start" => ["finish"]
66
+ @catalog.add "finish"
67
+ assert @catalog.keys.include?("start")
68
+ assert @catalog.can_transition?("start" => "finish")
69
+ end
70
+
71
+ test "number state" do
72
+ @catalog.add 1 => [2]
73
+ @catalog.add 2
74
+ assert @catalog.keys.include?("1")
75
+ assert @catalog.can_transition?(1 => 2)
76
+ end
77
+
78
+ test "turnstyle example" do
79
+ @catalog.add :open => [:closed, :error]
80
+ @catalog.add :closed => [:open, :error]
81
+ @catalog.add :error
82
+ @catalog.lock
83
+ assert @catalog.transitioners == ["open", "closed"]
84
+ assert @catalog.terminators == ["error"]
85
+ assert @catalog.can_transition?(:open => :closed)
86
+ assert @catalog.can_transition?(:closed => :open)
87
+ assert @catalog.can_transition?(:error => :open) == false
88
+ assert @catalog.can_transition?(:error => :closed) == false
89
+ end
90
+
91
+ test "phone call example" do
92
+ @catalog = StateJacket::Catalog.new
93
+ @catalog.add :idle => [:dialing]
94
+ @catalog.add :dialing => [:idle, :connecting]
95
+ @catalog.add :connecting => [:idle, :busy, :connected]
96
+ @catalog.add :busy => [:idle]
97
+ @catalog.add :connected => [:idle]
98
+ @catalog.lock
99
+ assert @catalog.transitioners == ["idle", "dialing", "connecting", "busy", "connected"]
100
+ assert @catalog.terminators == []
101
+ assert @catalog.can_transition?(:idle => :dialing)
102
+ assert @catalog.can_transition?(:dialing => [:idle, :connecting])
103
+ assert @catalog.can_transition?(:connecting => [:idle, :busy, :connected])
104
+ assert @catalog.can_transition?(:busy => :idle)
105
+ assert @catalog.can_transition?(:connected => :idle)
106
+ assert @catalog.can_transition?(:idle => [:dialing, :connected]) == false
107
+ end
108
+
109
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_jacket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,56 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-22 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-01-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: micro_test
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: pry
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: pry-stack_explorer
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
14
62
  description:
15
63
  email:
16
64
  - natehop@gmail.com
@@ -30,7 +78,8 @@ files:
30
78
  - lib/state_jacket/catalog.rb
31
79
  - lib/state_jacket/version.rb
32
80
  - state_jacket.gemspec
33
- homepage: ''
81
+ - test/catalog_test.rb
82
+ homepage: https://github.com/hopsoft/state_jacket
34
83
  licenses: []
35
84
  post_install_message:
36
85
  rdoc_options: []
@@ -50,9 +99,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
99
  version: '0'
51
100
  requirements: []
52
101
  rubyforge_project:
53
- rubygems_version: 1.8.24
102
+ rubygems_version: 1.8.23
54
103
  signing_key:
55
104
  specification_version: 3
56
105
  summary: Intuitively define state machine like states and transitions.
57
- test_files: []
106
+ test_files:
107
+ - test/catalog_test.rb
58
108
  has_rdoc: