<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Moq.Tests
{
	public class ExtensibilityFixture
	{
		[Fact]
		public void ShouldExtendMatching()
		{
			var mock = new Mock<IOrderRepository>();
			mock.Setup(repo => repo.Save(OrderIs.Big()))
				.Throws(new InvalidOperationException());

			try
			{
				mock.Object.Save(new Order { Amount = 1000 });

				Assert.True(false, "Should have failed for big order");
			}
			catch (InvalidOperationException)
			{
			}
		}

		[Fact]
		public void ShouldExtendWithSimpleMatchers()
		{
			var order = new Order();
			var mock = new Mock<IOrderRepository>();

			Order repo = Match<Order>.Create(r => true);

			mock.Setup(x => x.Save(Orders.Contains(order)))
				 .Throws<ArgumentException>();

			Assert.Throws<ArgumentException>(() => mock.Object.Save(new [] { order }));
		}

		[Fact]
		public void ShouldExtendWithPropertyMatchers()
		{
			var mock = new Mock<IOrderRepository>();
			mock.Setup(repo => repo.Save(Orders.IsSmall))
				.Throws(new InvalidOperationException());

			try
			{
				mock.Object.Save(new Order { Amount = 50 });

				Assert.True(false, "Should have failed for small order");
			}
			catch (InvalidOperationException)
			{
			}
		}

		[Fact]
		public void SetterMatcherRendersNicely()
		{
			var mock = new Mock<IOrderRepository>();

			try
			{
				mock.VerifySet(repo => repo.Value = It.IsAny<int>());
			}
			catch (MockException me)
			{
				Console.WriteLine(me.Message);
			}
		}
	}

	public static class Orders
	{
		public static IEnumerable<Order> Contains(Order order)
		{
			return Match<IEnumerable<Order>>.Create(orders => orders.Contains(order));
		}

		public static Order IsBig()
		{
			return Match<Order>.Create(o => o.Amount >= 1000);
		}

		public static Order IsSmall
		{
			get
			{
				return Match<Order>.Create(o => o.Amount <= 1000);
			}
		}
	}

	public interface IOrderRepository
	{
		void Save(Order order);
		void Save(IEnumerable<Order> orders);
		int Value { get; set; }
	}

	public static class OrderIs
	{
		[AdvancedMatcher(typeof(BigOrderMatcher))]
		public static Order Big()
		{
			return null;
		}

		public class BigOrderMatcher : IMatcher
		{
			public void Initialize(System.Linq.Expressions.Expression matcherExpression)
			{
			}

			public bool Matches(object value)
			{
				if (value is Order &&
					((Order)value).Amount >= 1000)
					return true;

				return false;
			}
		}
	}

	public class Order
	{
		public int Amount { get; set; }
	}
}
<commit_msg>Added more tests for setter with matchers<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Moq.Tests
{
	public class ExtensibilityFixture
	{
		[Fact]
		public void ShouldExtendMatching()
		{
			var mock = new Mock<IOrderRepository>();
			mock.Setup(repo => repo.Save(OrderIs.Big()))
				.Throws(new InvalidOperationException());

			try
			{
				mock.Object.Save(new Order { Amount = 1000 });

				Assert.True(false, "Should have failed for big order");
			}
			catch (InvalidOperationException)
			{
			}
		}

		[Fact]
		public void ShouldExtendWithSimpleMatchers()
		{
			var order = new Order();
			var mock = new Mock<IOrderRepository>();

			Order repo = Match<Order>.Create(r => true);

			mock.Setup(x => x.Save(Orders.Contains(order)))
				 .Throws<ArgumentException>();

			Assert.Throws<ArgumentException>(() => mock.Object.Save(new [] { order }));
		}

		[Fact]
		public void ShouldExtendWithPropertyMatchers()
		{
			var mock = new Mock<IOrderRepository>();
			mock.Setup(repo => repo.Save(Orders.IsSmall))
				.Throws(new InvalidOperationException());

			try
			{
				mock.Object.Save(new Order { Amount = 50 });

				Assert.True(false, "Should have failed for small order");
			}
			catch (InvalidOperationException)
			{
			}
		}

		[Fact]
		public void SetterMatcherRendersNicely()
		{
			var mock = new Mock<IOrderRepository>();

			try
			{
				mock.VerifySet(repo => repo.Value = It.IsAny<int>());
			}
			catch (MockException me)
			{
				Console.WriteLine(me.Message);
			}

			mock.Object.Value = 25;

			mock.VerifySet(repo => repo.Value = It.IsInRange(10, 25, Range.Inclusive));
		}
	}

	public static class Orders
	{
		public static IEnumerable<Order> Contains(Order order)
		{
			return Match<IEnumerable<Order>>.Create(orders => orders.Contains(order));
		}

		public static Order IsBig()
		{
			return Match<Order>.Create(o => o.Amount >= 1000);
		}

		public static Order IsSmall
		{
			get
			{
				return Match<Order>.Create(o => o.Amount <= 1000);
			}
		}
	}

	public interface IOrderRepository
	{
		void Save(Order order);
		void Save(IEnumerable<Order> orders);
		int Value { get; set; }
	}

	public static class OrderIs
	{
		[AdvancedMatcher(typeof(BigOrderMatcher))]
		public static Order Big()
		{
			return null;
		}

		public class BigOrderMatcher : IMatcher
		{
			public void Initialize(System.Linq.Expressions.Expression matcherExpression)
			{
			}

			public bool Matches(object value)
			{
				if (value is Order &&
					((Order)value).Amount >= 1000)
					return true;

				return false;
			}
		}
	}

	public class Order
	{
		public int Amount { get; set; }
	}
}

{'fraction_non_alphanumeric': 0.12070226773957571, 'fraction_numerical': 0.007681053401609363, 'mean_word_length': 2.6630944407233756, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5428668', 'n_tokens_mistral': 2166, 'n_tokens_neox': 1702, 'n_words': 385}