FindByMultipleAttributeConstraintsTests.cs :  » Web-Testing » WatiN » WatiN » Core » UnitTests » AttributeConstraintTests » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Web Testing » WatiN 
WatiN » WatiN » Core » UnitTests » AttributeConstraintTests » FindByMultipleAttributeConstraintsTests.cs
#region WatiN Copyright (C) 2006-2009 Jeroen van Menen

//Copyright 2006-2009 Jeroen van Menen
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

#endregion Copyright

using System;
using Moq;
using NUnit.Framework;
using WatiN.Core.Constraints;
using WatiN.Core.Exceptions;
using WatiN.Core.Interfaces;
using WatiN.Core.UnitTests.TestUtils;

namespace WatiN.Core.UnitTests.AttributeConstraintTests{
    [TestFixture]
    public class FindByMultipleAttributeConstraintsTests
    {
        [Test]
        public void AndTrue()
        {
            // GIVEN
            var mockAttributeBag = new Mock<IAttributeBag>();

            var findBy = Find.ByName("X").And(Find.ByValue("Cancel"));

            mockAttributeBag.Expect(bag => bag.GetAttributeValue("name")).Returns("X");
            mockAttributeBag.Expect(bag => bag.GetAttributeValue("value")).Returns("Cancel");

            // WHEN
            var compare = findBy.Matches(mockAttributeBag.Object, new ConstraintContext());
            
            // THEN
            Assert.IsTrue(compare);
        }

        [Test]
        public void AndFalseFirstSoSecondPartShouldNotBeEvaluated()
        {
            // GIVEN
            var mockAttributeBag = new Mock<IAttributeBag>();

            var findBy = Find.ByName("X").And(Find.ByValue("Cancel"));

            mockAttributeBag.Expect(bag => bag.GetAttributeValue("name")).Returns("Y");

            // WHEN
            var compare = findBy.Matches(mockAttributeBag.Object, new ConstraintContext());

            // THEN
            Assert.IsFalse(compare);
        }

        [Test]
        public void AndFalseSecond()
        {
            var findBy = Find.ByName("X").And(Find.ByValue("Cancel"));

            var mockAttributeBag = new MockAttributeBag("name", "X");
            mockAttributeBag.Add("value", "OK");

            var compare = findBy.Matches(mockAttributeBag, new ConstraintContext());
            Assert.IsFalse(compare);
        }

        [Test]
        public void OrFirstTrue()
        {
            var findBy = Find.ByName("X").Or(Find.ByName("Y"));
            var mockAttributeBag = new MockAttributeBag("name", "X");
            var compare = findBy.Matches(mockAttributeBag, new ConstraintContext());
            Assert.IsTrue(compare);
        }

        [Test]
        public void OrSecondTrue()
        {
            var findBy = Find.ByName("X").Or(Find.ByName("Y"));
            var mockAttributeBag = new MockAttributeBag("name", "Y");
            ConstraintContext context = new ConstraintContext();
            Assert.IsTrue(findBy.Matches(mockAttributeBag, context));
        }

        [Test]
        public void OrFalse()
        {
            var findBy = Find.ByName("X").Or(Find.ByName("Y"));
            var mockAttributeBag = new MockAttributeBag("name", "Z");
            var compare = findBy.Matches(mockAttributeBag, new ConstraintContext());
            Assert.IsFalse(compare);
        }

        [Test]
        public void AndOr()
        {
            var findByNames = Find.ByName("X").Or(Find.ByName("Y"));
            var findBy = Find.ByValue("Cancel").And(findByNames);

            var mockAttributeBag = new MockAttributeBag("name", "X");
            mockAttributeBag.Add("value", "Cancel");
            var compare = findBy.Matches(mockAttributeBag, new ConstraintContext());
            Assert.IsTrue(compare);
        }

        [Test]
        public void AndOrThroughOperatorOverloads()
        {
            var findBy = Find.ByName("X") & Find.ByValue("Cancel") | (Find.ByName("Z") & Find.ByValue("Cancel"));

            var mockAttributeBag = new MockAttributeBag("name", "Z");
            mockAttributeBag.Add("value", "OK");
            var compare = findBy.Matches(mockAttributeBag, new ConstraintContext());
            Assert.IsFalse(compare);
        }

        [Test, ExpectedException(typeof (ArgumentOutOfRangeException))]
        public void OccurrenceShouldNotAcceptNegativeValue()
        {
            new IndexConstraint(-1);
        }

        [Test]
        public void Occurence0()
        {
            Constraint findBy = new IndexConstraint(0);

            var mockAttributeBag = new MockAttributeBag("name", "Z");

            ConstraintContext context = new ConstraintContext();
            Assert.IsTrue(findBy.Matches(mockAttributeBag, context));
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
        }

        [Test]
        public void Occurence2()
        {
            Constraint findBy = new IndexConstraint(2);

            var mockAttributeBag = new MockAttributeBag("name", "Z");

            ConstraintContext context = new ConstraintContext();
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
            Assert.IsTrue(findBy.Matches(mockAttributeBag, context));
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
        }

