assertsyntaxtests.vb

来自「NUnit-2.4.1-net-2.0.rar NUnit 测试用例详细的步骤」· VB 代码 · 共 588 行 · 第 1/2 页

VB
588
字号
' ****************************************************************
' Copyright 2007, Charlie Poole
' This is free software licensed under the NUnit license. You may
' obtain a copy of the license at http:'nunit.org/?p=license&r=2.4
' ****************************************************************

Option Explicit On 

Imports System
Imports NUnit.Framework
Imports NUnit.Framework.Constraints
Imports NUnit.Framework.SyntaxHelpers
Imports Tis = NUnit.Framework.SyntaxHelpers.Is
Imports Text = NUnit.Framework.SyntaxHelpers.Text

Namespace NUnit.Samples

    ' This test fixture attempts to exercise all the syntactic
    ' variations of Assert without getting into failures, errors 
    ' or corner cases. Thus, some of the tests may be duplicated 
    ' in other fixtures.
    ' 
    ' Each test performs the same operations using the classic
    ' syntax (if available) and the new syntax in both the
    ' helper-based and inherited forms.
    ' 
    ' This Fixture will eventually be duplicated in other
    ' supported languages. 

    <TestFixture()> _
    Public Class AssertSyntaxTests
        Inherits AssertionHelper

        <Test()> _
        Public Sub IsNull()
            ' Classic syntax
            Assert.IsNull(Nothing)

            ' Helper syntax
            Assert.That(Nothing, Tis.Null)

            ' Inherited syntax
            Expect(Nothing, Null)
        End Sub


        <Test()> _
        Public Sub IsNotNull()
            ' Classic syntax
            Assert.IsNotNull(42)

            ' Helper syntax
            Assert.That(42, Tis.Not.Null)

            ' Inherited syntax
            Expect(42, Tis.Not.Null)
        End Sub

        <Test()> _
        Public Sub IsTrue()
            ' Classic syntax
            Assert.IsTrue(2 + 2 = 4)

            ' Helper syntax
            Assert.That(2 + 2 = 4, Tis.True)
            Assert.That(2 + 2 = 4)

            ' Inherited syntax
            Expect(2 + 2 = 4, Tis.True)
            Expect(2 + 2 = 4)
        End Sub

        <Test()> _
        Public Sub IsFalse()
            ' Classic syntax
            Assert.IsFalse(2 + 2 = 5)

            ' Helper syntax
            Assert.That(2 + 2 = 5, Tis.False)

            ' Inherited syntax
            Expect(2 + 2 = 5, Tis.False)
        End Sub

        <Test()> _
        Public Sub IsNaN()
            Dim d As Double = Double.NaN
            Dim f As Single = Single.NaN

            ' Classic syntax
            Assert.IsNaN(d)
            Assert.IsNaN(f)

            ' Helper syntax
            Assert.That(d, Tis.NaN)
            Assert.That(f, Tis.NaN)

            ' Inherited syntax
            Expect(d, NaN)
            Expect(f, NaN)
        End Sub

        <Test()> _
        Public Sub EmptyStringTests()
            ' Classic syntax
            Assert.IsEmpty("")
            Assert.IsNotEmpty("Hello!")

            ' Helper syntax
            Assert.That("", Tis.Empty)
            Assert.That("Hello!", Tis.Not.Empty)

            ' Inherited syntax
            Expect("", Empty)
            Expect("Hello!", Tis.Not.Empty)
        End Sub

        <Test()> _
        Public Sub EmptyCollectionTests()

            Dim boolArray As Boolean() = New Boolean() {}
            Dim nonEmpty As Integer() = New Integer() {1, 2, 3}

            ' Classic syntax
            Assert.IsEmpty(boolArray)
            Assert.IsNotEmpty(nonEmpty)

            ' Helper syntax
            Assert.That(boolArray, Tis.Empty)
            Assert.That(nonEmpty, Tis.Not.Empty)

            ' Inherited syntax
            Expect(boolArray, Tis.Empty)
            Expect(nonEmpty, Tis.Not.Empty)
        End Sub

        <Test()> _
        Public Sub ExactTypeTests()
            ' Classic syntax workarounds
            Assert.AreEqual(GetType(String), "Hello".GetType())
            Assert.AreEqual("System.String", "Hello".GetType().FullName)
            Assert.AreNotEqual(GetType(Integer), "Hello".GetType())
            Assert.AreNotEqual("System.Int32", "Hello".GetType().FullName)

            ' Helper syntax
            Assert.That("Hello", Tis.TypeOf(GetType(String)))
            Assert.That("Hello", Tis.Not.TypeOf(GetType(Integer)))

            ' Inherited syntax
            Expect("Hello", Tis.TypeOf(GetType(String)))
            Expect("Hello", Tis.Not.TypeOf(GetType(Integer)))
        End Sub

        <Test()> _
        Public Sub InstanceOfTypeTests()
            ' Classic syntax
            Assert.IsInstanceOfType(GetType(String), "Hello")
            Assert.IsNotInstanceOfType(GetType(String), 5)

            ' Helper syntax
            Assert.That("Hello", Tis.InstanceOfType(GetType(String)))
            Assert.That(5, Tis.Not.InstanceOfType(GetType(String)))

            ' Inherited syntax
            Expect("Hello", InstanceOfType(GetType(String)))
            Expect(5, Tis.Not.InstanceOfType(GetType(String)))
        End Sub

        <Test()> _
        Public Sub AssignableFromTypeTests()
            ' Classic syntax
            Assert.IsAssignableFrom(GetType(String), "Hello")
            Assert.IsNotAssignableFrom(GetType(String), 5)

            ' Helper syntax
            Assert.That("Hello", Tis.AssignableFrom(GetType(String)))
            Assert.That(5, Tis.Not.AssignableFrom(GetType(String)))

            ' Inherited syntax
            Expect("Hello", AssignableFrom(GetType(String)))
            Expect(5, Tis.Not.AssignableFrom(GetType(String)))
        End Sub

        <Test()> _
        Public Sub SubstringTests()
            Dim phrase As String = "Hello World!"
            Dim array As String() = New String() {"abc", "bad", "dba"}

            ' Classic Syntax
            StringAssert.Contains("World", phrase)

            ' Helper syntax
            Assert.That(phrase, Text.Contains("World"))
            ' Only available using new syntax
            Assert.That(phrase, Text.DoesNotContain("goodbye"))
            Assert.That(phrase, Text.Contains("WORLD").IgnoreCase)
            Assert.That(phrase, Text.DoesNotContain("BYE").IgnoreCase)
            Assert.That(Array, Text.All.Contains("b"))

            ' Inherited syntax
            Expect(phrase, Contains("World"))
            ' Only available using new syntax
            Expect(phrase, Text.Not.Contains("goodbye"))
            Expect(phrase, Contains("WORLD").IgnoreCase)
            Expect(phrase, Text.Not.Contains("BYE").IgnoreCase)
            Expect(Array, All.Contains("b"))
        End Sub

        <Test()> _
        Public Sub StartsWithTests()
            Dim phrase As String = "Hello World!"
            Dim greetings As String() = New String() {"Hello!", "Hi!", "Hola!"}

            ' Classic syntax
            StringAssert.StartsWith("Hello", phrase)

            ' Helper syntax
            Assert.That(phrase, Text.StartsWith("Hello"))
            ' Only available using new syntax
            Assert.That(phrase, Text.DoesNotStartWith("Hi!"))
            Assert.That(phrase, Text.StartsWith("HeLLo").IgnoreCase)
            Assert.That(phrase, Text.DoesNotStartWith("HI").IgnoreCase)
            Assert.That(greetings, Text.All.StartsWith("h").IgnoreCase)

            ' Inherited syntax
            Expect(phrase, StartsWith("Hello"))
            ' Only available using new syntax
            Expect(phrase, Text.Not.StartsWith("Hi!"))
            Expect(phrase, StartsWith("HeLLo").IgnoreCase)
            Expect(phrase, Text.Not.StartsWith("HI").IgnoreCase)
            Expect(greetings, All.StartsWith("h").IgnoreCase)
        End Sub

        <Test()> _
        Public Sub EndsWithTests()
            Dim phrase As String = "Hello World!"
            Dim greetings As String() = New String() {"Hello!", "Hi!", "Hola!"}

            ' Classic Syntax
            StringAssert.EndsWith("!", phrase)

            ' Helper syntax
            Assert.That(phrase, Text.EndsWith("!"))
            ' Only available using new syntax
            Assert.That(phrase, Text.DoesNotEndWith("?"))
            Assert.That(phrase, Text.EndsWith("WORLD!").IgnoreCase)
            Assert.That(greetings, Text.All.EndsWith("!"))

            ' Inherited syntax
            Expect(phrase, EndsWith("!"))
            ' Only available using new syntax
            Expect(phrase, Text.Not.EndsWith("?"))
            Expect(phrase, EndsWith("WORLD!").IgnoreCase)
            Expect(greetings, All.EndsWith("!"))
        End Sub

        <Test()> _
        Public Sub EqualIgnoringCaseTests()

            Dim phrase As String = "Hello World!"
            Dim array1 As String() = New String() {"Hello", "World"}
            Dim array2 As String() = New String() {"HELLO", "WORLD"}
            Dim array3 As String() = New String() {"HELLO", "Hello", "hello"}

            ' Classic syntax
            StringAssert.AreEqualIgnoringCase("hello world!", phrase)

            ' Helper syntax
            Assert.That(phrase, Tis.EqualTo("hello world!").IgnoreCase)
            'Only available using new syntax
            Assert.That(phrase, Tis.Not.EqualTo("goodbye world!").IgnoreCase)
            Assert.That(array1, Tis.EqualTo(array2).IgnoreCase)
            Assert.That(array3, Tis.All.EqualTo("hello").IgnoreCase)

            ' Inherited syntax
            Expect(phrase, EqualTo("hello world!").IgnoreCase)
            'Only available using new syntax
            Expect(phrase, Tis.Not.EqualTo("goodbye world!").IgnoreCase)
            Expect(array1, EqualTo(array2).IgnoreCase)
            Expect(array3, All.EqualTo("hello").IgnoreCase)
        End Sub

        <Test()> _
        Public Sub RegularExpressionTests()
            Dim phrase As String = "Now is the time for all good men to come to the aid of their country."
            Dim quotes As String() = New String() {"Never say never", "It's never too late", "Nevermore!"}

            ' Classic syntax
            StringAssert.IsMatch("all good men", phrase)
            StringAssert.IsMatch("Now.*come", phrase)

            ' Helper syntax
            Assert.That(phrase, Text.Matches("all good men"))
            Assert.That(phrase, Text.Matches("Now.*come"))

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?