github.com/storacha/go-ucanto@v0.7.2/core/schema/uri_test.go (about) 1 package schema_test 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 "github.com/storacha/go-ucanto/core/schema" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestURIDefaultReader(t *testing.T) { 13 testCases := []struct { 14 source string 15 output string 16 errMatch *regexp.Regexp 17 }{ 18 { 19 source: "", 20 errMatch: regexp.MustCompile("Invalid URI"), 21 }, 22 { 23 source: "did:key:zAlice", 24 output: "did:key:zAlice", 25 }, 26 { 27 source: "mailto:alice@mail.net", 28 output: "mailto:alice@mail.net", 29 }, 30 } 31 32 for _, testCase := range testCases { 33 t.Run(fmt.Sprintf("schema.URI.read(%s)", testCase.source), func(t *testing.T) { 34 output, err := schema.URI().Read(testCase.source) 35 if testCase.errMatch == nil { 36 require.NoError(t, err) 37 require.Equal(t, testCase.output, output.String()) 38 } else { 39 require.Regexp(t, testCase.errMatch, err.Error()) 40 } 41 }) 42 } 43 } 44 45 func TestURIReaderWithProtocol(t *testing.T) { 46 testCases := []struct { 47 source any 48 protocol string 49 output string 50 errMatch *regexp.Regexp 51 }{ 52 {nil, "did:", "", regexp.MustCompile("Expected URI but got <nil>")}, 53 {"", "did:", "", regexp.MustCompile("Invalid URI")}, 54 {"did:key:zAlice", "did:", "did:key:zAlice", nil}, 55 {"did:key:zAlice", "mailto:", "", regexp.MustCompile("Expected mailto: URI instead got did:key:zAlice")}, 56 {"mailto:alice@mail.net", "mailto:", "mailto:alice@mail.net", nil}, 57 {"mailto:alice@mail.net", "did:", "", regexp.MustCompile("Expected did: URI instead got mailto:alice@mail.net")}, 58 } 59 60 for _, testCase := range testCases { 61 t.Run(fmt.Sprintf("schema.URI(WithProtocol(%s)).read(%s)", testCase.protocol, testCase.source), func(t *testing.T) { 62 output, err := schema.URI(schema.WithProtocol((testCase.protocol))).Read(testCase.source) 63 if testCase.errMatch == nil { 64 require.NoError(t, err) 65 require.Equal(t, testCase.output, output.String()) 66 } else { 67 require.Regexp(t, testCase.errMatch, err.Error()) 68 } 69 }) 70 } 71 // for (const [input, protocol, expect] of dataset) { 72 // test(`URI.match(${JSON.stringify({ 73 // protocol, 74 // })}).read(${JSON.stringify(input)})}}`, () => { 75 // matchResult(URI.match({ protocol }).read(input), expect) 76 // }) 77 // } 78 }