Kouhei Sutou
null+****@clear*****
Fri Sep 13 11:18:23 JST 2013
Kouhei Sutou 2013-09-13 11:18:23 +0900 (Fri, 13 Sep 2013) New Revision: e5edbd329f5635aa97b0e0706f63fccc4a62e829 https://github.com/groonga/groonga-command/commit/e5edbd329f5635aa97b0e0706f63fccc4a62e829 Message: Extract command escape value code as a public class method Added files: test/command/format/test-command.rb Modified files: lib/groonga/command/format/command.rb Modified: lib/groonga/command/format/command.rb (+16 -10) =================================================================== --- lib/groonga/command/format/command.rb 2013-09-13 11:07:15 +0900 (fee2ff0) +++ lib/groonga/command/format/command.rb 2013-09-13 11:18:23 +0900 (d19eb2b) @@ -22,6 +22,21 @@ module Groonga module Command module Format class Command + class << self + def escape_value(value) + escaped_value = value.gsub(/[\n"\\]/) do + special_character = $MATCH + case special_character + when "\n" + "\\n" + else + "\\#{special_character}" + end + end + "\"#{escaped_value}\"" + end + end + def initialize(name, arguments) @name = name @arguments = arguments @@ -33,17 +48,8 @@ module Groonga name.to_s end sorted_arguments.each do |name, value| - escaped_value = value.gsub(/[\n"\\]/) do - special_character = $MATCH - case special_character - when "\n" - "\\n" - else - "\\#{special_character}" - end - end components << "--#{name}" - components << "\"#{escaped_value}\"" + components << self.class.escape_value(value) end components.join(" ") end Added: test/command/format/test-command.rb (+37 -0) 100644 =================================================================== --- /dev/null +++ test/command/format/test-command.rb 2013-09-13 11:18:23 +0900 (52eadbe) @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2013 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +class CommandFormatTest < Test::Unit::TestCase + class EscapeValueTest < self + test '\n' do + assert_equal('"\\n"', escape_value("\n")) + end + + test '"' do + assert_equal('"\\""', escape_value("\"")) + end + + test "\\" do + assert_equal('"\\\\"', escape_value("\\")) + end + + def escape_value(value) + Groonga::Command::Format::Command.escape_value(value) + end + end +end -------------- next part -------------- HTML����������������������������...Download