...

Source file src/fmt/stringer_test.go

Documentation: fmt

		 1  // Copyright 2010 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 fmt_test
		 6  
		 7  import (
		 8  	. "fmt"
		 9  	"testing"
		10  )
		11  
		12  type TI int
		13  type TI8 int8
		14  type TI16 int16
		15  type TI32 int32
		16  type TI64 int64
		17  type TU uint
		18  type TU8 uint8
		19  type TU16 uint16
		20  type TU32 uint32
		21  type TU64 uint64
		22  type TUI uintptr
		23  type TF float64
		24  type TF32 float32
		25  type TF64 float64
		26  type TB bool
		27  type TS string
		28  
		29  func (v TI) String() string	 { return Sprintf("I: %d", int(v)) }
		30  func (v TI8) String() string	{ return Sprintf("I8: %d", int8(v)) }
		31  func (v TI16) String() string { return Sprintf("I16: %d", int16(v)) }
		32  func (v TI32) String() string { return Sprintf("I32: %d", int32(v)) }
		33  func (v TI64) String() string { return Sprintf("I64: %d", int64(v)) }
		34  func (v TU) String() string	 { return Sprintf("U: %d", uint(v)) }
		35  func (v TU8) String() string	{ return Sprintf("U8: %d", uint8(v)) }
		36  func (v TU16) String() string { return Sprintf("U16: %d", uint16(v)) }
		37  func (v TU32) String() string { return Sprintf("U32: %d", uint32(v)) }
		38  func (v TU64) String() string { return Sprintf("U64: %d", uint64(v)) }
		39  func (v TUI) String() string	{ return Sprintf("UI: %d", uintptr(v)) }
		40  func (v TF) String() string	 { return Sprintf("F: %f", float64(v)) }
		41  func (v TF32) String() string { return Sprintf("F32: %f", float32(v)) }
		42  func (v TF64) String() string { return Sprintf("F64: %f", float64(v)) }
		43  func (v TB) String() string	 { return Sprintf("B: %t", bool(v)) }
		44  func (v TS) String() string	 { return Sprintf("S: %q", string(v)) }
		45  
		46  func check(t *testing.T, got, want string) {
		47  	if got != want {
		48  		t.Error(got, "!=", want)
		49  	}
		50  }
		51  
		52  func TestStringer(t *testing.T) {
		53  	s := Sprintf("%v %v %v %v %v", TI(0), TI8(1), TI16(2), TI32(3), TI64(4))
		54  	check(t, s, "I: 0 I8: 1 I16: 2 I32: 3 I64: 4")
		55  	s = Sprintf("%v %v %v %v %v %v", TU(5), TU8(6), TU16(7), TU32(8), TU64(9), TUI(10))
		56  	check(t, s, "U: 5 U8: 6 U16: 7 U32: 8 U64: 9 UI: 10")
		57  	s = Sprintf("%v %v %v", TF(1.0), TF32(2.0), TF64(3.0))
		58  	check(t, s, "F: 1.000000 F32: 2.000000 F64: 3.000000")
		59  	s = Sprintf("%v %v", TB(true), TS("x"))
		60  	check(t, s, "B: true S: \"x\"")
		61  }
		62  

View as plain text