示例#1
0
        /// <summary>
        /// Internal logic of object validation.
        /// </summary>
        /// <param name="obj">The object to validate.</param>
        /// <param name="fullPath">The full object path in its structure.</param>
        /// <returns>
        /// <see cref="FullReport"/> with <c>IsValid</c> flag is set to <c>true</c>
        /// if the <paramref name="obj"/> is acceptable. Otherwise, the flag is set to <c>false</c>
        /// and <see cref="FullReport"/> contains a report of all problems.
        /// </returns>
        public override FullReport Validate(object obj, string fullPath = "")
        {
            FullReport fullReport = new FullReport(isValid: true);

            if (obj == null)
            {
                return(fullReport);
            }

            foreach (PropertyInfo prop in obj.GetType().GetProperties())
            {
                object value = prop.GetValue(obj);

                foreach (ValidationAttribute attr in prop.GetCustomAttributes <ValidationAttribute>())
                {
                    // SingleReport singleReport = attr.Validate(value);
                    var singleReport = attr.Validate(value);
                    fullReport += new SingleReport(singleReport.IsValid,
                                                   singleReport.Details != null ? $"{fullPath}.{prop.Name} : {singleReport.Details}" : null);
                }
            }

            WriteReport(fullReport);

            return(fullReport);
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="report"></param>
        /// <param name="transferTemplateUnforgeable"></param>
        /// <returns></returns>
        private List <Transaction> FindTransferComm(SingleReport report, Par transferTemplateUnforgeable)
        {
            var transfers    = new List <ReportProto>();
            var transactions = new List <Transaction>();

            foreach (var item in report.Events)
            {
                if (item.Comm != null)
                {
                    var channel = item.Comm.Consume.Channels[0];
                    if (channel.Unforgeables.Count > 0 && channel.Unforgeables[0].GPrivateBody != null &&
                        channel.Unforgeables[0].GPrivateBody.Id == transferTemplateUnforgeable.Unforgeables[0].GPrivateBody.Id)
                    {
                        transfers.Add(item);
                        var fromAddr = item.Comm.Produces[0].Data.Pars[0].Exprs[0].GString;
                        var toAddr   = item.Comm.Produces[0].Data.Pars[2].Exprs[0].GString;
                        var amount   = item.Comm.Produces[0].Data.Pars[3].Exprs[0].GInt;

                        var ret = item.Comm.Produces[0].Data.Pars[5];
                        transactions.Add(new Transaction()
                        {
                            FromAddress     = fromAddr,
                            ToAddress       = toAddr,
                            Amount          = amount,
                            ret_unforgeable = ret,
                        });
                    }
                }
            }

            transactions.ForEach((transaction) => {
                bool walletCreated = false;
                foreach (var item in report.Events)
                {
                    if (item.Produce != null)
                    {
                        var channel = item.Produce.Channel;
                        if (channel.Unforgeables.Count > 0 &&
                            channel.Unforgeables[0].GPrivateBody != null &&
                            channel.Unforgeables[0].GPrivateBody.Id == transaction.ret_unforgeable.Unforgeables[0].GPrivateBody.Id)
                        {
                            var data            = item.Produce.Data;
                            var result          = data.Pars[0].Exprs[0].ETupleBody.Ps[0].Exprs[0].GBool;
                            var reason          = result ? "" : data.Pars[0].Exprs[0].ETupleBody.Ps[1].Exprs[0].GString;
                            transaction.Success = result;
                            transaction.Reason  = reason;
                            walletCreated       = true;
                        }
                    }
                }
                if (walletCreated == false)
                {
                    transaction.Success = true;
                    transaction.Reason  = "Possibly the transfer toAddr wallet is not created in chain. Create the wallet to make transaction succeed.";
                }
            });
            return(transactions);
        }
        public void NotEmptyString()
        {
            // arrange
            string value = "123";

            // act
            var          attr   = new RequiredAttribute();
            SingleReport result = attr.Validate(value);

            // assert
            Assert.AreEqual(true, result.IsValid);
        }
        public void StringWithSpaces()
        {
            // arrange
            string value = "    ";

            // act
            var          attr   = new RequiredAttribute();
            SingleReport result = attr.Validate(value);

            // assert
            Assert.AreEqual(false, result.IsValid);
        }
        public void NullObjectTest()
        {
            // arrange
            object value = null;

            // act
            var          attr   = new RequiredAttribute();
            SingleReport result = attr.Validate(value);

            // assert
            Assert.AreEqual(false, result.IsValid);
        }
示例#6
0
        public void EqualMinTest()
        {
            // arrange
            string value = "15";

            // act
            var          attr   = new MinLengthAttribute(2);
            SingleReport result = attr.Validate(value);

            // assert
            Assert.AreEqual(true, result.IsValid);
        }
示例#7
0
        public void NullTest()
        {
            // arrange
            object value = null;

            // act
            var          attr   = new MinLengthAttribute(2);
            SingleReport result = attr.Validate(value);

            // assert
            Assert.AreEqual(true, result.IsValid);
        }
示例#8
0
        public void MinGreaterMaxTest()
        {
            // arrange
            object value = 7;

            // act
            var          attr   = new RangeAttribute(2, 1);
            SingleReport result = attr.Validate(value);

            // assert
            Assert.AreEqual(false, result.IsValid);
        }
示例#9
0
        public void CornerDigitTest()
        {
            // arrange
            object value = 7;

            // act
            var          attr   = new RangeAttribute(1, 7);
            SingleReport result = attr.Validate(value);

            // assert
            Assert.AreEqual(true, result.IsValid);
        }
示例#10
0
        public void OutOfRangeTest()
        {
            // arrange
            object value = 10;

            // act
            var          attr   = new RangeAttribute(1, 7);
            SingleReport result = attr.Validate(value);

            // assert
            Assert.AreEqual(false, result.IsValid);
        }
        public void EmptyStringWithAllow()
        {
            // arrange
            string value = "";

            // act
            var attr = new RequiredAttribute();

            attr.AllowEmptyString = true;
            SingleReport result = attr.Validate(value);

            // assert
            Assert.AreEqual(true, result.IsValid);
        }