private static Arbitrary <Party> ArbParty(NonEmptyString role, NonEmptyString partyId)
 {
     return(Gen.OneOf(
                Gen.Fresh(() => new Party(role.Get, partyId.Get)),
                Gen.Constant <Party>(null))
            .ToArbitrary());
 }
        private static Gen <Tuple <PublicKeyCertificateChoiceType, object> > CreateEncryptionCertificateInfoGen(
            string findValue,
            string certificate)
        {
            var genCertFindCriteria = Gen.OneOf(
                Gen.Constant(Tuple.Create(
                                 PublicKeyCertificateChoiceType.CertificateFindCriteria,
                                 (object)null)),
                Gen.Fresh(() => Tuple.Create(
                              PublicKeyCertificateChoiceType.CertificateFindCriteria,
                              (object)new CertificateFindCriteria {
                CertificateFindValue = findValue
            })));

            var genPublicKeyCert = Gen.OneOf(
                Gen.Constant(Tuple.Create(
                                 PublicKeyCertificateChoiceType.PublicKeyCertificate,
                                 (object)null)),
                Gen.Fresh(() => Tuple.Create(
                              PublicKeyCertificateChoiceType.PublicKeyCertificate,
                              (object)new PublicKeyCertificate {
                Certificate = certificate
            })));

            return(Gen.OneOf(genCertFindCriteria, genPublicKeyCert));
        }
        public void ListOfGenerator()
        {
            var gen = Gen.Constant(42)
                      .ListOf(1);

            var value = Gen.Sample(1, 10, gen);
        }
        public static Gen <Method> CreateMethodGen()
        {
            var parameterGen =
                Arb.Generate <string>()
                .Two()
                .SelectMany(t => Gen.OneOf(
                                Gen.Constant((Parameter)null),
                                Gen.Constant(new Parameter {
                Name = t.Item1, Value = t.Item2
            })));

            var parametersGen =
                Arb.Generate <string>()
                .Two()
                .ListOf()
                .SelectMany(xs => Gen.OneOf(
                                Gen.Constant((IList <Parameter>)null),
                                parameterGen.ListOf()));

            return(Gen.OneOf(
                       Gen.Constant((Method)null),
                       Arb.Generate <string>()
                       .SelectMany(s => parametersGen.Select(
                                       p => new Method {
                Type = s, Parameters = p?.ToList()
            }))));
        }
Пример #5
0
        private static Gen <CollaborationInfo> GenCollaborationInfo()
        {
            var genAgreementRef =
                Arb.Generate <NonNull <string> >()
                .Select(x => new AgreementReference(x.Get).AsMaybe())
                .Or(Gen.Constant(Maybe <AgreementReference> .Nothing));

            var genServiceWithoutType =
                Arb.Generate <NonNull <string> >()
                .Select(x => new Service(x.Get));

            var genServiceWithType =
                Arb.Generate <NonNull <string> >()
                .Two()
                .Select(t => new Service(t.Item1.Get, t.Item2.Get));

            var genActionConversation =
                Arb.Generate <NonNull <string> >()
                .Two();

            return(genAgreementRef
                   .Zip(genServiceWithoutType.Or(genServiceWithType))
                   .Zip(genActionConversation)
                   .Select(tt => new CollaborationInfo(
                               tt.Item1.Item1,
                               tt.Item1.Item2,
                               tt.Item2.Item1.Get,
                               tt.Item2.Item2.Get)));
        }
        private static Gen <Tuple <TlsCertificateChoiceType, object> > CreateTlsCertificateInfoGen(
            string clientCertFindValue,
            string password,
            string certificate)
        {
            var genClientCertRef = Gen.OneOf(
                Gen.Constant(Tuple.Create(
                                 TlsCertificateChoiceType.ClientCertificateReference,
                                 (object)null)),
                Gen.Fresh(() => Tuple.Create(
                              TlsCertificateChoiceType.ClientCertificateReference,
                              (object)new ClientCertificateReference {
                ClientCertificateFindValue = clientCertFindValue
            })));

            var genPrivateKeyCert = Gen.OneOf(
                Gen.Constant(Tuple.Create(
                                 TlsCertificateChoiceType.PrivateKeyCertificate,
                                 (object)null)),
                Gen.Fresh(() => Tuple.Create(
                              TlsCertificateChoiceType.PrivateKeyCertificate,
                              (object)new PrivateKeyCertificate {
                Password = password, Certificate = certificate
            })));

            return(Gen.OneOf(genClientCertRef, genPrivateKeyCert));
        }
        public static Gen <Tuple <PrivateKeyCertificateChoiceType, object> > CreatePrivateKeyCertificateGen(
            string findValue,
            string certificate,
            string password)
        {
            var genCertFindCriteria = Gen.OneOf(
                Gen.Constant(Tuple.Create(
                                 PrivateKeyCertificateChoiceType.CertificateFindCriteria,
                                 (object)null)),
                Gen.Fresh(() => Tuple.Create(
                              PrivateKeyCertificateChoiceType.CertificateFindCriteria,
                              (object)new CertificateFindCriteria {
                CertificateFindValue = findValue
            })));

            var genPrivateKeyCert = Gen.OneOf(
                Gen.Constant(Tuple.Create(
                                 PrivateKeyCertificateChoiceType.PrivateKeyCertificate,
                                 (object)null)),
                Gen.Fresh(() => Tuple.Create(
                              PrivateKeyCertificateChoiceType.PrivateKeyCertificate,
                              (object)new PrivateKeyCertificate {
                Certificate = certificate, Password = password
            })));

            return(Gen.OneOf(genCertFindCriteria, genPrivateKeyCert));
        }
