logo
down
shadow

Can I write tests for custom attributes without defining x^n classes?


Can I write tests for custom attributes without defining x^n classes?

By : Srinivas
Date : November 22 2020, 11:17 AM
hope this fix your issue I'm adding another answer, because since you now provided some code, the old one is too broad. It's now (mostly) obvious that:
you control the attribute-reading code you are reading the code via reflection (PropertyInfo.GetCustomAttributes)
code :
somemethod(PropertyInfo prop)
{
    // ...
    object[] attrs = prop.GetCustomAttributes(true); // read ATTRs from PROP
    foreach (object attr in attrs) // scan the PROP's ATTRs
    {
        // check attr type, do something
    }
    // ...
}
object[] attributeReader(PropertyInfo prop)
{
    return prop.GetCustomAttributes(true);
}

somemethod(PropertyInfo prop)
{
    // ...
    object[] attrs = attributeReader(prop); // read ATTRs from PROP
    foreach (object attr in attrs) // scan the PROP's ATTRs
    {
        // check attr type, do something
    }
    // ...
}
// original class:
virtual object[] attributeReader(PropertyInfo prop)
{
    return prop.GetCustomAttributes(true);
}

// derived class:
object[] AttributesOverrides {get;set;}
override object[] attributeReader(PropertyInfo prop)
{
    if(prop.Name = "ShoeSize") return AttributesOverrides; // return what I say!
    return base.attributeReader(prop);
}

// your test setup
var t = ... // that DERIVED object
t.AttributesOverrides = new [] { ... } ; // attributes to use
// note the attributeReader is now a field, not function
Func<PropertyInfo, object[]> attributeReader = defaultAttributeReader;

static object[] defaultAttributeReader(PropertyInfo prop)
{
    return prop.GetCustomAttributes(true);
}


// and your test setup
var t = ... // that ORIGNAL object
t.attributeReader = customReaderForTheTest; // change the reader on the fly

// that's the reader-function to use in THIS TEST setup
static object[] customReaderForTheTest(PropertyInfo prop)
{
    if(prop.Name = "ShoeSize") return null; // crash when I say so! muhaHAHAhaa!
    return prop.GetCustomAttributes(true);
}


Share : facebook icon twitter icon
Defining custom attributes in CIL

Defining custom attributes in CIL


By : dnathan
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Hard to guess what your problem might be. If I apply this attribute to a Program.Test() method, I get this:
code :
  .method private hidebysig static void  Test() cil managed
  {
    .custom instance void ConsoleApplication1.FooAttribute::.ctor(int32[]) = ( 01 00 04 00 00 00 01 00 00 00 02 00 00 00 03 00 
                                                                               00 00 04 00 00 00 00 00 ) 
    // Code size       2 (0x2)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ret
  } // end of method Program::Test
Defining unit tests in abstract generic test classes

Defining unit tests in abstract generic test classes


By : markiv
Date : March 29 2020, 07:55 AM
I hope this helps you . Ok, I went for a different approach, similar to what @stijn suggested a while ago.
I removed the abstract base class completely, and created a helper class which gets called from actual parser tests. This is a much better solution, because it allows reusing the same method with different combination of parameters and validation methods in each derived class (while abstract class only had a single pair of CreateData/TestResults abstract methods).
code :
public class ParserTestHelper
{
    public static void Test<T>(
         Func<IParser<T>> getParser,
         Func<byte[]> getInput,
         Action<T> checkResult)
    {
        // get parser
        var parser = getParser();

        // get input data
        var input = getInput();

        // parse
        var result = parser.Parse(input, 0);

        // common assertions
        Assert.AreEqual(ParserResultType.Success, result.ResultType);
        Assert.AreEqual(input.Length, result.NextDataOffset);

        // validate results
        checkResult(result.ParsedValue);
    }
}
[TestClass]
public class HeaderParserTest
{
     [TestMethod]
     public void TestHeader() 
     {
         ParserTestHelper.Test(
            () => new HeaderParser(),
            () => /* generate data */,
            () => /* validate results */);
     }
}
React.js: Defining custom attributes

React.js: Defining custom attributes


By : S P
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Starting React v0.12, transferPropsTo are replaced by JSX Spread Attributes:
code :
return <path {...this.props}/>
Confused on how to write tests for classes that are refactored to compose other classes

Confused on how to write tests for classes that are refactored to compose other classes


