Likeness - polishing and packaging

后端 未结 1 1404
轻奢々
轻奢々 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<T, NewMappingsEvent> WithoutProgrammaticIdentifier<T>( Likeness<T, NewMappingsEvent> that )
    {
        return that.Without( x => x.ProgrammaticIdentifier );
    }
    

    [2]

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

    [3]

    static Likeness<T, NewMappingsEvent> WithoutProgrammaticIdentifier<T>( Likeness<T, NewMappingsEvent> that )
    {
        return that.WithoutProgrammaticIdentifier();
    }
    

    [4]

    static class AssertResemblance
    {
        public static void Like<T, T2>( T expected, T2 actual )
        {
            Like( expected, actual, x => x );
        }
    
        public static void Like<T, T2>( T expected, T2 actual, Func<Likeness<T, T2>, Likeness<T, T2>> configureLikeness )
        {
            var likeness = expected.AsSource().OfLikeness<T2>();
            configureLikeness( likeness ).ShouldEqual( actual );
        }
    }
    
    0 讨论(0)
提交回复
热议问题