예제 #1
0
        public void TryAdd_Should_ReturnTrue()
        {
            var source    = _fixture.Create <string>();
            var converted = _fixture.Create <object>();

            _jsonConvertible.TryConvert(source, out Arg.Any <object>())
            .Returns(x =>
            {
                x[1] = converted;
                return(true);
            });

            _schemaValidation.IsValid(converted)
            .Returns(true);

            var convertible = _fixture.Create <Dictionary <string, object> >();

            _convertible.Convert(converted)
            .Returns(convertible);

            var info = Substitute.For <ThingActionInformation>();

            _factory.Convert(convertible)
            .Returns(info);

            _collection.TryAdd(source, out _).Should().BeTrue();

            _jsonConvertible
            .Received(1)
            .TryConvert(source, out Arg.Any <object>());

            _schemaValidation
            .Received(1)
            .IsValid(converted);

            _convertible
            .Received(1)
            .Convert(converted);

            _factory
            .Received(1)
            .Convert(convertible);

            _collection.Should().NotBeEmpty();
            _collection.Should().HaveCount(1);

            foreach (var item in _collection)
            {
                item.Should().Be(info);
            }
        }
        /// <summary>
        /// Try to add Action to collection.
        /// </summary>
        /// <param name="source">The <see cref="object"/> to be convert to <see cref="ThingActionInformation"/>.</param>
        /// <param name="info">The <see cref="ThingActionInformation"/> created.</param>
        /// <returns>Return true if could convert and added on collection, otherwise return false.</returns>
        public bool TryAdd(object source, [NotNullWhen(true)] out ThingActionInformation?info)
        {
            info = null;

            if (!_inputConvertible.TryConvert(source, out var inputProperty))
            {
                return(false);
            }

            if (!_inputValidation.IsValid(inputProperty))
            {
                return(false);
            }

            info = _actionInformationFactory.Convert(_convertible.Convert(inputProperty) as Dictionary <string, object?>);

            info.StatusChanged += OnStatusChange;

            return(_actions.TryAdd(info.GetId(), info));
        }