By : Julien Dumas
Date : March 29 2020, 07:55 AM
it fixes the issue I would start with focusing on the behaviour.
You want your Calculator class to be able to add 2 numbers. Its (pretty) irrelevant if you do this via some other class as that is an implementation detail of the calculator. Your tests should give you coverage of the Number class if you implement the functionality there.
Defining custom css attributes and changing any value with animate()

Defining custom css attributes and changing any value with animate()


By : user3719661
Date : March 29 2020, 07:55 AM
this will help I'm trying to make a result screen in javascript that displays animated scores. For the specific field in the example it's supposed to go from the value to 0 in two seconds, two seconds after the screen appears. Since it's easy to change css values with animate() I thought I could assign the values to some custom css property, then have the element's value take it from there. According to this article I just have to place -- in front of custom css attributes and this one shows me how to use the animate() method. So I tried doing this , Try this:
code :
var scorePlus=100;
var $elementSuccess=$('.success');
var myObject = {score:scorePlus};
$elementSuccess.html(scorePlus);
$(myObject).delay(2000).animate({score:0},{duration:2000,progress:function(){$elementSuccess.html(Math.round(myObject.score))}});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="success"></div>
Related Posts Related Posts :
  • Replace a # character with a space and a # character
  • DataGrid SelectedItem and CurrentItem on row change and on loosing focus
  • Parse Json to class with C#
  • Publish .exe with Visual Studio Mac
  • c# out causing a no overload for method error?
  • Which Thread resume the method execution after await in Asp.net Web application
  • How to open Asp.Net project in another computer
  • Azure app registrations - SQL Server permission for C# App
  • How to Submit String with 536000 Characters to API
  • Server side TcpClient's stream.read gets no data when a different client (connected to the same port) sends data
  • Add combo-box in windows form grid view by using LINQ query
  • Remove XLL addin when multiple XLL addins are installed
  • Deserialize dynamic json into generic .NET object
  • How to search node in Jobject
  • Iron Webscraper connects to host but doesn't scrape?
  • Change values from textbox at the touch of a button
  • Data access structure with Entity Framework
  • Link Assemblys failed unexpectedly (Xamarin.ios.dll)
  • ASP.NET Core Razor pages - not binding on POST Request
  • How to update appsettings.json from wix custom actions with the values passed as parameter during command line installat
  • Load undefined images from folders
  • How to add a new button in title bar next the minimize button in UWP?
  • Missing DLL in docker image after copying published tree inside
  • One of the identified items was in an invalid format
  • TLS1.2 does not exist with .NET v4.7
  • How do I parse a XML document into object models?
  • Why is a conditional style being set on a html element by Razor is always html encoded incorrectly
  • Counting the amount of repetitions a letter has in a string
  • How to get the encoding of a file in a repository git via C#?
  • Communication between an Azure web application and a windows form app on Azure VM
  • WCF Service - Client receiving CommunicationException after many requests to localhost
  • How to correctly view pdf in browser using DotVVM?
  • Reading part of json into c# object
  • YoutubeAPI v3 - Setting thumbnail returns Completed status, but no thumbnail is set on video
  • Using the SimpleInjector, is it possible to get a singleton by its implementation type?
  • Why the LogLevel rule is not set for my NLog?
  • Converting short? to TimeSpan
  • Publish build artifact through build.cake instead of Azure Devops
  • Unable to get new token using JWT method (C#)
  • Running HttpRequest synchronously C#
  • How to avoid format in "XDocument.Parse(strHtml)"
  • How to update naviation property with new values in EF Core?
  • Queued function not executing with dequeue
  • How to Control Sequence of columns automatically Generated in DataGrid
  • Getting error when try to add a file from library to main project?
  • Aggregate Roots DDD/UoW/Repo/Service
  • How to change the horizontal movement direction of the player in Unity2D?
  • Getting a two dimensional array of all possible unique combination of numbers lower than a mixmum for each cell starting
  • How to get LDAP nested groups from attribute
  • Get array consisting of first values of a list of tuples
  • How do I pass information from my View to my Controller as a Parameter
  • UWP BarcodeScanner Preview: CaptureElement doesn't show any Preview
  • How to register open generic with custom factory method?
  • Change image via in scriptable object in Unity
  • Decreasing value of one float when other go up
  • Xceed CheckComboBox not properly showing selected values from items hardcoded in XAML
  • Why does char array display contents on console while string and int arrays dont in c#?
  • How to prevent custom filters twice calls?
  • c#: remove brackets of certain size if existing including its content
  • Is there a simple way of handling nested Dictionary<string, object> types where object is either string or another
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org