| 133 | } |
| 134 | |
| 135 | func TestJSONUnmarshal(t *testing.T) { |
| 136 | var f Foo |
| 137 | if err := json.Unmarshal([]byte(`{"foo": "abc-def123", "other": 123}`), &f); err != nil { |
| 138 | t.Fatalf("Unmarshal: %v", err) |
| 139 | } |
| 140 | if !f.B.Valid() { |
| 141 | t.Fatal("blobref is nil") |
| 142 | } |
| 143 | if g, e := f.B.String(), "abc-def123"; g != e { |
| 144 | t.Errorf("got %q, want %q", g, e) |
| 145 | } |
| 146 | |
| 147 | f = Foo{} |
| 148 | if err := json.Unmarshal([]byte(`{}`), &f); err != nil { |
| 149 | t.Fatalf("Unmarshal: %v", err) |
| 150 | } |
| 151 | if f.B.Valid() { |
| 152 | t.Fatal("blobref is valid and shouldn't be") |
| 153 | } |
| 154 | |
| 155 | f = Foo{} |
| 156 | if err := json.Unmarshal([]byte(`{"foo":null}`), &f); err != nil { |
| 157 | t.Fatalf("Unmarshal: %v", err) |
| 158 | } |
| 159 | if f.B.Valid() { |
| 160 | t.Fatal("blobref is valid and shouldn't be") |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func TestJSONMarshal(t *testing.T) { |
| 165 | f := &Foo{} |