Kouhei Sutou
null+****@clear*****
Tue Dec 10 16:56:41 JST 2013
Kouhei Sutou 2013-12-10 16:56:41 +0900 (Tue, 10 Dec 2013) New Revision: f0a153f29d7d2cc9cec775cf356c03faf8598b5f https://github.com/droonga/drntest/commit/f0a153f29d7d2cc9cec775cf356c03faf8598b5f Message: Extract test executor from test runner Added files: lib/drntest/test-executor.rb Modified files: lib/drntest/test-runner.rb Added: lib/drntest/test-executor.rb (+108 -0) 100644 =================================================================== --- /dev/null +++ lib/drntest/test-executor.rb 2013-12-10 16:56:41 +0900 (a311849) @@ -0,0 +1,108 @@ +# Copyright (C) 2013 Droonga Project +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +require "yajl" + +require "drntest/request-executor" + +module Drntest + class TestExecutor + attr_reader :owner, :test_path + + def initialize(owner, test_path) + @owner = owner + @test_path = test_path + end + + def execute + actuals = [] + logging = true + load_request_envelopes.each do |request| + if request.is_a?(Directive) + case request.type + when :enable_logging + logging = true + when :disable_logging + logging = false + end + next + end + executor = RequestExecutor.new(@owner, request) + response = executor.execute + actuals << response if logging + end + actuals + end + + private + def resolve_relative_path(path) + path = path.to_s + path = path[2..-1] if path[0..1] == "./" + Pathname(path).expand_path(@owner.base_path) + end + + def load_request_envelopes + load_jsons(@test_path) + end + + def load_jsons(path) + parser = Yajl::Parser.new + json_objects = [] + parser.on_parse_complete = Proc.new do |json_object| + json_objects << json_object + end + Pathname(path).read.each_line do |line| + if line[0] == "#" + if Directive.directive?(line) + directive = Directive.new(line) + if directive.type == :include + included = resolve_relative_path(directive.value) + included_jsons = load_jsons(included) + json_objects += included_jsons + else + json_objects << directive + end + end + else + begin + parser << line + rescue StandardError => error + p "Failed to load JSONs file: #{path.to_s}" + raise error + end + end + end + json_objects + end + + class Directive + MATCHER = /\A\#\@([^\s]+)(?:\s+(.+))?\z/.freeze + + class << self + def directive?(source) + MATCHER =~ source.strip + end + end + + attr_reader :type, :value + + def initialize(source) + MATCHER =~ source.strip + @value = $2 + @type = $1.gsub("-", "_").to_sym + end + end + end +end Modified: lib/drntest/test-runner.rb (+8 -63) =================================================================== --- lib/drntest/test-runner.rb 2013-12-10 16:49:16 +0900 (50c3366) +++ lib/drntest/test-runner.rb 2013-12-10 16:56:41 +0900 (fac6c19) @@ -21,7 +21,7 @@ require "fileutils" require "drntest/path" require "drntest/test-results" -require "drntest/request-executor" +require "drntest/test-executor" module Drntest class TestRunner @@ -70,12 +70,6 @@ module Drntest end private - def resolve_relative_path(path, base) - path = path.to_s - path = path[2..-1] if path[0..1] == "./" - Pathname(path).expand_path(base) - end - def prepare if catalog_file.exist? catalog_json = JSON.parse(catalog_file.read, :symbolize_names => true) @@ -139,21 +133,8 @@ module Drntest def process_requests results = TestResults.new(@target_path) - logging = true - load_request_envelopes.each do |request| - if request.is_a?(Directive) - case request.type - when :enable_logging - logging = true - when :disable_logging - logging = false - end - next - end - executor = RequestExecutor.new(self, request) - response = executor.execute - results.actuals << response if logging - end + executor = TestExecutor.new(self, @target_path) + results.actuals = executor.execute if expected_exist? results.expecteds = load_expected_responses end @@ -176,32 +157,10 @@ module Drntest results end - def load_request_envelopes - load_jsons(@target_path) - end - def load_expected_responses load_jsons(expected_path) end - class Directive - MATCHER = /\A\#\@([^\s]+)(?:\s+(.+))?\z/.freeze - - class << self - def directive?(source) - MATCHER =~ source.strip - end - end - - attr_reader :type, :value - - def initialize(source) - MATCHER =~ source.strip - @value = $2 - @type = $1.gsub("-", "_").to_sym - end - end - def load_jsons(path, options={}) parser = Yajl::Parser.new json_objects = [] @@ -209,25 +168,11 @@ module Drntest json_objects << json_object end Pathname(path).read.each_line do |line| - if line[0] == "#" - if Directive.directive?(line) - directive = Directive.new(line) - if directive.type == :include - included = resolve_relative_path(directive.value, - options[:base_path] || @base_path) - included_jsons = load_jsons(included) - json_objects += included_jsons - else - json_objects << directive - end - end - else - begin - parser << line - rescue StandardError => error - p "Failed to load JSONs file: #{path.to_s}" - raise error - end + begin + parser << line + rescue StandardError => error + p "Failed to load JSONs file: #{path.to_s}" + raise error end end json_objects -------------- next part -------------- HTML����������������������������... 下载