2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package strconv_test
|
|
|
|
|
|
|
|
import (
|
2014-06-05 01:15:33 +02:00
|
|
|
"bytes"
|
2010-12-03 05:34:57 +01:00
|
|
|
. "strconv"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type atobTest struct {
|
|
|
|
in string
|
|
|
|
out bool
|
2011-12-03 03:17:34 +01:00
|
|
|
err error
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var atobtests = []atobTest{
|
2011-12-02 20:34:41 +01:00
|
|
|
{"", false, ErrSyntax},
|
|
|
|
{"asdf", false, ErrSyntax},
|
2010-12-03 05:34:57 +01:00
|
|
|
{"0", false, nil},
|
|
|
|
{"f", false, nil},
|
|
|
|
{"F", false, nil},
|
|
|
|
{"FALSE", false, nil},
|
|
|
|
{"false", false, nil},
|
2011-09-16 17:47:21 +02:00
|
|
|
{"False", false, nil},
|
2010-12-03 05:34:57 +01:00
|
|
|
{"1", true, nil},
|
|
|
|
{"t", true, nil},
|
|
|
|
{"T", true, nil},
|
|
|
|
{"TRUE", true, nil},
|
|
|
|
{"true", true, nil},
|
2011-09-16 17:47:21 +02:00
|
|
|
{"True", true, nil},
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
func TestParseBool(t *testing.T) {
|
2010-12-03 05:34:57 +01:00
|
|
|
for _, test := range atobtests {
|
2011-12-14 16:41:54 +01:00
|
|
|
b, e := ParseBool(test.in)
|
2010-12-03 05:34:57 +01:00
|
|
|
if test.err != nil {
|
|
|
|
// expect an error
|
|
|
|
if e == nil {
|
|
|
|
t.Errorf("%s: expected %s but got nil", test.in, test.err)
|
|
|
|
} else {
|
|
|
|
// NumError assertion must succeed; it's the only thing we return.
|
2011-12-03 03:17:34 +01:00
|
|
|
if test.err != e.(*NumError).Err {
|
2010-12-03 05:34:57 +01:00
|
|
|
t.Errorf("%s: expected %s but got %s", test.in, test.err, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if e != nil {
|
2011-01-21 19:19:03 +01:00
|
|
|
t.Errorf("%s: expected no error but got %s", test.in, e)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if b != test.out {
|
|
|
|
t.Errorf("%s: expected %t but got %t", test.in, test.out, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-05 01:15:33 +02:00
|
|
|
|
|
|
|
var boolString = map[bool]string{
|
|
|
|
true: "true",
|
|
|
|
false: "false",
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormatBool(t *testing.T) {
|
|
|
|
for b, s := range boolString {
|
|
|
|
if f := FormatBool(b); f != s {
|
|
|
|
t.Errorf(`FormatBool(%v): expected %q but got %q`, b, s, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type appendBoolTest struct {
|
|
|
|
b bool
|
|
|
|
in []byte
|
|
|
|
out []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var appendBoolTests = []appendBoolTest{
|
|
|
|
{true, []byte("foo "), []byte("foo true")},
|
|
|
|
{false, []byte("foo "), []byte("foo false")},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppendBool(t *testing.T) {
|
|
|
|
for _, test := range appendBoolTests {
|
|
|
|
b := AppendBool(test.in, test.b)
|
|
|
|
if !bytes.Equal(b, test.out) {
|
|
|
|
t.Errorf("AppendBool(%q, %v): expected %q but got %q", test.in, test.b, test.out, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|