package deadlinks import ( "bytes" "fmt" "strings" "testing" "github.com/stretchr/testify/assert" ) func TestParser(t *testing.T) { t.Parallel() parser := NewParser() tests := []struct { mimeType string body string wantURLs []URL wantErrs []string }{ { "image/jpg", "ANYTHING", nil, nil, }, { "text/gemini", ``, nil, nil, }, { "text/gemini", ` # HEADER => https://foo.com some link => empty/path => /foo/bar here's an absolute path => what.com a domain? ok here's some text `, []URL{ "https://foo.com", "empty/path", "/foo/bar", "what.com", }, nil, }, { "text/gemini", ` # HEADER => https://foo.com some link => empty/path => /foo/bar here's an absolute path => what.com a domain? ok here's some text `, []URL{ "https://foo.com", "empty/path", "/foo/bar", "what.com", }, nil, }, { "text/gemini", ` => : NO FISH ALLOWED => /good/dog `, []URL{"/good/dog"}, []string{ `parsing URL from line "=> : NO FISH ALLOWED\n": parse ":": missing protocol scheme`, }, }, } for i := range tests { test := tests[i] name := fmt.Sprintf( "%d-%s", i, strings.ReplaceAll(test.mimeType, "/", "_"), ) t.Run(name, func(t *testing.T) { t.Parallel() body := bytes.NewBufferString(test.body) gotURLs, gotErr := parser.Parse(test.mimeType, body) assert.Equal(t, test.wantURLs, gotURLs) if len(test.wantErrs) == 0 { assert.NoError(t, gotErr) return } type joinedErr interface { Unwrap() []error } var gotErrs []error if joinedErr, ok := gotErr.(joinedErr); ok { gotErrs = joinedErr.Unwrap() } else if gotErr != nil { gotErrs = []error{gotErr} } gotErrStrs := make([]string, len(gotErrs)) for i := range gotErrs { gotErrStrs[i] = gotErrs[i].Error() } assert.Equal(t, test.wantErrs, gotErrStrs) }) } }