Sha256: 27e3b57f51efdb88129953295083f171deb1eb16be20aac51d7830fa9457b598

Contents?: true

Size: 1.95 KB

Versions: 20

Compression:

Stored size: 1.95 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 if obj1 and obj2
      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

20 entries across 19 versions & 5 rubygems

Version Path
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/hashdiff-0.3.4/spec/hashdiff/best_diff_spec.rb
logstash-output-icinga-1.1.0 vendor/jruby/2.3.0/gems/hashdiff-0.3.4/spec/hashdiff/best_diff_spec.rb
logstash-output-icinga-1.1.0 vendor/jruby/1.9/gems/hashdiff-0.3.4/spec/hashdiff/best_diff_spec.rb
vagrant-unbundled-1.9.5.1 vendor/bundle/ruby/2.4.0/gems/hashdiff-0.3.4/spec/hashdiff/best_diff_spec.rb
logstash-output-icinga-1.0.0 vendor/jruby/1.9/gems/hashdiff-0.3.4/spec/hashdiff/best_diff_spec.rb
hashdiff-0.3.4 spec/hashdiff/best_diff_spec.rb
logstash-input-salesforce-3.0.0 vendor/jruby/1.9/gems/hashdiff-0.3.2/spec/hashdiff/best_diff_spec.rb
vagrant-unbundled-1.9.1.1 vendor/bundle/ruby/2.4.0/gems/hashdiff-0.3.0/spec/hashdiff/best_diff_spec.rb
hashdiff-0.3.2 spec/hashdiff/best_diff_spec.rb
hashdiff-0.3.1 spec/hashdiff/best_diff_spec.rb
vagrant-unbundled-1.8.5.2 vendor/bundle/ruby/2.3.0/gems/hashdiff-0.3.0/spec/hashdiff/best_diff_spec.rb
vagrant-unbundled-1.8.5.1 vendor/bundle/ruby/2.3.0/gems/hashdiff-0.3.0/spec/hashdiff/best_diff_spec.rb
vagrant-unbundled-1.8.4.2 vendor/bundle/ruby/2.3.0/gems/hashdiff-0.3.0/spec/hashdiff/best_diff_spec.rb
vagrant-unbundled-1.8.4.1 vendor/bundle/ruby/2.3.0/gems/hashdiff-0.3.0/spec/hashdiff/best_diff_spec.rb
hashdiff-0.3.0 spec/hashdiff/best_diff_spec.rb
vagrant-unbundled-1.8.1.1 vendor/bundle/ruby/2.3.0/gems/hashdiff-0.2.3/spec/hashdiff/best_diff_spec.rb
dwolla_swagger-1.0.6 vendor/bundle/ruby/2.2.0/gems/hashdiff-0.2.3/spec/hashdiff/best_diff_spec.rb
hashdiff-0.2.3 spec/hashdiff/best_diff_spec.rb
hashdiff-0.2.2 spec/hashdiff/best_diff_spec.rb
hashdiff-0.2.1 spec/hashdiff/best_diff_spec.rb