﻿// ***********************************************************************
// Copyright (c) 2007 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.
// ***********************************************************************
#if !PORTABLE
using System;
using BenchmarkSuite.Framework.Interfaces;
using BenchmarkSuite.Framework.Internal;
using BenchmarkSuite.Framework.Common;

namespace BenchmarkSuite.Framework
{
    /// <summary>
    /// PlatformAttribute is used to mark a test fixture or an
    /// individual method as applying to a particular platform only.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Assembly, AllowMultiple = true, Inherited=false)]
    public class PlatformAttribute : IncludeExcludeAttribute, IApplyToTest
    {
        private PlatformHelper platformHelper = new PlatformHelper();

        /// <summary>
        /// Constructor with no platforms specified, for use
        /// with named property syntax.
        /// </summary>
        public PlatformAttribute() { }

        /// <summary>
        /// Constructor taking one or more platforms
        /// </summary>
        /// <param name="platforms">Comma-deliminted list of platforms</param>
        public PlatformAttribute(string platforms) : base(platforms) { }

        #region IApplyToTest members

        /// <summary>
        /// Causes a test to be skipped if this PlatformAttribute is not satisfied.
        /// </summary>
        /// <param name="test">The test to modify</param>
        public void ApplyToTest(Test test)
        {
            if (test.RunState != RunState.NotRunnable && !platformHelper.IsPlatformSupported(this))
            {
                test.RunState = RunState.Skipped;
                test.Properties.Add(PropertyNames.SkipReason, platformHelper.Reason);
            }
        }

        #endregion
    }
}
#endif