public void SaveExercise(IExerciseDefinition exercise)
        {
            var doc=_repository.Load();

            var exerciseExists = !(doc.Descendants("exercises").Single().Descendants("exercise")
                                       .SingleOrDefault(e => e.Attribute("name").Value == exercise.Name) == null);

            if (exerciseExists)
                throw new ValidationException("Oefening bestaat reeds");

            var exerciseXml = new XElement("exercise");
            exerciseXml.Add(new XAttribute("name", exercise.Name));
            exerciseXml.Add(new XAttribute("template", exercise.Template));
            var numbersXml = new XElement("numbers");
            exerciseXml.Add(numbersXml);
            foreach (var number in exercise.Numbers)
            {
                var numberXml = new XElement("number");
                numberXml.Add(new XAttribute("name", number.Name));
                numberXml.Add(new XAttribute("minvalue", number.MinValue));
                numberXml.Add(new XAttribute("maxvalue", number.MaxValue));
                numberXml.Add(new XAttribute("decimals", number.Decimals));

                numbersXml.Add(numberXml);
            }

            doc.Element("exercisebuilder")
                .Element("exercises")
                .Add(exerciseXml);

            _repository.Save();
        }
Esempio n. 2
0
        public void ValidExercise()
        {
            var exercises = new IExerciseDefinition[]
            {
                new ValidExercise()
            };

            var errors = NameValidation.GetErrors(exercises);

            Assert.AreEqual(0, errors.Count, string.Join(",", errors));
        }
        public void ExerciseWithLabelTest()
        {
            var exercises = new IExerciseDefinition[]
            {
                new ExerciseWithLabel(),
            };

            var errors = ImplementationValidation.GetErrors(exercises);

            Assert.AreEqual(0, errors.Count, string.Join(",", errors));
        }
        public void Exception()
        {
            var exercises = new IExerciseDefinition[]
            {
                new ExceptionExercise(),
            };

            var errors = ImplementationValidation.GetErrors(exercises);

            Assert.AreEqual(1, errors.Count, string.Join(",", errors));
        }
        public async Task SlowExercise()
        {
            var exercises = new IExerciseDefinition[]
            {
                new LongCalculationExercise(),
            };

            var errors = await TimeValidation.GetErrors(exercises);

            Assert.AreEqual(1, errors.Count, string.Join(",", errors));
        }
        /// <summary>
        /// Validate if the exercise implemented the <see cref="IExerciseDefinition"/> interface correctly.
        /// Instantiation, answer cell, get expected solution and answer details are validated.
        /// </summary>
        /// <param name="exercise">The exercises which is validated.</param>
        /// <param name="seed">The seed with which the exercise is validated.</param>
        /// <returns>Returns an error or an empty string if the exercise is valid for the given seed.</returns>
        public static string GetError(IExerciseDefinition exercise, string seed)
        {
            //Validate the instantiation
            exercise.Instantiate(seed);

            //Validate the answer cells
            var answerCells = exercise.AnswerCells;

            if (answerCells == null || answerCells.Count < 1)
            {
                return($"{exercise.Name} does not generate any answer cells.");
            }

            //Validate the exercise values
            var values = exercise.GetValues();

            //Validate the expected solutions
            var solutions = exercise.GetExpectedSolutions();

            //Valdaite the answer cells
            var answerDetails = exercise.GetAnswerDetails(solutions);

            //Validate if the expected solutions are considered correct
            foreach (var answerDetail in answerDetails)
            {
                if (answerDetail.Value != Enum.AnswerCellStatus.Correct)
                {
                    return($"{exercise.Name}: The expected solutions is incorrect at {answerDetail.Key}.");
                }
            }

            //Check if there is a solution for each answer cell
            foreach (var answerCell in answerCells)
            {
                if (answerCell.Value.InputType != InputTypes.Label &&
                    !solutions.ContainsKey(answerCell.Key))
                {
                    return($"{exercise.Name}: The answer cell {answerCell.Key} is not part of the solution.");
                }
            }

            //Check if there is an answer cell for each solution
            foreach (var solution in solutions)
            {
                if (!answerCells.ContainsKey(solution.Key))
                {
                    return($"{exercise.Name}: The solution {solution.Key} is not part of the answer cells.");
                }
            }

            //No errors detected
            return(string.Empty);
        }
Esempio n. 7
0
        /// <summary>
        /// Validate if the exercise calculates quick enough.
        /// </summary>
        /// <param name="exercise">The exercises which is validated.</param>
        /// <param name="seed">The seed with which the exercise is validated.</param>
        /// <returns>Returns an error or an empty string if the exercise is valid for the given seed.</returns>
        public static async Task <string> GetError(IExerciseDefinition exercise, string seed)
        {
            //Validate the instantiation
            var task = Task.Run(() => exercise.Instantiate(seed));

            if (await Task.WhenAny(task, Task.Delay(MAX_INSTANTIATE)) != task)
            {
                return($"{exercise.Name} took too long to instantiate for the seed {seed}.");
            }

            task = Task.Run(() => exercise.GetExpectedSolutions());
            if (await Task.WhenAny(task, Task.Delay(MAX_SOLUTION)) != task)
            {
                return($"{exercise.Name} took too long to calculate the solution.");
            }

            //No errors detected
            return(string.Empty);
        }