Пример #8
0
        private static Gen <Regex <char> > DefineGen()
        {
            var delayedRegex = Gen.Delay(new GetRegex());

            return(Gen.ChoiceRecursive(
                       new[]
            {
                from c in Gen.Unicode
                select Regex <char> .Just(c),

                Gen.Constant(Regex <char> .Fail),

                Gen.Constant(Regex <char> .Epsilon),
            },
                       new[]
            {
                from c in delayedRegex
                select ~c,

                from c in delayedRegex
                select !c,

                from l in delayedRegex
                from r in delayedRegex
                select l *r,

                from l in delayedRegex
                from r in delayedRegex
                select l + r,

                from l in delayedRegex
                from r in delayedRegex
                select l& r,
            }));
        }
Пример #9
0
        private static IGen <T> CreateGenGeneric <T>(IReflectedGenHandler innerHandler, ReflectedGenHandlerContext context, ContextualErrorFactory errorFactory)
        {
            var constructor = TryFindConstructor(typeof(T)) !;

            var parameterGens = constructor
                                .GetParameters()
                                .Select(parameter => innerHandler
                                        .CreateGen(parameter.ParameterType, context.Next(parameter.Name, parameter.ParameterType)) // TODO: Indicate it's a ctor param in the path
                                        .Cast <object>());

            return(Gen
                   .Zip(parameterGens)
                   .SelectMany(parameters =>
            {
                try
                {
                    return Gen.Constant((T)constructor.Invoke(parameters.ToArray()));
                }
                catch (TargetInvocationException ex)
                {
                    var innerEx = ex.InnerException;
                    var message = $"'{innerEx.GetType()}' was thrown while calling constructor with message '{innerEx.Message}'";
                    return errorFactory(message, context).Cast <T>();
                }
            }));
        }
Пример #10
0
 private static Gen <IList <IList <int> > > GenSolutionRows(int numCols, int numSolutionRows)
 {
     return
         (from solutionRows in Gen.Constant(0).ListOf(numCols).ListOf(numSolutionRows)
          from randomRowIdxs in Gen.Choose(0, numSolutionRows - 1).ListOf(numCols)
          select RandomlySprinkleOnesIntoSolutionRows(solutionRows, randomRowIdxs));
 }
        public Gen <Command <ISet <int>, ISet <int> > > Next(ISet <int> value)
        {
            var count = value.Count;

            return(Gen.Frequency(Tuple.Create(Math.Max(1, 5 - count), Build(i => new AddSet(i))),
                                 Tuple.Create(1 + 3 * count, Build(i => new RemoveSet(i))),
                                 Tuple.Create(1, Gen.Constant <Command <ISet <int>, ISet <int> > >(new ClearSet()))));
        }
