...

Source file src/strconv/atob_test.go

Documentation: strconv

     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  	. "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 test.err != nil {
    40  			// expect an error
    41  			if e == nil {
    42  				t.Errorf("ParseBool(%s) = nil; want %s", test.in, test.err)
    43  			} else {
    44  				// NumError assertion must succeed; it's the only thing we return.
    45  				if e.(*NumError).Err != test.err {
    46  					t.Errorf("ParseBool(%s) = %s; want %s", test.in, e, test.err)
    47  				}
    48  			}
    49  		} else {
    50  			if e != nil {
    51  				t.Errorf("ParseBool(%s) = %s; want nil", test.in, e)
    52  			}
    53  			if b != test.out {
    54  				t.Errorf("ParseBool(%s) = %t; want %t", test.in, b, test.out)
    55  			}
    56  		}
    57  	}
    58  }
    59  
    60  var boolString = map[bool]string{
    61  	true:  "true",
    62  	false: "false",
    63  }
    64  
    65  func TestFormatBool(t *testing.T) {
    66  	for b, s := range boolString {
    67  		if f := FormatBool(b); f != s {
    68  			t.Errorf("FormatBool(%v) = %q; want %q", b, f, s)
    69  		}
    70  	}
    71  }
    72  
    73  type appendBoolTest struct {
    74  	b   bool
    75  	in  []byte
    76  	out []byte
    77  }
    78  
    79  var appendBoolTests = []appendBoolTest{
    80  	{true, []byte("foo "), []byte("foo true")},
    81  	{false, []byte("foo "), []byte("foo false")},
    82  }
    83  
    84  func TestAppendBool(t *testing.T) {
    85  	for _, test := range appendBoolTests {
    86  		b := AppendBool(test.in, test.b)
    87  		if !bytes.Equal(b, test.out) {
    88  			t.Errorf("AppendBool(%q, %v) = %q; want %q", test.in, test.b, b, test.out)
    89  		}
    90  	}
    91  }
    92  

View as plain text