开发者

MSUnit: Assert.AreEqual fails trees

I have to test the equality of trees. In other other words objects which contains List<T> with childs and the childs also contains List<T> with childs and so on.

I've found that you can test List with CollectionAssert, however it does not work that well with composites.

Any suggestions? MSUnit is my test library.

Example

IReagentComposed bronzeBarParsed = (from n in composedCrafts where n.ItemId == 2841 select n).Single();

IReagentComposed bronzeBar = new Craft()
{
    ItemId = 2841,
    Profession = Profession.Mining,
    Quantity = 0,
    QuantityCrafted = 0,
    Skill = 50,
    Reagents = new List()
    {
        new Craft()
        {
            ItemId = 2840,
            Quantity = 0,
            Skill = 1,
            Profession = Profession.Mining,
            Reagents = new List()
            {
                new Reagent()
                {
                    ItemId = 2770,
                    Quantity = 1开发者_如何学运维
                }
            }
        },
        new Craft()
        {
            ItemId = 3576,
            Quantity = 0,
            Skill = 50,
            Profession = Profession.Mining,
            Reagents = new List()
            {
                new Reagent()
                {
                    ItemId = 2771,
                    Quantity = 1
                }
            }
        }
    }
};

Assert.AreEqual(bronzeBar, bronzeBarParsed);

Craft and Reagent

public class Craft : IReagentComposed
{
    public int QuantityCrafted { get; set; }
    public int Quantity { get; set;}
    public int ItemId { get; set; }
    public int Skill { get; set; }
    public Profession Profession { get; set; }
    public IEnumerable Reagents { get; set; }


    public override bool Equals(object other)
    {
        if (other == null || GetType() != other.GetType()) return false;

        IReagentComposed o = other as IReagentComposed;

        return o != null && this.Quantity == o.Quantity &&
                this.ItemId == o.ItemId &&
                this.Profession == o.Profession &&
                this.Reagents == o.Reagents && //also tried Equals
                this.Skill == o.Skill;
    }

    public override int GetHashCode()
    {
        return 0;
    }
}

public class Reagent : IReagent
{
    public int ItemId { get; set; }
    public int Quantity { get; set; }

    public override bool Equals(object other)
    {
        if (other == null || GetType() != other.GetType()) return false;

        IReagent o = other as IReagent;

        return o != null && o.ItemId == this.ItemId && o.Quantity == this.Quantity;
    }

    public override int GetHashCode()
    {
        return 0;
    }
}


        return o != null && this.Quantity == o.Quantity &&
                this.ItemId == o.ItemId &&
                this.Profession == o.Profession &&
                this.Reagents == o.Reagents && //also tried Equals
                this.Skill == o.Skill;

The Reagents are unlikely to match, they are distinct List objects. List<> doesn't override Equals although it isn't that clear what actual type you use. Write a little helper function that takes two lists of reagents and checks for equality. You'd typically start at comparing List<>.Count and then work down the elements one by one.


Added a extension method for IEnumberable<T>:

public static class IEnumberableExtensions
{
    public static bool AreEnumerablesEqual<T>(this IEnumerable<T> x, IEnumerable<T> y)
    {
        if (x.Count() != y.Count()) return false;

        bool equals = false;

        foreach (var a in x)
        {
            foreach (var b in y)
            {
                if (a.Equals(b))
                {
                    equals = true;
                    break;
                }
            }

            if (!equals)
            {
                return false;
            }

            equals = false;
        }

        return true;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