-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
machine_test.go
68 lines (58 loc) · 1.91 KB
/
machine_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package urn
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDefaultParsingMode(t *testing.T) {
m := NewMachine()
require.NotNil(t, m)
require.IsType(t, &machine{}, m)
require.Equal(t, DefaultParsingMode, m.(*machine).parsingMode)
}
func exec(t *testing.T, testCases []testCase, mode ParsingMode) {
t.Helper()
m := NewMachine(WithParsingMode(mode))
for ii, tt := range testCases {
urn, err := m.Parse([]byte(tt.in))
ok := err == nil
if ok {
assert.True(t, tt.ok, herror(ii, tt))
assert.Equal(t, tt.obj.prefix, urn.prefix, herror(ii, tt))
assert.Equal(t, tt.obj.ID, urn.ID, herror(ii, tt))
assert.Equal(t, tt.obj.SS, urn.SS, herror(ii, tt))
assert.Equal(t, tt.str, urn.String(), herror(ii, tt))
assert.Equal(t, tt.norm, urn.Normalize().String(), herror(ii, tt))
if mode == RFC8141Only {
assert.Equal(t, tt.obj.rComponent, urn.rComponent, herror(ii, tt))
assert.Equal(t, tt.obj.rComponent, urn.rComponent, herror(ii, tt))
assert.Equal(t, tt.obj.rComponent, urn.rComponent, herror(ii, tt))
assert.Equal(t, urn.RFC(), RFC8141, herror(ii, tt))
}
if mode == RFC7643Only {
assert.Equal(t, tt.isSCIM, urn.IsSCIM(), herror(ii, tt))
}
} else {
assert.False(t, tt.ok, herror(ii, tt))
assert.Empty(t, urn, herror(ii, tt))
assert.Equal(t, tt.estr, err.Error(), herror(ii, tt))
assert.Equal(t, tt.estr, m.Error().Error(), herror(ii, tt))
}
}
}
func TestParseDefaultMode(t *testing.T) {
require.Equal(t, RFC2141Only, DefaultParsingMode)
exec(t, urn2141OnlyTestCases, DefaultParsingMode)
}
func TestParse2141Only(t *testing.T) {
exec(t, urn2141OnlyTestCases, RFC2141Only)
}
func TestParseUrnLex2141Only(t *testing.T) {
exec(t, urnlexTestCases, RFC2141Only)
}
func TestSCIMOnly(t *testing.T) {
exec(t, scimOnlyTestCases, RFC7643Only)
}
func TestParse8141Only(t *testing.T) {
exec(t, rfc8141TestCases, RFC8141Only)
}