Skip to content

Commit 2eaff13

Browse files
Daniel BoyceDaniel Boyce
authored andcommitted
chore:addd testing for java quiz
1 parent 9aebee2 commit 2eaff13

File tree

16 files changed

+168
-89
lines changed

16 files changed

+168
-89
lines changed

lesson_02/quiz-java/app/src/main/resources/application.yml

Whitespace-only changes.

lesson_02/quiz-java/app/src/test/java/org/example/AppTest.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

lesson_02/quiz-java/app/src/test/java/org/example/LessonTest.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

lesson_02/quiz-java/app/src/main/java/org/example/Lesson.java renamed to lesson_02/quiz_java/app/src/main/java/org/example/Lesson.java

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
*/
44
package org.example;
55

6+
import java.util.ArrayList;
67
import java.util.HashMap;
8+
import java.util.List;
79
import java.util.Map;
810

911
import com.codedifferently.instructional.quiz.AnswerChoice;
@@ -13,150 +15,157 @@ public class Lesson {
1315

1416
public static void main(String[] args) {
1517
MultipleChoiceQuizQuestion[] questions = makeQuestions();
16-
for (MultipleChoiceQuizQuestion q : questions) {
18+
for(MultipleChoiceQuizQuestion q : questions) {
1719
System.out.println(q);
1820
}
1921
}
2022

21-
public static MultipleChoiceQuizQuestion[] makeQuestions() {
22-
MultipleChoiceQuizQuestion[] questions = new MultipleChoiceQuizQuestion[11];
23-
questions[0] = makeQuestion0();
24-
questions[1] = makeQuestion1();
25-
questions[2] = makeQuestion2();
26-
questions[3] = makeQuestion3();
27-
questions[4] = makeQuestion4();
28-
questions[5] = makeQuestion5();
29-
questions[6] = makeQuestion6();
30-
questions[7] = makeQuestion7();
31-
questions[8] = makeQuestion8();
32-
questions[9] = makeQuestion9();
33-
questions[10] = makeQuestion10();
23+
public static List<MultipleChoiceQuizQuestion> makeQuestions() {
24+
25+
List<MultipleChoiceQuizQuestion> questions = new ArrayList<>();
26+
27+
questions.add(makeQuestion0());
28+
questions.add(makeQuestion1());
29+
questions.add(makeQuestion2());
30+
questions.add(makeQuestion3());
31+
questions.add(makeQuestion4());
32+
questions.add(makeQuestion5());
33+
questions.add(makeQuestion6());
34+
questions.add(makeQuestion7());
35+
questions.add(makeQuestion8());
36+
questions.add(makeQuestion9());
37+
questions.add(makeQuestion10());
3438

3539
return questions;
40+
3641
}
42+
3743

44+
3845
public static MultipleChoiceQuizQuestion makeQuestion0() {
46+
3947
Map<AnswerChoice, String> answerChoices = new HashMap<>();
4048
answerChoices.put(AnswerChoice.A, "To make backups of files");
4149
answerChoices.put(AnswerChoice.B, "To keep a record of changes over time");
4250
answerChoices.put(AnswerChoice.C, " To delete unnecessary files");
4351
answerChoices.put(AnswerChoice.D, "To run code more efficiently");
4452

4553
return new MultipleChoiceQuizQuestion(
46-
0, "What is the main purpose of version control?", answerChoices, AnswerChoice.B);
54+
0,"What is the main purpose of version control?",answerChoices,AnswerChoice.B);
4755
}
4856

4957
public static MultipleChoiceQuizQuestion makeQuestion1() {
50-
Map<AnswerChoice, String> answerChoices = new HashMap<>();
58+
59+
Map<AnswerChoice, String> answerChoices = new HashMap<>();
5160
answerChoices.put(AnswerChoice.A, "A duplicate copy of a repository that you own and modify");
5261
answerChoices.put(AnswerChoice.B, "A temporary backup of the code");
5362
answerChoices.put(AnswerChoice.C, "A tool for merging branches");
5463
answerChoices.put(AnswerChoice.D, "What is a fork in Git?");
5564

5665
return new MultipleChoiceQuizQuestion(
57-
1, "What is a fork in Git?", answerChoices, AnswerChoice.A);
58-
}
66+
1,"What is the main purpose of version control?",answerChoices,AnswerChoice.B);
67+
}
5968

