//
// Unit tests for WriteStaticFieldFromInstanceMethodRule
//
// Authors:
//	Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2006-2008 Novell, Inc (http://www.novell.com)
//
// 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 Gendarme.Rules.Concurrency;

using NUnit.Framework;
using Test.Rules.Fixtures;

namespace Test.Rules.Concurrency {

	[TestFixture]
	public class WriteStaticFieldFromInstanceMethodTest : MethodRuleTestFixture<WriteStaticFieldFromInstanceMethodRule> {

		public class TestCase {

			public const string public_const_field = "";

			private static string private_static_field = String.Empty;
			public static int private_public_field = 1;

			[ThreadStatic]
			private static string thread_local_field;

			static TestCase ()
			{
				private_static_field = "static";
			}

			public TestCase ()
			{
				private_static_field = "instance";
			}


			public string GetConstField ()
			{
				return public_const_field;
			}

			public string GetStaticField ()
			{
				return private_static_field;
			}

			public void SetStaticField (string user_value)
			{
				private_static_field = user_value;
			}

			public string Append (string user_value)
			{
				return private_static_field + user_value;
			}

			public string AppendChange (string user_value)
			{
				private_static_field += user_value;
				return private_static_field;
			}

			public int MultipleChanges ()
			{
				private_public_field = 10 + private_public_field;
				private_public_field = private_public_field + 12;
				return private_public_field;
			}

			public string ThreadStatic (string user_value)
			{
				thread_local_field = (thread_local_field ?? String.Empty) + user_value;
				return thread_local_field;
			}
		}

		[Test]
		public void GetConstField ()
		{
			AssertRuleDoesNotApply<TestCase> ("GetConstField");
		}

		[Test]
		public void GetStaticField ()
		{
			AssertRuleDoesNotApply<TestCase> ("GetStaticField");
		}

		[Test]
		public void SetStaticField ()
		{
			AssertRuleFailure<TestCase> ("SetStaticField", 1);
		}

		[Test]
		public void Append ()
		{
			AssertRuleDoesNotApply<TestCase> ("Append");
		}

		[Test]
		public void AppendChange ()
		{
			AssertRuleFailure<TestCase> ("AppendChange", 1);
		}

		[Test]
		public void MultipleChanges ()
		{
			AssertRuleFailure<TestCase> ("MultipleChanges", 2);
		}

		[Test]
		public void StaticConstructor ()
		{
			AssertRuleDoesNotApply<TestCase> (".cctor");
		}

		[Test]
		public void ThreadStatic ()
		{
			AssertRuleSuccess<TestCase> ("ThreadStatic");
		}

		[Test]
		public void Constructor ()
		{
			AssertRuleFailure<TestCase> (".ctor", 1);
		}
	}
}
