urasandesu / Prig

Prig is a lightweight framework for test indirections in .NET Framework.
Other
117 stars 21 forks source link

Propose empty mock implementation for replacement target method #62

Closed urasandesu closed 8 years ago

urasandesu commented 8 years ago

In Cheat Sheet, we explained that how a replacement target method will be represented as Indirection Delegate. However, when you use actually, you probably can't notice soon the little difference -- the case that non-public signature type exists, the difference between an instance method and a static method and so on -- even if it is wrong. To avoid that, I think it is better that an empty mock implementation is proposed at the same time as getting Indirection Stub Setting. For example, the following are our expected results:

public static DateTime Now { get; } of System.DateTime in mscorlib
<!--
    PDateTime.NowGet().Body = 
        () => 
        {
            throw new NotImplementedException(); 
        };
-->
<add name="NowGet" alias="NowGet">
  <RuntimeMethodInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:Id="1" z:FactoryType="MemberInfoSerializationHolder" z:Type="System.Reflection.MemberInfoSerializationHolder" z:Assem" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
    <Name z:Id="2" z:Type="System.String" z:Assembly="0" xmlns="">get_Now</Name>
    <AssemblyName z:Id="3" z:Type="System.String" z:Assembly="0" xmlns="">mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
    <ClassName z:Id="4" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime</ClassName>
    <Signature z:Id="5" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime get_Now()</Signature>
    <Signature2 z:Id="6" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime get_Now()</Signature2>
    <MemberType z:Id="7" z:Type="System.Int32" z:Assembly="0" xmlns="">8</MemberType>
    <GenericArguments i:nil="true" xmlns="" />
  </RuntimeMethodInfo>
</add>

This is one of the most simple result.

public static T Exchange<T>(ref T location1, T value) where T : class of System.Threading.Interlocked in mscorlib
<!--
    PInterlocked.ExchangeOfTTRefT<T>().Body = 
        (ref T location1, T value) =>
        {
            throw new NotImplementedException(); 
        };
-->
<add name="ExchangeOfTTRefT" alias="ExchangeOfTTRefT">
  <RuntimeMethodInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:Id="1" z:FactoryType="MemberInfoSerializationHolder" z:Type="System.Reflection.MemberInfoSerializationHolder" z:Assem" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
    <Name z:Id="2" z:Type="System.String" z:Assembly="0" xmlns="">Exchange</Name>
    <AssemblyName z:Id="3" z:Type="System.String" z:Assembly="0" xmlns="">mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
    <ClassName z:Id="4" z:Type="System.String" z:Assembly="0" xmlns="">System.Threading.Interlocked</ClassName>
    <Signature z:Id="5" z:Type="System.String" z:Assembly="0" xmlns="">T Exchange[T](T ByRef, T)</Signature>
    <Signature2 z:Id="6" z:Type="System.String" z:Assembly="0" xmlns="">!!T Exchange[T](!!T&amp;, !!T)</Signature2>
    <MemberType z:Id="7" z:Type="System.Int32" z:Assembly="0" xmlns="">8</MemberType>
    <GenericArguments i:nil="true" xmlns="" />
  </RuntimeMethodInfo>
</add>

Generic parameter will be proposed as it is, because Prig cannot know that it will be replaced to another.

internal unsafe static bool TryParse(string s, DateTimeFormatInfo dtfi, DateTimeStyles styles, ref DateTimeResult result) of System.DateTimeParse in mscorlib
<!--
    PDateTimeParse.TryParseStringDateTimeFormatInfoDateTimeStylesDateTimeResultRef().Body = 
        args => 
        {   // args[0]: System.String s, args[1]: System.Globalization.DateTimeFormatInfo dtfi, args[2]: System.Globalization.DateTimeStyles styles, args[3]: ref System.DateTimeResult result
            throw new NotImplementedException(); 
        };
-->
<add name="TryParseStringDateTimeFormatInfoDateTimeStylesDateTimeResultRef" alias="TryParseStringDateTimeFormatInfoDateTimeStylesDateTimeResultRef">
  <RuntimeMethodInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:Id="1" z:FactoryType="MemberInfoSerializationHolder" z:Type="System.Reflection.MemberInfoSerializationHolder" z:Assem" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
    <Name z:Id="2" z:Type="System.String" z:Assembly="0" xmlns="">TryParse</Name>
    <AssemblyName z:Id="3" z:Type="System.String" z:Assembly="0" xmlns="">mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
    <ClassName z:Id="4" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTimeParse</ClassName>
    <Signature z:Id="5" z:Type="System.String" z:Assembly="0" xmlns="">Boolean TryParse(System.String, System.Globalization.DateTimeFormatInfo, System.Globalization.DateTimeStyles, System.DateTimeResult ByRef)</Signature>
    <MemberType z:Id="6" z:Type="System.Int32" z:Assembly="0" xmlns="">8</MemberType>
    <GenericArguments i:nil="true" xmlns="" />
  </RuntimeMethodInfo>
</add>

Non-public signature will be proposed by comment, because they cause a build failure.

internal virtual string InternalToString() of System.Exception in mscorlib
<!--
    PException.InternalToString().Body = 
        @this => 
        {
            throw new NotImplementedException(); 
        };
-->
<add name="InternalToString" alias="InternalToString">
  <RuntimeMethodInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:Id="1" z:FactoryType="MemberInfoSerializationHolder" z:Type="System.Reflection.MemberInfoSerializationHolder" z:Assem" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
    <Name z:Id="2" z:Type="System.String" z:Assembly="0" xmlns="">InternalToString</Name>
    <AssemblyName z:Id="3" z:Type="System.String" z:Assembly="0" xmlns="">mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
    <ClassName z:Id="4" z:Type="System.String" z:Assembly="0" xmlns="">System.Exception</ClassName>
    <Signature z:Id="5" z:Type="System.String" z:Assembly="0" xmlns="">System.String InternalToString()</Signature>
    <Signature2 z:Id="6" z:Type="System.String" z:Assembly="0" xmlns="">System.String InternalToString()</Signature2>
    <MemberType z:Id="7" z:Type="System.Int32" z:Assembly="0" xmlns="">8</MemberType>
    <GenericArguments i:nil="true" xmlns="" />
  </RuntimeMethodInfo>
</add>

This is the example of a instance method of a class.

public Nullable(T value) of System.Nullable<T> in mscorlib
<!--
    PNullable<T>.ConstructorT().Body = 
        (ref Nullable<T> @this, T value) =>
        {
            throw new NotImplementedException(); 
        };
-->
<add name="ConstructorT" alias="ConstructorT">
  <RuntimeConstructorInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:Id="1" z:FactoryType="MemberInfoSerializationHolder" z:Type="System.Reflection.MemberInfoSerializationHolder" z:ly="0" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
    <Name z:Id="2" z:Type="System.String" z:Assembly="0" xmlns="">.ctor</Name>
    <AssemblyName z:Id="3" z:Type="System.String" z:Assembly="0" xmlns="">mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
    <ClassName z:Id="4" z:Type="System.String" z:Assembly="0" xmlns="">System.Nullable`1</ClassName>
    <Signature z:Id="5" z:Type="System.String" z:Assembly="0" xmlns="">Void .ctor(T)</Signature>
    <Signature2 z:Id="6" z:Type="System.String" z:Assembly="0" xmlns="">.ctor(!T)</Signature2>
    <MemberType z:Id="7" z:Type="System.Int32" z:Assembly="0" xmlns="">1</MemberType>
    <GenericArguments i:nil="true" xmlns="" />
  </RuntimeConstructorInfo>
</add>

This is the example of a instance method of a structure.