Sha256: 8c5533147a8233a0c2d3e4716d92c133cf9eb704afae3ca9f8b05c37b8d50cc2

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'spec_helper'

describe HashDiff do
  it "should be able to best diff" do
    a = {'x' => [{'a' => 1, 'c' => 3, 'e' => 5}, {'y' => 3}]}
    b = {'x' => [{'a' => 1, 'b' => 2, 'e' => 5}] }

    diff = HashDiff.best_diff(a, b)
    diff.should == [["-", "x[0].c", 3], ["+", "x[0].b", 2], ["-", "x[1]", {"y"=>3}]]
  end

  it "should use custom delimiter when provided" do
    a = {'x' => [{'a' => 1, 'c' => 3, 'e' => 5}, {'y' => 3}]}
    b = {'x' => [{'a' => 1, 'b' => 2, 'e' => 5}] }

    diff = HashDiff.best_diff(a, b, :delimiter => "\t")
    diff.should == [["-", "x[0]\tc", 3], ["+", "x[0]\tb", 2], ["-", "x[1]", {"y"=>3}]]
  end

  it "should use custom comparison when provided" do
    a = {'x' => [{'a' => 'foo', 'c' => 'goat', 'e' => 'snake'}, {'y' => 'baz'}]}
    b = {'x' => [{'a' => 'bar', 'b' => 'cow', 'e' => 'puppy'}] }

    diff = HashDiff.best_diff(a, b) do |path, obj1, obj2|
      case path
      when /^x\[.\]\..$/
        obj1.length == obj2.length
      end
    end

    diff.should == [["-", "x[0].c", 'goat'], ["+", "x[0].b", 'cow'], ["-", "x[1]", {"y"=>'baz'}]]
  end

  it "should be able to best diff array in hash" do
    a = {"menu" => {
      "id" => "file",
      "value" => "File",
      "popup" => {
        "menuitem" => [
          {"value" => "New", "onclick" => "CreateNewDoc()"},
          {"value" => "Close", "onclick" => "CloseDoc()"}
        ]
      }
    }}

    b = {"menu" => {
      "id" => "file 2",
      "value" => "File",
      "popup" => {
        "menuitem" => [
          {"value" => "New1", "onclick" => "CreateNewDoc()"},
          {"value" => "Open", "onclick" => "OpenDoc()"},
          {"value" => "Close", "onclick" => "CloseDoc()"}
        ]
      }
    }}

    diff = HashDiff.best_diff(a, b)
    diff.should == [
      ['~', 'menu.id', 'file', 'file 2'],
      ['~', 'menu.popup.menuitem[0].value', 'New', 'New1'],
      ['+', 'menu.popup.menuitem[1]', {"value" => "Open", "onclick" => "OpenDoc()"}]
    ]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hashdiff-0.2.0 spec/hashdiff/best_diff_spec.rb