| 
									
										
										
										
											2024-01-26 20:00:03 -06:00
										 |  |  | package models | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Type assertions | 
					
						
							|  |  |  | var _ encoding.TextMarshaler = Meta{} | 
					
						
							| 
									
										
										
										
											2024-01-27 16:07:27 -06:00
										 |  |  | var _ encoding.TextUnmarshaler = new(Meta) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var skipMarshalTest = errors.New("skip marshal") | 
					
						
							| 
									
										
										
										
											2024-01-26 20:00:03 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestMeta(t *testing.T) { | 
					
						
							|  |  |  | 	when := time.Now() | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							| 
									
										
										
										
											2024-01-27 16:07:27 -06:00
										 |  |  | 		name   string | 
					
						
							|  |  |  | 		key    string | 
					
						
							|  |  |  | 		value  any | 
					
						
							|  |  |  | 		out    string | 
					
						
							|  |  |  | 		err    error | 
					
						
							|  |  |  | 		newVal any | 
					
						
							| 
									
										
										
										
											2024-01-26 20:00:03 -06:00
										 |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2024-01-27 16:07:27 -06:00
										 |  |  | 		{"int", "num", 42, "@num 42", nil, 42}, | 
					
						
							|  |  |  | 		{"float", "num", 42.13, "@num 42.13", nil, 42.13}, | 
					
						
							|  |  |  | 		{"string", "word", "hello", "@word hello", nil, "hello"}, | 
					
						
							|  |  |  | 		{"json number", "num", json.Number("42.13"), "@num 42.13", nil, 42.13}, | 
					
						
							|  |  |  | 		{"true", "b", true, "@b true", nil, true}, | 
					
						
							|  |  |  | 		{"false", "b", false, "@b false", nil, false}, | 
					
						
							| 
									
										
										
										
											2024-01-28 12:41:55 -06:00
										 |  |  | 		{"nil", "n", nil, "", nil, ErrorParsing}, | 
					
						
							| 
									
										
										
										
											2024-01-27 16:07:27 -06:00
										 |  |  | 		{"time", "when", when, "@when " + when.Format(time.RFC3339), nil, when}, | 
					
						
							|  |  |  | 		{"rune", "char", '@', "@char @", nil, "@"}, | 
					
						
							|  |  |  | 		{"bytes", "byteme", []byte("yo"), "@byteme yo", nil, "yo"}, | 
					
						
							|  |  |  | 		{"byte", "byteme", byte(67), "@byteme C", nil, "C"}, | 
					
						
							|  |  |  | 		{"json-obj", "obj", json.RawMessage(`{"foo":"bar","baz":"quux"}`), `@obj {"foo":"bar","baz":"quux"}`, nil, json.RawMessage(`{"foo":"bar","baz":"quux"}`)}, | 
					
						
							|  |  |  | 		{"json-arr", "arr", json.RawMessage(`["foo",42,"bar", null,"quux", true]`), `@arr ["foo",42,"bar", null,"quux", true]`, nil, json.RawMessage(`["foo",42,"bar", null,"quux", true]`)}, | 
					
						
							|  |  |  | 		{"chan", "nope", make(chan bool), "", errors.New("Unknown type chan bool"), ""}, | 
					
						
							|  |  |  | 		{"whitespace-key", "no space", "hi", "", errors.New("whitespace is not allowed in key: no space"), ""}, | 
					
						
							| 
									
										
										
										
											2024-01-28 12:41:55 -06:00
										 |  |  | 		{"empty-mar", "nope", skipMarshalTest, "", nil, ErrorParsing}, | 
					
						
							|  |  |  | 		{"no-key-mar", "nope", skipMarshalTest, "nope", nil, ErrorParsing}, | 
					
						
							|  |  |  | 		{"no-value-mar", "nope", skipMarshalTest, "@nope ", nil, ErrorParsing}, | 
					
						
							|  |  |  | 		{"space-value-mar", "nope", skipMarshalTest, "@nope  ", nil, ErrorParsing}, | 
					
						
							|  |  |  | 		{"space-nl-value-mar", "nope", skipMarshalTest, "@nope  \n  ", nil, ErrorParsing}, | 
					
						
							| 
									
										
										
										
											2024-01-27 16:07:27 -06:00
										 |  |  | 		{"null-value-mar", "nope", skipMarshalTest, "@nope null", nil, nil}, | 
					
						
							|  |  |  | 		{"tilda-value-mar", "nope", skipMarshalTest, "@nope ~", nil, nil}, | 
					
						
							|  |  |  | 		{"none-value-mar", "nope", skipMarshalTest, "@nope none", nil, nil}, | 
					
						
							|  |  |  | 		{"nil-value-mar", "nope", skipMarshalTest, "@nope nil", nil, nil}, | 
					
						
							|  |  |  | 		{"yes-value-mar", "nope", skipMarshalTest, "@nope yes", nil, true}, | 
					
						
							|  |  |  | 		{"on-value-mar", "nope", skipMarshalTest, "@nope on", nil, true}, | 
					
						
							|  |  |  | 		{"no-value-mar", "nope", skipMarshalTest, "@nope no", nil, false}, | 
					
						
							|  |  |  | 		{"off-value-mar", "nope", skipMarshalTest, "@nope off", nil, false}, | 
					
						
							| 
									
										
										
										
											2024-01-26 20:00:03 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tt := range tests { | 
					
						
							| 
									
										
										
										
											2024-01-27 16:07:27 -06:00
										 |  |  | 		t.Run(tt.name, getMetaTestRunner(tt.key, tt.value, tt.out, tt.err, tt.newVal)) | 
					
						
							| 
									
										
										
										
											2024-01-26 20:00:03 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-27 16:07:27 -06:00
										 |  |  | func getMetaTestRunner(key string, value any, out string, err error, newVal any) func(*testing.T) { | 
					
						
							| 
									
										
										
										
											2024-01-26 20:00:03 -06:00
										 |  |  | 	return func(t *testing.T) { | 
					
						
							|  |  |  | 		st := Meta{key, value} | 
					
						
							| 
									
										
										
										
											2024-01-27 16:07:27 -06:00
										 |  |  | 		n := &Meta{} | 
					
						
							|  |  |  | 		var e error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if valE, ok := value.(error); !ok || !errors.Is(valE, skipMarshalTest) { | 
					
						
							|  |  |  | 			var o []byte | 
					
						
							|  |  |  | 			o, e = st.MarshalText() | 
					
						
							|  |  |  | 			assert.Equal(t, out, string(o)) | 
					
						
							|  |  |  | 			assert.Equal(t, err, e) | 
					
						
							|  |  |  | 			if e != nil { | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			e = n.UnmarshalText(o) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			e = n.UnmarshalText([]byte(out)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if newE, ok := newVal.(error); ok { | 
					
						
							| 
									
										
										
										
											2024-01-28 12:41:55 -06:00
										 |  |  | 			assert.ErrorIs(t, e, newE) | 
					
						
							| 
									
										
										
										
											2024-01-27 16:07:27 -06:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			assert.Equal(t, key, n.Key) | 
					
						
							|  |  |  | 			if ti, ok := newVal.(time.Time); ok { | 
					
						
							|  |  |  | 				valT, ok := n.Value.(time.Time) | 
					
						
							|  |  |  | 				if assert.True(t, ok) { | 
					
						
							|  |  |  | 					assert.WithinRange(t, valT, ti.Add(-time.Second), ti.Add(time.Second)) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				assert.Equal(t, newVal, n.Value) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-26 20:00:03 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |