Imports System
Imports System.Text
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports Mercury.Mobile.Data

<TestClass()> Public Class Filter_specs

    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = Value
        End Set
    End Property

#Region "Additional test attributes"
    '
    ' You can use the following additional attributes as you write your tests:
    '
    ' Use ClassInitialize to run code before running the first test in the class
    ' <ClassInitialize()> Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
    ' End Sub
    '
    ' Use ClassCleanup to run code after all tests in a class have run
    ' <ClassCleanup()> Public Shared Sub MyClassCleanup()
    ' End Sub
    '
    ' Use TestInitialize to run code before running each test
    ' <TestInitialize()> Public Sub MyTestInitialize()
    ' End Sub
    '
    ' Use TestCleanup to run code after each test has run
    ' <TestCleanup()> Public Sub MyTestCleanup()
    ' End Sub
    '
#End Region

    <TestMethod()> _
    Public Sub FilterInfo_can_be_created()
        Dim fi = New FilterInfo("Id", "CCA*", FilterComparisonMode.Equal, FilterConjunctionMode.AndFilter)
        Assert.IsNotNull(fi)
        Assert.IsTrue(fi.ComparisonMode = FilterComparisonMode.Equal)
        Assert.IsTrue(fi.ConjunctionMode = FilterConjunctionMode.AndFilter)
        Assert.IsTrue(fi.Expression = "CCA*")
        Assert.IsFalse(fi.IsCaseSensitive)
        fi.IsCaseSensitive = True
        Assert.IsTrue(fi.IsCaseSensitive)
        Assert.IsTrue(fi.PropertyName = "Id")
    End Sub

    <TestMethod()> _
    Public Sub FilterInfo_can_find_exact_matches()
        Dim value = "CCA123456789"
        Dim fi = New FilterInfo("Id", value, FilterComparisonMode.Equal, FilterConjunctionMode.AndFilter)
        Assert.IsNotNull(fi)

        Assert.IsTrue(fi.IsMatch(value))
        fi.IsCaseSensitive = True
        Assert.IsFalse(fi.IsMatch(value.ToLower()))
        fi.IsCaseSensitive = False
        Assert.IsTrue(fi.IsMatch(value.ToLower()))
    End Sub

    <TestMethod()> _
    Public Sub Filter_can_find_by_wild_card()
        ' Wildcard on right
        Dim fi = New FilterInfo("Id", "CCA*", FilterComparisonMode.Equal, FilterConjunctionMode.AndFilter)
        Assert.IsTrue(fi.IsMatch("CCA123"))
        Assert.IsTrue(fi.IsMatch("CCA0000000000000000000000000000001"))
        Assert.IsTrue(fi.IsMatch("cca0"))
        Assert.IsTrue(fi.IsMatch("CCA"))
        Assert.IsFalse(fi.IsMatch(" CCA"))

        ' Wildcard on left
        fi = New FilterInfo("Id", "*1A3", FilterComparisonMode.Equal, FilterConjunctionMode.AndFilter)
        Assert.IsTrue(fi.IsMatch("12348568fdfd1A3"))
        Assert.IsTrue(fi.IsMatch(" 1a3"))
        Assert.IsTrue(fi.IsMatch("1A3"))

        ' Wildcard on both sides
        fi = New FilterInfo("Id", "*abc*", FilterComparisonMode.Equal, FilterConjunctionMode.AndFilter)
        Assert.IsTrue(fi.IsMatch("ABC"))
        Assert.IsTrue(fi.IsMatch("ABCDEFG"))
        Assert.IsTrue(fi.IsMatch("123abc"))
        Assert.IsTrue(fi.IsMatch("9998abc421T"))

        ' Wildcard in middle?
        fi = New FilterInfo("Id", "ab*1", FilterComparisonMode.Equal, FilterConjunctionMode.AndFilter)
        Assert.IsTrue(fi.IsMatch("AB1"))
        Assert.IsTrue(fi.IsMatch("ab0000001"))
    End Sub

    <TestMethod()> _
    Public Sub Filter_can_find_by_alternate_comparisons()
        ' Greater than
        Dim fi = New FilterInfo("Id", "CCA001", FilterComparisonMode.GreaterThan, FilterConjunctionMode.AndFilter)
        Assert.IsTrue(fi.IsMatch("cca002"))
        Assert.IsTrue(fi.IsMatch("cca123456789"))
        Assert.IsFalse(fi.IsMatch("CCA000"))
        Assert.IsFalse(fi.IsMatch("CCA001"))

        ' Greater than or equal
        fi = New FilterInfo("Id", "CCA001", FilterComparisonMode.GreaterThanOrEqual, FilterConjunctionMode.AndFilter)
        Assert.IsTrue(fi.IsMatch("cca002"))
        Assert.IsTrue(fi.IsMatch("cca123456789"))
        Assert.IsFalse(fi.IsMatch("CCA000"))
        Assert.IsTrue(fi.IsMatch("CCA001"))

        ' Less than
        fi = New FilterInfo("Id", "CCA2112", FilterComparisonMode.LessThan, FilterConjunctionMode.AndFilter)
        Assert.IsTrue(fi.IsMatch("cca002"))
        Assert.IsTrue(fi.IsMatch("CCA001"))
        Assert.IsTrue(fi.IsMatch("CCA2111"))
        Assert.IsFalse(fi.IsMatch("cca999999"))
        Assert.IsFalse(fi.IsMatch("CCA2113"))
        Assert.IsFalse(fi.IsMatch("CCA2112"))

        ' Less than or equal
        fi = New FilterInfo("Id", "CCA2112", FilterComparisonMode.LessThanOrEqual, FilterConjunctionMode.AndFilter)
        Assert.IsTrue(fi.IsMatch("cca002"))
        Assert.IsTrue(fi.IsMatch("CCA001"))
        Assert.IsTrue(fi.IsMatch("CCA2111"))
        Assert.IsFalse(fi.IsMatch("cca999999"))
        Assert.IsFalse(fi.IsMatch("CCA2113"))
        Assert.IsTrue(fi.IsMatch("CCA2112"))

        ' Not equal
        fi = New FilterInfo("Id", "CCA2112", FilterComparisonMode.NotEqual, FilterConjunctionMode.AndFilter)
        Assert.IsTrue(fi.IsMatch("cca002"))
        Assert.IsTrue(fi.IsMatch("CCA001"))
        Assert.IsTrue(fi.IsMatch("CCA2111"))
        Assert.IsTrue(fi.IsMatch("cca999999"))
        Assert.IsTrue(fi.IsMatch("CCA2113"))
        Assert.IsTrue(fi.IsMatch(""))
        Assert.IsFalse(fi.IsMatch("CCA2112"))
        fi = New FilterInfo("Id", "a*bc", FilterComparisonMode.NotEqual)
        Assert.IsTrue(fi.IsMatch("ac"))
        Assert.IsTrue(fi.IsMatch("cba"))
        Assert.IsTrue(fi.IsMatch(""))
        Assert.IsFalse(fi.IsMatch("abc"))
        Assert.IsFalse(fi.IsMatch("a123tyBC"))
        fi = New FilterInfo("Id", "*abc*", FilterComparisonMode.NotEqual)
        Assert.IsFalse(fi.IsMatch("abc"))
        Assert.IsFalse(fi.IsMatch("555abc123"))
        Assert.IsTrue(fi.IsMatch(""))
        Assert.IsTrue(fi.IsMatch("cdefg"))
        Assert.IsTrue(fi.IsMatch("acdefbg"))
    End Sub

End Class
