Likeness - polishing and packaging

后端 未结 1 1420
轻奢々
轻奢々 2021-01-24 05:20

I\'m using Ploeh.SemanticComparison\'s Likeness as a way to effectively express intended outputs of a mapping process (as described in Mark Seemann\'s

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-24 05:55

    If you had an Assertion helper class called AssertResemblance (like [4]), and a static helper like [1] in scope you could say it like:

    var expected = new { ProductId = id, Name = name, Version = version };
    AssertResemblance.Like( expected, result, WithoutProgrammaticIdentifier );
    

    Or if you had an extension method like [2], you could do it like:

    AssertResemblance.Like( expected,result,x=>x.WithoutProgrammaticIdentifier());
    

    Or you could have the best of both worlds (no noise as in the first snippet) yet naming the Resemblance (by having the actual impl in an extension method) by implementing a local static helper in terms of the extension method ([2]) as in [3].


    [1]

    public static Likeness WithoutProgrammaticIdentifier( Likeness that )
    {
        return that.Without( x => x.ProgrammaticIdentifier );
    }
    

    [2]

    static class NewMappingsEventResemblances
    {
        public static Likeness WithoutProgrammaticIdentifier( this Likeness that )
        {
            return that.Without( x => x.ProgrammaticIdentifier );
        }
    }
    

    [3]

    static Likeness WithoutProgrammaticIdentifier( Likeness that )
    {
        return that.WithoutProgrammaticIdentifier();
    }
    

    [4]

    static class AssertResemblance
    {
        public static void Like( T expected, T2 actual )
        {
            Like( expected, actual, x => x );
        }
    
        public static void Like( T expected, T2 actual, Func, Likeness> configureLikeness )
        {
            var likeness = expected.AsSource().OfLikeness();
            configureLikeness( likeness ).ShouldEqual( actual );
        }
    }
    

    0 讨论(0)
提交回复
热议问题