// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics.Contracts;
using Xunit;

namespace System.Diagnostics.Contracts.Tests
{
    public class BasicContractsTest
    {
        [Fact]
        public static void AssertTrueDoesNotRaiseEvent()
        {
            bool eventRaised = false;
            Contract.ContractFailed += (s, e) =>
            {
                eventRaised = true;
                e.SetHandled();
            };

            Contract.Assert(true);
            Assert.False(eventRaised, "ContractFailed event was raised");
        }

        [Fact]
        public static void AssertFalseRaisesEvent()
        {
            bool eventRaised = false;
            Contract.ContractFailed += (s, e) =>
            {
                eventRaised = true;
                e.SetHandled();
            };

            Contract.Assert(false);
            Assert.True(eventRaised, "ContractFailed event not raised");
        }

        [Fact]
        public static void AssertFalseThrows()
        {
            bool eventRaised = false;
            Contract.ContractFailed += (s, e) =>
            {
                eventRaised = true;
                e.SetUnwind();
            };


            Assert.ThrowsAny<Exception>(() =>
            {
                Contract.Assert(false, "Some kind of user message");
            });

            Assert.True(eventRaised, "Event was not raised");
        }
    }
}

// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics.Contracts;
using Xunit;

namespace System.Diagnostics.Contracts.Tests
{
    public class BasicContractsTest
    {
        [Fact]
        public static void AssertTrueDoesNotRaiseEvent()
        {
            bool eventRaised = false;
            Contract.ContractFailed += (s, e) =>
            {
                eventRaised = true;
                e.SetHandled();
            };

            Contract.Assert(true);

            Assert.False(eventRaised, "ContractFailed event was raised");
        }

        [Fact]
        public static void AssertFalseRaisesEvent()
        {
            bool eventRaised = false;
            Contract.ContractFailed += (s, e) =>
            {
                eventRaised = true;
                e.SetHandled();
            };

            Contract.Assert(false);
#if DEBUG
            Assert.True(eventRaised, "ContractFailed event not raised");            
#else
            Assert.False(eventRaised, "ContractFailed event was raised");
#endif            
        }

        [Fact]
        public static void AssertFalseThrows()
        {
            bool eventRaised = false;
            Contract.ContractFailed += (s, e) =>
            {
                eventRaised = true;
                e.SetUnwind();
            };


#if DEBUG
            Assert.ThrowsAny<Exception>(() =>
            {
                Contract.Assert(false, "Some kind of user message");
            });

            Assert.True(eventRaised, "Event was not raised");
#else
            Contract.Assert(false, "Some kind of user message");
            
            Assert.False(eventRaised, "ContractFailed event was raised");
#endif            
        }
    }
}

Update S.Diag.Contracts tests to verify behavior for DEBUG and !DEBUG
Update S.Diag.Contracts tests to verify behavior for DEBUG and !DEBUG

System.Diagnostics.Contracts is implemented in mscorlib.  In versions
of mscorlib compiled with the DEBUG property, the Contract.Assert
methods are active.  In versions of mscorlib compiled without the
DEBUG property, the Contract.Assert methods are no-ops.  This change
updates the test to verify the behavior based on if it was compiled
with or without the DEBUG property.  It's then important that the
configuration of the test (debug or retail) match that of the mscorlib
it is testing.
