public IPipelineTask <TContext> Build()
        {
            Ensure.Arg(this.Task, nameof(this.Task)).IsNotNull();
            Ensure.Arg(this.Selector, nameof(this.Selector)).IsNotNull();

            return(new ForEachTask <TContext, TElement>(this.Selector.Compile(), this.Task.Build()));
        }
Exemple #2
0
        public FiniteStateAutomatonValidator(List <Control> states)
        {
            Ensure.Arg(states).IsNotNull();

            _states = states;
            EnableValidTransitions();
        }
Exemple #3
0
        private void DrawScale(double min, double max, int width, int height, string mode)
        {
            Ensure.Arg(mode)
            .IsNotNullOrEmpty()
            .And()
            .IsNotNullOrWhiteSpace();

            if (mode == "x")
            {
                _painter.ResetTransform();
                _painter.DrawString(new PointF(_originOffset, height - _originOffset + _fontSize),
                                    Math.Round(min, 2).ToString(CultureInfo.InvariantCulture),
                                    _fontSize);
                _painter.DrawString(new PointF(width - _originOffset + _fontSize, height - _originOffset + _fontSize),
                                    Math.Round(max, 2).ToString(CultureInfo.InvariantCulture),
                                    _fontSize);
            }
            else
            {
                _painter.ResetTransform();
                _painter.DrawString(new PointF(0, height - _fontSize - _originOffset),
                                    Math.Round(min, 2).ToString(CultureInfo.InvariantCulture), _fontSize);
                _painter.DrawString(new PointF(0, 0), Math.Round(max, 2).ToString(CultureInfo.InvariantCulture),
                                    _fontSize);
            }
        }
Exemple #4
0
 public void LogText(IEnumerable <string> strings)
 {
     // Ensure the strings argument is not null or an empty collection.
     // If it is a null an ArgumentNullException will be thrown.
     // If it is an empty collection an ArgumentException will be thrown.
     Ensure.Arg(strings, "strings").IsNotNullOrEmpty();
 }
Exemple #5
0
        public static List <float> GetScaledValues(List <float> points, double scaledMin, double scaledMax)
        {
            Ensure.Arg(points).IsNotNullOrEmpty();

            var result = new List <float>(points.Count);

            var minValue = points.Min();
            var maxValue = points.Max();

            if (Math.Abs(minValue - maxValue) < 1e-3)
            {
                minValue -= 5;
                maxValue += 5;
            }

            foreach (var point in points)
            {
                var newValue = (scaledMax - scaledMin) * (point - minValue) / (maxValue - minValue) + scaledMin;

                result.Add((float)newValue);
            }

            Ensure.Arg(points.Count == result.Count);

            return(result);
        }
Exemple #6
0
        public AggregatePipelineNewContextTaskBuilder(TParentPipelineTaskBuilder builder)
        {
            Ensure.Arg(builder, nameof(builder)).IsNotNull();

            this.Builder = builder;
            this.Tasks   = new Queue <IPipelineTaskBuilder <TNewContext> >();
        }
Exemple #7
0
        public IPipelineTask <TContext> Build()
        {
            Ensure.Arg(this.Cases, nameof(this.Cases)).IsNotNullOrEmpty();

            return(new SwitchPipelineTask <TContext>(
                       this.Cases.Select(b => (ConditionalPipelineTask <TContext>)b.Build()),
                       this.DefaultPipelineTask.Build()));
        }
Exemple #8
0
 public void LogText(int startIndex, params string[] strings)
 {
     // Ensures the startIndex argument is greater than or equal to 0
     // and it is less than or equal to strings.Length - 1.
     // If these conditions are not meet an ArgumentOutOfRangeException will be thrown.
     // Also supports Ensure.Arg().IsBetween();
     Ensure.Arg(startIndex, "startIndex").IsBetweenOrEqualTo(0, strings.Length - 1);
 }
Exemple #9
0
        public FunctionObject(Func<double, double> function)
        {
            Ensure.Arg(function).IsNotNull();

            GraphObjectType = GraphObjectType.Function;
            Value = function;
            FunctionName = function.Method.Name;
        }
Exemple #10
0
 public void LogText(int startIndex, int endIndex, params string[] strings)
 {
     // Ensure the startIndex argument is less than or equal to endIndex.
     // If startIndex is greater to endIndex then an
     // ArgumentOutOfRangeException will be thrown.
     // Also supports IsLessThan, IsGreaterThan, and IsGreaterThanOrEqualTo.
     Ensure.Arg(startIndex, "startIndex").IsLessThanOrEqualTo(endIndex);
 }
Exemple #11
0
        public int CountDigits(string digits)
        {
            // Ensure the digits argument is not null or an empty string.
            // If it is null an ArgumentNullException will be thrown.
            // If it is empty an ArgumentException will be thrown.
            Ensure.Arg(digits, "digits").IsNotNullOrEmpty();

            return(0);
        }
