Skip to content

Commit

Permalink
Merge pull request #4074 from abetomo/use-config_element-in-tests
Browse files Browse the repository at this point in the history
test: use `Fluent::Config::Element` in test
  • Loading branch information
ashie authored Mar 7, 2023
2 parents 8e9f46a + 28c9bef commit c0125d8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
10 changes: 5 additions & 5 deletions test/compat/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ def test_lookup_known_parser(data)

def test_parse_with_return
parser = Fluent::TextParser.new
parser.configure('format' => 'none')
parser.configure(config_element('test', '', 'format' => 'none'))
_time, record = parser.parse('log message!')
assert_equal({'message' => 'log message!'}, record)
end

def test_parse_with_block
parser = Fluent::TextParser.new
parser.configure('format' => 'none')
parser.configure(config_element('test', '', 'format' => 'none'))
parser.parse('log message!') { |time, record|
assert_equal({'message' => 'log message!'}, record)
}
end

def test_multi_event_parser
parser = Fluent::TextParser.new
parser.configure('format' => 'multi_event_test')
parser.configure(config_element('test', '', 'format' => 'multi_event_test'))
i = 0
parser.parse('log message!') { |time, record|
assert_equal('log message!', record['message'])
Expand All @@ -67,7 +67,7 @@ def test_setting_estimate_current_event_value
assert_nil p1.estimate_current_event
assert_nil p1.parser

p1.configure('format' => 'none')
p1.configure(config_element('test', '', 'format' => 'none'))
assert_equal true, p1.parser.estimate_current_event

p2 = Fluent::TextParser.new
Expand All @@ -76,7 +76,7 @@ def test_setting_estimate_current_event_value

p2.estimate_current_event = false

p2.configure('format' => 'none')
p2.configure(config_element('test', '', 'format' => 'none'))
assert_equal false, p2.parser.estimate_current_event
end

Expand Down
54 changes: 29 additions & 25 deletions test/test_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ def record
{'message' => 'awesome', 'greeting' => 'hello'}
end

def config_element_in_params(params)
config_element('test', '', params)
end

class BaseFormatterTest < ::Test::Unit::TestCase
include FormatterTest

def test_call
formatter = Formatter.new
formatter.configure({})
formatter.configure(config_element())
assert_raise NotImplementedError do
formatter.format('tag', Engine.now, {})
end
Expand Down Expand Up @@ -135,7 +139,7 @@ def setup
end

def test_format
@formatter.configure({})
@formatter.configure(config_element())
formatted = @formatter.format(tag, @time, record)

assert_equal(record.to_msgpack, formatted)
Expand All @@ -159,27 +163,27 @@ def test_config_params
assert_equal "\t", @formatter.delimiter
assert_equal ":", @formatter.label_delimiter

@formatter.configure(
@formatter.configure(config_element_in_params(
'delimiter' => ',',
'label_delimiter' => '=',
)
))

assert_equal ",", @formatter.delimiter
assert_equal "=", @formatter.label_delimiter
end

def test_format
@formatter.configure({})
@formatter.configure(config_element())
formatted = @formatter.format(tag, @time, record)

assert_equal("message:awesome\tgreeting:hello#{@newline}", formatted)
end

def test_format_with_customized_delimiters
@formatter.configure(
@formatter.configure(config_element_in_params(
'delimiter' => ',',
'label_delimiter' => '=',
)
))
formatted = @formatter.format(tag, @time, record)

assert_equal("message=awesome,greeting=hello#{@newline}", formatted)
Expand All @@ -190,26 +194,26 @@ def record_with_tab
end

def test_format_suppresses_tab
@formatter.configure({})
@formatter.configure(config_element())
formatted = @formatter.format(tag, @time, record_with_tab)

assert_equal("message:awe some\tgreeting:hello #{@newline}", formatted)
end

def test_format_suppresses_tab_custom_replacement
@formatter.configure(
@formatter.configure(config_element_in_params(
'replacement' => 'X',
)
))
formatted = @formatter.format(tag, @time, record_with_tab)

assert_equal("message:aweXsome\tgreeting:helloX#{@newline}", formatted)
end

def test_format_suppresses_custom_delimiter
@formatter.configure(
@formatter.configure(config_element_in_params(
'delimiter' => 'w',
'label_delimiter' => '=',
)
))
formatted = @formatter.format(tag, @time, record)

assert_equal("message=a esomewgreeting=hello#{@newline}", formatted)
Expand All @@ -236,13 +240,13 @@ def test_config_params
'pipe' => ['|', '|'])
def test_config_params_with_customized_delimiters(data)
expected, target = data
@formatter.configure('delimiter' => target, 'fields' => 'a,b,c')
@formatter.configure(config_element_in_params('delimiter' => target, 'fields' => 'a,b,c'))
assert_equal expected, @formatter.delimiter
assert_equal ['a', 'b', 'c'], @formatter.fields
end

