Added SetUint helper to Args

This commit is contained in:
Aliaksandr Valialkin
2015-11-19 12:27:01 +02:00
parent ed68dfc5f6
commit b5a101843a
2 changed files with 45 additions and 0 deletions
+12
View File
@@ -172,6 +172,18 @@ func (a *Args) GetUint(key string) (int, error) {
return ParseUint(value)
}
// SetUint sets uint value for the given key.
func (a *Args) SetUint(key string, value int) {
a.bufKV.key = AppendBytesStr(a.bufKV.key[:0], key)
a.SetUintBytes(a.bufKV.key, value)
}
// SetUintBytes sets uint value for the given key.
func (a *Args) SetUintBytes(key []byte, value int) {
a.bufKV.value = AppendUint(a.bufKV.value[:0], value)
a.SetBytesKV(key, a.bufKV.value)
}
// GetUintOrZero returns uint value for the given key.
//
// Zero (0) is returned on error.
+33
View File
@@ -6,6 +6,39 @@ import (
"testing"
)
func TestArgsUint(t *testing.T) {
var a Args
a.SetUint("foo", 123)
a.SetUint("bar", 0)
a.SetUint("aaaa", 34566)
expectedS := "foo=123&bar=0&aaaa=34566"
s := string(a.AppendBytes(nil))
if s != expectedS {
t.Fatalf("unexpected args %q. Expecting %q", s, expectedS)
}
if a.GetUintOrZero("foo") != 123 {
t.Fatalf("unexpected arg value %d. Expecting %d", a.GetUintOrZero("foo"), 123)
}
if a.GetUintOrZero("bar") != 0 {
t.Fatalf("unexpected arg value %d. Expecting %d", a.GetUintOrZero("bar"), 0)
}
if a.GetUintOrZero("aaaa") != 34566 {
t.Fatalf("unexpected arg value %d. Expecting %d", a.GetUintOrZero("aaaa"), 34566)
}
if string(a.Peek("foo")) != "123" {
t.Fatalf("unexpected arg value %q. Expecting %q", a.Peek("foo"), "123")
}
if string(a.Peek("bar")) != "0" {
t.Fatalf("unexpected arg value %q. Expecting %q", a.Peek("bar"), "0")
}
if string(a.Peek("aaaa")) != "34566" {
t.Fatalf("unexpected arg value %q. Expecting %q", a.Peek("aaaa"), "34566")
}
}
func TestArgsCopyTo(t *testing.T) {
var a Args