Skip to content

Commit d725923

Browse files
committed
test: check custom (un)marshalling for RFC 8141 URNs
Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
1 parent ebf11e5 commit d725923

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

urn8141_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)