ruby-pwsh 0.10.3 → 0.11.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,90 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe Pwsh::Util do
6
- context '.invalid_directories?' do
6
+ let(:snake_case_string) { 'this_is_a_string' }
7
+ let(:pascal_case_string) { 'ThisIsAString' }
8
+ let(:kebab_case_string) { 'this-is-a-string' }
9
+ let(:camel_case_string) { 'thisIsAString' }
10
+ let(:snake_case_hash_in_an_array) do
11
+ [
12
+ 'just a string',
13
+ {
14
+ some_key: 'a value'
15
+ },
16
+ 1,
17
+ {
18
+ another_key: {
19
+ nested_key: 1,
20
+ nested_array: [
21
+ 1,
22
+ 'another string',
23
+ { super_nested_key: 'value' }
24
+ ]
25
+ }
26
+ }
27
+ ]
28
+ end
29
+ let(:snake_case_hash) do
30
+ {
31
+ a: 1,
32
+ apple_butter: %w[a b c],
33
+ some_key_value: {
34
+ nested_key: 1,
35
+ another_nested_key: 2
36
+ }
37
+ }
38
+ end
39
+ let(:pascal_case_hash_in_an_array) do
40
+ [
41
+ 'just a string',
42
+ {
43
+ SomeKey: 'a value'
44
+ },
45
+ 1,
46
+ {
47
+ AnotherKey: {
48
+ NestedKey: 1,
49
+ NestedArray: [
50
+ 1,
51
+ 'another string',
52
+ { SuperNestedKey: 'value' }
53
+ ]
54
+ }
55
+ }
56
+ ]
57
+ end
58
+ let(:pascal_case_hash) do
59
+ {
60
+ A: 1,
61
+ AppleButter: %w[a b c],
62
+ SomeKeyValue: {
63
+ NestedKey: 1,
64
+ AnotherNestedKey: 2
65
+ }
66
+ }
67
+ end
68
+ let(:kebab_case_hash) do
69
+ {
70
+ a: 1,
71
+ 'apple-butter': %w[a b c],
72
+ 'some-key-value': {
73
+ 'nested-key': 1,
74
+ 'another-nested-key': 2
75
+ }
76
+ }
77
+ end
78
+ let(:camel_case_hash) do
79
+ {
80
+ a: 1,
81
+ appleButter: %w[a b c],
82
+ someKeyValue: {
83
+ nestedKey: 1,
84
+ anotherNestedKey: 2
85
+ }
86
+ }
87
+ end
88
+
89
+ describe '.invalid_directories?' do
7
90
  let(:valid_path_a) { 'C:/some/folder' }
8
91
  let(:valid_path_b) { 'C:/another/folder' }
9
92
  let(:valid_paths) { 'C:/some/folder;C:/another/folder' }
@@ -56,90 +139,7 @@ RSpec.describe Pwsh::Util do
56
139
  end
57
140
  end
58
141
 
59
- let(:camel_case_hash) do
60
- {
61
- a: 1,
62
- appleButter: %w[a b c],
63
- someKeyValue: {
64
- nestedKey: 1,
65
- anotherNestedKey: 2
66
- }
67
- }
68
- end
69
- let(:kebab_case_hash) do
70
- {
71
- a: 1,
72
- 'apple-butter': %w[a b c],
73
- 'some-key-value': {
74
- 'nested-key': 1,
75
- 'another-nested-key': 2
76
- }
77
- }
78
- end
79
- let(:pascal_case_hash) do
80
- {
81
- A: 1,
82
- AppleButter: %w[a b c],
83
- SomeKeyValue: {
84
- NestedKey: 1,
85
- AnotherNestedKey: 2
86
- }
87
- }
88
- end
89
- let(:pascal_case_hash_in_an_array) do
90
- [
91
- 'just a string',
92
- {
93
- SomeKey: 'a value'
94
- },
95
- 1,
96
- {
97
- AnotherKey: {
98
- NestedKey: 1,
99
- NestedArray: [
100
- 1,
101
- 'another string',
102
- { SuperNestedKey: 'value' }
103
- ]
104
- }
105
- }
106
- ]
107
- end
108
- let(:snake_case_hash) do
109
- {
110
- a: 1,
111
- apple_butter: %w[a b c],
112
- some_key_value: {
113
- nested_key: 1,
114
- another_nested_key: 2
115
- }
116
- }
117
- end
118
- let(:snake_case_hash_in_an_array) do
119
- [
120
- 'just a string',
121
- {
122
- some_key: 'a value'
123
- },
124
- 1,
125
- {
126
- another_key: {
127
- nested_key: 1,
128
- nested_array: [
129
- 1,
130
- 'another string',
131
- { super_nested_key: 'value' }
132
- ]
133
- }
134
- }
135
- ]
136
- end
137
- let(:camel_case_string) { 'thisIsAString' }
138
- let(:kebab_case_string) { 'this-is-a-string' }
139
- let(:pascal_case_string) { 'ThisIsAString' }
140
- let(:snake_case_string) { 'this_is_a_string' }
141
-
142
- context '.snake_case' do
142
+ describe '.snake_case' do
143
143
  it 'converts a string to snake_case' do
