♻️ Add tools.WriteValue
This commit is contained in:
		
					parent
					
						
							
								fd5d315164
							
						
					
				
			
			
				commit
				
					
						286ac4557d
					
				
			
		
					 4 changed files with 97 additions and 31 deletions
				
			
		
							
								
								
									
										49
									
								
								tools/write_buffer_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								tools/write_buffer_test.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,49 @@ | |||
| package tools | ||||
| 
 | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"encoding/json" | ||||
| 	"errors" | ||||
| 	"testing" | ||||
| 	"time" | ||||
| 
 | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
| 
 | ||||
| func TestWriteBuffer(t *testing.T) { | ||||
| 	when := time.Now() | ||||
| 	tests := []struct { | ||||
| 		name  string | ||||
| 		value any | ||||
| 		out   string | ||||
| 		err   error | ||||
| 	}{ | ||||
| 		{"nil", nil, "", nil}, | ||||
| 		{"string", "hi", "hi", nil}, | ||||
| 		{"bytes", []byte{104, 105}, "hi", nil}, | ||||
| 		{"byte", byte(104), "h", nil}, | ||||
| 		{"rune", 'h', "h", nil}, | ||||
| 		{"int", 42, "42", nil}, | ||||
| 		{"int64", int64(42), "42", nil}, | ||||
| 		{"float", 42.13, "42.13", nil}, | ||||
| 		{"bool", false, "false", nil}, | ||||
| 		{"json.Number", json.Number("42.13"), "42.13", nil}, | ||||
| 		{"json.RawMessage", json.RawMessage("{}"), "{}", nil}, | ||||
| 		{"time", when, when.Format(time.RFC3339), nil}, | ||||
| 		{"struct", struct{}{}, "", errors.New("Unsupported type struct {}")}, | ||||
| 	} | ||||
| 
 | ||||
| 	for _, tt := range tests { | ||||
| 		t.Run(tt.name, getWriteTestRunner(tt.value, tt.out, tt.err)) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func getWriteTestRunner(value any, out string, err error) func(*testing.T) { | ||||
| 	return func(t *testing.T) { | ||||
| 		buff := &bytes.Buffer{} | ||||
| 		n, er := WriteValue(buff, value) | ||||
| 		assert.Equal(t, len(out), n) | ||||
| 		assert.Equal(t, err, er) | ||||
| 		assert.Equal(t, out, buff.String()) | ||||
| 	} | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue