|
| 1 | +package urn |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestURN8141JSONMarshaling(t *testing.T) { |
| 12 | + t.Run("roundtrip", func(t *testing.T) { |
| 13 | + // Marshal |
| 14 | + expected := URN8141{ |
| 15 | + URN: &URN{ |
| 16 | + ID: "lex", |
| 17 | + SS: "it:ministero.giustizia:decreto:1992-07-24;358~art5", |
| 18 | + rComponent: "r", |
| 19 | + qComponent: "q%D0", |
| 20 | + fComponent: "frag", |
| 21 | + }, |
| 22 | + } |
| 23 | + bytes, err := json.Marshal(expected) |
| 24 | + if !assert.NoError(t, err) { |
| 25 | + return |
| 26 | + } |
| 27 | + require.Equal(t, `"urn:lex:it:ministero.giustizia:decreto:1992-07-24;358~art5?+r?=q%D0#frag"`, string(bytes)) |
| 28 | + // Unmarshal |
| 29 | + var got URN8141 |
| 30 | + err = json.Unmarshal(bytes, &got) |
| 31 | + if !assert.NoError(t, err) { |
| 32 | + return |
| 33 | + } |
| 34 | + assert.Equal(t, expected.String(), got.String()) |
| 35 | + assert.Equal(t, expected.fComponent, got.FComponent()) |
| 36 | + assert.Equal(t, expected.qComponent, got.QComponent()) |
| 37 | + assert.Equal(t, expected.rComponent, got.RComponent()) |
| 38 | + }) |
| 39 | + |
| 40 | + t.Run("invalid URN", func(t *testing.T) { |
| 41 | + var actual URN8141 |
| 42 | + err := json.Unmarshal([]byte(`"not a URN"`), &actual) |
| 43 | + assert.EqualError(t, err, "invalid URN per RFC 8141: not a URN") |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("empty", func(t *testing.T) { |
| 47 | + var actual URN8141 |
| 48 | + err := actual.UnmarshalJSON(nil) |
| 49 | + assert.EqualError(t, err, "unexpected end of JSON input") |
| 50 | + }) |
| 51 | +} |
0 commit comments