def test_format
@formatter.configure('fields' => 'message,message2')
@formatter.configure(config_element_in_params('fields' => 'message,message2'))
formatted = @formatter.format(tag, @time, {
'message' => 'awesome',
'message2' => 'awesome2'
Expand All @@ -251,10 +255,10 @@ def test_format
end

def test_format_with_customized_delimiters
@formatter.configure(
@formatter.configure(config_element_in_params(
'fields' => 'message,message2',
'delimiter' => '\t'
)
))
formatted = @formatter.format(tag, @time, {
'message' => 'awesome',
'message2' => 'awesome2'
Expand All @@ -263,10 +267,10 @@ def test_format_with_customized_delimiters
end

def test_format_with_non_quote
@formatter.configure(
@formatter.configure(config_element_in_params(
'fields' => 'message,message2',
'force_quotes' => 'false'
)
))
formatted = @formatter.format(tag, @time, {
'message' => 'awesome',
'message2' => 'awesome2'
Expand All @@ -286,9 +290,9 @@ def test_format_with_non_quote
'message3' => 'awesome3'
})
def test_format_with_empty_fields(data)
@formatter.configure(
@formatter.configure(config_element_in_params(
'fields' => 'message,message2,message3'
)
))
formatted = @formatter.format(tag, @time, data)
assert_equal("\"awesome\",\"\",\"awesome3\"\n", formatted)
end
Expand All @@ -298,7 +302,7 @@ def test_format_with_empty_fields(data)
'white_space' => 'one , two , three',
'blank' => 'one,,two,three')
def test_config_params_with_fields(data)
@formatter.configure('fields' => data)
@formatter.configure(config_element_in_params('fields' => data))
assert_equal %w(one two three), @formatter.fields
end
end
Expand All @@ -318,27 +322,27 @@ def test_config_params
formatter = TextFormatter::SingleValueFormatter.new
assert_equal "message", formatter.message_key

formatter.configure('message_key' => 'foobar')
formatter.configure(config_element_in_params('message_key' => 'foobar'))
assert_equal "foobar", formatter.message_key
end

def test_format
formatter = Fluent::Plugin.new_formatter('single_value')
formatter.configure({})
formatter.configure(config_element())
formatted = formatter.format('tag', Engine.now, {'message' => 'awesome'})
assert_equal("awesome#{@newline}", formatted)
end

def test_format_without_newline
formatter = Fluent::Plugin.new_formatter('single_value')
formatter.configure('add_newline' => 'false')
formatter.configure(config_element_in_params('add_newline' => 'false'))
formatted = formatter.format('tag', Engine.now, {'message' => 'awesome'})
assert_equal("awesome", formatted)
end

def test_format_with_message_key
formatter = TextFormatter::SingleValueFormatter.new
formatter.configure('message_key' => 'foobar')
formatter.configure(config_element_in_params('message_key' => 'foobar'))
formatted = formatter.format('tag', Engine.now, {'foobar' => 'foo'})

assert_equal("foo#{@newline}", formatted)
Expand Down

0 comments on commit c0125d8

Please sign in to comment.