1 package urn
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 "github.com/stretchr/testify/require"
8 )
9
10 func TestDefaultParsingMode(t *testing.T) {
11 m := NewMachine()
12 require.NotNil(t, m)
13 require.IsType(t, &machine{}, m)
14 require.Equal(t, DefaultParsingMode, m.(*machine).parsingMode)
15 }
16
17 func exec(t *testing.T, testCases []testCase, mode ParsingMode) {
18 t.Helper()
19 m := NewMachine(WithParsingMode(mode))
20 for ii, tt := range testCases {
21 urn, err := m.Parse([]byte(tt.in))
22 ok := err == nil
23
24 if ok {
25 assert.True(t, tt.ok, herror(ii, tt))
26 assert.Equal(t, tt.obj.prefix, urn.prefix, herror(ii, tt))
27 assert.Equal(t, tt.obj.ID, urn.ID, herror(ii, tt))
28 assert.Equal(t, tt.obj.SS, urn.SS, herror(ii, tt))
29 assert.Equal(t, tt.str, urn.String(), herror(ii, tt))
30 assert.Equal(t, tt.norm, urn.Normalize().String(), herror(ii, tt))
31 if mode == RFC8141Only {
32 assert.Equal(t, tt.obj.rComponent, urn.rComponent, herror(ii, tt))
33 assert.Equal(t, tt.obj.rComponent, urn.rComponent, herror(ii, tt))
34 assert.Equal(t, tt.obj.rComponent, urn.rComponent, herror(ii, tt))
35 assert.Equal(t, urn.RFC(), RFC8141, herror(ii, tt))
36 }
37 if mode == RFC7643Only {
38 assert.Equal(t, tt.isSCIM, urn.IsSCIM(), herror(ii, tt))
39 }
40 } else {
41 assert.False(t, tt.ok, herror(ii, tt))
42 assert.Empty(t, urn, herror(ii, tt))
43 assert.Equal(t, tt.estr, err.Error(), herror(ii, tt))
44 assert.Equal(t, tt.estr, m.Error().Error(), herror(ii, tt))
45 }
46 }
47 }
48
49 func TestParseDefaultMode(t *testing.T) {
50 require.Equal(t, RFC2141Only, DefaultParsingMode)
51 exec(t, urn2141OnlyTestCases, DefaultParsingMode)
52 }
53
54 func TestParse2141Only(t *testing.T) {
55 exec(t, urn2141OnlyTestCases, RFC2141Only)
56 }
57
58 func TestParseUrnLex2141Only(t *testing.T) {
59 exec(t, urnlexTestCases, RFC2141Only)
60 }
61
62 func TestSCIMOnly(t *testing.T) {
63 exec(t, scimOnlyTestCases, RFC7643Only)
64 }
65
66 func TestParse8141Only(t *testing.T) {
67 exec(t, rfc8141TestCases, RFC8141Only)
68 }
69
View as plain text