Kouhei Sutou
null+****@clear*****
Fri Jan 31 17:19:43 JST 2014
Kouhei Sutou 2014-01-31 17:19:43 +0900 (Fri, 31 Jan 2014) New Revision: 76a65c06e6461ada323fd9d086b4f110f8ff9248 https://github.com/droonga/fluent-plugin-droonga/commit/76a65c06e6461ada323fd9d086b4f110f8ff9248 Message: command: support nonexistent path is specified case Modified files: lib/droonga/command.rb test/unit/test_command.rb Modified: lib/droonga/command.rb (+8 -2) =================================================================== --- lib/droonga/command.rb 2014-01-31 16:52:28 +0900 (83dcd99) +++ lib/droonga/command.rb 2014-01-31 17:19:43 +0900 (ba335f3) @@ -80,10 +80,16 @@ module Droonga def match_pattern?(pattern, message) path, operator, *arguments = pattern - target = path.split(".").inject(message) do |result, component| + target = resolve_path(path, message) + apply_operator(operator, target, arguments) + end + + NONEXISTENT_PATH = Object.new + def resolve_path(path, message) + path.split(".").inject(message) do |result, component| + return NONEXISTENT_PATH if result.nil? result[component] end - apply_operator(operator, target, arguments) end def apply_operator(operator, target, arguments) Modified: test/unit/test_command.rb (+41 -13) =================================================================== --- test/unit/test_command.rb 2014-01-31 16:52:28 +0900 (7bf328a) +++ test/unit/test_command.rb 2014-01-31 17:19:43 +0900 (f992891) @@ -16,9 +16,48 @@ require "droonga/command" class CommandTest < Test::Unit::TestCase + def command(method_name, options={}) + Droonga::Command.new(method_name, options) + end + + class ResolvePathTest < self + def command + super(:method_name) + end + + def resolve_path(path, message) + command.send(:resolve_path, path, message) + end + + def test_nonexistent + assert_equal(Droonga::Command::NONEXISTENT_PATH, + resolve_path("nonexistent.path", {})) + end + + def test_top_level + assert_equal("select", + resolve_path("type", + { + "type" => "select" + })) + end + + def test_nested + assert_equal(10, + resolve_path("body.output.limit", + { + "body" => { + "output" => { + "limit" => 10, + }, + }, + })) + end + end + class MatchTest < self def command(patterns) - Droonga::Command.new(:method_name, :patterns => patterns) + super(:method_name, :patterns => patterns) end def match?(patterns, message) @@ -26,24 +65,13 @@ class CommandTest < Test::Unit::TestCase end class EqualTest < self - def test_top_level + def test_same_value assert_true(match?([["type", :equal, "select"]], { "type" => "select" })) end - def test_nested - assert_true(match?([["body.output.limit", :equal, 10]], - { - "body" => { - "output" => { - "limit" => 10, - }, - }, - })) - end - def test_different_value assert_false(match?([["type", :equal, "select"]], { -------------- next part -------------- HTML����������������������������... Download