Пример #12
0
 public static Gen <RiskFactor> NoneOrLowRisk()
 {
     return(Gen.Frequency(new[]
     {
         System.Tuple.Create(1, Gen.Constant(RiskFactor.None)),
         System.Tuple.Create(4, Gen.Constant(RiskFactor.Low))
     }));
 }
        public Gen <Command <IDictionary <int, string>, IDictionary <int, string> > > Next(IDictionary <int, string> value)
        {
            var count = value.Count;

            return(Gen.Frequency(Tuple.Create(Math.Max(1, 5 - count), Build(tuple => new AddDictionary(tuple))),
                                 Tuple.Create(1 + 3 * count, Build(i => new RemoveDictionary(i))),
                                 Tuple.Create(1, Gen.Constant <Command <IDictionary <int, string>, IDictionary <int, string> > >(new ClearDictionary()))));
        }
Пример #14
0
 public static Gen <double> Range(double lower, double upper) =>
 Gen.Frequency
     (Tuple(1, Gen.Constant(lower)),
     Tuple(1, Gen.Constant(lower + double.Epsilon)),
     Tuple(1, Gen.Constant(upper - double.Epsilon)),
     Tuple(1, Gen.Constant(upper)),
     Tuple(20, GenFloat._Range(lower, upper))
     );
 public static Arbitrary <IntervalStringWithInvalidFormat <T, TComparer> > NormalIntervalStringsWithInvalidFormat <T, TComparer>() where TComparer : struct, IBoundaryValueComparer <T> => Arb.From(
     from interval in NonEmptyIntervals <T, TComparer>().Generator
     from lowerBoundaryType in Gen.OneOf(Gen.Elements('[', '('), Arb.Generate <char>())
     let lowerBoundaryValue = interval.LowerBoundary.Value
                              from separator in Gen.OneOf(Gen.Constant(','), Arb.Generate <char>())
                              let upperBoundaryValue = interval.UpperBoundary.Value
                                                       from upperBoundaryType in Gen.OneOf(Gen.Elements(']', ')'), Arb.Generate <char>())
                                                       select new IntervalStringWithInvalidFormat <T, TComparer>(Padded(lowerBoundaryType, lowerBoundaryValue, separator, upperBoundaryValue, upperBoundaryType)));
Пример #16
0
 private static Gen <IList <IList <int> > > GenPartitionedSolutionRows(int numCols, int startIdx, int endIdx, int numSolutionRows)
 {
     return
         (from solutionRows in Gen.Constant(InitPartitionedSolutionRows(numCols, startIdx, endIdx, numSolutionRows))
          from randomRowIdxs in Gen.Choose(0, numSolutionRows - 1).ListOf(endIdx - startIdx)
          where Enumerable.Range(0, numSolutionRows).All(randomRowIdxs.Contains)
          select RandomlySprinkleOnesIntoSolutionRows(solutionRows, randomRowIdxs, startIdx));
 }
Пример #17
0
 public static Gen <SigHash> SigHashType() =>
 Gen.OneOf <SigHash>(new List <Gen <SigHash> > {
     Gen.Constant(SigHash.All),
     Gen.Constant(SigHash.Single),
     Gen.Constant(SigHash.None),
     Gen.Constant(SigHash.AnyoneCanPay | SigHash.All),
     Gen.Constant(SigHash.AnyoneCanPay | SigHash.Single),
     Gen.Constant(SigHash.AnyoneCanPay | SigHash.None)
 });
Пример #18
0
        public Property String_LengthMax(NonNull <string> s, NonNull <string> x)
        {
            Gen <string> gen = Gen.Constant(s.Get).Or(Gen.Constant(x.Get));

            return(Prop.ForAll(gen.ToArbitrary(), target =>
            {
                Spec <string> spec = Spec.Of <string>().LengthMax(s.Get.Length + 1, "should have max string length");
                return spec.ToProperty(target, target.Length <= s.Get.Length + 1);
            }));
        }