144
144
  expect(described_class.snake_case(camel_case_string)).to eq snake_case_string
145
145
  expect(described_class.snake_case(kebab_case_string)).to eq snake_case_string
@@ -147,7 +147,7 @@ RSpec.describe Pwsh::Util do
147
147
  end
148
148
  end
149
149
 
150
- context '.snake_case_hash_keys' do
150
+ describe '.snake_case_hash_keys' do
151
151
  it 'snake_cases the keys in a passed hash' do
152
152
  expect(described_class.snake_case_hash_keys(camel_case_hash)).to eq snake_case_hash
153
153
  expect(described_class.snake_case_hash_keys(kebab_case_hash)).to eq snake_case_hash
@@ -156,7 +156,7 @@ RSpec.describe Pwsh::Util do
156
156
  end
157
157
  end
158
158
 
159
- context '.pascal_case' do
159
+ describe '.pascal_case' do
160
160
  it 'converts a string to PascalCase' do
161
161
  expect(described_class.pascal_case(camel_case_string)).to eq pascal_case_string
162
162
  expect(described_class.pascal_case(kebab_case_string)).to eq pascal_case_string
@@ -164,7 +164,7 @@ RSpec.describe Pwsh::Util do
164
164
  end
165
165
  end
166
166
 
167
- context '.pascal_case_hash_keys' do
167
+ describe '.pascal_case_hash_keys' do
168
168
  it 'PascalCases the keys in a passed hash' do
169
169
  expect(described_class.pascal_case_hash_keys(camel_case_hash)).to eq pascal_case_hash
170
170
  expect(described_class.pascal_case_hash_keys(kebab_case_hash)).to eq pascal_case_hash
@@ -173,7 +173,7 @@ RSpec.describe Pwsh::Util do
173
173
  end
174
174
  end
175
175
 
176
- context '.symbolize_hash_keys' do
176
+ describe '.symbolize_hash_keys' do
177
177
  let(:array_with_string_keys_in_hashes) do
178
178
  [
179
179
  'just a string',
@@ -218,7 +218,7 @@ RSpec.describe Pwsh::Util do
218
218
  end
219
219
  end
220
220
 
221
- context '.escape_quotes' do
221
+ describe '.escape_quotes' do
222
222
  it 'handles single quotes' do
223
223
  expect(described_class.escape_quotes("The 'Cats' go 'meow'!")).to match(/The ''Cats'' go ''meow''!/)
224
224
  end
@@ -232,11 +232,11 @@ RSpec.describe Pwsh::Util do
232
232
  end
233
233
 
234
234
  it 'handles dollar signs' do
235
- expect(described_class.escape_quotes("This should show \$foo variable")).to match(/This should show \$foo variable/)
235
+ expect(described_class.escape_quotes('This should show $foo variable')).to match(/This should show \$foo variable/)
236
236
  end
237
237
  end
238
238
 
239
- context '.format_powershell_value' do
239
+ describe '.format_powershell_value' do
240
240
  let(:ruby_array) { ['string', 1, :symbol, true] }
241
241
  let(:powershell_array) { "@('string', 1, symbol, $true)" }
242
242
  let(:ruby_hash) do
@@ -253,32 +253,39 @@ RSpec.describe Pwsh::Util do
253
253
  }
254
254
  end
255
255
  let(:powershell_hash) { "@{string = 'string'; number = 1; symbol = some_symbol; boolean = $true; nested_hash = @{another_string = 'foo'; another_number = 2; array = @(1, 2, 3)}}" }
