shithub: hell

ref: f34797ac5fc4a7413668238dc612a477bf8aa5c4
dir: /renderer_test.go/

View raw version
package main

import (
	"testing"

	mastodon "codeberg.org/penny64/hellclient-go-mastodon"
)

func TestStatusFormatterStatus(t *testing.T) {
	parameters := []struct {
		post, expected string
	}{
		{"hello world", "hello world"},
		{"<p>hello world</p>", "hello world"},
		{"<p>hello <a href=\"https://example.com\">world</a></p>", "hello (world) [1]"},
		{"<p>hello <a href=\"https://example.com\">world</a> and <a href=\"https://example.org\">universe</a></p>", "hello (world) [1] and (universe) [2]"},
		{"<p>hello &amp; world</p>", "hello & world"},
		{"<p>hello<br>world</p>", "hello\r\nworld"},
		{"<p>hello<br/>world</p>", "hello\r\nworld"},
		{"<p>hello<br />world</p>", "hello\r\nworld"},
		// consequetive NBSP shouldn't be collapsed
		{"<p>hello&nbsp;world</p>", "hello\u00a0world"},
		{"<p>hello&nbsp;&nbsp;world</p>", "hello\u00a0\u00a0world"},
		{"<p>hello&nbsp;&nbsp;&nbsp;world</p>", "hello\u00a0\u00a0\u00a0world"},
		{"<p>hello<br>&nbsp;world</p>", "hello\r\n\u00a0world"},
		{"<p>hello<br>&nbsp;&nbsp;world</p>", "hello\r\n\u00a0\u00a0world"},
		{"<p>hello<br>&nbsp;&nbsp;&nbsp;world</p>", "hello\r\n\u00a0\u00a0\u00a0world"},
		// <pre> tests
		{"<pre>hello world</pre>", "hello world"},
		{"<pre>hello\nworld</pre>", "hello\nworld"},
		{"<pre>hello <a href=\"https://example.com\">world</a></pre>", "hello (world) [1]"},
		{"<pre>hello &amp; world</pre>", "hello & world"},
		// Mentions
		{"<p>hello <a href=\"https://example.com\" class=\"mention\">username</a> world</p>", "hello username world"},
		// a without href
		{"<p>hello<a>world</a></p>", "hello"},
	}
	for _, param := range parameters {
		sf := &StatusFormatter{
			status: &mastodon.Status{
				Content: param.post,
			},
			postContext: &postref{ref: "1", urlmap: make(map[string][]string)},
		}
		rendered := sf.statusContent(sf.status)
		if rendered != param.expected {
			t.Errorf("Expected rendered content to be '%s', got '%s'", param.expected, rendered)
		}
	}
}