Skip to content

Commit 00c9d85

Browse files
authored
Merge pull request #247 from buty4649/add-raw-string-option-to-yaml-filter
Add comprehensive tests for YAML filter with various options
2 parents 119bf84 + 27fcf7d commit 00c9d85

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

mrblib/rf/00filter/yaml.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ module Filter
33
class Yaml < Base
44
class << self
55
def config
6-
@config ||= Struct.new(:no_doc, :boolean_mode).new.tap do |config|
6+
@config ||= Struct.new(:no_doc, :boolean_mode, :raw).new.tap do |config|
77
config.no_doc = true
88
config.boolean_mode = true
9+
config.raw = false
910
end
1011
end
1112

1213
def configure(opt)
14+
opt.on('-r', '--raw-string', 'output raw strings') do
15+
config.raw = true
16+
end
1317
opt.on('--disable-boolean-mode', 'consider true/false/null as yaml literal') do
1418
config.boolean_mode = false
1519
end
@@ -26,6 +30,10 @@ def boolean_mode?
2630
config.boolean_mode
2731
end
2832

33+
def raw?
34+
config.raw
35+
end
36+
2937
def format(val, record)
3038
return unless yaml_obj = obj_to_yaml(val, record)
3139

@@ -36,6 +44,12 @@ def format(val, record)
3644

3745
def obj_to_yaml(val, record)
3846
case val
47+
when String
48+
if raw?
49+
val
50+
else
51+
val.to_yaml(colorize:)
52+
end
3953
when MatchData
4054
record.to_yaml(colorize:)
4155
when Regexp

spec/filter/yaml_spec.rb

+24-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,32 @@
1414
context 'with -r option' do
1515
describe 'Output string' do
1616
let(:input) { load_fixture('yaml/multibyte_string.yml') }
17-
let(:output) { '🍣🍣🍣' }
17+
let(:args) { '-t yaml -r _' }
18+
let(:expect_output) { "🍣🍣🍣\n" }
1819

19-
before { run_rf('-t yaml -r _', input) }
20+
it_behaves_like 'a successful exec'
21+
end
2022

21-
it { expect(last_command_started).to be_successfully_executed }
22-
it { expect(last_command_started).to have_output output_string_eq output }
23+
describe 'Ouput yaml to JSON string' do
24+
let(:input) { load_fixture('json/hash.json') }
25+
let(:args) { '-t yaml -r "_.to_json(pretty_print: true)"' }
26+
let(:expect_output) do
27+
<<~OUTPUT
28+
{
29+
"foo": 1,
30+
"bar": {
31+
"baz": [
32+
"a",
33+
"b",
34+
"c"
35+
]
36+
},
37+
"foo bar": "foo bar"
38+
}
39+
OUTPUT
40+
end
41+
42+
it_behaves_like 'a successful exec'
2343
end
2444
end
2545

spec/global_options/help_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
-m, --minify minify json output
3232
3333
yaml options:
34+
-r, --raw-string output raw strings
3435
--disable-boolean-mode consider true/false/null as yaml literal
3536
--[no-]doc [no] output document sperator (refers to ---) (default:--no-doc)
3637
TEXT

0 commit comments

Comments
 (0)