Daniel
Daniel

Reputation: 19

Insert values in class without explicit writing them

I have some huge classes and don't want to write them all out for testing, because it's a huge effort and I could forget some values what makes the test invalid.

Messages = new List<Request.Notif.NotifRuleMessages>
{
    new Request.Notif.NotifRuleMessages
    {
        Code = 1234,
        Message = new List<Request.Notif.NotifRuleMessagesMessage>
        {
            new Request.Notif.NotifMessagesMessage
            {
                Status = new Request.Notif.NotifMessagesMessageStatus
                {
                    Code = 1,
                    Bool = true,
                    Test1 = "Test",
                    Test2 = "Test"
                },
                Rules = new List<Request.Notif.NotifMessagesMessageRule>
                {
                    new Request.Notif.NotifMessagesMessageRule
                    {
                        Lengths = new Request.Notif.NotifMessagesMessageRuleLength
                        {
                            Lenght = 1,
                            Lengths = new List<Request.Notif.NotifMessagesMessageRuleLengthLength>
                            {
                                new Request.Notif.NotifMessagesMessageRuleLengthLength
                                {
                                    Type = "Test",
                                    Value = 1
                                }
                            }
                        },
                        Status = new List<Request.Notif.NotifMessagesMessageRuleStatus>
                        {
                            new Request.Notif.NotifMessagesMessageRuleStatus
                            {
                                Test1 = "Test",
                                Test2 = "Test"

Is there a way to automaticly fill all int values with 1 or 0 and all string values with Test and especially all objects with the right class without unit testing and external libs?

Upvotes: 0

Views: 216

Answers (1)

Xerillio
Xerillio

Reputation: 5259

Using reflection you could populate your objects recursively and set whatever default values you choose. A small example of a helper function that could do that for you:

void SetDefaults(object testObj)
{
    var props = testObj.GetType().GetProperties();
    foreach (var prop in props)
    {
        if (prop.GetSetMethod() == null)
        {
            continue;
        }

        var propType = prop.PropertyType;
        if (propType == typeof(int))
        {
            prop.SetValue(testObj, 1);
        }
        else if (propType == typeof(bool))
        {
            prop.SetValue(testObj, false);
        }
        // More conditions...
        else
        {
            var ctor = propType.GetConstructor(Type.EmptyTypes);
            var propertyObject = ctor.Invoke(new object[0]);
            SetDefaults(propertyObject);
            prop.SetValue(testObj, propertyObject);
        }
    }
}

As you can see, if your tree of objects use types that don't have default constructors (parameterless constructors) you need some more complicated logic in the else-condition. Basically the stuff going on here is a very simplified version of what happens in a dependency injection framework.

To use it, do something like:

void Main()
{
    TestObject obj = new TestObject();
    SetDefaults(obj);

    Console.WriteLine(obj);
}

class TestObject {
    public int MyInt { get; set; }
    public SubTestObject SubObj { get; set; }
}

class SubTestObject {
    public int MyOwnInt { get; set; }
    public bool MyBoolGetter => 1 > 0;
}

Upvotes: 2

Related Questions