通过单元测试覆盖此代码块中的所有块

时间:2014-05-02 09:03:36

标签: c# unit-testing

我试图覆盖这段代码中的所有块。代码覆盖率表示,所有行似乎都被覆盖了。但是两个街区仍未被发现,我无法弄明白。

这是我正在编写的代码:

public TRange Intersect(TRange range)
{
    Validator.IsNotNull(range, "range");
    if (!HasOverlap(range))
        return null;
    return Calculator.FromStartStop(
        Tools.Max(Start, range.Start) ?? Start ?? range.Start, 
        Tools.Min(Stop, range.Stop) ?? Stop ?? range.Stop);
}

解释:此代码返回两个范围的交集:假设您有[10..20]的IntegerRange(以下为:IntRange)和[15..25]之一;交叉的结果是[15..20]。如上所述,它取最开始的值和停止的最小值。

验证器检查空范围。 HasOverlap检查两个范围是否在彼此“内部”:[10..20]和[15..25],但[10..20]和[25..30]不是。

到目前为止,我的单位测试涵盖了38个街区中的36个。

[TestMethod]
public void IntersectTest()
{
    IntRange intRange510 = IntRange.FromStartStop(5, 10);
    IntRange intRange520 = IntRange.FromStartStop(5, 20);
    IntRange intRange4050 = IntRange.FromStartStop(40, 50);
    IntRange intRange1020 = IntRange.FromStartStop(10, 20);
    IntRange intRange1530 = IntRange.FromStartStop(15, 30);
    IntRange intRange2030 = IntRange.FromStartStop(20, 30);
    IntRange intRangenull5 = IntRange.FromStartStop(null, 5);
    IntRange intRangenull10 = IntRange.FromStartStop(null, 10);
    IntRange intRangenull20 = IntRange.FromStartStop(null, 20);
    IntRange intRange5null = IntRange.FromStartStop(5, null);
    IntRange intRange10null = IntRange.FromStartStop(10, null);
    IntRange intRange20null = IntRange.FromStartStop(20, null);
    IntRange intRangenullnull = IntRange.FromStartStop(null, null);

    //4x x/null
    Assert.AreEqual(intRangenullnull, intRangenullnull.Intersect(intRangenullnull));
    Assert.AreEqual(IntRange.FromStartStop(15,20), intRange1020.Intersect(intRange1530));

    // 3x nulltest
    Assert.AreEqual(intRange10null, intRange10null.Intersect(intRangenullnull));
    Assert.AreEqual(intRangenull10, intRangenull10.Intersect(intRangenullnull));
    Assert.AreEqual(intRange10null, intRangenullnull.Intersect(intRange10null));
    Assert.AreEqual(intRangenull10, intRangenullnull.Intersect(intRangenull10));

    // 2x nulltest
    Assert.AreEqual(intRange1020, intRangenullnull.Intersect(intRange1020));
    Assert.AreEqual(intRangenull10, intRangenull10.Intersect(intRangenull20));
    Assert.AreEqual(intRangenull10, intRangenull20.Intersect(intRangenull10));
    Assert.AreEqual(intRange510, intRangenull10.Intersect(intRange5null));
    Assert.AreEqual(intRange510, intRange5null.Intersect(intRangenull10));
    Assert.AreEqual(intRange10null, intRange10null.Intersect(intRange5null));
    Assert.AreEqual(intRange20null, intRange10null.Intersect(intRange20null));
    Assert.AreEqual(intRange1020, intRange1020.Intersect(intRangenullnull));

    // 1x nulltest
    Assert.AreEqual(intRange510, intRangenull10.Intersect(intRange510));
    Assert.AreEqual(intRange510, intRangenull10.Intersect(intRange520));
    Assert.AreEqual(intRange1020, intRange10null.Intersect(intRange1020));
    Assert.AreEqual(intRange1020, intRange10null.Intersect(intRange520));
    Assert.AreEqual(intRange510, intRange510.Intersect(intRangenull20));
    Assert.AreEqual(intRange510, intRange520.Intersect(intRangenull10));
    Assert.AreEqual(intRange2030, intRange1530.Intersect(intRange20null));
    Assert.AreEqual(intRange1530, intRange1530.Intersect(intRange5null));

    // No overlap
    Assert.IsNull(intRange510.Intersect(intRange4050));
}

[TestMethod]
[ExpectedException(typeof(ValidationException))]
public void EmptyIntersectTest()
{
    IntRange intRange = null;
    _Range10To20.Intersect(intRange);
} 

我还检查了解编译(和转换)的代码,但找不到我遗漏的内容:

public TRange Intersect(TRange range)
{
    Validator.IsNotNull(range, "range");
    TRange result;
    if (!this.HasOverlap(range))
    {
        result = default(TRange);
    }
    else
    {
        RangeCalculatorBase<TRange, TStartStop, TLength> arg_ED_0 = Range<TRange, TStartStop, TLength>.Calculator;
        TStartStop? tStartStop = Tools.Max<TStartStop?>(this.Start, range.Start);
        TStartStop? arg_ED_1;
        if (!tStartStop.HasValue)
        {
            TStartStop? tStartStop2 = this.Start;
            arg_ED_1 = (tStartStop2.HasValue ? new TStartStop?(tStartStop2.GetValueOrDefault()) : range.Start);
        }
        else
        {
            arg_ED_1 = new TStartStop?(tStartStop.GetValueOrDefault());
        }
        tStartStop = Tools.Min<TStartStop?>(this.Stop, range.Stop);
        TStartStop? arg_ED_2;
        if (!tStartStop.HasValue)
        {
            TStartStop? tStartStop2 = this.Stop;
            arg_ED_2 = (tStartStop2.HasValue ? new TStartStop?(tStartStop2.GetValueOrDefault()) : range.Stop);
        }
        else
        {
            arg_ED_2 = new TStartStop?(tStartStop.GetValueOrDefault());
        }
        result = arg_ED_0.FromStartStop(arg_ED_1, arg_ED_2);
    }
    return result;
}

我在这里测试的是什么?

添加:IntRange继承自TRange,但不添加任何新内容。

0 个答案:

没有答案