github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/printf/printf_test.go (about)

     1  package printf
     2  
     3  import "testing"
     4  
     5  func BenchmarkParseVerb(b *testing.B) {
     6  	for i := 0; i < b.N; i++ {
     7  		ParseVerb("%[3]*.[2]*[1]f")
     8  	}
     9  }
    10  
    11  func TestParseVerb(t *testing.T) {
    12  	var tests = []struct {
    13  		in  string
    14  		out Verb
    15  	}{
    16  		{
    17  			`%d`,
    18  			Verb{
    19  				Letter:    'd',
    20  				Width:     Default{},
    21  				Precision: Default{},
    22  				Value:     -1,
    23  			},
    24  		},
    25  		{
    26  			`%#d`,
    27  			Verb{
    28  				Letter:    'd',
    29  				Flags:     "#",
    30  				Width:     Default{},
    31  				Precision: Default{},
    32  				Value:     -1,
    33  			},
    34  		},
    35  		{
    36  			`%+#d`,
    37  			Verb{
    38  				Letter:    'd',
    39  				Flags:     "+#",
    40  				Width:     Default{},
    41  				Precision: Default{},
    42  				Value:     -1,
    43  			},
    44  		},
    45  		{
    46  			`%[2]d`,
    47  			Verb{
    48  				Letter:    'd',
    49  				Width:     Default{},
    50  				Precision: Default{},
    51  				Value:     2,
    52  			},
    53  		},
    54  		{
    55  			`%[3]*.[2]*[1]f`,
    56  			Verb{
    57  				Letter:    'f',
    58  				Width:     Star{3},
    59  				Precision: Star{2},
    60  				Value:     1,
    61  			},
    62  		},
    63  		{
    64  			`%6.2f`,
    65  			Verb{
    66  				Letter:    'f',
    67  				Width:     Literal(6),
    68  				Precision: Literal(2),
    69  				Value:     -1,
    70  			},
    71  		},
    72  		{
    73  			`%#[1]x`,
    74  			Verb{
    75  				Letter:    'x',
    76  				Flags:     "#",
    77  				Width:     Default{},
    78  				Precision: Default{},
    79  				Value:     1,
    80  			},
    81  		},
    82  		{
    83  			"%%",
    84  			Verb{
    85  				Letter:    '%',
    86  				Width:     Default{},
    87  				Precision: Default{},
    88  				Value:     0,
    89  			},
    90  		},
    91  		{
    92  			"%*%",
    93  			Verb{
    94  				Letter:    '%',
    95  				Width:     Star{Index: -1},
    96  				Precision: Default{},
    97  				Value:     0,
    98  			},
    99  		},
   100  		{
   101  			"%[1]%",
   102  			Verb{
   103  				Letter:    '%',
   104  				Width:     Default{},
   105  				Precision: Default{},
   106  				Value:     0,
   107  			},
   108  		},
   109  	}
   110  
   111  	for _, tt := range tests {
   112  		tt.out.Raw = tt.in
   113  		v, n, err := ParseVerb(tt.in)
   114  		if err != nil {
   115  			t.Errorf("unexpected error %s while parsing %s", err, tt.in)
   116  		}
   117  		if n != len(tt.in) {
   118  			t.Errorf("ParseVerb only consumed %d of %d bytes", n, len(tt.in))
   119  		}
   120  		if v != tt.out {
   121  			t.Errorf("%s parsed to %#v, want %#v", tt.in, v, tt.out)
   122  		}
   123  	}
   124  }