Skip to content

Commit

Permalink
Add unit tests for Changit::Merger
Browse files Browse the repository at this point in the history
  • Loading branch information
aonemd committed Dec 3, 2016
1 parent ddfb15d commit fe7a829
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/changit/config_merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def initialize(src_config, target_config)
@target = target_config
end

# comapre and merge src_config into target_config into a lexer
def merge
src_lexer = Lexer.new(@src)
src_token_hash = src_lexer.token_hash
Expand All @@ -21,12 +22,17 @@ def merge
end
end

config_difference = src_token_hash.dup.delete_if { |k, _| target_token_hash.key?(k) }
config_difference = hash_difference(src_token_hash, target_token_hash)
target_token_hash.merge!(config_difference)
target_lexer.reconstruct_tokens_from_hash!
end

target_lexer
end

# return the difference between two hashes
def hash_difference(src_token_hash, target_token_hash)
src_token_hash.dup.delete_if { |k, _| target_token_hash.key?(k) }
end
end
end
25 changes: 25 additions & 0 deletions test/merger_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'test_helper'
require 'changit/lexer'
require 'changit/lexer/section_token'

class ConfigMergerTest < Minitest::Test
def test_merge
merger = config_merger
merged_config = merger.merge

assert_kind_of Changit::Lexer, merged_config
end

def test_hash_difference
difference_hash = config_merger.hash_difference({ "key1" => 1, "key2" => 2 }, { "key1" => 7 })
assert_equal difference_hash, { "key2" => 2 }
end

private

def config_merger
src_config = "[user]\n\tname = anothername\n".freeze
target_config = "[user]\n\tname = testname\n\temail = testmail@domain.com\n[core]\n\teditor = vim\n[color]\n\tui = true\n[rebase]\n\tautosquash = true\n".freeze
Changit::ConfigMerger.new(src_config, target_config)
end
end

0 comments on commit fe7a829

Please sign in to comment.