6069
public static MultipleChoiceQuizQuestion makeQuestion2() {
70+
6171
Map<AnswerChoice, String> answerChoices = new HashMap<>();
6272
answerChoices.put(AnswerChoice.A, "Pull the latest changes");
6373
answerChoices.put(AnswerChoice.B, "Commit changes locally");
6474
answerChoices.put(AnswerChoice.C, "Push changes to the server");
6575
answerChoices.put(AnswerChoice.D, "Write code directly in GitHub");
6676

6777
return new MultipleChoiceQuizQuestion(
68-
2, "Which of the following is NOT part of the basic Git workflow?", answerChoices, AnswerChoice.D);
78+
2,"Which of the following is NOT part of the basic Git workflow?",answerChoices,AnswerChoice.D);
6979
}
70-
7180
public static MultipleChoiceQuizQuestion makeQuestion3() {
81+
7282
Map<AnswerChoice, String> answerChoices = new HashMap<>();
7383
answerChoices.put(AnswerChoice.A, "git commit");
7484
answerChoices.put(AnswerChoice.B, "git merge");
7585
answerChoices.put(AnswerChoice.C, "git branch");
7686
answerChoices.put(AnswerChoice.D, "git pull");
7787

7888
return new MultipleChoiceQuizQuestion(
79-
3, "What command is used to combine changes from different branches?", answerChoices, AnswerChoice.B);
89+
3,"What command is used to combine changes from different branches?",answerChoices,AnswerChoice.B);
8090
}
8191

8292
public static MultipleChoiceQuizQuestion makeQuestion4() {
93+
8394
Map<AnswerChoice, String> answerChoices = new HashMap<>();
8495
answerChoices.put(AnswerChoice.A, "Eclipse");
8596
answerChoices.put(AnswerChoice.B, "IntelliJ IDEA");
8697
answerChoices.put(AnswerChoice.C, "NetBeans");
8798
answerChoices.put(AnswerChoice.D, "VS Code");
8899

89100
return new MultipleChoiceQuizQuestion(
90-
4, "Which IDE is being used in the class?", answerChoices, AnswerChoice.D);
101+
4,"Which IDE is being used in the class?",answerChoices,AnswerChoice.D);
91102
}
92103