256
+
256
257
  it 'returns a symbol as a non-interpolated string' do
257
258
  expect(described_class.format_powershell_value(:apple)).to eq('apple')
258
259
  end
260
+
259
261
  it 'returns a number as a non interpolated string' do
260
262
  expect(described_class.format_powershell_value(101)).to eq('101')
261
263
  expect(described_class.format_powershell_value(1.1)).to eq('1.1')
262
264
  end
265
+
263
266
  it 'returns boolean values as the appropriate PowerShell automatic variable' do
264
267
  expect(described_class.format_powershell_value(true)).to eq('$true')
265
268
  expect(described_class.format_powershell_value(:false)).to eq('$false') # rubocop:disable Lint/BooleanSymbol
266
269
  end
270
+
267
271
  it 'returns a string as an escaped string' do
268
272
  expect(described_class.format_powershell_value('some string')).to eq("'some string'")
269
273
  end
274
+
270
275
  it 'returns an array as a string representing a PowerShell array' do
271
276
  expect(described_class.format_powershell_value(ruby_array)).to eq(powershell_array)
272
277
  end
278
+
273
279
  it 'returns a hash as a string representing a PowerShell hash' do
274
280
  expect(described_class.format_powershell_value(ruby_hash)).to eq(powershell_hash)
275
281
  end
282
+
276
283
  it 'raises an error if an unknown type is passed' do
277
284
  expect { described_class.format_powershell_value(described_class) }.to raise_error(/unsupported type Module/)
278
285
  end
279
286
  end
280
287
 
281
- context '.custom_powershell_property' do
288
+ describe '.custom_powershell_property' do
282
289
  it 'returns a powershell hash with the name and expression keys' do
283
290
  expect(described_class.custom_powershell_property('apple', '$_.SomeValue / 5')).to eq("@{Name = 'apple'; Expression = {$_.SomeValue / 5}}")
284
291
  end
@@ -4,7 +4,7 @@ require 'spec_helper'
4
4
 
5
5
  RSpec.describe Pwsh do
6
6
  it 'has a version number' do
7
- expect(described_class::VERSION).not_to be nil
8
- expect(described_class::VERSION).to be_a_kind_of(String)
7
+ expect(described_class::VERSION).not_to be_nil
8
+ expect(described_class::VERSION).to be_a(String)
9
9
  end
10
10
  end
@@ -6,7 +6,7 @@ RSpec.describe Pwsh::WindowsPowerShell do
6
6
  describe '.version' do
7
7
  context 'on non-Windows platforms', unless: Pwsh::Util.on_windows? do
8
8
  it 'is not defined' do
9
- expect(defined?(described_class.version)).to eq(nil)
9
+ expect(defined?(described_class.version)).to be_nil
10
10
  end
11
11
  end
12
12
 
@@ -18,7 +18,7 @@ RSpec.describe Pwsh::WindowsPowerShell do
18
18
  end
19
19
 
20
20
  it 'calls the Windows PowerShell three registry path' do
21
- reg_key = instance_double('bob')
21
+ reg_key = instance_double(bob)
22
22
  allow(reg_key).to receive(:[]).with('PowerShellVersion').and_return('5.0.10514.6')
23
23
  allow_any_instance_of(Win32::Registry).to receive(:open).with('SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine', Win32::Registry::KEY_READ | 0x100).and_yield(reg_key)
24
24
 
@@ -26,7 +26,7 @@ RSpec.describe Pwsh::WindowsPowerShell do
26
26
  end
27
27
 
28
28
  it 'does not call Windows PowerShell one registry path' do
29
- reg_key = instance_double('bob')
29
+ reg_key = instance_double(bob)
30
30
  allow(reg_key).to receive(:[]).with('PowerShellVersion').and_return('5.0.10514.6')
31
31
  allow_any_instance_of(Win32::Registry).to receive(:open).with('SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine', Win32::Registry::KEY_READ | 0x100).and_yield(reg_key)
32
32
  expect_any_instance_of(Win32::Registry).not_to receive(:open).with('SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine', Win32::Registry::KEY_READ | 0x100)
@@ -43,7 +43,7 @@ RSpec.describe Pwsh::WindowsPowerShell do
43
43
  end
