diff --git a/posts_test.go b/posts_test.go index 612c178..7b13065 100644 --- a/posts_test.go +++ b/posts_test.go @@ -1,45 +1,93 @@ /* * Copyright © 2020-2021 Musing Studio LLC. * * This file is part of WriteFreely. * * WriteFreely is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License, included * in the LICENSE file in this source code package. */ -package writefreely_test +package writefreely import ( "testing" "github.com/guregu/null/zero" "github.com/stretchr/testify/assert" - "github.com/writefreely/writefreely" ) func TestPostSummary(t *testing.T) { testCases := map[string]struct { - given writefreely.Post + given Post expected string }{ "no special chars": {givenPost("Content."), "Content."}, "HTML content": {givenPost("Content
with a
paragraph."), "Content with a paragraph."}, "content with escaped char": {givenPost("Content's all OK."), "Content's all OK."}, "multiline content": {givenPost(`Content in multiple lines.`), "Content in multiple lines."}, } for name, test := range testCases { t.Run(name, func(t *testing.T) { actual := test.given.Summary() assert.Equal(t, test.expected, actual) }) } } -func givenPost(content string) writefreely.Post { - return writefreely.Post{Title: zero.StringFrom("Title"), Content: content} +func givenPost(content string) Post { + return Post{Title: zero.StringFrom("Title"), Content: content} +} + +func TestExtractImageAltText(t *testing.T) { + testCases := map[string]struct { + content string + expected map[string]string + }{ + "basic image": { + "", + map[string]string{"https://example.com/cat.png": "a cat"}, + }, + "image with title": { + ``, + map[string]string{"https://example.com/cat.png": "a cat"}, + }, + "empty alt text is omitted": { + "", + map[string]string{}, + }, + "whitespace-only alt text is omitted": { + "", + map[string]string{}, + }, + "alt text is trimmed": { + "", + map[string]string{"https://example.com/cat.png": "a cat"}, + }, + "multiple images": { + " and ", + map[string]string{ + "https://example.com/cat.png": "cat", + "https://example.com/dog.jpg": "dog", + }, + }, + "bare url has no alt text": { + "See https://example.com/cat.png", + map[string]string{}, + }, + "non-image markdown link ignored": { + "[a link](https://example.com/page)", + map[string]string{}, + }, + } + + for name, test := range testCases { + t.Run(name, func(t *testing.T) { + assert.Equal(t, test.expected, extractImageAltText(test.content)) + }) + } }