.net - How to compare two anonymous types or two collection of different types using SemanticComparison -


1. there simple way compare 2 anonymous types using semanticcomparison autofixture ? current issue can't construct likeness second anonymous object. simplified example :

var srcanon = new { time = expectedtime, data = docsarray }; var resultanon = new { time=actualtime, data = sutresponsearray };  var expectedalike = srcanon.assource()             .oflikeness<??whatshere??>() 

2. think question pretty related first 1 both use semanticcomparison create iequatable implementations. in question mark seemann provided answer on how using mstest assertions , linq sequenceequal method.

is possible use xunit2 assertions library in similar scenario? xunit supports assert.equal() collections of same type, can used collections of different types, if elements implement iequatable (using likeness). (this doesn't work result , alllikeness have different types):

assert.equal(alllikeness.toarray(),result.toarray()); 

independently of unit testing framework, can drop down the sequenceequals<object> overload takes comparer. enable compare disparate lists. test demonstrates how can 'trick' .net 'thinking' 2 heterogeneous arrays identical:

[fact] public void testdisparatesequences() {     var ints = new[] { 1, 3, 5, 7 };     var strings = new[] { "foo", "bar", "baz", "qux" };      assert.true(         ints.cast<object>().sequenceequal(             strings.cast<object>(),             new truecomparer<object>()),         "arrays equal."); }  private class truecomparer<t> : iequalitycomparer<t> {     public bool equals(t x, t y)     {         return true;     }      public int gethashcode(t obj)     {         return 0;     } } 

this test passes, because truecomparer returns true.

obviously, that's not particularly practical, points out relevant building blocks 1 can use compare heterogeneous sequences.

semanticomparison provides semanticcomparer<t> class implements iequalitycomparer<t>, works on values of same type. thus, in order use compare heterogeneous sequences, you'll need map 1 of lists sequence of other type.

often, you'll have such map lying around, if not, it's motivation building one. otherwise, can use use semantic mapper automapper.

assume, example, have foo class this:

public class foo {     public int number { get; set; }      public string text { get; set; } } 

and bar class, similar:

public class bar {     public int number { get; set; }      public string text { get; set; } } 

you can compare foos , bars using map , semanticcomparison<bar>:

[fact] public void testequivalentsequences() {     var foos = new[]     {         new foo { number = 42, text = "ploeh" },         new foo { number = 1337, text = "fnaah" }     };     var bars = new[]     {         new bar { number = 42, text = "ploeh" },         new bar { number = 1337, text = "fnaah" }     };      automapper.mapper.createmap<foo, bar>();      assert.true(         foos.select(automapper.mapper.map<bar>).sequenceequal(             bars,             new semanticcomparer<bar>()),         "mapped arrays should equivalent."); } 

still, make life whole lot easier if give objects structural equality. answer sketches out what's possible, not what's recommended.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -