begin require 'rubygems' rescue LoadError # meh end require 'shoulda' require 'mocha' require 'git_meta' class GitMetaTest < Test::Unit::TestCase context "A Scanner instance" do should "parse the data" do scanner = GitMeta::Scanner.new("\nfoo: bar\nfuzz: bazz\n") expected = {"foo"=> "bar", "fuzz"=> "bazz"} assert_equal expected, scanner.info end should "accept hypens in the keys" do scanner = GitMeta::Scanner.new("\nhi-there: you\nfoo: bar\n") expected = {"hi-there" => "you", "foo" => "bar"} assert_equal expected, scanner.info end should "accept whitespace in the values" do scanner = GitMeta::Scanner.new("\nSigned-off-by: José Valim \nfoo: bear\n") expected = {"Signed-off-by" => "José Valim ", "foo" => "bear"} assert_equal expected, scanner.info end should "play nice with comrades" do scanner = GitMeta::Scanner.new("\nhi-there: you\nfoo: bar\n") other_scanner = GitMeta::Scanner.new("\nhi-there: you\nfoo: bar\n") assert_equal scanner, other_scanner end end context "A Get instance" do setup do @cat_file = <<-FOO tree 0b31db52b0f96659a93dee8c5c7a1f8a95f0bb2b parent e033a2271fa3c59ae2c8b30f4b5ef8eb83e739db author Sam Elliott 1266027667 +0100 committer Sam Elliott 1266027667 +0100 foo: bar Signed-Off-By: Me! FOO IO.expects(:popen).with("git-cat-file -p HEAD", 'r').returns(@cat_file).at_least_once end should "initialize correctly without key" do @getter = GitMeta::Get.new("HEAD", nil) assert_nil @getter.key assert_equal @getter.scanner, GitMeta::Scanner.new(@cat_file) end should "initialize correctly with a key" do @getter = GitMeta::Get.new("HEAD", "foo") assert_equal @getter.key, "foo" assert_equal @getter.scanner, GitMeta::Scanner.new(@cat_file) end should "display itself nicely" do @getter = GitMeta::Get.new("HEAD", "foo") assert_equal @getter.to_s, "bar" long_expectation = "foo: bar\nSigned-Off-By: Me!\n" @getter = GitMeta::Get.new("HEAD", nil) assert_equal @getter.to_s, long_expectation end end context "A Hash instance" do should "print correctly" do hash = GitMeta::Hash.new hash["foo"] = "bar" hash["fuzz"] = "bazz" expected = "foo: bar\nfuzz: bazz\n" assert_equal expected, hash.to_s end end end