github.com/gotranspile/cxgo@v0.3.7/runtime/stdio/format_test.go (about)

     1  package stdio
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestFormat(t *testing.T) {
    10  	cases := []struct {
    11  		name   string
    12  		format string
    13  		words  []formatWord
    14  	}{
    15  		{
    16  			name:   "plain",
    17  			format: `some string here`,
    18  			words: []formatWord{
    19  				{Str: "some string here"},
    20  			},
    21  		},
    22  		{
    23  			name:   "string",
    24  			format: `%s`,
    25  			words: []formatWord{
    26  				{Str: "%s", Verb: true},
    27  			},
    28  		},
    29  		{
    30  			name:   "wstring",
    31  			format: `%S`,
    32  			words: []formatWord{
    33  				{Str: "%S", Verb: true},
    34  			},
    35  		},
    36  		{
    37  			name:   "int",
    38  			format: `%d`,
    39  			words: []formatWord{
    40  				{Str: "%d", Verb: true},
    41  			},
    42  		},
    43  		{
    44  			name:   "verb repeat",
    45  			format: `%dd`,
    46  			words: []formatWord{
    47  				{Str: "%d", Verb: true},
    48  				{Str: "d"},
    49  			},
    50  		},
    51  		{
    52  			name:   "mixed start",
    53  			format: `%d num`,
    54  			words: []formatWord{
    55  				{Str: "%d", Verb: true},
    56  				{Str: " num"},
    57  			},
    58  		},
    59  		{
    60  			name:   "mixed start 2",
    61  			format: `%dnum`,
    62  			words: []formatWord{
    63  				{Str: "%d", Verb: true},
    64  				{Str: "num"},
    65  			},
    66  		},
    67  		{
    68  			name:   "mixed middle",
    69  			format: `v = %d num`,
    70  			words: []formatWord{
    71  				{Str: "v = "},
    72  				{Str: "%d", Verb: true},
    73  				{Str: " num"},
    74  			},
    75  		},
    76  		{
    77  			name:   "mixed middle 2",
    78  			format: `v=%dnum`,
    79  			words: []formatWord{
    80  				{Str: "v="},
    81  				{Str: "%d", Verb: true},
    82  				{Str: "num"},
    83  			},
    84  		},
    85  		{
    86  			name:   "mixed end",
    87  			format: `v = %d`,
    88  			words: []formatWord{
    89  				{Str: "v = "},
    90  				{Str: "%d", Verb: true},
    91  			},
    92  		},
    93  		{
    94  			name:   "mixed end 2",
    95  			format: `v=%d`,
    96  			words: []formatWord{
    97  				{Str: "v="},
    98  				{Str: "%d", Verb: true},
    99  			},
   100  		},
   101  		{
   102  			name:   "percents",
   103  			format: `%d%% = %%%d %%`,
   104  			words: []formatWord{
   105  				{Str: "%d", Verb: true},
   106  				{Str: "%%", Verb: true},
   107  				{Str: " = "},
   108  				{Str: "%%", Verb: true},
   109  				{Str: "%d", Verb: true},
   110  				{Str: " "},
   111  				{Str: "%%", Verb: true},
   112  			},
   113  		},
   114  		{
   115  			name:   "percents 2",
   116  			format: `%%%d = %d%% `,
   117  			words: []formatWord{
   118  				{Str: "%%", Verb: true},
   119  				{Str: "%d", Verb: true},
   120  				{Str: " = "},
   121  				{Str: "%d", Verb: true},
   122  				{Str: "%%", Verb: true},
   123  				{Str: " "},
   124  			},
   125  		},
   126  		{
   127  			name:   "all types",
   128  			format: "%%%c%d%ld%10d%010d%x%o%#x%#o%4.2f%+.0e%E%*d%s%S%%",
   129  			words: []formatWord{
   130  				{Str: "%%", Verb: true},
   131  				{Str: "%c", Verb: true},
   132  				{Str: "%d", Verb: true},
   133  				{Str: "%ld", Verb: true},
   134  				{Str: "%10d", Verb: true},
   135  				{Str: "%010d", Verb: true},
   136  				{Str: "%x", Verb: true},
   137  				{Str: "%o", Verb: true},
   138  				{Str: "%#x", Verb: true},
   139  				{Str: "%#o", Verb: true},
   140  				{Str: "%4.2f", Verb: true},
   141  				{Str: "%+.0e", Verb: true},
   142  				{Str: "%E", Verb: true},
   143  				{Str: "%*d", Verb: true},
   144  				{Str: "%s", Verb: true},
   145  				{Str: "%S", Verb: true},
   146  				{Str: "%%", Verb: true},
   147  			},
   148  		},
   149  	}
   150  	for _, c := range cases {
   151  		t.Run(c.name, func(t *testing.T) {
   152  			words := parseFormat(c.format)
   153  			require.Equal(t, c.words, words)
   154  		})
   155  	}
   156  }