Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Separate JSON and URI validation.

JSON is fast, URIs are slow.
  • Loading branch information...
commit 5dfe2b1e37add1415a63e9b88a62cc8ef8738dc7 1 parent 0a58805
@georgebrock georgebrock authored
View
16 lib/helpers/trail_runner.rb
@@ -2,22 +2,16 @@
require 'rubygems'
class TrailRunner
- def run
- print_starting_message
- run_validations_on_json_files
+ def run(purpose, &block)
+ print_starting_message(purpose)
+ json_files.each(&block)
print_closing_puts
end
private
- def print_starting_message
- puts "Starting JSON validation\n"
- end
-
- def run_validations_on_json_files
- json_files.each do |file|
- JSONValidator.new(file).run && URIValidator.new(file).run
- end
+ def print_starting_message(purpose)
+ puts "Starting #{purpose}\n"
end
def json_files
View
2  lib/validate_json.rb
@@ -5,4 +5,4 @@
Dir[helper_files].each { |file| require File.expand_path(file) }
-TrailRunner.new.run
+TrailRunner.new.run('JSON validation') { |file| JSONValidator.new(file).run }
View
8 lib/validate_uris.rb
@@ -0,0 +1,8 @@
+#!/usr/bin/env ruby
+
+script_directory = File.dirname(__FILE__)
+helper_files = script_directory + '/helpers/*.rb'
+
+Dir[helper_files].each { |file| require File.expand_path(file) }
+
+TrailRunner.new.run('URI validation') { |file| URIValidator.new(file).run }
View
29 spec/trail_runner_spec.rb
@@ -4,40 +4,29 @@
require 'bourne'
describe TrailRunner, '#run' do
- it 'returns a starting message' do
+ it 'outputs a starting message' do
$stdout = io = StringIO.new
- uri_validator_stub = stub('uri validator stub', :run)
- URIValidator.stubs(:new).returns(uri_validator_stub)
- TrailRunner.new.run
+ TrailRunner.new.run('JSON validation') {}
- io.string.split("\n")[0].should =~ /Starting/
+ io.string.split("\n")[0].should =~ /Starting JSON validation/
end
- it 'calls JSONValidator.new(file).run on each file' do
- validator_stub = stub('json validator stub', run: true)
- JSONValidator.stubs(:new).returns(validator_stub)
-
- uri_validator_stub = stub('uri validator stub', :run)
- URIValidator.stubs(:new).returns(uri_validator_stub)
-
- file_name = File.expand_path('../fixtures/valid.json', __FILE__)
- files = [file_name]
+ it 'yields each file name' do
runner = TrailRunner.new
+ files = %w(rails.json vim.json)
runner.stubs(:json_files).returns(files)
+ yielded_files = []
- runner.run
+ runner.run('Yield test') { |f| yielded_files << f }
- JSONValidator.should have_received(:new)
- URIValidator.should have_received(:new)
+ yielded_files.should eq files
end
it 'prints a closing puts' do
$stdout = io = StringIO.new
- uri_validator_stub = stub('uri validator stub', :run)
- URIValidator.stubs(:new).returns(uri_validator_stub)
- TrailRunner.new.run
+ TrailRunner.new.run('Closing test') {}
output = io.string[-1, 1]
output.should == "\n"
Please sign in to comment.
Something went wrong with that request. Please try again.