Пример #19
0
        public Property NonEmpty(NonEmptyArray <string> array)
        {
            Gen <string[]> gen = Gen.Constant(Array.Empty <string>()).Or(Gen.Constant(array.Get));

            return(Prop.ForAll(gen.ToArbitrary(), target =>
            {
                Spec <string[]> spec = Spec.Of <string[]>().NonEmpty("should not be empty");
                return spec.ToProperty(target, target.Length != 0);
            }));
        }
Пример #20
0
        public Property SequenceEqual(NonNull <int[]> array)
        {
            Gen <int[]> gen = Gen.Constant(array.Get).Or(Gen.Shuffle(array.Get)).Or(Arb.Generate <int[]>());

            return(Prop.ForAll(gen.ToArbitrary(), target =>
            {
                Spec <int[]> spec = Spec.Of <int[]>().SequenceEqual(array.Get, "should be sequence eaual");
                return spec.ToProperty(target, target.SequenceEqual(array.Get));
            }));
        }
Пример #21
0
 public static List <AttributeTypeDTO> ListOfAttributeTypeDtos(int size = 10, int attributeListSize = 10)
 {
     return(Enumerable.Repeat <AttributeTypeDTO>(new AttributeTypeDTO()
     {
         AllowMultipleSelections = Gen.Sample(1, 1, Gen.OneOf(Gen.Constant(true), Gen.Constant(false))).HeadOrDefault,
         Attributes = ListOfAttributeDtos(attributeListSize),
         AttributeTypeId = Gen.Sample(1, 1, Gen.OneOf(Arb.Generate <int>())).HeadOrDefault,
         Name = Gen.Sample(1, 1, Gen.OneOf(Arb.Generate <string>())).HeadOrDefault
     }, size).ToList());
 }
Пример #22
0
        public static Arbitrary <Maybe <T> > MaybeGen <T>()
        {
            Gen <Maybe <T> > just    = Arb.From <T>().Generator.Select(Maybe.Just);
            Gen <Maybe <T> > nothing = Gen.Constant(Maybe <T> .Nothing);

            return(Gen.Frequency(
                       Tuple.Create(1, nothing),
                       Tuple.Create(2, just))
                   .ToArbitrary());
        }
Пример #23
0
 private static Gen <int[, ]> GenMatrixOfIntWithSingleSolution()
 {
     return
         (from numCols in Gen.Choose(2, 20)
          from solution in GenSolution(numCols)
          from numRows in Gen.Choose(solution.Count, solution.Count * 5)
          from matrix in Gen.Constant(0).ListOf(numCols).ListOf(numRows)
          from randomRowIdxs in PickRandomRowIdxs(solution.Count, numRows)
          select PokeSolutionRowsIntoMatrix(matrix, solution, randomRowIdxs).To2DArray());
 }
Пример #24
0
        public Property ReplyHandling_Must_Be_Specified_When_There_Isnt_A_Forward_Element(
            string responsePMode,
            string forwardPMode)
        {
            var genForward = Gen.OneOf(
                Gen.Constant((object)null),
                Gen.Fresh(() => (object)new Forward {
                SendingPMode = forwardPMode
            }),
                Gen.Fresh(() => (object)new Deliver()));

            var genReplyHandling = Gen.OneOf(
                Gen.Constant((ReplyHandling)null),
                Gen.Fresh(() => new ReplyHandling
            {
                ResponseConfiguration = new PushConfiguration
                {
                    Protocol = { Url = "http://not/empty/url" }
                }
            }));

            return(Prop.ForAll(
                       genForward.ToArbitrary(),
                       genReplyHandling.ToArbitrary(),
                       (messageHandlingImp, replyHandling) =>
            {
                // Arrange
                ReceivingProcessingMode pmode = CreateValidPMode();
                pmode.ReplyHandling = replyHandling;
                pmode.MessageHandling.Item = messageHandlingImp;

                // Act
                ValidationResult result = ExerciseValidation(pmode);

                // Assert
                bool specifiedDeliver = messageHandlingImp is Deliver;
                bool specifiedForward =
                    messageHandlingImp is Forward f &&
                    !String.IsNullOrWhiteSpace(f.SendingPMode);

                bool specifiedReplyHandling =
                    replyHandling?.ResponseConfiguration != null;

                return result.IsValid
                .Equals(specifiedReplyHandling && specifiedDeliver)
                .Or(!specifiedReplyHandling && specifiedForward)
                .Or(specifiedReplyHandling && specifiedForward)
                .Label(
                    $"Validation has {(result.IsValid ? "succeeded" : "failed")} "
                    + $"but ReplyHandling {(specifiedReplyHandling ? "is" : "isn't")} specified and "
                    + $"MessageHandling is {(specifiedDeliver ? "a Deliver" : specifiedForward ? "a Forward" : "empty")} element. "
                    + $"{(result.IsValid ? String.Empty : result.AppendValidationErrorsToErrorMessage("Validation Failure: "))}");
            }));
        }
