github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/ufmt/ufmt_test.gno (about)

     1  package ufmt
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  type stringer struct{}
     9  
    10  func (stringer) String() string {
    11  	return "I'm a stringer"
    12  }
    13  
    14  func TestSprintf(t *testing.T) {
    15  	cases := []struct {
    16  		format         string
    17  		values         []interface{}
    18  		expectedOutput string
    19  	}{
    20  		{"hello %s!", []interface{}{"planet"}, "hello planet!"},
    21  		{"hi %%%s!", []interface{}{"worl%d"}, "hi %worl%d!"},
    22  		{"string [%s]", []interface{}{"foo"}, "string [foo]"},
    23  		{"int [%d]", []interface{}{int(42)}, "int [42]"},
    24  		{"int8 [%d]", []interface{}{int8(8)}, "int8 [8]"},
    25  		{"int16 [%d]", []interface{}{int16(16)}, "int16 [16]"},
    26  		{"int32 [%d]", []interface{}{int32(32)}, "int32 [32]"},
    27  		{"int64 [%d]", []interface{}{int64(64)}, "int64 [64]"},
    28  		{"uint [%d]", []interface{}{uint(42)}, "uint [42]"},
    29  		{"uint8 [%d]", []interface{}{uint8(8)}, "uint8 [8]"},
    30  		{"uint16 [%d]", []interface{}{uint16(16)}, "uint16 [16]"},
    31  		{"uint32 [%d]", []interface{}{uint32(32)}, "uint32 [32]"},
    32  		{"uint64 [%d]", []interface{}{uint64(64)}, "uint64 [64]"},
    33  		{"bool [%t]", []interface{}{true}, "bool [true]"},
    34  		{"bool [%t]", []interface{}{false}, "bool [false]"},
    35  		{"invalid bool [%t]", []interface{}{"invalid"}, "invalid bool [(unhandled)]"},
    36  		{"invalid integer [%d]", []interface{}{"invalid"}, "invalid integer [(unhandled)]"},
    37  		{"invalid string [%s]", []interface{}{1}, "invalid string [(unhandled)]"},
    38  		{"no args", nil, "no args"},
    39  		{"finish with %", nil, "finish with %"},
    40  		{"stringer [%s]", []interface{}{stringer{}}, "stringer [I'm a stringer]"},
    41  		{"รข", nil, "รข"},
    42  		{"Hello, World! ๐Ÿ˜Š", nil, "Hello, World! ๐Ÿ˜Š"},
    43  		{"unicode formatting: %s", []interface{}{"๐Ÿ˜Š"}, "unicode formatting: ๐Ÿ˜Š"},
    44  	}
    45  
    46  	for _, tc := range cases {
    47  		name := fmt.Sprintf(tc.format, tc.values...)
    48  		t.Run(name, func(t *testing.T) {
    49  			got := Sprintf(tc.format, tc.values...)
    50  			if got != tc.expectedOutput {
    51  				t.Errorf("got %q, want %q.", got, tc.expectedOutput)
    52  			}
    53  		})
    54  	}
    55  }
    56  
    57  func TestErrorf(t *testing.T) {
    58  	tests := []struct {
    59  		name     string
    60  		format   string
    61  		args     []interface{}
    62  		expected string
    63  	}{
    64  		{
    65  			name:     "simple string",
    66  			format:   "error: %s",
    67  			args:     []interface{}{"something went wrong"},
    68  			expected: "error: something went wrong",
    69  		},
    70  		{
    71  			name:     "integer value",
    72  			format:   "value: %d",
    73  			args:     []interface{}{42},
    74  			expected: "value: 42",
    75  		},
    76  		{
    77  			name:     "boolean value",
    78  			format:   "success: %t",
    79  			args:     []interface{}{true},
    80  			expected: "success: true",
    81  		},
    82  		{
    83  			name:     "multiple values",
    84  			format:   "error %d: %s (success=%t)",
    85  			args:     []interface{}{123, "failure occurred", false},
    86  			expected: "error 123: failure occurred (success=false)",
    87  		},
    88  		{
    89  			name:     "literal percent",
    90  			format:   "literal %%",
    91  			args:     []interface{}{},
    92  			expected: "literal %",
    93  		},
    94  	}
    95  
    96  	for _, tt := range tests {
    97  		t.Run(tt.name, func(t *testing.T) {
    98  			err := Errorf(tt.format, tt.args...)
    99  			if err.Error() != tt.expected {
   100  				t.Errorf("Errorf(%q, %v) = %q, expected %q", tt.format, tt.args, err.Error(), tt.expected)
   101  			}
   102  		})
   103  	}
   104  }
   105  
   106  // NOTE: Currently, there is no way to get the output of Println without using os.Stdout,
   107  // so we can only test that it doesn't panic and print arguments well.
   108  func TestPrintln(t *testing.T) {
   109  	tests := []struct {
   110  		name     string
   111  		args     []interface{}
   112  		expected string
   113  	}{
   114  		{
   115  			name:     "Empty args",
   116  			args:     []interface{}{},
   117  			expected: "",
   118  		},
   119  		{
   120  			name:     "String args",
   121  			args:     []interface{}{"Hello", "World"},
   122  			expected: "Hello World",
   123  		},
   124  		{
   125  			name:     "Integer args",
   126  			args:     []interface{}{1, 2, 3},
   127  			expected: "1 2 3",
   128  		},
   129  		{
   130  			name:     "Mixed args",
   131  			args:     []interface{}{"Hello", 42, true, false, "World"},
   132  			expected: "Hello 42 true false World",
   133  		},
   134  		{
   135  			name:     "Unhandled type",
   136  			args:     []interface{}{"Hello", 3.14, []int{1, 2, 3}},
   137  			expected: "Hello (unhandled) (unhandled)",
   138  		},
   139  	}
   140  
   141  	// TODO: replace os.Stdout with a buffer to capture the output and test it.
   142  	for _, tt := range tests {
   143  		t.Run(tt.name, func(t *testing.T) {
   144  			Println(tt.args...)
   145  		})
   146  	}
   147  }