Exemple #1
0
func TestNewPlayer(t *testing.T) {
	expect := goexpect.New(t)

	accessToken := "123"
	player := models.Player{}.New(accessToken)

	expect(player.AccessToken).ToBe(accessToken)
}
func TestPlayersController(t *testing.T) {
	expect := goexpect.New(t)

	res := post("/players", M{})

	expect(res.code).ToBe(http.StatusCreated)
	expect(len(res.body["access_token"].(string)) > 0).ToBe(true)
}
Exemple #3
0
func TestNewGame(t *testing.T) {
	expect := goexpect.New(t)

	game := models.Game{}.New()

	expect(len(game.Word)).ToNotBe(0)
	expect(game.LettersRemaining).ToBe(game.Word)
	expect(game.Status).ToBe(models.GameStatus.Busy)
	expect(game.TriesLeft).ToBe(models.GameDefaultTriesLeft)
}
Exemple #4
0
func TestIsOver(t *testing.T) {
	expect := goexpect.New(t)

	game := &models.Game{
		Status: models.GameStatus.Busy,
	}
	expect(game.IsOver()).ToBe(false)

	game.Status = models.GameStatus.Failed
	expect(game.IsOver()).ToBe(true)

	game.Status = models.GameStatus.Success
	expect(game.IsOver()).ToBe(true)
}
func TestCreateGame(t *testing.T) {
	expect := goexpect.New(t)

	res := makeRequestFromFn(POST, M{}, func(c appengine.Context, req *http.Request) {
		player, _ := models.Player{}.Create(c)

		req.Header.Add("Authorization", fmt.Sprint("Basic ", player.AccessToken))

		url, _ := url.Parse(fmt.Sprint("/games"))
		req.URL = url
	})

	expect(res.code).ToBe(http.StatusCreated)
}
func TestGuessALetter(t *testing.T) {
	expect := goexpect.New(t)

	res := makeRequestFromFn(PUT, M{"char": "a"}, func(c appengine.Context, req *http.Request) {
		player, _ := models.Player{}.Create(c)
		game, _ := models.Game{}.Create(c, player)

		req.Header.Add("Authorization", fmt.Sprint("Basic ", player.AccessToken))

		url, _ := url.Parse(fmt.Sprint("/games/", game.Id, "/guess"))
		req.URL = url
	})

	expect(res.code).ToBe(http.StatusNoContent)
}
Exemple #7
0
func TestValidGuess(t *testing.T) {
	expect := goexpect.New(t)

	game := models.Game{}.New()

	// Invalid guess

	expect(game.ValidGuess("1")).ToBe(false)
	expect(game.ValidGuess("as")).ToBe(false)
	expect(game.ValidGuess("[")).ToBe(false)
	expect(game.ValidGuess("A")).ToBe(false)

	// Valid guess

	expect(game.ValidGuess("a")).ToBe(true)
	expect(game.ValidGuess("z")).ToBe(true)
}
Exemple #8
0
func TestGuessALetter(t *testing.T) {
	expect := goexpect.New(t)

	game := models.Game{}.New()

	// Make a correct guess

	existingLetter := string(game.Word[0])
	game.GuessALetter(existingLetter)

	expect(game.TriesLeft).ToBe(11)
	expect(strings.Contains(game.LettersRemaining, existingLetter)).ToBe(false)

	// Incorrect guess

	game.GuessALetter(existingLetter)
	expect(game.TriesLeft).ToBe(10)

	// Failed game

	loopUntil := game.TriesLeft
	for i := 0; i < loopUntil; i++ {
		game.GuessALetter(existingLetter)
	}
	expect(game.TriesLeft).ToBe(0)
	expect(game.Status).ToBe(models.GameStatus.Failed)

	// Success game

	game = models.Game{}.New()

	for i, _ := range game.Word {
		game.GuessALetter(string(game.Word[i]))
	}
	expect(game.Status).ToBe(models.GameStatus.Success)
}