Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs2-20wi
project02
Commits
7b30bbd9
Commit
7b30bbd9
authored
4 years ago
by
Ethan Ordentlich
Browse files
Options
Download
Plain Diff
Merge branch 'tests' into 'master'
Review tests for Project 2 Closes #2 and #5 See merge request
!1
parents
989ad63f
34b986d1
master
1 merge request
!1
Review tests for Project 2
Pipeline
#36441
canceled with stage
Changes
8
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
src/edu/caltech/cs2/project02/HangmanGame.java
+2
-2
src/edu/caltech/cs2/project02/HangmanGame.java
src/edu/caltech/cs2/project02/choosers/EvilHangmanChooser.java
+4
-0
...du/caltech/cs2/project02/choosers/EvilHangmanChooser.java
src/edu/caltech/cs2/project02/choosers/RandomHangmanChooser.java
+4
-0
.../caltech/cs2/project02/choosers/RandomHangmanChooser.java
src/edu/caltech/cs2/project02/guessers/AIHangmanGuesser.java
+1
-1
src/edu/caltech/cs2/project02/guessers/AIHangmanGuesser.java
src/edu/caltech/cs2/project02/interfaces/IHangmanGuesser.java
+1
-1
...edu/caltech/cs2/project02/interfaces/IHangmanGuesser.java
tests/edu/caltech/cs2/helpers/Reflection.java
+15
-0
tests/edu/caltech/cs2/helpers/Reflection.java
tests/edu/caltech/cs2/project02/ChooserTests.java
+38
-5
tests/edu/caltech/cs2/project02/ChooserTests.java
tests/edu/caltech/cs2/project02/GuesserTests.java
+26
-1
tests/edu/caltech/cs2/project02/GuesserTests.java
with
91 additions
and
10 deletions
+91
-10
src/edu/caltech/cs2/project02/HangmanGame.java
View file @
7b30bbd9
...
...
@@ -10,7 +10,7 @@ import java.util.Scanner;
import
java.util.Set
;
public
class
HangmanGame
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
throws
FileNotFoundException
{
System
.
out
.
println
(
"Welcome to the cs2 console hangman game."
);
System
.
out
.
println
();
...
...
@@ -30,7 +30,7 @@ public class HangmanGame {
}
// Plays one game with the user
public
static
void
playGame
(
IHangmanChooser
chooser
,
IHangmanGuesser
guesser
)
{
public
static
void
playGame
(
IHangmanChooser
chooser
,
IHangmanGuesser
guesser
)
throws
FileNotFoundException
{
while
(!
chooser
.
isGameOver
())
{
String
pattern
=
chooser
.
getPattern
();
Set
<
Character
>
guesses
=
chooser
.
getGuesses
();
...
...
This diff is collapsed.
Click to expand it.
src/edu/caltech/cs2/project02/choosers/EvilHangmanChooser.java
View file @
7b30bbd9
...
...
@@ -7,6 +7,10 @@ import java.io.FileNotFoundException;
import
java.util.*
;
public
class
EvilHangmanChooser
implements
IHangmanChooser
{
public
EvilHangmanChooser
(
int
wordLength
,
int
maxGuesses
)
throws
FileNotFoundException
{
}
@Override
public
int
makeGuess
(
char
letter
)
{
...
...
This diff is collapsed.
Click to expand it.
src/edu/caltech/cs2/project02/choosers/RandomHangmanChooser.java
View file @
7b30bbd9
...
...
@@ -5,6 +5,10 @@ import edu.caltech.cs2.project02.interfaces.IHangmanChooser;
import
java.util.*
;
public
class
RandomHangmanChooser
implements
IHangmanChooser
{
public
RandomHangmanChooser
(
int
wordLength
,
int
maxGuesses
)
throws
FileNotFoundException
{
}
@Override
public
int
makeGuess
(
char
letter
)
{
...
...
This diff is collapsed.
Click to expand it.
src/edu/caltech/cs2/project02/guessers/AIHangmanGuesser.java
View file @
7b30bbd9
...
...
@@ -7,7 +7,7 @@ import java.util.*;
public
class
AIHangmanGuesser
implements
IHangmanGuesser
{
@Override
public
char
getGuess
(
String
pattern
,
Set
<
Character
>
guesses
)
{
public
char
getGuess
(
String
pattern
,
Set
<
Character
>
guesses
)
throws
FileNotFoundException
{
return
0
;
}
}
This diff is collapsed.
Click to expand it.
src/edu/caltech/cs2/project02/interfaces/IHangmanGuesser.java
View file @
7b30bbd9
...
...
@@ -3,5 +3,5 @@ package edu.caltech.cs2.project02.interfaces;
import
java.util.Set
;
public
interface
IHangmanGuesser
{
public
char
getGuess
(
String
pattern
,
Set
<
Character
>
guesses
);
public
char
getGuess
(
String
pattern
,
Set
<
Character
>
guesses
)
throws
FileNotFoundException
;
}
This diff is collapsed.
Click to expand it.
tests/edu/caltech/cs2/helpers/Reflection.java
View file @
7b30bbd9
...
...
@@ -232,6 +232,21 @@ public class Reflection {
assertFieldsEqualTo
(
clazz
,
getFields
(
clazz
).
filter
(
doesNotHaveModifier
(
"static"
)),
"public"
,
null
,
0
);
}
public
static
void
assertNoProtectedFields
(
Class
clazz
)
{
assertFieldsEqualTo
(
clazz
,
getFields
(
clazz
).
filter
(
doesNotHaveModifier
(
"static"
)),
"protected"
,
null
,
0
);
}
public
static
void
assertFieldModifiers
(
Class
clazz
)
{
List
<
Field
>
fields
=
getFields
(
clazz
).
collect
(
Collectors
.
toList
());
for
(
Field
f
:
fields
)
{
int
m
=
f
.
getModifiers
();
assertTrue
(
Modifier
.
isPrivate
(
m
)
||
Modifier
.
isProtected
(
m
)
||
Modifier
.
isPublic
(
m
),
"Field \""
+
f
.
getName
()
+
"\" does not have one of {public, private, protected}"
);
}
}
public
static
Field
getFieldByName
(
Class
clazz
,
String
name
)
{
try
{
...
...
This diff is collapsed.
Click to expand it.
tests/edu/caltech/cs2/project02/ChooserTests.java
View file @
7b30bbd9
...
...
@@ -11,6 +11,7 @@ import edu.caltech.cs2.project02.interfaces.IHangmanChooser;
import
org.junit.jupiter.api.*
;
import
org.junit.jupiter.params.ParameterizedTest
;
import
java.io.FileNotFoundException
;
import
java.lang.reflect.Constructor
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Method
;
...
...
@@ -52,6 +53,22 @@ public class ChooserTests {
Reflection
.
assertNoPublicFields
(
RandomHangmanChooser
.
class
);
}
@Order
(
3
)
@Tag
(
"C"
)
@DisplayName
(
"There are no protected fields in RandomHangmanChooser"
)
@Test
public
void
testNoProtectedFieldsRHC
()
{
Reflection
.
assertNoProtectedFields
(
RandomHangmanChooser
.
class
);
}
@Order
(
3
)
@Tag
(
"C"
)
@DisplayName
(
"All fields in RandomHangmanChooser have modifiers"
)
@Test
public
void
testFieldModifiersRHC
()
{
Reflection
.
assertFieldModifiers
(
RandomHangmanChooser
.
class
);
}
@Order
(
3
)
@Tag
(
"C"
)
@DisplayName
(
"Chosen word and random are the only final fields"
)
...
...
@@ -95,7 +112,7 @@ public class ChooserTests {
IntStream
.
range
(
0
,
20
).
forEach
(
i
->
assertThrows
(
IllegalArgumentException
.
class
,
()
->
Reflection
.
invoke
(
m
,
chooser
,
(
char
)
(
'z'
+
(
i
+
1
)))));
}
public
void
runTestGame
(
Class
<?
extends
IHangmanChooser
>
clazz
,
int
wordLength
,
int
wrongAnswersAllowed
,
String
guesses
)
{
public
void
runTestGame
(
Class
<?
extends
IHangmanChooser
>
clazz
,
int
wordLength
,
int
wrongAnswersAllowed
,
String
guesses
)
throws
FileNotFoundException
{
Constructor
<?
extends
IHangmanChooser
>
constructor
=
Reflection
.
getConstructor
(
clazz
,
int
.
class
,
int
.
class
);
HangmanGame
.
playGame
(
Reflection
.
newInstance
(
constructor
,
wordLength
,
wrongAnswersAllowed
),
...
...
@@ -107,7 +124,7 @@ public class ChooserTests {
@Tag
(
"C"
)
@DisplayName
(
"Expected game end after revealing word for RandomHangmanChooser"
)
@Test
public
void
testGameEndOnRevealWordRHC
()
{
public
void
testGameEndOnRevealWordRHC
()
throws
FileNotFoundException
{
RandomHangmanChooser
chooser
=
new
RandomHangmanChooser
(
8
,
1
);
chooser
.
getWord
();
assertThrows
(
IllegalStateException
.
class
,
()
->
chooser
.
makeGuess
(
'a'
));
...
...
@@ -154,7 +171,7 @@ public class ChooserTests {
"trace14.txt"
,
}
)
public
void
testPlayGameWithRandomChooser
(
Map
<
String
,
String
>
arguments
,
String
expectedOutput
,
CaptureSystemOutput
.
OutputCapture
capture
)
{
public
void
testPlayGameWithRandomChooser
(
Map
<
String
,
String
>
arguments
,
String
expectedOutput
,
CaptureSystemOutput
.
OutputCapture
capture
)
throws
FileNotFoundException
{
// Parse arguments
int
seed
=
Integer
.
parseInt
(
arguments
.
get
(
"seed"
));
int
length
=
Integer
.
parseInt
(
arguments
.
get
(
"word length"
));
...
...
@@ -188,6 +205,22 @@ public class ChooserTests {
Reflection
.
assertNoPublicFields
(
EvilHangmanChooser
.
class
);
}
@Order
(
2
)
@Tag
(
"B"
)
@DisplayName
(
"There are no protected fields in EvilHangmanChooser"
)
@Test
public
void
testNoProtectedFieldsEHC
()
{
Reflection
.
assertNoProtectedFields
(
EvilHangmanChooser
.
class
);
}
@Order
(
2
)
@Tag
(
"B"
)
@DisplayName
(
"All fields in EvilHangmanChooser have modifiers"
)
@Test
public
void
testFieldModifiersEHC
()
{
Reflection
.
assertFieldModifiers
(
EvilHangmanChooser
.
class
);
}
@Order
(
2
)
@Tag
(
"B"
)
@DisplayName
(
"There is no map field in EvilHangmanChooser"
)
...
...
@@ -225,7 +258,7 @@ public class ChooserTests {
@Tag
(
"B"
)
@DisplayName
(
"Expected game end after revealing word for EvilHangmanChooser"
)
@Test
public
void
testGameEndOnRevealWordEHC
()
{
public
void
testGameEndOnRevealWordEHC
()
throws
FileNotFoundException
{
EvilHangmanChooser
chooser
=
new
EvilHangmanChooser
(
8
,
1
);
chooser
.
getWord
();
assertThrows
(
IllegalStateException
.
class
,
()
->
chooser
.
makeGuess
(
'a'
));
...
...
@@ -271,7 +304,7 @@ public class ChooserTests {
"trace14-evil.txt"
,
}
)
public
void
testPlayGameWithEvilChooser
(
Map
<
String
,
String
>
arguments
,
String
expectedOutput
,
CaptureSystemOutput
.
OutputCapture
capture
)
{
public
void
testPlayGameWithEvilChooser
(
Map
<
String
,
String
>
arguments
,
String
expectedOutput
,
CaptureSystemOutput
.
OutputCapture
capture
)
throws
FileNotFoundException
{
// Parse arguments
int
length
=
Integer
.
parseInt
(
arguments
.
get
(
"word length"
));
int
wrongAllowed
=
Integer
.
parseInt
(
arguments
.
get
(
"max wrong guesses"
));
...
...
This diff is collapsed.
Click to expand it.
tests/edu/caltech/cs2/project02/GuesserTests.java
View file @
7b30bbd9
...
...
@@ -5,6 +5,7 @@ import edu.caltech.cs2.helpers.Reflection;
import
edu.caltech.cs2.project02.guessers.AIHangmanGuesser
;
import
org.junit.jupiter.api.*
;
import
java.io.FileNotFoundException
;
import
java.lang.reflect.Field
;
import
java.util.List
;
import
java.util.Set
;
...
...
@@ -32,8 +33,32 @@ public class GuesserTests {
public
void
testNoFields
()
{
Reflection
.
assertFieldsLessThan
(
AIHangmanGuesser
.
class
,
"private"
,
1
);
}
@Order
(
1
)
@Tag
(
"A"
)
@DisplayName
(
"There are no public fields in AIHangmanGuesser"
)
@Test
public
void
testNoPublicFieldsAIHG
()
{
Reflection
.
assertNoPublicFields
(
AIHangmanGuesser
.
class
);
}
@Order
(
1
)
@Tag
(
"A"
)
@DisplayName
(
"There are no protected fields in AIHangmanGuesser"
)
@Test
public
void
testNoProtectedFieldsAIHG
()
{
Reflection
.
assertNoProtectedFields
(
AIHangmanGuesser
.
class
);
}
@Order
(
1
)
@Tag
(
"A"
)
@DisplayName
(
"All fields in AIHangmanGuesser have modifiers"
)
@Test
public
void
testFieldModifiersAIHG
()
{
Reflection
.
assertFieldModifiers
(
AIHangmanGuesser
.
class
);
}
@Order
(
1
)
@Tag
(
"A"
)
@DisplayName
(
"Test that the dictionary is static"
)
@Test
...
...
@@ -46,7 +71,7 @@ public class GuesserTests {
@Tag
(
"A"
)
@DisplayName
(
"Test getGuess Method in AIHangmanGuesser"
)
@Test
public
void
testGetGuess
()
{
public
void
testGetGuess
()
throws
FileNotFoundException
{
AIHangmanGuesser
guesser
=
new
AIHangmanGuesser
();
// test character with most occurrences is chosen
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help