Esempio n. 1
0
        /*
         * The only thing left is to define the implementation for
         * `IArbitrary<Command>`. We register it in the static constructor. We can
         * use the `Arbitrary<T>` wrapper class as a helper.
         */
        static Command()
        {
            Arbitrary.Register(new Arbitrary <Command> (

                                   /*
                                    * The generator consist of two branches. We will use the `OneOf`
                                    * combinator to first randomly chooses whether we push a digit
                                    * button or an operator button.
                                    */
                                   Gen.OneOf(

                                       /*
                                        * If the digit button is selected, then we choose a random
                                        * number from the range of 0 to 9, and create a `Digit`
                                        * command for the chosen number.
                                        */
                                       from i in Gen.ChooseInt(0, 10)
                                       select Digit(i),

                                       /*
                                        * If the winner was the other branch, then we choose a random
                                        * command from the set of operators.
                                        */
                                       Gen.ChooseFrom(Add, Subtract, Multiply, Divide, Equals)
                                       ),

                                   /*
                                    * How to implement shrinking is a bit harder to figure out.
                                    * Shrinking of operators is not something we can easily define,
                                    * so we will be content with shrinking just the digit command
                                    * utilizing the shrinking defined for integers. We achieve the
                                    * true power of shrinking by reducing the lists of commands to
                                    * shorter examples when LinqCheck finds a failing sequence. This
                                    * functionality we get out-of-the-box without any additional code.
                                    */
                                   com => com is _Digit n ?
                                   Arbitrary.Shrink(n.Value).Select(Digit) :
                                   EnumerableExt.Enumerate(com)
                                   ));
        }