Skip to content

Commit 4044383

Browse files
committed
add compose refinements specs
1 parent d47fa8d commit 4044383

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

lib/bashly/refinements/compose_refinements.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module ComposeRefinements
44
refine Hash do
5-
def compose(keyword = 'load')
5+
def compose(keyword = 'import')
66
result = {}
77
each do |k, v|
88
if k.to_s == keyword
@@ -23,7 +23,7 @@ def compose(keyword = 'load')
2323
end
2424

2525
refine Array do
26-
def compose(keyword = 'load')
26+
def compose(keyword = 'import')
2727
map do |x|
2828
if x.respond_to? :compose
2929
x.compose keyword
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
array:
3+
- hello
4+
- - hello
5+
- key: value
6+
loaded: yes indeed
7+
- key: value
8+
loaded: yes indeed
9+
args:
10+
- name: protocol
11+
required: true
12+
- name: user
13+
allowed:
14+
- root
15+
- admin

spec/bashly/extensions/string_spec.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
describe Array do
3+
describe String do
44
describe '#sanitize_for_print' do
55
subject { %Q[this is\na "new line"] }
66
it "escapes newlines and quotes" do
@@ -72,4 +72,34 @@
7272
end
7373
end
7474
end
75+
76+
describe '#remove_front_matter' do
77+
context "with a string that does not have front matter" do
78+
subject { "this is a\nsample script" }
79+
80+
it "returns it as is" do
81+
expect(subject.remove_front_matter).to eq subject
82+
end
83+
end
84+
85+
context "with a string that contains front matter" do
86+
subject { "#{front_matter}\n---\n#{rest}" }
87+
let(:front_matter) { "this is the front matter" }
88+
let(:rest) { "this is\nthe script" }
89+
90+
it "returns the string without the front matter" do
91+
expect(subject.remove_front_matter).to eq rest
92+
end
93+
end
94+
95+
context "with a string that contains front matter with a leading separator" do
96+
subject { "---\n#{front_matter}\n---\n#{rest}" }
97+
let(:front_matter) { "this is the front matter" }
98+
let(:rest) { "this is\nthe script" }
99+
100+
it "returns the string without the front matter" do
101+
expect(subject.remove_front_matter).to eq rest
102+
end
103+
end
104+
end
75105
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'spec_helper'
2+
3+
describe ComposeRefinements, :focus do
4+
using ComposeRefinements
5+
6+
subject do
7+
{
8+
"array" => [string, array, hash],
9+
"args" => imported_array
10+
}
11+
end
12+
13+
let(:string) { "hello" }
14+
let(:array) { [string, hash] }
15+
let(:hash) { { "key" => 'value', "import" => fixture } }
16+
let(:imported_array) { { "import" => 'spec/fixtures/array.yml' } }
17+
let(:fixture) { 'spec/fixtures/config.yml' }
18+
19+
describe '#compose' do
20+
it "calls #compose on all elements that respond to #compose" do
21+
expect(subject.compose.to_yaml).to match_approval "refinements/compose/array"
22+
end
23+
end
24+
end

spec/fixtures/array.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- name: protocol
2+
required: true
3+
- name: user
4+
allowed: [root, admin]

0 commit comments

Comments
 (0)