quick_shoulda 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,8 +4,7 @@ module QuickShoulda
4
4
  module PathResolver
5
5
  include StringHelpers
6
6
 
7
- private
8
-
7
+ private
9
8
  def spec_file_exist?
10
9
  File.file? spec_file_path
11
10
  end
@@ -15,17 +14,22 @@ module QuickShoulda
15
14
  end
16
15
 
17
16
  def _spec_file_path
18
- model_path = path.split(/\/?models\//)[1]
17
+ model_path = is_a_constant?(path) ? path.downcase.gsub('::', '/') : path.split(/\/?models\//)[1]
19
18
  file_name = "#{File.basename(model_path, ".rb")}_spec.rb"
20
- dir_name = File.dirname(model_path) == '.' ? spec_folder : "#{spec_folder}#{File.dirname(model_path)}/"
19
+ dir_name = File.dirname(model_path) == '.' ? spec_folder : "#{spec_folder}#{File.dirname(model_path)}/".gsub('//','/')
21
20
  "#{dir_name}#{file_name}"
22
21
  end
23
22
 
24
23
  # given models/namespace/modelname.rb
25
24
  # return Namespace::ModelName
26
25
  def _model_full_namespace_in_str
26
+ return path if is_a_constant?(path)
27
27
  model_path = path.split(/\/?models\//)[1].gsub('.rb','')
28
28
  model_path.split('/').map { |token| camelize(token) }.join('::')
29
29
  end
30
+
31
+ def is_a_constant?(path)
32
+ ( path[0] =~ /[A-Z]/ || path[0] =~ /:/ ) && File.extname(path).empty? && !path.include?('/')
33
+ end
30
34
  end
31
35
  end
@@ -1,3 +1,3 @@
1
1
  module QuickShoulda
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/quick_shoulda.rb CHANGED
@@ -17,9 +17,12 @@ module QuickShoulda
17
17
 
18
18
  def process(path, spec_folder = 'spec/models/')
19
19
  raise PathNotGivenError unless path
20
- raise NotAModelPathError unless path =~ /models\//
21
- raise FileDoesNotExistError unless File.file?(path)
22
- raise NotRubyFileError unless File.extname(path) == '.rb'
20
+ unless is_a_constant?(path)
21
+ raise NotAModelPathError unless path =~ /models\//
22
+ raise FileDoesNotExistError unless File.file?(path)
23
+ raise NotRubyFileError unless File.extname(path) == '.rb'
24
+ end
25
+
23
26
  configure_and_generate(path, spec_folder)
24
27
  end
25
28
  end
@@ -8,32 +8,44 @@ describe 'QuickShoulda::PathResolver' do
8
8
  @spec_folder = 'spec/'
9
9
  end
10
10
 
11
- describe '_model_full_namespace_in_str' do
12
- context 'valid path' do
13
- context 'without namespace' do
14
- let(:path) { '/models/user_account'}
15
-
16
- context 'with .rb' do
17
- it 'should return valid model name' do
18
- _model_full_namespace_in_str.should eq 'UserAccount'
11
+ describe '#_model_full_namespace_in_str' do
12
+ context 'constant given' do
13
+ before { should_receive(:is_a_constant?).and_return(true)}
14
+ let(:path) { 'User::Friend' }
15
+ it 'should given constant' do
16
+ _model_full_namespace_in_str.should eq 'User::Friend'
17
+ end
18
+ end
19
+
20
+ context 'path given' do
21
+ context 'valid path' do
22
+ before { should_receive(:is_a_constant?).and_return(false)}
23
+
24
+ context 'without namespace' do
25
+ let(:path) { '/models/user_account'}
26
+
27
+ context 'with .rb' do
28
+ it 'should return valid model name' do
29
+ _model_full_namespace_in_str.should eq 'UserAccount'
30
+ end
19
31
  end
20
- end
21
32
 
22
- context 'without .rb' do
23
- before { path = "#{path}.rb" }
24
- it 'should return valid model name' do
25
- _model_full_namespace_in_str.should eq 'UserAccount'
33
+ context 'without .rb' do
34
+ before { path = "#{path}.rb" }
35
+ it 'should return valid model name' do
36
+ _model_full_namespace_in_str.should eq 'UserAccount'
37
+ end
26
38
  end
27
39
  end
28
- end
29
40
 
30
- context 'with namespace' do
31
- let(:path) { 'models/user/friend.rb' }
32
- it 'should return valid model name with namespace' do
33
- _model_full_namespace_in_str.should eq 'User::Friend'
41
+ context 'with namespace' do
42
+ let(:path) { 'models/user/friend.rb' }
43
+ it 'should return valid model name with namespace' do
44
+ _model_full_namespace_in_str.should eq 'User::Friend'
45
+ end
34
46
  end
35
47
  end
36
- end
48
+ end
37
49
  end
38
50
 
39
51
  describe '_model_full_namespace' do
@@ -45,43 +57,88 @@ describe 'QuickShoulda::PathResolver' do
45
57
  end
46
58
  end
47
59
 
48
- context '_spec_file_path' do
60
+ context '#_spec_file_path' do
49
61
  before { File.stub(:file?).and_return(true) }
50
- context 'valid file path' do
51
- context 'without namespace' do
52
- let(:path) { 'models/user.rb' }
62
+ context 'constant given' do
63
+ context 'User' do
64
+ let(:path) { 'User' }
53
65
  let(:expected) { 'spec/user_spec.rb' }
54
66
 
55
67
  it 'should return valid test file path' do
56
68
  _spec_file_path.should eq expected
57
69
  end
58
- end
70
+ end
59
71
 
60
- context 'with namespace' do
61
- let(:path) { 'models/friendly/user.rb' }
62
- let(:expected) { 'spec/friendly/user_spec.rb'}
72
+ context 'User::Friend' do
73
+ let(:path) { 'User::Friend' }
74
+ let(:expected) { 'spec/user/friend_spec.rb' }
63
75
 
64
76
  it 'should return valid test file path' do
65
77
  _spec_file_path.should eq expected
66
78
  end
67
79
  end
68
80
 
69
- context 'with slash at beginning of file path' do
70
- let(:path) { '/models/friendly/user.rb' }
71
- let(:expected) { 'spec/friendly/user_spec.rb'}
81
+ context '::User::Friend' do
82
+ let(:path) { '::User::Friend' }
83
+ let(:expected) { 'spec/user/friend_spec.rb' }
72
84
 
73
85
  it 'should return valid test file path' do
74
86
  _spec_file_path.should eq expected
75
87
  end
76
88
  end
89
+ end
77
90
 
78
- context 'without .rb' do
79
- let(:path) { 'models/friendly/user' }
80
- let(:expected) { 'spec/friendly/user_spec.rb'}
91
+ context 'path given' do
92
+ context 'valid file path' do
93
+ context 'without namespace' do
94
+ let(:path) { 'models/user.rb' }
95
+ let(:expected) { 'spec/user_spec.rb' }
81
96
 
82
- it 'should return valid test file path' do
83
- _spec_file_path.should eq expected
97
+ it 'should return valid test file path' do
98
+ _spec_file_path.should eq expected
99
+ end
100
+ end
101
+
102
+ context 'with namespace' do
103
+ let(:path) { 'models/friendly/user.rb' }
104
+ let(:expected) { 'spec/friendly/user_spec.rb'}
105
+
106
+ it 'should return valid test file path' do
107
+ _spec_file_path.should eq expected
108
+ end
109
+ end
110
+
111
+ context 'with slash at beginning of file path' do
112
+ let(:path) { '/models/friendly/user.rb' }
113
+ let(:expected) { 'spec/friendly/user_spec.rb'}
114
+
115
+ it 'should return valid test file path' do
116
+ _spec_file_path.should eq expected
117
+ end
84
118
  end
119
+
120
+ context 'without .rb' do
121
+ let(:path) { 'models/friendly/user' }
122
+ let(:expected) { 'spec/friendly/user_spec.rb'}
123
+
124
+ it 'should return valid test file path' do
125
+ _spec_file_path.should eq expected
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ context '#is_a_constant?' do
133
+ ['::User::Friend', 'Friend', 'User::Friend', 'User::Friend::Best'].each do |constant|
134
+ it "should return true for #{constant}" do
135
+ is_a_constant?(constant).should be_true
136
+ end
137
+ end
138
+
139
+ ['user.rb', 'User.rb', 'user'].each do |constant|
140
+ it "should return false for #{constant}" do
141
+ is_a_constant?(constant).should be_false
85
142
  end
86
143
  end
87
144
  end
@@ -8,12 +8,13 @@ end
8
8
 
9
9
  describe 'QuickShoulda' do
10
10
  describe '#process' do
11
+ let(:spec_folder) { 'spec/models/' }
12
+
11
13
  describe 'all valid' do
12
14
  let(:path) { 'models/user/friend.rb' }
13
15
  before { File.should_receive(:file?).with(path).once.and_return(true) }
14
16
 
15
- context 'with spec_folder given' do
16
- let(:spec_folder) { 'spec/models/' }
17
+ context 'with spec_folder given' do
17
18
  it 'invoke Main::configure_and_generate with given spec_folder' do
18
19
  QuickShoulda.should_receive(:configure_and_generate).with(path, spec_folder)
19
20
  QuickShoulda.process(path, spec_folder)
@@ -25,39 +26,49 @@ describe 'QuickShoulda' do
25
26
  QuickShoulda.should_receive(:configure_and_generate).with(path, 'spec/models/')
26
27
  QuickShoulda.process(path)
27
28
  end
28
- end
29
-
29
+ end
30
30
  end
31
31
 
32
- describe 'path is nil' do
33
- it 'should raise QuickShoulda::Errors::PathNotGivenError' do
34
- expect {
35
- QuickShoulda.process(nil)
36
- }.to raise_error(QuickShoulda::Errors::PathNotGivenError)
32
+ context 'path is given' do
33
+ describe 'path is nil' do
34
+ it 'should raise QuickShoulda::Errors::PathNotGivenError' do
35
+ expect {
36
+ QuickShoulda.process(nil)
37
+ }.to raise_error(QuickShoulda::Errors::PathNotGivenError)
38
+ end
37
39
  end
38
- end
39
40
 
40
- describe 'path does not contain "models/"' do
41
- it 'should raise QuickShoulda::Errors::NotAModelPathError' do
42
- expect {
43
- QuickShoulda.process("not/contain/model/s")
44
- }.to raise_error(QuickShoulda::Errors::NotAModelPathError)
41
+ describe 'path does not contain "models/"' do
42
+ it 'should raise QuickShoulda::Errors::NotAModelPathError' do
43
+ expect {
44
+ QuickShoulda.process("not/contain/model/s")
45
+ }.to raise_error(QuickShoulda::Errors::NotAModelPathError)
46
+ end
45
47
  end
46
- end
47
48
 
48
- describe 'file does not exist' do
49
- it 'should raise QuickShoulda::Errors::FileDoesNotExistError' do
50
- expect {
51
- QuickShoulda.process("models/nothere.rb")
52
- }.to raise_error(QuickShoulda::Errors::FileDoesNotExistError)
49
+ describe 'file does not exist' do
50
+ it 'should raise QuickShoulda::Errors::FileDoesNotExistError' do
51
+ expect {
52
+ QuickShoulda.process("models/nothere.rb")
53
+ }.to raise_error(QuickShoulda::Errors::FileDoesNotExistError)
54
+ end
55
+ end
56
+
57
+ describe 'not a ruby file' do
58
+ it 'should raise QuickShoulda::Errors::NotRubyFileError' do
59
+ expect {
60
+ QuickShoulda.process("models/not_a_ruby_file.txt")
61
+ }.to raise_error(QuickShoulda::Errors::FileDoesNotExistError)
62
+ end
53
63
  end
54
64
  end
55
65
 
56
- describe 'not a ruby file' do
57
- it 'should raise QuickShoulda::Errors::NotRubyFileError' do
58
- expect {
59
- QuickShoulda.process("models/not_a_ruby_file.txt")
60
- }.to raise_error(QuickShoulda::Errors::FileDoesNotExistError)
66
+ context 'constant is given' do
67
+ let(:constant) { "User::Name" }
68
+
69
+ it 'should not raise any exception' do
70
+ QuickShoulda.should_receive(:configure_and_generate).with(constant, spec_folder)
71
+ expect { QuickShoulda.process(constant) }.not_to raise_error
61
72
  end
62
73
  end
63
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quick_shoulda
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-30 00:00:00.000000000 Z
12
+ date: 2013-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: debugger