Пример #25
0
        private Gen <List <Concurrent <bool, bool, bool> .CommandResult> > AlwaysTrueCommandResult(int listLen)
        {
            var item = from client in Gen.Choose(0, 1)
                       from result in Gen.Constant(true)
                       from command in Gen.Constant(new BoolIdentity())
                       let clientCommand = new Concurrent <bool, bool, bool> .ClientCommand(client, command)
                                           select new Concurrent <bool, bool, bool> .CommandResult(clientCommand, result);

            return(from list in Gen.ListOf(listLen, item)
                   select list.ToList());
        }
Пример #26
0
        private Gen <List <Concurrent <int, int, int> .CommandResult> > ParralelIntCommandGen(int listLen)
        {
            var item = from client in Gen.Choose(0, 5)
                       from result in Gen.Choose(1, 1000)
                       from command in Gen.Constant(new IntSutIdentity())
                       let clientCommand = new Concurrent <int, int, int> .ClientCommand(client, command)
                                           select new Concurrent <int, int, int> .CommandResult(clientCommand, result);

            return(from list in Gen.ListOf(listLen, item)
                   select list.ToList());
        }
Пример #27
0
            public static Arbitrary <ObjectDescriptor> ChildDescriptions()
            {
                var generatorIjsCsGlue = Gen.Constant(0).Select(index => NSubstitute.Substitute.For <IJsCsGlue>());

                var generatorChildDescription = Gen.zip(Arb.Default.String().Generator, generatorIjsCsGlue);

                var generator = Gen.zip(generatorIjsCsGlue, Gen.NonEmptyListOf(generatorChildDescription))
                                .Select(tuple => new ObjectDescriptor(tuple.Item1, tuple.Item2.Select(t => t.Item1).ToArray(), tuple.Item2.Select(t => t.Item2).ToArray()));

                return(Arb.From(generator));
            }
Пример #28
0
 public static Gen <Developer> KarumieGenerator()
 {
     return(Gen.OneOf(
                Gen.Constant(Karumies.DAVIDE),
                Gen.Constant(Karumies.PEDRO),
                Gen.Constant(Karumies.FRAN),
                Gen.Constant(Karumies.JORGE),
                Gen.Constant(Karumies.SERGIO),
                Gen.Constant(Karumies.TONI)
                ));
 }
Пример #29
0
 private Arbitrary <string> Dna() =>
 Gen.Sized(n =>
           Gen.ArrayOf(n, Gen.OneOf(new List <Gen <string> > {
     Gen.Constant("A"),
     Gen.Constant("C"),
     Gen.Constant("G"),
     Gen.Constant("T")
 }))
           .Select(r => string.Join("", r)))
 .Select(r => r ?? "")
 .ToArbitrary();
            public static Arbitrary <EntityDescriptor <int> > ChildDescriptions()
            {
                var generatorIJSCSGlue = Gen.Constant(0).Select(index => NSubstitute.Substitute.For <IJSCSGlue>());

                var generatorChildDescription = Gen.zip(generatorIJSCSGlue, Gen.Choose(0, 100))
                                                .Select(tuple => new ChildDescription <int>(tuple.Item2, tuple.Item1));

                var generator = Gen.zip(generatorIJSCSGlue, Gen.NonEmptyListOf(generatorChildDescription))
                                .Select(tuple => new EntityDescriptor <int>(tuple.Item1, tuple.Item2));

                return(Arb.From(generator));
            }