plain_test.go (6017B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package text_test 19 20 import ( 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/suite" 25 ) 26 27 const ( 28 simple = "this is a plain and simple status" 29 simpleExpected = "<p>this is a plain and simple status</p>" 30 simpleExpectedNoParagraph = "this is a plain and simple status" 31 withTag = "here's a simple status that uses hashtag #welcome!" 32 withTagExpected = "<p>here's a simple status that uses hashtag <a href=\"http://localhost:8080/tags/welcome\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>welcome</span></a>!</p>" 33 withTagExpectedNoParagraph = "here's a simple status that uses hashtag <a href=\"http://localhost:8080/tags/welcome\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>welcome</span></a>!" 34 withHTML = "<div>blah this should just be html escaped blah</div>" 35 withHTMLExpected = "<p><div>blah this should just be html escaped blah</div></p>" 36 moreComplex = "Another test @foss_satan@fossbros-anonymous.io\n\n#Hashtag\n\nText\n\n:rainbow:" 37 moreComplexExpected = "<p>Another test <span class=\"h-card\"><a href=\"http://fossbros-anonymous.io/@foss_satan\" class=\"u-url mention\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">@<span>foss_satan</span></a></span><br><br><a href=\"http://localhost:8080/tags/Hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>Hashtag</span></a><br><br>Text<br><br>:rainbow:</p>" 38 ) 39 40 type PlainTestSuite struct { 41 TextStandardTestSuite 42 } 43 44 func (suite *PlainTestSuite) TestParseSimple() { 45 formatted := suite.FromPlain(simple) 46 suite.Equal(simpleExpected, formatted.HTML) 47 } 48 49 func (suite *PlainTestSuite) TestParseSimpleNoParagraph() { 50 formatted := suite.FromPlainNoParagraph(simple) 51 suite.Equal(simpleExpectedNoParagraph, formatted.HTML) 52 } 53 54 func (suite *PlainTestSuite) TestParseWithTag() { 55 formatted := suite.FromPlain(withTag) 56 suite.Equal(withTagExpected, formatted.HTML) 57 } 58 59 func (suite *PlainTestSuite) TestParseWithTagNoParagraph() { 60 formatted := suite.FromPlainNoParagraph(withTag) 61 suite.Equal(withTagExpectedNoParagraph, formatted.HTML) 62 } 63 64 func (suite *PlainTestSuite) TestParseWithHTML() { 65 formatted := suite.FromPlain(withHTML) 66 suite.Equal(withHTMLExpected, formatted.HTML) 67 } 68 69 func (suite *PlainTestSuite) TestParseMoreComplex() { 70 formatted := suite.FromPlain(moreComplex) 71 suite.Equal(moreComplexExpected, formatted.HTML) 72 } 73 74 func (suite *PlainTestSuite) TestLinkNoMention() { 75 statusText := `here's a link to a post by zork 76 77 https://example.com/@the_mighty_zork/statuses/01FGVP55XMF2K6316MQRX6PFG1 78 79 that link shouldn't come out formatted as a mention!` 80 81 menchies := suite.FromPlain(statusText).Mentions 82 suite.Empty(menchies) 83 } 84 85 func (suite *PlainTestSuite) TestDeriveMentionsEmpty() { 86 statusText := `` 87 menchies := suite.FromPlain(statusText).Mentions 88 assert.Len(suite.T(), menchies, 0) 89 } 90 91 func (suite *PlainTestSuite) TestDeriveHashtagsOK() { 92 statusText := `weeeeeeee #testing123 #also testing 93 94 # testing this one shouldn't work 95 96 #thisshouldwork #dupe #dupe!! #dupe 97 98 here's a link with a fragment: https://example.org/whatever#ahhh 99 here's another link with a fragment: https://example.org/whatever/#ahhh 100 101 (#ThisShouldAlsoWork) #this_should_be_split 102 103 #111111 thisalsoshouldn'twork#### ## 104 105 #alimentación, #saúde, #lävistää, #ö, #네 106 #ThisOneIsThirtyOneCharactersLon... ...ng 107 #ThisOneIsThirteyCharactersLong 108 ` 109 110 tags := suite.FromPlain(statusText).Tags 111 assert.Len(suite.T(), tags, 13) 112 assert.Equal(suite.T(), "testing123", tags[0].Name) 113 assert.Equal(suite.T(), "also", tags[1].Name) 114 assert.Equal(suite.T(), "thisshouldwork", tags[2].Name) 115 assert.Equal(suite.T(), "dupe", tags[3].Name) 116 assert.Equal(suite.T(), "ThisShouldAlsoWork", tags[4].Name) 117 assert.Equal(suite.T(), "this", tags[5].Name) 118 assert.Equal(suite.T(), "111111", tags[6].Name) 119 assert.Equal(suite.T(), "alimentación", tags[7].Name) 120 assert.Equal(suite.T(), "saúde", tags[8].Name) 121 assert.Equal(suite.T(), "lävistää", tags[9].Name) 122 assert.Equal(suite.T(), "ö", tags[10].Name) 123 assert.Equal(suite.T(), "네", tags[11].Name) 124 assert.Equal(suite.T(), "ThisOneIsThirteyCharactersLong", tags[12].Name) 125 126 statusText = `#올빼미 hej` 127 tags = suite.FromPlain(statusText).Tags 128 assert.Equal(suite.T(), "올빼미", tags[0].Name) 129 } 130 131 func (suite *PlainTestSuite) TestDeriveMultiple() { 132 statusText := `Another test @foss_satan@fossbros-anonymous.io 133 134 #Hashtag 135 136 Text` 137 138 f := suite.FromPlain(statusText) 139 140 assert.Len(suite.T(), f.Mentions, 1) 141 assert.Equal(suite.T(), "@foss_satan@fossbros-anonymous.io", f.Mentions[0].NameString) 142 143 assert.Len(suite.T(), f.Tags, 1) 144 assert.Equal(suite.T(), "Hashtag", f.Tags[0].Name) 145 146 assert.Len(suite.T(), f.Emojis, 0) 147 } 148 149 func (suite *PlainTestSuite) TestZalgoHashtag() { 150 statusText := `yo who else loves #praying to #z̸͉̅a̸͚͋l̵͈̊g̸̫͌ỏ̷̪?` 151 f := suite.FromPlain(statusText) 152 assert.Len(suite.T(), f.Tags, 1) 153 assert.Equal(suite.T(), "praying", f.Tags[0].Name) 154 } 155 156 func TestPlainTestSuite(t *testing.T) { 157 suite.Run(t, new(PlainTestSuite)) 158 }