Exemple #12
0
        public void DrawShape(Shape shape)
        {
            Ensure.Arg(shape, "shape")
            .IsValidEnumValue("{argName} was not a valid {enumType.Name} value. Actual value was {arg}");

            // Given a invalid shape of 45, such as calling DrawShape((Shape)45);
            // The above code throws an InvalidEnumArgumentException with a message of
            // "shape was not a valid Shape value. Actual value was 45"
        }
Exemple #13
0
        public void LessThanTest(int min, int max)
        {
            Ensure.Arg(min, "min", "Expected {argName} to be less than max but min was {arg} and max was {other}")
            .IsLessThan(max);

            // Given index = 1, startIndex = 3, and endIndex = 5 the above code
            // throws an ArgumentOutOfRangeException with a message of
            // "Expected min to be less than max but min was 5 and max was 1"
        }
Exemple #14
0
        public void RangeTest(int index, int startIndex, int endIndex)
        {
            Ensure.Arg(index, "index", "Expected {argName} to be between {min} and {max} but was {arg}")
            .IsBetween(startIndex, endIndex);

            // Given index = 1, startIndex = 3, and endIndex = 5 the above code
            // throws an ArgumentOutOfRangeException with a message of
            // "Expected index to be between 3 and 5 but was 1"
        }
Exemple #15
0
 public void EatApple(Apple apple)
 {
     // Conditions are evaluated in order of method calls.
     // If first condition fails and throws an exception subsequent conditions will not be evaluated.
     Ensure.Arg(apple, "apple")
     .IsNotNull()
     .IsNotEaten()
     .IsRipe();
 }
Exemple #16
0
        /// <summary>
        /// Adds, Deletes and Updates items from the <paramref name="destinationCollection"/> by using the <paramref name="sourceCollection"/> as the what the collection should like like now.
        /// </summary>
        /// <typeparam name="TDestination">The type of the destination.</typeparam>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <typeparam name="TKey">The type of the key used to match items in the <paramref name="sourceCollection"/> and <paramref name="destinationCollection"/>.</typeparam>
        /// <param name="destinationCollection">The destination collection.</param>
        /// <param name="sourceCollection">The source collection.</param>
        /// <param name="destinationKeySelector">The selector to match the key on the <paramref name="destinationCollection"/>. Must return type of <typeparamref name="TKey"/></param>
        /// <param name="sourceKeySelector">The selector to match the key on the <paramref name="sourceCollection"/>. Must return type of <typeparamref name="TKey"/></param>
        /// <param name="newItemActivator">The new item activator. Called when the item does not exist in the <typeparamref name="TDestination"/></param>
        /// <param name="updateItemActivator">The update item activator. Called when an update is required</param>
        public static ICollection <TDestination> Update <TDestination, TSource, TKey>(
            this ICollection <TDestination> destinationCollection,
            IEnumerable <TSource> sourceCollection,
            Func <TDestination, TKey> destinationKeySelector,
            Func <TSource, TKey> sourceKeySelector,
            Func <TSource, TKey, TDestination> newItemActivator,
            Action <TSource, TDestination> updateItemActivator)
            where TDestination : class
            where TSource : class
        {
            Ensure.Arg(destinationCollection, nameof(destinationCollection)).IsNotNull();
            Ensure.Arg(destinationKeySelector, nameof(destinationKeySelector)).IsNotNull();
            Ensure.Arg(sourceCollection, nameof(sourceCollection)).IsNotNull();
            Ensure.Arg(sourceKeySelector, nameof(sourceKeySelector)).IsNotNull();
            Ensure.Arg(newItemActivator, nameof(newItemActivator)).IsNotNull();

            var currentIds  = destinationCollection.Select(destinationKeySelector).ToArray();
            var incomingIds = sourceCollection.Select(sourceKeySelector).ToArray();

            var deleteIds = currentIds.Where(id => !incomingIds.Contains(id)).ToArray();
            var newIds    = incomingIds.Where(id => !currentIds.Any(cc => cc.Equals(id))).ToArray();

            // we support updates!
            if (updateItemActivator != null)
            {
                var updateIds = currentIds.Where(id => incomingIds.Contains(id));

                foreach (var id in updateIds)
                {
                    var itemToUpdate = destinationCollection.Single(i => destinationKeySelector(i).Equals(id));
                    var sourceItem   = sourceCollection.Single(i => sourceKeySelector(i).Equals(id));
                    updateItemActivator(sourceItem, itemToUpdate);
                }
            }

            // deletes
            if (deleteIds.Any())
            {
                foreach (var id in deleteIds)
                {
                    var itemToDelete = destinationCollection.Single(i => destinationKeySelector(i).Equals(id));
                    destinationCollection.Remove(itemToDelete);
                }
            }

            // adds
            if (newIds.Any())
            {
                foreach (var sourceItemToAdd in sourceCollection.Where(i => newIds.Any(id => sourceKeySelector(i).Equals(id))))
                {
                    destinationCollection.Add(newItemActivator(sourceItemToAdd, sourceKeySelector(sourceItemToAdd)));
                }
            }

            return(destinationCollection);
        }
