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