44
44
 
45
45
  it 'calls the Windows PowerShell one registry path' do
46
- reg_key = instance_double('bob')
46
+ reg_key = instance_double(bob)
47
47
  allow(reg_key).to receive(:[]).with('PowerShellVersion').and_return('2.0')
48
48
  allow_any_instance_of(Win32::Registry).to receive(:open).with('SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine',
49
49
  Win32::Registry::KEY_READ | 0x100).and_yield(reg_key)
@@ -54,14 +54,14 @@ RSpec.describe Pwsh::WindowsPowerShell do
54
54
  end
55
55
  end
56
56
 
57
- context 'when Windows PowerShell is not installed' do
57
+ context 'when Windows PowerShell is not installed' do
58
58
  it 'returns nil and not throw' do
59
59
  allow_any_instance_of(Win32::Registry).to receive(:open).with('SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine',
60
60
  Win32::Registry::KEY_READ | 0x100).and_raise(Win32::Registry::Error.new(2), 'nope')
61
61
  allow_any_instance_of(Win32::Registry).to receive(:open).with('SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine',
62
62
  Win32::Registry::KEY_READ | 0x100).and_raise(Win32::Registry::Error.new(2), 'nope')
63
63
 
64
- expect(described_class.version).to eq(nil)
64
+ expect(described_class.version).to be_nil
65
65
  end
66
66
  end
67
67
  end
@@ -70,7 +70,7 @@ RSpec.describe Pwsh::WindowsPowerShell do
70
70
  describe '.compatible_version?' do
71
71
  context 'on non-Windows platforms', unless: Pwsh::Util.on_windows? do
72
72
  it 'returns false' do
73
- expect(described_class.compatible_version?).to eq(false)
73
+ expect(described_class.compatible_version?).to be(false)
74
74
  end
75
75
  end
76
76
 
@@ -78,37 +78,42 @@ RSpec.describe Pwsh::WindowsPowerShell do
78
78
  context 'when the Windows PowerShell major version is nil' do
79
79
  it 'returns false' do
80
80
  expect(described_class).to receive(:version).and_return(nil)
81
- expect(described_class.compatible_version?).to eq(false)
81
+ expect(described_class.compatible_version?).to be(false)
82
82
  end
83
83
  end
84
+
84
85
  context 'when the Windows PowerShell major version is less than two' do
85
86
  it 'returns false' do
86
87
  expect(described_class).to receive(:version).and_return('1.0')
87
- expect(described_class.compatible_version?).to eq(false)
88
+ expect(described_class.compatible_version?).to be(false)
88
89
  end
89
90
  end
91
+
90
92
  context 'when the Windows PowerShell major version is two' do
91
93
  it 'returns true if .NET 3.5 is installed' do
92
94
  expect(described_class).to receive(:version).and_return('2.0')
93
95
  allow_any_instance_of(Win32::Registry).to receive(:open).with('SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5', Win32::Registry::KEY_READ | 0x100).and_yield
94
- expect(described_class.compatible_version?).to eq(true)
96
+ expect(described_class.compatible_version?).to be(true)
95
97
  end
98
+
96
99
  it 'returns false if .NET 3.5 is not installed' do
97
100
  expect(described_class).to receive(:version).and_return('2.0')
98
101
  allow_any_instance_of(Win32::Registry).to receive(:open).with('SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5', Win32::Registry::KEY_READ | 0x100).and_raise(Win32::Registry::Error, 1)
99
- expect(described_class.compatible_version?).to eq(false)
102
+ expect(described_class.compatible_version?).to be(false)
100
103
  end
101
104
  end
105
+
102
106
  context 'when the Windows PowerShell major version is three' do
103
107
  it 'returns true' do
104
108
  expect(described_class).to receive(:version).and_return('3.0')
105
- expect(described_class.compatible_version?).to eq(true)
109
+ expect(described_class.compatible_version?).to be(true)
106
110
  end
107
111
  end
112
+
108
113
  context 'when the Windows PowerShell major version is greater than three' do
109
114
  it 'returns true' do
110
115
  expect(described_class).to receive(:version).and_return('4.0')
111
- expect(described_class.compatible_version?).to eq(true)
116
+ expect(described_class.compatible_version?).to be(true)
112
117
  end
113
118
  end
114
119
  end