Exemple #17
0
        public static IDbDataParameter CreateParameter(this IDbCommand command, DbType dbType)
        {
            Ensure.Arg(command, "command").IsNotNull();

            // Ensure dbType is a valid enumeration value for the DbType enum type.
            // If it is not then an InvalidEnumArgumentException will be thrown.
            Ensure.Arg(dbType, "dbType").IsValidEnumValue();

            return(null);
        }
Exemple #18
0
        public void DoTransition(GraphObject nextState)
        {
            Ensure.Arg(nextState).IsNotNull();

            _currentState = nextState;

            Ensure.Arg(_currentState == nextState);

            EnableValidTransitions();
        }
Exemple #19
0
        public Painter(int width, int height)
        {
            Ensure.Arg(width != 0);
            Ensure.Arg(height != 0);

            _bitmap   = new Bitmap(width, height);
            _graphics = Graphics.FromImage(_bitmap);

            Init();
        }
        public void IsNotEmptyWithANonEmptyValueThrowsNothing(Arg <string> arg, Exception exception)
        {
            "Given an arg with a non-empty value"
            .x(() => arg = Ensure.Arg("name", "value"));

            "When IsNotEmpty is called"
            .x(() => exception = Record.Exception(() => arg.IsNotEmpty()));

            "Then nothing is thrown"
            .x(() => Assert.Null(exception));
        }
Exemple #21
0
        public void IsNotInRangeWithAPassingPredicateThrowsNothing(Arg <string> arg, Exception exception)
        {
            "Given an arg"
            .x(() => arg = Ensure.Arg("name", "value"));

            "When IsNotInRange is called with a passing predicate"
            .x(() => exception = Record.Exception(() => arg.IsNotInRange(a => a.Contains("ale"))));

            "Then nothing is thrown"
            .x(() => Assert.Null(exception));
        }
        public void When_a_valid_enum_value_is_passed_to_IsValidEnumValue()
        {
            MyTestEnum value = MyTestEnum.AnotherValue;

            // Act.
            Action action = () =>
                            Ensure.Arg(value).IsValidEnumValue();

            // Assert.
            action.ShouldNotThrow();
        }
        public void IsNotWhitespaceWithANullValueThrowsNothing(Arg <string> arg, Exception exception)
        {
            "Given an arg with a null value"
            .x(() => arg = Ensure.Arg <string>("name", null));

            "When IsNotWhitespace is called"
            .x(() => exception = Record.Exception(() => arg.IsNotWhitespace()));

            "Then nothing is thrown"
            .x(() => Assert.Null(exception));
        }
        public void ArgWithANonWhitespaceNameThrowsNothing(Exception exception)
        {
            "Given Ensure"
            .x(() => { });

            "When Arg is called with a non-whitespace name"
            .x(() => exception = Record.Exception(() => Ensure.Arg("name", "value")));

            "Then nothing is thrown"
            .x(() => Assert.Null(exception));
        }
Exemple #25
0
        public void When_Ensure_Arg_is_called_with_value_param()
        {
            // Arrange.
            object testObject = new object();

            // Act.
            IEnsureArg <object> ensureArg = Ensure.Arg(testObject);

            // Assert.
            ensureArg.Value.Should().BeSameAs(testObject);
        }
        public void When_IsNotNullOrWhiteSpace_is_called_with_a_valid_string()
        {
            // Arrange.
            string value = "abc";

            // Act.
            Action action = () =>
                            Ensure.Arg(value).IsNotNullOrWhiteSpace();

            // Assert.
            action.ShouldNotThrow();
        }
        public void When_IsNotNullOrEmpty_is_called_with_a_null_string()
        {
            // Arrange.
            string value = null;

            // Act.
            Action action = () =>
                            Ensure.Arg(value).IsNotNullOrEmpty();

            // Assert.
            action.ShouldThrow <ArgumentNullException>();
        }
        public void When_an_argument_is_null_then_an_ArgumentNullException_should_be_thrown()
        {
            // Arrange.
            object value = null;

            // Act.
            Action action = () =>
                            Ensure.Arg(value).IsNotNull();

            // Assert.
            action.ShouldThrow <ArgumentNullException>();
        }
        public void When_IsNotNullOrWhiteSpace_is_called_with_a_whitespace_string()
        {
            // Arrange.
            string value = "  ";

            // Act.
            Action action = () =>
                            Ensure.Arg(value).IsNotNullOrWhiteSpace();

            // Assert.
            action.ShouldThrow <ArgumentException>();
        }
        public void When_an_Argument_is_not_null()
        {
            // Arrange.
            object value = new object();

            // Act.
            Action action = () =>
                            Ensure.Arg(value, "value").IsNotNull();

            // Assert.
            action.ShouldNotThrow();
        }