mirror of
				https://github.com/asdine/storm.git
				synced 2025-10-31 10:27:03 +08:00 
			
		
		
		
	Move codec test helper to an internal package
This commit is contained in:
		
							
								
								
									
										44
									
								
								codec/internal/test_helpers.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								codec/internal/test_helpers.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| package internal | ||||
|  | ||||
| import ( | ||||
| 	"encoding/gob" | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/asdine/storm/codec" | ||||
| ) | ||||
|  | ||||
| type testStruct struct { | ||||
| 	Name string | ||||
| } | ||||
|  | ||||
| // RoundtripTester is a test helper to test a EncodeDecoder | ||||
| func RoundtripTester(t *testing.T, c codec.EncodeDecoder, vals ...interface{}) { | ||||
| 	var val, to interface{} | ||||
| 	if len(vals) > 0 { | ||||
| 		if len(vals) != 2 { | ||||
| 			panic("Wrong number of vals, expected 2") | ||||
| 		} | ||||
| 		val = vals[0] | ||||
| 		to = vals[1] | ||||
| 	} else { | ||||
| 		val = &testStruct{Name: "test"} | ||||
| 		to = &testStruct{} | ||||
| 	} | ||||
|  | ||||
| 	encoded, err := c.Encode(val) | ||||
| 	if err != nil { | ||||
| 		t.Fatal("Encode error:", err) | ||||
| 	} | ||||
| 	err = c.Decode(encoded, to) | ||||
| 	if err != nil { | ||||
| 		t.Fatal("Decode error:", err) | ||||
| 	} | ||||
| 	if !reflect.DeepEqual(val, to) { | ||||
| 		t.Fatalf("Roundtrip codec mismatch, expected\n%#v\ngot\n%#v", val, to) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func init() { | ||||
| 	gob.Register(&testStruct{}) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Bjørn Erik Pedersen
					Bjørn Erik Pedersen