コード例 #1
0
ファイル: SeqTests.cs プロジェクト: johtela/LinqCheck
        /*
        ## Implementing IArbitrary<T>
        ## We need an implementation of `IArbitrary<T>` interface for each type `T`
        ## we wish to use in our tests. This implementation generates random values
        ## of type `T`, and shrinks them when LinqCheck is looking  for a minimal
        ## failing test case.
        ##
        ## So, we need to define an implementation of `IArbitrary<Seq<T>>`. The
        ## simplest way to achieve that is to create an instance `Arbitrary<T>`
        ## class, which stores the generator and shrinker for type `T`. Let's
        ## define a method that creates this object.
        */
        public static IArbitrary <Seq <T> > ArbitrarySeq <T> ()
        {
            /*
             * First we need to define the generator for `Seq<T>` type. Generators
             * have the type `Gen<T>` where T is the type of data produced. They
             * are defined as Linq expressions where complex types are constructed
             * with built-in or user-defined combinators. In this case we can
             * utilize the combinator that creates an `Gen<IEnumerable<T>>` given
             * a generator of type `Gen<T>`.
             *
             * We don't know yet what is the item type `T` which will be contained
             * in the sequence. Luckily we don't have to. We can assume that the
             * generator for `T` is already registered with LinqCheck, and get it
             * by calling `Arbitrary.Gen<T> ()`.
             */
            return(new Arbitrary <Seq <T> > (
                       from e in Arbitrary.Gen <T> ().EnumerableOf()
                       select e.ToSeq(),

                       /*
                        * We also need to provide a way to shrink the failed sequence
                        * to simpler versions. Again, we take advantage of the fact
                        * that `Seq<T>` implements `IEnumerable<T>`, so we can use
                        * the built-in combinator to do the shrinking. Lastly, we
                        * convert the shrinked IEnumerables back to Seqs.
                        */
                       seq =>
                       from e in seq.ShrinkEnumerable()
                       select e.ToSeq()
                       ));
        }