Пример #1
0
func TestServices(t *testing.T) {
	assert := assert.Assert(t)
	services := new(Services)
	assert.Equal(len(services.Services), 0)

	// Addition
	services.Add(Service{Name: "google.com", Password: "******", Meta: "Personal"})
	assert.Equal(len(services.Services), 1)
	assert.Equal(services.Services[0].Name, "google.com")

	// replacement
	services.Add(Service{Name: "google.com", Password: "******"})
	assert.Equal(len(services.Services), 1)
	assert.Equal(services.Services[0].Password, "lkjpoiu")

	// Addition
	services.Add(Service{Name: "facebook.com", Password: "******"})
	assert.Equal(len(services.Services), 2)

	// another replacement
	services.Add(Service{Name: "google.com", Password: "******"})
	assert.Equal(services.Get("google.com").Password, "asghasd")
	assert.Equal(len(services.Services), 2)

	services.Add(Service{Name: "golang.org"})
	goServices := services.Search("go")
	assert.Equal(len(goServices), 2)
	assert.True(goServices[0].Name[:2] == "go", "Service 0 begins with go")
	assert.True(goServices[1].Name[:2] == "go", "Service 1 begins with go")
	assert.Equal(len(services.Search("*")), 3)
}
Пример #2
0
func TestPasswordGeneration(t *testing.T) {
	assert := assert.Assert(t)
	generator := NewPasswordGenerator("lunc20")
	assert.Equal(generator.length, 20)
	password := generator.generate()
	assert.Equal(len(password), 20)
	assert.True(strings.ContainsAny(password, LOWERCASE), "missing characters")
	assert.True(strings.ContainsAny(password, UPPERCASE), "missing characters")
	assert.True(strings.ContainsAny(password, NUMBERS), "missing characters")
	assert.True(strings.ContainsAny(password, CHARACTERS), "missing characters")
}
Пример #3
0
func Test_ConnectCounter(t *testing.T) {
	assert := assert.Assert(t)
	cc := new(ConnectCounter)
	assert.False(cc.incr(true), "f1")
	assert.False(cc.incr(true), "f2")
	assert.False(cc.incr(true), "f3")
	assert.True(cc.incr(true), "t")
}
Пример #4
0
func TestPasswordGenerationWithWords(t *testing.T) {
	assert := assert.Assert(t)
	generator := NewPasswordGenerator("w24")
	assert.Equal(generator.length, 24)
	assert.Equal(generator.words, true)
	password := generator.generate()
	assert.True(len(password) >= 24, "length")
}
Пример #5
0
func Test_HasWonUtilityFunction_returns_1_when_won(t *testing.T) {
	assert := assert.Assert(t)
	game := NewGame()
	wrapper := GameWrapper{game}
	game.SetRows("1110000")
	assert.Equal(HasWon(wrapper), 0)
	hasWon, _ := game.Play(4) // now it's player 2's turn, but 1 won
	assert.True(hasWon, "should have won")
	assert.Equal(HasWon(wrapper), 1)
}
Пример #6
0
func Test_Play_returns_true_when_wins_left_diagonal(t *testing.T) {
	assert := assert.Assert(t)
	g := NewGame()

	g.SetRows(
		"0000000",
		"0001200",
		"0012200",
		"0121200")
	win, err := g.Play(5)
	assert.Nil(err, "error after player 1 played final piece")
	assert.True(win, "player 1 should have won")
}
Пример #7
0
func Test_Play_returns_true_when_wins_in_row(t *testing.T) {
	assert := assert.Assert(t)
	g := NewGame()

	players := []int{1, 2}
	for i := 1; i < 4; i++ {
		for _, p := range players {
			won, err := g.Play(i)
			assert.False(won, "player %v should not have won", p)
			assert.Nil(err, "error after player %d played", p)
		}
	}
	won, err := g.Play(4)
	assert.Nil(err, "error when player 1 played final piece")
	assert.True(won, "player 1 should have won")
}
Пример #8
0
// Make sure I wrote heap game correctly before I try to win it.
func TestHeapGame(t *testing.T) {
	assert := assert.Assert(t)
	assert.Nil(nil, "good")

	f := HeapUtilityFunction

	game := NewHeapGame(1, 2, 3, 4, 5, 6, 7, 8)
	assert.Equal(f(game), 1)

	legalMoves := game.LegalMoves()
	move := legalMoves[0]
	game1 := game.Copy()
	over, _ := game1.Play(move)
	assert.False(over, "game not yet over")
	assert.NotEqual(game, game1)
	assert.Equal(f(game1), 2)

	game2 := game1.Copy()
	over, _ = game2.Play(1)
	assert.Equal(f(game2), 5)
	assert.True(over, "game should be over")
}