File tree Expand file tree Collapse file tree 10 files changed +112
-124
lines changed Expand file tree Collapse file tree 10 files changed +112
-124
lines changed Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ module Script
28
28
module Introspection
29
29
autoloads 'bashly/script/introspection' , %i[
30
30
Arguments Commands Dependencies EnvironmentVariables Examples Flags
31
- Variables Visibility
31
+ Validate Variables Visibility
32
32
]
33
33
end
34
34
end
Original file line number Diff line number Diff line change 3
3
module Bashly
4
4
module Script
5
5
class Argument < Base
6
+ include Introspection ::Validate
7
+
6
8
class << self
7
9
def option_keys
8
10
@option_keys ||= %i[
@@ -28,15 +30,6 @@ def label
28
30
def usage_string
29
31
required ? label : "[#{ label } ]"
30
32
end
31
-
32
- def validate
33
- return [ ] unless options [ 'validate' ]
34
-
35
- result = options [ 'validate' ]
36
- result . is_a? ( Array ) ? result : [ result ]
37
- end
38
-
39
- def validate? = validate . any?
40
33
end
41
34
end
42
35
end
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ module Bashly
2
2
module Script
3
3
class EnvironmentVariable < Base
4
4
include Introspection ::Visibility
5
+ include Introspection ::Validate
5
6
6
7
class << self
7
8
def option_keys
@@ -16,15 +17,6 @@ def usage_string(extended: false)
16
17
result << strings [ :required ] if required && extended
17
18
result . join ' '
18
19
end
19
-
20
- def validate
21
- return [ ] unless options [ 'validate' ]
22
-
23
- result = options [ 'validate' ]
24
- result . is_a? ( Array ) ? result : [ result ]
25
- end
26
-
27
- def validate? = validate . any?
28
20
end
29
21
end
30
22
end
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ module Script
5
5
class Flag < Base
6
6
include Completions ::Flag
7
7
include Introspection ::Visibility
8
+ include Introspection ::Validate
8
9
9
10
class << self
10
11
def option_keys
@@ -46,15 +47,6 @@ def usage_string(extended: false)
46
47
result << strings [ :repeatable ] if repeatable && extended
47
48
result . join ' '
48
49
end
49
-
50
- def validate
51
- return [ ] unless options [ 'validate' ]
52
-
53
- result = options [ 'validate' ]
54
- result . is_a? ( Array ) ? result : [ result ]
55
- end
56
-
57
- def validate? = validate . any?
58
50
end
59
51
end
60
52
end
Original file line number Diff line number Diff line change
1
+ module Bashly
2
+ module Script
3
+ module Introspection
4
+ module Validate
5
+ # Returns an array of validations
6
+ def validate
7
+ return [ ] unless options [ 'validate' ]
8
+
9
+ result = options [ 'validate' ]
10
+ result . is_a? ( Array ) ? result : [ result ]
11
+ end
12
+
13
+ # Returns true if there are any validations defined
14
+ def validate? = validate . any?
15
+ end
16
+ end
17
+ end
18
+ end
Original file line number Diff line number Diff line change 6
6
7
7
let ( :fixture ) { :basic_argument }
8
8
9
+ describe 'composition' do
10
+ it 'includes the necessary modules' do
11
+ modules = [ Script ::Introspection ::Validate ]
12
+ modules . each do |mod |
13
+ expect ( described_class . ancestors ) . to include ( mod )
14
+ end
15
+ end
16
+ end
17
+
9
18
describe '#default_string' do
10
19
context 'when default is an array' do
11
20
let ( :fixture ) { :default_array }
59
68
end
60
69
end
61
70
end
62
-
63
- describe '#validate' do
64
- context 'with a string value' do
65
- let ( :fixture ) { :validate_string }
66
-
67
- it 'returns it as an array' do
68
- expect ( subject . validate ) . to eq [ 'file_exists' ]
69
- end
70
- end
71
-
72
- context 'with an array value' do
73
- let ( :fixture ) { :validate_array }
74
-
75
- it 'returns it as is' do
76
- expect ( subject . validate ) . to eq [ 'file_exists' , 'file_is_writable' ]
77
- end
78
- end
79
- end
80
-
81
- describe '#validate?' do
82
- it 'returns false' do
83
- expect ( subject . validate? ) . to be false
84
- end
85
-
86
- context 'when validations are defined' do
87
- let ( :fixture ) { :validate_string }
88
-
89
- it 'returns true' do
90
- expect ( subject . validate? ) . to be true
91
- end
92
- end
93
- end
94
71
end
Original file line number Diff line number Diff line change 8
8
let ( :fixtures ) { load_fixture 'script/commands' }
9
9
let ( :fixture ) { :basic_command }
10
10
11
+ describe 'composition' do
12
+ it 'includes the necessary modules' do
13
+ modules = [
14
+ Script ::Introspection ::Arguments ,
15
+ Script ::Introspection ::Commands ,
16
+ Script ::Introspection ::Dependencies ,
17
+ Script ::Introspection ::EnvironmentVariables ,
18
+ Script ::Introspection ::Examples ,
19
+ Script ::Introspection ::Flags ,
20
+ Script ::Introspection ::Variables ,
21
+ Script ::Introspection ::Visibility ,
22
+ Completions ::Command
23
+ ]
24
+ modules . each do |mod |
25
+ expect ( described_class . ancestors ) . to include ( mod )
26
+ end
27
+ end
28
+ end
29
+
11
30
describe '#action_name' do
12
31
context 'when it is the root command' do
13
32
it 'returns root' do
Original file line number Diff line number Diff line change 6
6
7
7
let ( :fixture ) { :basic_env_var }
8
8
9
+ describe 'composition' do
10
+ it 'includes the necessary modules' do
11
+ modules = [ Script ::Introspection ::Visibility , Script ::Introspection ::Validate ]
12
+ modules . each do |mod |
13
+ expect ( described_class . ancestors ) . to include ( mod )
14
+ end
15
+ end
16
+ end
17
+
9
18
describe '#usage_string' do
10
19
it 'returns a string suitable to be used as a usage pattern' do
11
20
expect ( subject . usage_string ) . to eq 'BUILD_DIR'
27
36
end
28
37
end
29
38
end
30
-
31
- describe '#validate' do
32
- context 'with a string value' do
33
- let ( :fixture ) { :validate_string }
34
-
35
- it 'returns it as an array' do
36
- expect ( subject . validate ) . to eq [ 'dir_exists' ]
37
- end
38
- end
39
-
40
- context 'with an array value' do
41
- let ( :fixture ) { :validate_array }
42
-
43
- it 'returns it as is' do
44
- expect ( subject . validate ) . to eq [ 'dir_exists' , 'dir_is_writable' ]
45
- end
46
- end
47
- end
48
-
49
- describe '#validate?' do
50
- it 'returns false' do
51
- expect ( subject . validate? ) . to be false
52
- end
53
-
54
- context 'when validations are defined' do
55
- let ( :fixture ) { :validate_string }
56
-
57
- it 'returns true' do
58
- expect ( subject . validate? ) . to be true
59
- end
60
- end
61
- end
62
39
end
Original file line number Diff line number Diff line change 6
6
7
7
let ( :fixture ) { :basic_flag }
8
8
9
+ describe 'composition' do
10
+ it 'includes the necessary modules' do
11
+ modules = [
12
+ Script ::Introspection ::Visibility , Script ::Introspection ::Validate ,
13
+ Completions ::Flag
14
+ ]
15
+ modules . each do |mod |
16
+ expect ( described_class . ancestors ) . to include ( mod )
17
+ end
18
+ end
19
+ end
20
+
9
21
describe '#aliases' do
10
22
context 'with long and short options' do
11
23
it 'returns an array of both long and short values' do
109
121
end
110
122
end
111
123
end
112
-
113
- describe '#validate' do
114
- context 'with a string value' do
115
- let ( :fixture ) { :validate_string }
116
-
117
- it 'returns it as an array' do
118
- expect ( subject . validate ) . to eq [ 'file_exists' ]
119
- end
120
- end
121
-
122
- context 'with an array value' do
123
- let ( :fixture ) { :validate_array }
124
-
125
- it 'returns it as is' do
126
- expect ( subject . validate ) . to eq [ 'file_exists' , 'file_is_writable' ]
127
- end
128
- end
129
- end
130
-
131
- describe '#validate?' do
132
- it 'returns false' do
133
- expect ( subject . validate? ) . to be false
134
- end
135
-
136
- context 'when validations are defined' do
137
- let ( :fixture ) { :validate_string }
138
-
139
- it 'returns true' do
140
- expect ( subject . validate? ) . to be true
141
- end
142
- end
143
- end
144
124
end
Original file line number Diff line number Diff line change
1
+ describe Script ::Introspection ::Validate do
2
+ subject do
3
+ Script ::Flag . new fixtures [ fixture ]
4
+ end
5
+
6
+ let ( :fixtures ) { load_fixture 'script/arguments' }
7
+ let ( :fixture ) { :basic_argument }
8
+
9
+ describe '#validate' do
10
+ context 'with a string value' do
11
+ let ( :fixture ) { :validate_string }
12
+
13
+ it 'returns it as an array' do
14
+ expect ( subject . validate ) . to eq [ 'file_exists' ]
15
+ end
16
+ end
17
+
18
+ context 'with an array value' do
19
+ let ( :fixture ) { :validate_array }
20
+
21
+ it 'returns it as is' do
22
+ expect ( subject . validate ) . to eq [ 'file_exists' , 'file_is_writable' ]
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '#validate?' do
28
+ it 'returns false' do
29
+ expect ( subject . validate? ) . to be false
30
+ end
31
+
32
+ context 'when validations are defined' do
33
+ let ( :fixture ) { :validate_string }
34
+
35
+ it 'returns true' do
36
+ expect ( subject . validate? ) . to be true
37
+ end
38
+ end
39
+ end
40
+ end
You can’t perform that action at this time.
0 commit comments