        [Test]
        public void OccurenceAndTrue()
        {
            var findBy = new IndexConstraint(1).And(Find.ByName("Z"));

            var mockAttributeBag = new MockAttributeBag("name", "Z");

            ConstraintContext context = new ConstraintContext();
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
            Assert.IsTrue(findBy.Matches(mockAttributeBag, context));
        }

        [Test]
        public void OccurenceOr()
        {
            var findBy = new IndexConstraint(2).Or(Find.ByName("Z"));
            ConstraintContext context = new ConstraintContext();

            var mockAttributeBag = new MockAttributeBag("name", "Z");
            Assert.IsTrue(findBy.Matches(mockAttributeBag, context));

            mockAttributeBag = new MockAttributeBag("name", "y");

            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
            Assert.IsTrue(findBy.Matches(mockAttributeBag, context));
        }

        [Test]
        public void OccurenceAndFalse()
        {
            var findBy = new IndexConstraint(1).And(Find.ByName("Y"));

            var mockAttributeBag = new MockAttributeBag("name", "Z");

            ConstraintContext context = new ConstraintContext();
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
        }

        [Test]
        public void TrueAndOccurence()
        {
            var findBy = Find.ByName("Z").And(new IndexConstraint(1));

            var mockAttributeBag = new MockAttributeBag("name", "Z");

            ConstraintContext context = new ConstraintContext();
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
            Assert.IsTrue(findBy.Matches(mockAttributeBag, context));
        }

        [Test]
        public void FalseAndOccurence()
        {
            var findBy = Find.ByName("Y").And(new IndexConstraint(1));

            var mockAttributeBag = new MockAttributeBag("name", "Z");

            ConstraintContext context = new ConstraintContext();
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
        }

        [Test]
        public void TrueAndIndexConstraintAndTrue()
        {
            var mockAttributeBag = new Mock<IAttributeBag>();

            var findBy = Find.ByName("Z").And(new IndexConstraint(1)).And(Find.ByValue("text"));
            ConstraintContext context = new ConstraintContext();

            mockAttributeBag.Expect(bag => bag.GetAttributeValue("name")).Returns("Z");

            Assert.IsFalse(findBy.Matches(mockAttributeBag.Object, context), "False because index will be 0 when evaluated.");

            mockAttributeBag.Expect(bag => bag.GetAttributeValue("name")).Returns("Y");

            Assert.IsFalse(findBy.Matches(mockAttributeBag.Object, context), "False because index will be skipped since first clause fails to match.");

            mockAttributeBag.Expect(bag => bag.GetAttributeValue("name")).Returns("Z");
            mockAttributeBag.Expect(bag => bag.GetAttributeValue("value")).Returns("text");

            Assert.IsTrue(findBy.Matches(mockAttributeBag.Object, context), "True because index will be 1 when evaluated.");

            mockAttributeBag.Expect(bag => bag.GetAttributeValue("name")).Returns("Z");

            Assert.IsFalse(findBy.Matches(mockAttributeBag.Object, context), "False because index will be 2 when evaluated.");

            mockAttributeBag.VerifyAll();
        }

        [Test]
        public void OccurenceAndOrWithOrTrue()
        {
            var findBy = new IndexConstraint(2).And(Find.ByName("Y")).Or(Find.ByName("Z"));

            var mockAttributeBag = new MockAttributeBag("name", "Z");
            ConstraintContext context = new ConstraintContext();
            Assert.IsTrue(findBy.Matches(mockAttributeBag, context));
        }

        [Test]
        public void OccurenceAndOrWithAndTrue()
        {
            var findBy = new IndexConstraint(2).And(Find.ByName("Y")).Or(Find.ByName("Z"));

            ConstraintContext context = new ConstraintContext();
            var mockAttributeBag = new MockAttributeBag("name", "Y");
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
            Assert.IsTrue(findBy.Matches(mockAttributeBag, context));
            Assert.IsFalse(findBy.Matches(mockAttributeBag, context));
        }

        [Test, ExpectedException(typeof (ReEntryException))]
        public void RecusiveCallExceptionExpected()
        {
            var mockAttributeBag = new Mock<IAttributeBag>();
            
            // Note: Creating a re-entrant constraint is now much more difficult than
            //       before because constraints are immutable.  Even the code findBy |= findBy
            //       will not create a re-entrant constraint, it will just be an Or constraint
            //       with two identical clauses.  Given this change in constraint construction
            //       we should consider removing the re-entrance checks in the future.  -- Jeff.
            Constraint findBy = Find.By("tag", "value");
            findBy |=  new PredicateConstraint<IAttributeBag>(bag => findBy.Matches(bag, new ConstraintContext()));

            ConstraintContext context = new ConstraintContext();
            mockAttributeBag.Expect(bag => bag.GetAttributeValue("tag")).Returns("val");
            mockAttributeBag.Expect(bag => bag.GetAdapter<IAttributeBag>()).Returns(mockAttributeBag.Object);

            findBy.Matches(mockAttributeBag.Object, context);

            mockAttributeBag.VerifyAll();
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.