@@ -272,23 +272,79 @@ func TestAddImageFromBytes(t *testing.T) {
272272}
273273
274274func TestParseLinks (t * testing.T ) {
275- content := "Hello https://go.dev and https://pkg.go.dev!"
276- pb := NewPostBuilder (content )
277- pb .parseLinks ()
278- if len (pb .facets ) != 2 {
279- t .Errorf ("wanted 2 facets, got %d" , len (pb .facets ))
280- }
281- if pb .facets [0 ].Index .ByteStart != 6 || pb .facets [0 ].Index .ByteEnd != 20 {
282- t .Errorf ("wanted first facet index [6,20], got [%d,%d]" , pb .facets [0 ].Index .ByteStart , pb .facets [0 ].Index .ByteEnd )
283- }
284- if pb .facets [0 ].Features [0 ].Uri != "https://go.dev" {
285- t .Errorf ("wanted first facet URI 'https://go.dev', got '%s'" , pb .facets [0 ].Features [0 ].Uri )
286- }
287- if pb .facets [1 ].Index .ByteStart != 25 || pb .facets [1 ].Index .ByteEnd != 43 {
288- t .Errorf ("wanted second facet index [25,43], got [%d,%d]" , pb .facets [1 ].Index .ByteStart , pb .facets [1 ].Index .ByteEnd )
289- }
290- if pb .facets [1 ].Features [0 ].Uri != "https://pkg.go.dev" {
291- t .Errorf ("wanted second facet URI 'https://pkg.go.dev', got '%s'" , pb .facets [1 ].Features [0 ].Uri )
275+ tests := []struct {
276+ name string
277+ content string
278+ expectedFacets []struct {
279+ ByteStart int
280+ ByteEnd int
281+ Uri string
282+ }
283+ }{
284+ {
285+ name : "Two links" ,
286+ content : "Hello https://go.dev and https://pkg.go.dev!" ,
287+ expectedFacets : []struct {
288+ ByteStart int
289+ ByteEnd int
290+ Uri string
291+ }{
292+ {ByteStart : 6 , ByteEnd : 20 , Uri : "https://go.dev" },
293+ {ByteStart : 25 , ByteEnd : 43 , Uri : "https://pkg.go.dev" },
294+ },
295+ },
296+ {
297+ name : "No links" ,
298+ content : "Hello world!" ,
299+ expectedFacets : []struct {
300+ ByteStart int
301+ ByteEnd int
302+ Uri string
303+ }{},
304+ },
305+ {
306+ name : "One link at start" ,
307+ content : "https://go.dev Hello world!" ,
308+ expectedFacets : []struct {
309+ ByteStart int
310+ ByteEnd int
311+ Uri string
312+ }{
313+ {ByteStart : 0 , ByteEnd : 14 , Uri : "https://go.dev" },
314+ },
315+ },
316+ {
317+ name : "One link at end" ,
318+ content : "Hello world! https://go.dev" ,
319+ expectedFacets : []struct {
320+ ByteStart int
321+ ByteEnd int
322+ Uri string
323+ }{
324+ {ByteStart : 13 , ByteEnd : 27 , Uri : "https://go.dev" },
325+ },
326+ },
327+ }
328+
329+ for _ , tt := range tests {
330+ t .Run (tt .name , func (t * testing.T ) {
331+ pb := NewPostBuilder (tt .content )
332+ pb .parseLinks ()
333+
334+ if len (pb .facets ) != len (tt .expectedFacets ) {
335+ t .Errorf ("wanted %d facets, got %d" , len (tt .expectedFacets ), len (pb .facets ))
336+ return
337+ }
338+
339+ for i , expected := range tt .expectedFacets {
340+ if pb .facets [i ].Index .ByteStart != expected .ByteStart || pb .facets [i ].Index .ByteEnd != expected .ByteEnd {
341+ t .Errorf ("facet %d: wanted index [%d,%d], got [%d,%d]" , i , expected .ByteStart , expected .ByteEnd , pb .facets [i ].Index .ByteStart , pb .facets [i ].Index .ByteEnd )
342+ }
343+ if pb .facets [i ].Features [0 ].Uri != expected .Uri {
344+ t .Errorf ("facet %d: wanted URI '%s', got '%s'" , i , expected .Uri , pb .facets [i ].Features [0 ].Uri )
345+ }
346+ }
347+ })
292348 }
293349}
294350
0 commit comments