93104
public static MultipleChoiceQuizQuestion makeQuestion5() {
105+
94106
Map<AnswerChoice, String> answerChoices = new HashMap<>();
95107
answerChoices.put(AnswerChoice.A, "Extensions");
96108
answerChoices.put(AnswerChoice.B, "Debugger");
97109
answerChoices.put(AnswerChoice.C, "Dev Containers");
98110
answerChoices.put(AnswerChoice.D, "Source Control");
99111

100112
return new MultipleChoiceQuizQuestion(
101-
5, "What feature allows developers to work from the same pre-configured environment in VS Code?", answerChoices, AnswerChoice.C);
113+
5,"What feature allows developers to work from the same pre-configured environment in VS Code?",answerChoices,AnswerChoice.C);
102114
}
103-
104-
public static MultipleChoiceQuizQuestion makeQuestion6() {
115+
public static MultipleChoiceQuizQuestion makeQuestion6() {
105116
Map<AnswerChoice, String> answerChoices = new HashMap<>();
106-
answerChoices.put(AnswerChoice.A, "Editing and refactoring code");
107-
answerChoices.put(AnswerChoice.B, "Browsing code");
108-
answerChoices.put(AnswerChoice.C, "Playing music");
109-
answerChoices.put(AnswerChoice.D, "Managing source control");
117+
answerChoices.put(AnswerChoice.A, "Editing and refactoring code");
118+
answerChoices.put(AnswerChoice.B, "Browsing code");
119+
answerChoices.put(AnswerChoice.C, "Playing music");
120+
answerChoices.put(AnswerChoice.D, "Managing source control");
121+
110122

111123
return new MultipleChoiceQuizQuestion(
112-
6, "What is NOT a reason for using an IDE?", answerChoices, AnswerChoice.C);
124+
6,"What is NOT a reason for using an IDE?",answerChoices,AnswerChoice.C);
113125
}
114-
115126
public static MultipleChoiceQuizQuestion makeQuestion7() {
116127
Map<AnswerChoice, String> answerChoices = new HashMap<>();
117-
answerChoices.put(AnswerChoice.A, "pwd");
118-
answerChoices.put(AnswerChoice.B, "ls");
119-
answerChoices.put(AnswerChoice.C, "cd");
120-
answerChoices.put(AnswerChoice.D, "mkdir");
128+
answerChoices.put(AnswerChoice.A, "pwd");
129+
answerChoices.put(AnswerChoice.B, "ls");
130+
answerChoices.put(AnswerChoice.C, "cd");
131+
answerChoices.put(AnswerChoice.D, "mkdir");
121132

122133
return new MultipleChoiceQuizQuestion(
123-
7, "Which command is used to print the current working directory in the terminal?", answerChoices, AnswerChoice.A);
134+
7,"Which command is used to print the current working directory in the terminal?",answerChoices,AnswerChoice.A);
124135
}
125-
126-
public static MultipleChoiceQuizQuestion makeQuestion8() {
136+
public static MultipleChoiceQuizQuestion makeQuestion8() {
127137
Map<AnswerChoice, String> answerChoices = new HashMap<>();
128-
answerChoices.put(AnswerChoice.A, "pwd");
129-
answerChoices.put(AnswerChoice.B, "ls");
130-
answerChoices.put(AnswerChoice.C, "cd");
131-
answerChoices.put(AnswerChoice.D, "mkdir");
138+
answerChoices.put(AnswerChoice.A, "pwd");
139+
answerChoices.put(AnswerChoice.B, "ls");
140+
answerChoices.put(AnswerChoice.C, "cd");
141+
answerChoices.put(AnswerChoice.D, "mkdir");
132142

133143
return new MultipleChoiceQuizQuestion(
134-
8, "Which command is used to change directories in the terminal?", answerChoices, AnswerChoice.C);
144+
8,"Which command is used to change directories in the terminal?",answerChoices,AnswerChoice.C);
135145
}
136-
137-
public static MultipleChoiceQuizQuestion makeQuestion9() {
146+
public static MultipleChoiceQuizQuestion makeQuestion9() {
138147

139148
Map<AnswerChoice, String> answerChoices = new HashMap<>();
140-
answerChoices.put(AnswerChoice.A, "Change file or directory permissions");
141-
answerChoices.put(AnswerChoice.B, "List files in a directory");
142-
answerChoices.put(AnswerChoice.C, "Remove a file or directory");
143-
answerChoices.put(AnswerChoice.D, "Copy a file or directory");
149+
answerChoices.put(AnswerChoice.A, "Change file or directory permissions");
150+
answerChoices.put(AnswerChoice.B, "List files in a directory");
151+
answerChoices.put(AnswerChoice.C, "Remove a file or directory");
152+
answerChoices.put(AnswerChoice.D, "Copy a file or directory");
144153

145-
return new MultipleChoiceQuizQuestion(
146-
9, "What does the command `chmod` do?", answerChoices, AnswerChoice.A);
154+
return new MultipleChoiceQuizQuestion(
155+
9,"What does the command `chmod` do?",answerChoices,AnswerChoice.A);
147156

148157
}
149-
150-
public static MultipleChoiceQuizQuestion makeQuestion10() {
158+
public static MultipleChoiceQuizQuestion makeQuestion10() {
151159
Map<AnswerChoice, String> answerChoices = new HashMap<>();
152-
answerChoices.put(AnswerChoice.A, "⌘ + Shift + T");
153-
answerChoices.put(AnswerChoice.B, "⌘ + Spacebar, then type terminal");
154-
answerChoices.put(AnswerChoice.C, "⌘ + Q");
155-
answerChoices.put(AnswerChoice.D, "⌘ + S, then type terminal");
160+
answerChoices.put(AnswerChoice.A, "⌘ + Shift + T");
161+
answerChoices.put(AnswerChoice.B, "⌘ + Spacebar, then type terminal");
162+
answerChoices.put(AnswerChoice.C, "⌘ + Q");
163+
answerChoices.put(AnswerChoice.D, "⌘ + S, then type terminal");
156164

157-
return new MultipleChoiceQuizQuestion(
158-
10, "What is the shortcut for getting to the Mac terminal?", answerChoices, AnswerChoice.B);
165+
166+
return new MultipleChoiceQuizQuestion(
167+
10,"What is the shortcut for getting to the Mac terminal?",answerChoices,AnswerChoice.B);
159168

160169
}
161170

162-
}
171+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
quiz:
2+
answers:
3+
default:
4+
- $2y$10$Mijvs3PeAc.DsZBhT4T0jepZqixAOb61iPotSSlIVJGFqpGeBU5dS #0
5+
- $2y$10$fMlkDDq4L9g2Q0lzdTMG1u7aw6rjJhKh.B0e/AYfQHUsXhCM6UGIS #1
6+
- $2y$10$wJZxd8Ha4EEbb16UoUcqgO7oOU.h8sUOsv8FGu6gLiuKwtI4RKFT2 #2
7+
- $2y$10$xAUz.hXIqTamXZg28Hgepe28PS431mmq0WlM48Rji7EkSKD5NfzFy #3
8+
- $2y$10$hbV87MTi/JH51hdNfBP9juELK6LSAxPagrFqcnXAD3gLIEOCCqENW #4
9+
- $2y$10$kiOyjtBQ7B2BszTJajO9keCBlOPh4pLM.mzugzWB2vtGkOWCSJ366 #5
10+
- $2y$10$hCrDJhXV7Gwk0PsBm086p.obO0dk.ZseCfvrx.Vu7U/jqODPaxmFy #6
11+
- $2y$10$LvnU8fBR2ABdf2/gatkW1e1n5XcFQWSE6QfcUjIMo2g8Gu6p1NPJu #7
12+
- $2y$10$DQUbiJqDiMpypZ1FEwK1QejNDwk05F6suGWj6PdEowOvvsoLYGzZy #8
13+
- $2y$10$hzT0JgkSPhMzeuqK4VnD1eoNYkCZ.bg.QL3.VF0vJrmyceWq3bHgC #9
14+
- $2y$10$qyOdRBWT8MO.naUyhZRMr.VIy1NkxDY.1WvbnlTu2bcXkgTnpCBH2 #10
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* This source file was generated by the Gradle 'init' task
3+
*/
4+
package org.example;
5+
6+
import org.junit.jupiter.api.Test;
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.Comparator;
12+
import java.util.HashSet;
13+
import java.util.List;
14+
import java.util.Set;
15+
16+
import org.junit.jupiter.api.BeforeEach;
17+
18+
import com.codedifferently.instructional.quiz.AnswerChoice;
19+
import com.codedifferently.instructional.quiz.MultipleChoiceQuizQuestion;
20+
import com.codedifferently.instructional.quiz.QuizConfig;
21+
import com.codedifferently.instructional.quiz.QuizQuestion;
22+
23+
class LessonTest {
24+
private QuizConfig quizConfig;
25+
private List<QuizQuestion> quizQuestions;
26+
27+
private static final int EXPECTED_NUMBER_OF_QUESTIONS = 11;
28+
29+
@BeforeEach
30+
public void setUp() {
31+
Path quizPath = Paths.get("src", "main", "resources", "quiz.yaml");
32+
quizConfig = new QuizConfig(quizPath.toAbsolutePath().toString());
33+
getQuestions();
34+
}
35+
36+
private void getQuestions() {
37+
List<MultipleChoiceQuizQuestion> quizQuestions = Lesson.makeQuestions();
38+
quizQuestions.sort(Comparator.comparingInt(QuizQuestion::getQuestionNumber));
39+
}
40+
41+
@Test
42+
public void checkQuizQuestions_areAssembledCorrectly() {
43+
// Expect the right number of questions
44+
assertEquals(EXPECTED_NUMBER_OF_QUESTIONS, quizQuestions.size());
45+
46+
// Expect questions to be numbered correctly
47+
for (int i = 0; i < quizQuestions.size(); i++) {
48+
assertEquals(i, quizQuestions.get(i).getQuestionNumber());
49+
}
50+
}
51+
52+
@Test
53+
public void checkQuizQuestions_promptsAreUnique() {
54+
Set<String> questionPrompts = new HashSet<>();
55+
for (QuizQuestion q : quizQuestions) {
56+
questionPrompts.add(q.getQuestionPrompt());
57+
}
58+
assertEquals(EXPECTED_NUMBER_OF_QUESTIONS, questionPrompts.size());
59+
}
60+
61+
@Test
62+
public void checkQuestions_answeredCorrectly() throws Exception {
63+
assertEquals(quizQuestions.size(), quizConfig.size("default"));
64+
65+
for (QuizQuestion question : quizQuestions) {
66+
String actualAnswer = question.getAnswer();
67+
68+
// Check that the question was answered
69+
assertNotEquals(AnswerChoice.UNANSWERED, actualAnswer);
70+
71+
// Check that the answer is correct
72+
assertTrue(
73+
quizConfig.checkAnswer(
74+
"default",
75+
question.getQuestionNumber(),
76+
actualAnswer
77+
)
78+
);
79+
}
80+
}
81+
82+
}

0 commit comments

Comments
 (0)