Source file src/internal/strconv/atob_test.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package strconv_test
     6  
     7  import (
     8  	"bytes"
     9  	. "internal/strconv"
    10  	"testing"
    11  )
    12  
    13  type atobTest struct {
    14  	in  string
    15  	out bool
    16  	err error
    17  }
    18  
    19  var atobtests = []atobTest{
    20  	{"", false, ErrSyntax},
    21  	{"asdf", false, ErrSyntax},
    22  	{"0", false, nil},
    23  	{"f", false, nil},
    24  	{"F", false, nil},
    25  	{"FALSE", false, nil},
    26  	{"false", false, nil},
    27  	{"False", false, nil},
    28  	{"1", true, nil},
    29  	{"t", true, nil},
    30  	{"T", true, nil},
    31  	{"TRUE", true, nil},
    32  	{"true", true, nil},
    33  	{"True", true, nil},
    34  }
    35  
    36  func TestParseBool(t *testing.T) {
    37  	for _, test := range atobtests {
    38  		b, e := ParseBool(test.in)
    39  		if b != test.out || e != test.err {
    40  			t.Errorf("ParseBool(%s) = %v, %v, want %v, %v", test.in, b, e, test.out, test.err)
    41  		}
    42  	}
    43  }
    44  
    45  var boolString = map[bool]string{
    46  	true:  "true",
    47  	false: "false",
    48  }
    49  
    50  func TestFormatBool(t *testing.T) {
    51  	for b, s := range boolString {
    52  		if f := FormatBool(b); f != s {
    53  			t.Errorf("FormatBool(%v) = %q; want %q", b, f, s)
    54  		}
    55  	}
    56  }
    57  
    58  type appendBoolTest struct {
    59  	b   bool
    60  	in  []byte
    61  	out []byte
    62  }
    63  
    64  var appendBoolTests = []appendBoolTest{
    65  	{true, []byte("foo "), []byte("foo true")},
    66  	{false, []byte("foo "), []byte("foo false")},
    67  }
    68  
    69  func TestAppendBool(t *testing.T) {
    70  	for _, test := range appendBoolTests {
    71  		b := AppendBool(test.in, test.b)
    72  		if !bytes.Equal(b, test.out) {
    73  			t.Errorf("AppendBool(%q, %v) = %q; want %q", test.in, test.b, b, test.out)
    74  		}
    75  	}
    76  }
    77  

View as plain text