Exemplo n.º 1
0
func TestFloat64(t *T) {
	r := &Reply{Type: ErrorReply, Err: LoadingError}
	_, err := r.Float64()
	assert.Equal(t, LoadingError, err)

	r = &Reply{Type: IntegerReply, int: 5}
	_, err = r.Float64()
	assert.NotNil(t, err)

	r = &Reply{Type: BulkReply, buf: []byte("5.1")}
	b, err := r.Float64()
	assert.Nil(t, err)
	assert.Equal(t, float64(5.1), b)

	r = &Reply{Type: BulkReply, buf: []byte("foo")}
	_, err = r.Float64()
	assert.NotNil(t, err)
}
Exemplo n.º 2
0
func TestParse(t *T) {
	c := dial(t)

	parseString := func(b string) *Reply {
		c.reader = bufio.NewReader(bytes.NewBufferString(b))
		return c.parse()
	}

	// missing \n trailing
	r := parseString("foo")
	assert.Equal(t, ErrorReply, r.Type)
	assert.NotNil(t, r.Err)

	// error reply
	r = parseString("-ERR unknown command 'foobar'\r\n")
	assert.Equal(t, ErrorReply, r.Type)
	assert.Equal(t, "ERR unknown command 'foobar'", r.Err.Error())

	// LOADING error
	r = parseString("-LOADING Redis is loading the dataset in memory\r\n")
	assert.Equal(t, ErrorReply, r.Type)
	assert.Equal(t, LoadingError, r.Err)

	// status reply
	r = parseString("+OK\r\n")
	assert.Equal(t, StatusReply, r.Type)
	assert.Equal(t, []byte("OK"), r.buf)

	// integer reply
	r = parseString(":1337\r\n")
	assert.Equal(t, IntegerReply, r.Type)
	assert.Equal(t, int64(1337), r.int)

	// null bulk reply
	r = parseString("$-1\r\n")
	assert.Equal(t, NilReply, r.Type)

	// bulk reply
	r = parseString("$6\r\nfoobar\r\n")
	assert.Equal(t, BulkReply, r.Type)
	assert.Equal(t, []byte("foobar"), r.buf)

	// null multi bulk reply
	r = parseString("*-1\r\n")
	assert.Equal(t, NilReply, r.Type)

	// multi bulk reply
	r = parseString("*5\r\n:0\r\n:1\r\n:2\r\n:3\r\n$6\r\nfoobar\r\n")
	assert.Equal(t, MultiReply, r.Type)
	assert.Equal(t, 5, len(r.Elems))
	for i := 0; i < 4; i++ {
		assert.Equal(t, int64(i), r.Elems[i].int)
	}
	assert.Equal(t, []byte("foobar"), r.Elems[4].buf)
}
Exemplo n.º 3
0
func TestLoadGame(t *testing.T) {
	StartGame(aGameId, aFirstPlayerId)

	game, error := LoadGame(aGameId)

	assert.Nil(t, error)
	assert.NotNil(t, game)
	assert.Equal(t, game.GameId, aGameId)
	assert.Equal(t, game.GamerOneId, aFirstPlayerId)
	assert.Equal(t, game.LastPlayer, false)
	assert.Equal(t, len(game.State), 7, "Table should have 7 columns")
	assert.Equal(t, len(game.State[0]), 6, "Table should have 6 columns")
}
Exemplo n.º 4
0
func TestStartGame(t *testing.T) {
	game, error := StartGame(aGameId, aFirstPlayerId)

	assert.Nil(t, error)
	assert.NotNil(t, game)
	assert.Equal(t, len(game.State), 7, "Table should have 7 columns")
	assert.Equal(t, len(game.State[0]), 6, "Table should have 6 columns")

	for i := range game.State {
		for j := range game.State[i] {
			assert.Equal(t, game.State[i][j], uint8(0), "Table should be 0 initialized")
		}
	}
}
Exemplo n.º 5
0
func TestStr(t *T) {
	r := &Reply{Type: ErrorReply, Err: LoadingError}
	_, err := r.Str()
	assert.Equal(t, LoadingError, err)

	r = &Reply{Type: IntegerReply}
	_, err = r.Str()
	assert.NotNil(t, err)

	r = &Reply{Type: StatusReply, buf: []byte("foo")}
	b, err := r.Str()
	assert.Nil(t, err)
	assert.Equal(t, "foo", b)

	r = &Reply{Type: BulkReply, buf: []byte("foo")}
	b, err = r.Str()
	assert.Nil(t, err)
	assert.Equal(t, "foo", b)
}
Exemplo n.º 6
0
func TestBool(t *T) {
	r := &Reply{Type: IntegerReply, int: 0}
	b, err := r.Bool()
	assert.Nil(t, err)
	assert.Equal(t, false, b)

	r = &Reply{Type: StatusReply, buf: []byte("0")}
	b, err = r.Bool()
	assert.Nil(t, err)
	assert.Equal(t, false, b)

	r = &Reply{Type: IntegerReply, int: 2}
	b, err = r.Bool()
	assert.Nil(t, err)
	assert.Equal(t, true, b)

	r = &Reply{Type: NilReply}
	_, err = r.Bool()
	assert.NotNil(t, err)
}
Exemplo n.º 7
0
func TestRead(t *T) {
	var m *Message
	var err error

	_, err = NewMessage(nil)
	assert.NotNil(t, err)

	_, err = NewMessage([]byte{})
	assert.NotNil(t, err)

	// Simple string
	m, _ = NewMessage([]byte("+ohey\r\n"))
	assert.Equal(t, SimpleStr, m.Type)
	assert.Equal(t, []byte("ohey"), m.val)

	// Empty simple string
	m, _ = NewMessage([]byte("+\r\n"))
	assert.Equal(t, SimpleStr, m.Type)
	assert.Equal(t, []byte(""), m.val.([]byte))

	// Error
	m, _ = NewMessage([]byte("-ohey\r\n"))
	assert.Equal(t, Err, m.Type)
	assert.Equal(t, []byte("ohey"), m.val.([]byte))

	// Empty error
	m, _ = NewMessage([]byte("-\r\n"))
	assert.Equal(t, Err, m.Type)
	assert.Equal(t, []byte(""), m.val.([]byte))

	// Int
	m, _ = NewMessage([]byte(":1024\r\n"))
	assert.Equal(t, Int, m.Type)
	assert.Equal(t, int64(1024), m.val.(int64))

	// Bulk string
	m, _ = NewMessage([]byte("$3\r\nfoo\r\n"))
	assert.Equal(t, BulkStr, m.Type)
	assert.Equal(t, []byte("foo"), m.val.([]byte))

	// Empty bulk string
	m, _ = NewMessage([]byte("$0\r\n\r\n"))
	assert.Equal(t, BulkStr, m.Type)
	assert.Equal(t, []byte(""), m.val.([]byte))

	// Nil bulk string
	m, _ = NewMessage([]byte("$-1\r\n"))
	assert.Equal(t, Nil, m.Type)

	// Array
	m, _ = NewMessage([]byte("*2\r\n+foo\r\n+bar\r\n"))
	assert.Equal(t, Array, m.Type)
	assert.Equal(t, 2, len(m.val.([]*Message)))
	assert.Equal(t, SimpleStr, m.val.([]*Message)[0].Type)
	assert.Equal(t, []byte("foo"), m.val.([]*Message)[0].val.([]byte))
	assert.Equal(t, SimpleStr, m.val.([]*Message)[1].Type)
	assert.Equal(t, []byte("bar"), m.val.([]*Message)[1].val.([]byte))

	// Empty array
	m, _ = NewMessage([]byte("*0\r\n"))
	assert.Equal(t, Array, m.Type)
	assert.Equal(t, 0, len(m.val.([]*Message)))

	// Nil Array
	m, _ = NewMessage([]byte("*-1\r\n"))
	assert.Equal(t, Nil, m.Type)

	// Embedded Array
	m, _ = NewMessage([]byte("*3\r\n+foo\r\n+bar\r\n*2\r\n+foo\r\n+bar\r\n"))
	assert.Equal(t, Array, m.Type)
	assert.Equal(t, 3, len(m.val.([]*Message)))
	assert.Equal(t, SimpleStr, m.val.([]*Message)[0].Type)
	assert.Equal(t, []byte("foo"), m.val.([]*Message)[0].val.([]byte))
	assert.Equal(t, SimpleStr, m.val.([]*Message)[1].Type)
	assert.Equal(t, []byte("bar"), m.val.([]*Message)[1].val.([]byte))
	m = m.val.([]*Message)[2]
	assert.Equal(t, 2, len(m.val.([]*Message)))
	assert.Equal(t, SimpleStr, m.val.([]*Message)[0].Type)
	assert.Equal(t, []byte("foo"), m.val.([]*Message)[0].val.([]byte))
	assert.Equal(t, SimpleStr, m.val.([]*Message)[1].Type)
	assert.Equal(t, []byte("bar"), m.val.([]*Message)[1].val.([]byte))

	// Test that two bulks in a row read correctly
	m, _ = NewMessage([]byte("*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n"))
	assert.Equal(t, Array, m.Type)
	assert.Equal(t, 2, len(m.val.([]*Message)))
	assert.Equal(t, BulkStr, m.val.([]*Message)[0].Type)
	assert.Equal(t, []byte("foo"), m.val.([]*Message)[0].val.([]byte))
	assert.Equal(t, BulkStr, m.val.([]*Message)[1].Type)
	assert.Equal(t, []byte("bar"), m.val.([]*Message)[1].val.([]byte))
}