Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Created June 23, 2013 08:52
Show Gist options
  • Save ackintosh/5844341 to your computer and use it in GitHub Desktop.
Save ackintosh/5844341 to your computer and use it in GitHub Desktop.
require 'optparse'
class Diff
def initialize(new, old, delimiter = ' ')
@new = new
@old = old
@delimiter = delimiter
end
def file_each_line(file)
raise "No block" unless block_given?
File.open(file) do |f|
f.each_line do |line|
yield line
end
end
end
def init
@new_hash = Hash.new
file_each_line(@new) do |line|
checksum, path = line.split(@delimiter)
@new_hash[path] = checksum
end
end
def checksum
init
result = []
file_each_line(@old) do |line|
checksum, path = line.split(@delimiter)
result << path if @new_hash.has_key?(path) && @new_hash[path] != checksum
end
result
end
def exists_only_in_old
init
result = []
file_each_line(@old) do |line|
checksum, path = line.split(@delimiter)
result << path unless @new_hash.has_key?(path)
end
result
end
def exists_only_in_new
init
file_each_line(@old) do |line|
checksum, path = line.split(@delimiter)
@new_hash.delete(path)
end
@new_hash.keys
end
end
if __FILE__ == $0
args = {}
OptionParser.new do |parser|
parser.on('-m', '--method METHOD_NAME') {|v| args[:method] = v }
parser.parse!(ARGV)
end
args[:method] = 'checksum' unless args[:method]
diff = Diff.new($*[0], $*[1])
raise 'invalid argument' unless diff.respond_to?(args[:method])
puts diff.send(args[:method])
end
require './Diff.rb'
describe Diff do
before :each do
@diff = Diff.new('filepath_dummy1', 'filepath_dummy2');
end
describe '#initialize' do
context 'when passed filepath' do
it 'set it to property' do
@diff.instance_eval { @new }.should == 'filepath_dummy1'
@diff.instance_eval { @old }.should == 'filepath_dummy2'
end
end
context 'when passed nothing to delimiter' do
it 'set single space to delimiter' do
@diff.instance_eval { @delimiter }.should == ' '
end
end
context 'when passed something to delimiter' do
it 'set it to delimiter' do
delimiter = '---'
delimiter_set = Diff.new('dummy1.txt', 'dummy2.txt', delimiter)
delimiter_set.instance_eval { @delimiter }.should == delimiter
end
end
end
describe '#file_each_line' do
context 'when block is not given' do
it 'should raise RuntimeError' do
lambda{ @diff.file_each_line('dummy.txt') }.should raise_error
end
end
end
describe 'check method' do
before :each do
@dummy1 =<<'EOS'
7e0214a120fecc15ef524f15187a70b9 ./websocket/server/server.rb
4786f7b4e011d1e243347b91e5cb7f1d ./websocket/js/ws.js
d3fd67f82c6ab0b7ac6a359375f0dff0 ./websocket/index3.html
d3fd67f82c6ab0b7ac6a359375f0dff0 ./websocket/index2.html
c0603d5a956c75407a9110ffd08e30d7 ./websocket/index.html
605632f7874dedb4de036350c187b933 ./using_test.rb
EOS
@dummy2 =<<'EOS'
7e0214a120fecc15ef524f15187a70bf ./websocket/server/server.rb
4786f7b4e011d1e243347b91e5cb7f1f ./websocket/js/ws.js
d3fd67f82c6ab0b7ac6a359375f0dff0 ./websocket/index3.html
c0603d5a956c75407a9110ffd08e30df ./websocket/index.html
605632f7874dedb4de036350c187b933 ./using_test.rb
4c925a413d88e74bcb7f26ab447ffeeb ./using.rb
EOS
dummy1 = @dummy1
dummy2 = @dummy2
(class << @diff; self; end;).class_eval do
define_method(:file_each_line) do |filepath, &block|
target = filepath == 'filepath_dummy1' ? dummy1 : dummy2
target.split("\n").each do |line|
block.call(line)
end
end
end
end
describe '#init' do
it 'should set dummy1 to @new_hash' do
@diff.init
expect = Hash.new
@dummy1.split("\n").each do |line|
value, key = line.split(' ')
expect[key] = value
end
@diff.instance_eval { @new_hash }.should == expect
end
end
describe '#checksum' do
it 'should return array' do
@diff.checksum.should be_kind_of(Array)
end
it 'should return the file path the checksum are different' do
@diff.checksum.should == %w{./websocket/server/server.rb ./websocket/js/ws.js ./websocket/index.html}
end
end
describe '#exists_only_in_old' do
it 'should return array' do
@diff.exists_only_in_old.should be_kind_of(Array)
end
it 'should return the file path the exists only in old(dummy2)' do
@diff.exists_only_in_old.should == %w{./using.rb}
end
end
describe '#exists_only_in_new' do
it 'should return array' do
@diff.exists_only_in_new.should be_kind_of(Array)
end
it 'should return the file path the exists only in new(dummy1)' do
@diff.exists_only_in_new.should == %w{./websocket/index2.html}
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment