|
| 1 | +package null |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +var ( |
| 9 | + stringJSON = []byte(`"test"`) |
| 10 | + nullStringJSON = []byte(`{"String":"test","Valid":true}`) |
| 11 | + nullJSON = []byte(`null`) |
| 12 | +) |
| 13 | + |
| 14 | +func TestStringFrom(t *testing.T) { |
| 15 | + str := From("test") |
| 16 | + assert(t, str, "From() string") |
| 17 | + |
| 18 | + null := From("") |
| 19 | + assertNull(t, null, "From() empty string") |
| 20 | +} |
| 21 | + |
| 22 | +func TestUnmarshalString(t *testing.T) { |
| 23 | + var str String |
| 24 | + err := json.Unmarshal(stringJSON, &str) |
| 25 | + maybePanic(err) |
| 26 | + assert(t, str, "string json") |
| 27 | + |
| 28 | + var ns String |
| 29 | + err = json.Unmarshal(nullStringJSON, &ns) |
| 30 | + maybePanic(err) |
| 31 | + assert(t, ns, "null string object json") |
| 32 | + |
| 33 | + var null String |
| 34 | + err = json.Unmarshal(nullJSON, &null) |
| 35 | + maybePanic(err) |
| 36 | + assertNull(t, null, "null json") |
| 37 | +} |
| 38 | + |
| 39 | +func TestMarshalString(t *testing.T) { |
| 40 | + str := From("test") |
| 41 | + data, err := json.Marshal(str) |
| 42 | + maybePanic(err) |
| 43 | + assertJSONEquals(t, data, `"test"`, "non-empty json marshal") |
| 44 | + |
| 45 | + // invalid values should be encoded as an empty string |
| 46 | + null := From("") |
| 47 | + data, err = json.Marshal(null) |
| 48 | + maybePanic(err) |
| 49 | + assertJSONEquals(t, data, `""`, "non-empty json marshal") |
| 50 | +} |
| 51 | + |
| 52 | +func TestPointer(t *testing.T) { |
| 53 | + str := From("test") |
| 54 | + ptr := str.Pointer() |
| 55 | + if *ptr != "test" { |
| 56 | + t.Errorf("bad %s string: %#v ≠ %s\n", "pointer", ptr, "test") |
| 57 | + } |
| 58 | + |
| 59 | + null := From("") |
| 60 | + ptr = null.Pointer() |
| 61 | + if ptr != nil { |
| 62 | + t.Errorf("bad %s: %#v ≠ %s\n", "nil pointer", ptr, "nil") |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestScan(t *testing.T) { |
| 67 | + var str String |
| 68 | + err := str.Scan("test") |
| 69 | + maybePanic(err) |
| 70 | + assert(t, str, "scanned string") |
| 71 | + |
| 72 | + var null String |
| 73 | + err = null.Scan(nil) |
| 74 | + maybePanic(err) |
| 75 | + assertNull(t, null, "scanned null") |
| 76 | +} |
| 77 | + |
| 78 | +func maybePanic(err error) { |
| 79 | + if err != nil { |
| 80 | + panic(err) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func assert(t *testing.T, s String, from string) { |
| 85 | + if s.String != "test" { |
| 86 | + t.Errorf("bad %s string: %s ≠ %s\n", from, s.String, "test") |
| 87 | + } |
| 88 | + if !s.Valid { |
| 89 | + t.Error(from, "is invalid, but should be valid") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func assertNull(t *testing.T, s String, from string) { |
| 94 | + if s.Valid { |
| 95 | + t.Error(from, "is valid, but should be invalid") |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func assertJSONEquals(t *testing.T, data []byte, cmp string, from string) { |
| 100 | + if string(data) != cmp { |
| 101 | + t.Errorf("bad %s data: %s ≠ %s\n", from, data, cmp) |
| 102 | + } |
| 103 | +} |
0 commit comments