public void DefaultInstanceIsValid()
        {
            // Arrange

            // Act
            var model = new IndexViewModel();

            // Assert
            Assert.True(ModelIsValid(model));
        }
        public void ConstructorSetsDefaultValueForPayoffForCooperateAndCooperate()
        {
            // Arrange

            // Act
            var model = new IndexViewModel();

            // Assert
            Assert.Equal(3, model.PayoffForCooperateAndCooperate);
        }
        public void ConstructorSetsDefaultValueForPayoffForDefectAndDefect()
        {
            // Arrange

            // Act
            var model = new IndexViewModel();

            // Assert
            Assert.Equal(1, model.PayoffForDefectAndDefect);
        }
        public void ConstructorSetsDefaultValueForNumberOfRounds()
        {
            // Arrange

            // Act
            var model = new IndexViewModel();

            // Assert
            Assert.Equal<uint>(10, model.NumberOfRounds);
        }
        public void IndexPostActionReturnsViewResultWithModelSetToInstanceSupplied()
        {
            // Arrange
            var homeController = new HomeController();
            var indexViewModel = new IndexViewModel();

            // Act
            var viewResult = homeController.Index(indexViewModel);

            // Assert
            Assert.Same(indexViewModel, viewResult.Model);
        }
        public ViewResult Index(IndexViewModel model)
        {
            // Only perform the simulation when the data that was entered was valid
            if (ModelState.IsValid)
            {
                // Create the fitness evaluator using the configuration specified by the user
                var fitnessEvaluator = new CooperationStrategiesFitnessEvaluator
                                           {
                                               NumberOfRounds = model.NumberOfRounds,
                                               CooperationChoicesPayoff =
                                                   {
                                                       PayoffForCooperateAndCooperate = model.PayoffForCooperateAndCooperate,
                                                       PayoffForCooperateAndDefect = model.PayoffForCooperateAndDefect,
                                                       PayoffForDefectAndCooperate = model.PayoffForDefectAndCooperate,
                                                       PayoffForDefectAndDefect = model.PayoffForDefectAndDefect
                                                   }
                                           };

                // Evaluate the fitness of the strategies
                model.StrategyFitnesses = fitnessEvaluator.Evaluate();
            }

            return this.View(model);
        }
        public void ModelWithNumberOfRoundsOutOfRangeIsNotValid(uint numberOfRounds)
        {
            // Arrange
            var model = new IndexViewModel();

            // Act
            model.NumberOfRounds = numberOfRounds;

            // Assert
            Assert.False(ModelIsValid(model));
        }
 private static bool ModelIsValid(IndexViewModel model)
 {
     return Validator.TryValidateObject(model, new ValidationContext(model, null, null), null, true);
 }
        public void IndexPostActionWithValidModelRunsSimulationWithSuppliedValues()
        {
            // Arrange
            var homeController = new HomeController();

            // We create a model with values all different from the default. That way
            // we can know for sure if those values are used
            var indexViewModel = new IndexViewModel
                                     {
                                         NumberOfRounds = 50,
                                         PayoffForCooperateAndCooperate = 10,
                                         PayoffForCooperateAndDefect = 11,
                                         PayoffForDefectAndCooperate = 12,
                                         PayoffForDefectAndDefect = 13
                                     };

            // Act
            var viewResult = homeController.Index(indexViewModel);

            // Assert
            var viewResultModel = (IndexViewModel)viewResult.Model;
            Assert.Equal(3, viewResultModel.StrategyFitnesses.Count);
            Assert.Equal(2050, viewResultModel.StrategyFitnesses[0].TotalPayoff);
            Assert.Equal(2549, viewResultModel.StrategyFitnesses[1].TotalPayoff);
            Assert.Equal(2148, viewResultModel.StrategyFitnesses[2].TotalPayoff);
        }