﻿// ***********************************************************************
// Copyright (c) 2013 Charlie Poole
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using System.Collections.Generic;
using NUnit.Framework.Interfaces;

namespace NUnit.Framework.Internal.Filters
{
    /// <summary>
    /// ValueMatchFilter selects tests based on some value, which
    /// is expected to be contained in the test.
    /// </summary>
    /// <typeparam name="T">The Type of the value</typeparam>
    [Serializable]
    public abstract class ValueMatchFilter<T> : TestFilter
    {
        private List<T> _values = new List<T>();

        /// <summary>
        /// Return a list of the values matched by the filter - used for testing
        /// </summary>
        public IList<T> Values { get { return _values; } }

        /// <summary>
        /// Construct an empty ValueMatchFilter
        /// </summary>
        public ValueMatchFilter() { }

        /// <summary>
        /// Construct a ValueMatchFilter for a single value.
        /// </summary>
        /// <param name="value">The value to be included.</param>
        public ValueMatchFilter(T value)
        {
            this.Add(value);
        }

        /// <summary>
        /// Construct a ValueMatchFilter for multiple values
        /// </summary>
        /// <param name="values">The set of values the filter will recognize.</param>
        public ValueMatchFilter(IEnumerable<T> values)
        {
            foreach (T value in values)
                this.Add(value);
        }

        /// <summary>
        /// Add a single value to the filter
        /// </summary>
        /// <param name="value">The value to be added.</param>
        public void Add(T value)
        {
            _values.Add(value);
        }

        /// <summary>
        /// Check whether the filter matches a test
        /// </summary>
        /// <param name="test">The test to be matched</param>
        /// <returns>True if it matches, otherwise false</returns>
        public override bool Match(ITest test)
        {
            foreach (T value in _values)
                if (Match(test, value))
                    return true;

            return false;
        }

        /// <summary>
        /// Match a test against a single value.
        /// </summary>
        /// <returns></returns>
        protected abstract bool Match(ITest test, T value);
    }
}
