Skip to content

Commit

Permalink
Set proc to look up type against sub proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
okkez committed Apr 20, 2016
1 parent 544281f commit 4d43a98
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
10 changes: 6 additions & 4 deletions lib/fluent/config/configure_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,19 @@ def desc(description)
@current_description = description
end

def config_section(name, *args, &block)
def config_section(name, opts = {}, &block)
unless block_given?
raise ArgumentError, "#{self.name}: config_section requires block parameter"
end
name = name.to_sym

unless args.empty? || args.size == 1 && args.first.is_a?(Hash)
raise ArgumentError, "#{self.name}: unknown config_section arguments: #{args.inspect}"
unless opts.is_a?(Hash)
raise ArgumentError, "#{self.name}: unknown config_section arguments: #{opts.inspect}"
end

sub_proxy = ConfigureProxy.new(name, *args)
opts[:type_lookup] = @type_lookup

sub_proxy = ConfigureProxy.new(name, opts)
sub_proxy.instance_exec(&block)

if sub_proxy.init?
Expand Down
54 changes: 29 additions & 25 deletions test/config/test_configure_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

module Fluent::Config
class TestConfigureProxy < ::Test::Unit::TestCase
setup do
@type_lookup = ->(type) { Fluent::Configurable.lookup_type(type) }
end

sub_test_case 'to generate a instance' do
sub_test_case '#initialize' do
test 'has default values' do
Expand Down Expand Up @@ -134,67 +138,67 @@ class TestConfigureProxy < ::Test::Unit::TestCase
end

sub_test_case '#config_param / #config_set_default / #config_argument' do
setup do
@proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
end

test 'does not permit config_set_default for param w/ :default option' do
proxy = Fluent::Config::ConfigureProxy.new(:section)
proxy.config_param(:name, :string, default: "name1")
assert_raise(ArgumentError) { proxy.config_set_default(:name, "name2") }
@proxy.config_param(:name, :string, default: "name1")
assert_raise(ArgumentError) { @proxy.config_set_default(:name, "name2") }
end

test 'does not permit default value specification twice' do
proxy = Fluent::Config::ConfigureProxy.new(:section)
proxy.config_param(:name, :string)
proxy.config_set_default(:name, "name1")
assert_raise(ArgumentError) { proxy.config_set_default(:name, "name2") }
@proxy.config_param(:name, :string)
@proxy.config_set_default(:name, "name1")
assert_raise(ArgumentError) { @proxy.config_set_default(:name, "name2") }
end

test 'does not permit default value specification twice, even on config_argument' do
proxy = Fluent::Config::ConfigureProxy.new(:section)
proxy.config_param(:name, :string)
proxy.config_set_default(:name, "name1")
@proxy.config_param(:name, :string)
@proxy.config_set_default(:name, "name1")

proxy.config_argument(:name)
assert_raise(ArgumentError) { proxy.config_argument(:name, default: "name2") }
@proxy.config_argument(:name)
assert_raise(ArgumentError) { @proxy.config_argument(:name, default: "name2") }
end
end

sub_test_case '#config_param without default values cause error if section is configured as init:true' do
setup do
@proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
end

test 'with simple config_param with default value' do
proxy = Fluent::Config::ConfigureProxy.new(:section)
assert_nothing_raised do
proxy.config_section :subsection, init: true do
@proxy.config_section :subsection, init: true do
config_param :param1, :integer, default: 1
end
end
end
test 'with simple config_param without default value' do
proxy = Fluent::Config::ConfigureProxy.new(:section)
assert_raise ArgumentError do
proxy.config_section :subsection, init: true do
@proxy.config_section :subsection, init: true do
config_param :param1, :integer
end
end
end
test 'with config_param with config_set_default' do
proxy = Fluent::Config::ConfigureProxy.new(:section)
assert_nothing_raised do
proxy.config_section :subsection, init: true do
@proxy.config_section :subsection, init: true do
config_param :param1, :integer
config_set_default :param1, 1
end
end
end
test 'with config_argument' do
proxy = Fluent::Config::ConfigureProxy.new(:section)
assert_raise ArgumentError do
proxy.config_section :subsection, init: true do
@proxy.config_section :subsection, init: true do
config_argument :param0, :string
end
end
end
test 'with config_argument with default value' do
proxy = Fluent::Config::ConfigureProxy.new(:section)
assert_nothing_raised do
proxy.config_section :subsection, init: true do
@proxy.config_section :subsection, init: true do
config_argument :param0, :string, default: ''
end
end
Expand All @@ -203,7 +207,7 @@ class TestConfigureProxy < ::Test::Unit::TestCase

sub_test_case '#config_set_desc' do
setup do
@proxy = Fluent::Config::ConfigureProxy.new(:section)
@proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
end

test 'does not permit description specification twice w/ :desc option' do
Expand All @@ -220,7 +224,7 @@ class TestConfigureProxy < ::Test::Unit::TestCase

sub_test_case '#desc' do
setup do
@proxy = Fluent::Config::ConfigureProxy.new(:section)
@proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
end

test 'permit to specify description twice' do
Expand All @@ -240,7 +244,7 @@ class TestConfigureProxy < ::Test::Unit::TestCase

sub_test_case '#dump' do
setup do
@proxy = Fluent::Config::ConfigureProxy.new(:section)
@proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
end

test 'empty proxy' do
Expand Down

0 comments on commit 4d43a98

Please sign in to comment.