Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to find differences that are nested deep inside a property? #12

Closed
SubasishMohapatra opened this issue May 15, 2020 · 2 comments
Closed
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@SubasishMohapatra
Copy link

SubasishMohapatra commented May 15, 2020

How to find differences that are nested deep inside a property?
In the classes mentioned below, how do I get the differences which are listed 2 level deep e.g., Cool.Author.Address.City and Cool.Author.Address.Country

 public class Cool
    {
        public int id;
        public int revision;
        public string[] vehicles;
        public Author author;
        public string tagline;
        public Gender gender;
    }

    public class Author
    {
        public string FirstName;
        public string LastName;
        public Address Address;
    }

    public class Address
    {
        public string City;
        public string Country;
    }
@replaysMike
Copy link
Owner

@SubasishMohapatra what should have been a simple use case turned out to be a bug! I've fixed this in version 1.0.73 - it turned out I had missed a use case in the tests for include/exclude expressions that have a direct path.

Here's an example that should be working now. The following will compare 2 different objects, but only compare City and Country because we specified that the expressions passed are property includes using ComparisonOptions.IncludeList (by default it treats expressions as excludes, same as option ComparisonOptions.ExcludeList)

var obj1 = new Cool {
  Author = new Author {
    FirstName = "John",
    LastName = "Smith",
    Address = new Address { 
      City = "Los Angeles",
      Country = "USA"
    } 
  }
};
var obj1 = new Cool {
  Author = new Author { 
    FirstName = "Jane",
    LastName = "Doe",
    Address = new Address { 
      City = "Beijing",
      Country = "China"
    } 
  }
};

var diff = obj1.Diff(obj2, ComparisonOptions.All | ComparisonOptions.IncludeList, x => x.Author.Address.City, x => x.Author.Address.Country);
Assert.AreEqual(2, diff.Count); // only City and Country are different

@replaysMike
Copy link
Owner

replaysMike commented May 23, 2020

also note you don't have to use expressions - you could also just pass the path as follows:

var diff = obj1.Diff(obj2, ComparisonOptions.All | ComparisonOptions.IncludeList, ".Author.Address.City", ".Author.Address.Country");

@replaysMike replaysMike self-assigned this May 23, 2020
@replaysMike replaysMike added bug Something isn't working question Further information is requested labels May 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants