github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/vet/test_print.go (about)

     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  // +build vet_test
     6  
     7  // This file contains tests for the printf checker.
     8  
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"unsafe" // just for test case printing unsafe.Pointer
    14  )
    15  
    16  func UnsafePointerPrintfTest() {
    17  	var up unsafe.Pointer
    18  	fmt.Printf("%p, %x %X", up, up, up)
    19  }
    20  
    21  // Error methods that do not satisfy the Error interface and should be checked.
    22  type errorTest1 int
    23  
    24  func (errorTest1) Error(...interface{}) string {
    25  	return "hi"
    26  }
    27  
    28  type errorTest2 int // Analogous to testing's *T type.
    29  func (errorTest2) Error(...interface{}) {
    30  }
    31  
    32  type errorTest3 int
    33  
    34  func (errorTest3) Error() { // No return value.
    35  }
    36  
    37  type errorTest4 int
    38  
    39  func (errorTest4) Error() int { // Different return type.
    40  	return 3
    41  }
    42  
    43  type errorTest5 int
    44  
    45  func (errorTest5) error() { // niladic; don't complain if no args (was bug)
    46  }
    47  
    48  // This function never executes, but it serves as a simple test for the program.
    49  // Test with make test.
    50  func PrintfTests() {
    51  	var b bool
    52  	var i int
    53  	var r rune
    54  	var s string
    55  	var x float64
    56  	var p *int
    57  	// Some good format/argtypes
    58  	fmt.Printf("")
    59  	fmt.Printf("%b %b", 3, i)
    60  	fmt.Printf("%c %c %c %c", 3, i, 'x', r)
    61  	fmt.Printf("%d %d", 3, i)
    62  	fmt.Printf("%e %e", 3e9, x)
    63  	fmt.Printf("%E %E", 3e9, x)
    64  	fmt.Printf("%f %f", 3e9, x)
    65  	fmt.Printf("%F %F", 3e9, x)
    66  	fmt.Printf("%g %g", 3e9, x)
    67  	fmt.Printf("%G %G", 3e9, x)
    68  	fmt.Printf("%o %o", 3, i)
    69  	fmt.Printf("%p %p", p, nil)
    70  	fmt.Printf("%q %q %q %q", 3, i, 'x', r)
    71  	fmt.Printf("%s %s", "hi", s)
    72  	fmt.Printf("%t %t", true, b)
    73  	fmt.Printf("%T %T", 3, i)
    74  	fmt.Printf("%U %U", 3, i)
    75  	fmt.Printf("%v %v", 3, i)
    76  	fmt.Printf("%x %x %x %x", 3, i, "hi", s)
    77  	fmt.Printf("%X %X %X %X", 3, i, "hi", s)
    78  	fmt.Printf("%.*s %d %g", 3, "hi", 23, 2.3)
    79  	// Some bad format/argTypes
    80  	fmt.Printf("%b", "hi")                     // ERROR "arg .hi. for printf verb %b of wrong type"
    81  	fmt.Printf("%c", 2.3)                      // ERROR "arg 2.3 for printf verb %c of wrong type"
    82  	fmt.Printf("%d", 2.3)                      // ERROR "arg 2.3 for printf verb %d of wrong type"
    83  	fmt.Printf("%e", "hi")                     // ERROR "arg .hi. for printf verb %e of wrong type"
    84  	fmt.Printf("%E", true)                     // ERROR "arg true for printf verb %E of wrong type"
    85  	fmt.Printf("%f", "hi")                     // ERROR "arg .hi. for printf verb %f of wrong type"
    86  	fmt.Printf("%F", 'x')                      // ERROR "arg 'x' for printf verb %F of wrong type"
    87  	fmt.Printf("%g", "hi")                     // ERROR "arg .hi. for printf verb %g of wrong type"
    88  	fmt.Printf("%G", i)                        // ERROR "arg i for printf verb %G of wrong type"
    89  	fmt.Printf("%o", x)                        // ERROR "arg x for printf verb %o of wrong type"
    90  	fmt.Printf("%p", 23)                       // ERROR "arg 23 for printf verb %p of wrong type"
    91  	fmt.Printf("%q", x)                        // ERROR "arg x for printf verb %q of wrong type"
    92  	fmt.Printf("%s", b)                        // ERROR "arg b for printf verb %s of wrong type"
    93  	fmt.Printf("%t", 23)                       // ERROR "arg 23 for printf verb %t of wrong type"
    94  	fmt.Printf("%U", x)                        // ERROR "arg x for printf verb %U of wrong type"
    95  	fmt.Printf("%x", nil)                      // ERROR "arg nil for printf verb %x of wrong type"
    96  	fmt.Printf("%X", 2.3)                      // ERROR "arg 2.3 for printf verb %X of wrong type"
    97  	fmt.Printf("%.*s %d %g", 3, "hi", 23, 'x') // ERROR "arg 'x' for printf verb %g of wrong type"
    98  	// TODO
    99  	fmt.Println()                      // not an error
   100  	fmt.Println("%s", "hi")            // ERROR "possible formatting directive in Println call"
   101  	fmt.Printf("%s", "hi", 3)          // ERROR "wrong number of args for format in Printf call"
   102  	fmt.Printf("%"+("s"), "hi", 3)     // ERROR "wrong number of args for format in Printf call"
   103  	fmt.Printf("%s%%%d", "hi", 3)      // correct
   104  	fmt.Printf("%08s", "woo")          // correct
   105  	fmt.Printf("% 8s", "woo")          // correct
   106  	fmt.Printf("%.*d", 3, 3)           // correct
   107  	fmt.Printf("%.*d", 3, 3, 3)        // ERROR "wrong number of args for format in Printf call"
   108  	fmt.Printf("%.*d", "hi", 3)        // ERROR "arg .hi. for \* in printf format not of type int"
   109  	fmt.Printf("%.*d", i, 3)           // correct
   110  	fmt.Printf("%.*d", s, 3)           // ERROR "arg s for \* in printf format not of type int"
   111  	fmt.Printf("%q %q", multi()...)    // ok
   112  	fmt.Printf("%#q", `blah`)          // ok
   113  	printf("now is the time", "buddy") // ERROR "no formatting directive"
   114  	Printf("now is the time", "buddy") // ERROR "no formatting directive"
   115  	Printf("hi")                       // ok
   116  	const format = "%s %s\n"
   117  	Printf(format, "hi", "there")
   118  	Printf(format, "hi") // ERROR "wrong number of args for format in Printf call"
   119  	f := new(File)
   120  	f.Warn(0, "%s", "hello", 3)  // ERROR "possible formatting directive in Warn call"
   121  	f.Warnf(0, "%s", "hello", 3) // ERROR "wrong number of args for format in Warnf call"
   122  	f.Warnf(0, "%r", "hello")    // ERROR "unrecognized printf verb"
   123  	f.Warnf(0, "%#s", "hello")   // ERROR "unrecognized printf flag"
   124  	// Something that satisfies the error interface.
   125  	var e error
   126  	fmt.Println(e.Error()) // ok
   127  	// Something that looks like an error interface but isn't, such as the (*T).Error method
   128  	// in the testing package.
   129  	var et1 errorTest1
   130  	fmt.Println(et1.Error())        // ERROR "no args in Error call"
   131  	fmt.Println(et1.Error("hi"))    // ok
   132  	fmt.Println(et1.Error("%d", 3)) // ERROR "possible formatting directive in Error call"
   133  	var et2 errorTest2
   134  	et2.Error()        // ERROR "no args in Error call"
   135  	et2.Error("hi")    // ok, not an error method.
   136  	et2.Error("%d", 3) // ERROR "possible formatting directive in Error call"
   137  	var et3 errorTest3
   138  	et3.Error() // ok, not an error method.
   139  	var et4 errorTest4
   140  	et4.Error() // ok, not an error method.
   141  	var et5 errorTest5
   142  	et5.error() // ok, not an error method.
   143  }
   144  
   145  // printf is used by the test.
   146  func printf(format string, args ...interface{}) {
   147  	panic("don't call - testing only")
   148  }
   149  
   150  // multi is used by the test.
   151  func multi() []interface{} {
   152  	panic("don't call - testing only")
   153  }