Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
package parser
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func emailMatch(start, end int, address string) GFMEmailMatch {
return GFMEmailMatch{Start: start, End: end, Address: []byte(address)}
}
func TestFindGFMEmailMatches(t *testing.T) {
tests := []struct {
name string
source string
expected []GFMEmailMatch
}{
{name: "basic", source: "[email protected]", expected: []GFMEmailMatch{emailMatch(0, 16, "[email protected]")}},
{name: "slash starts a new local part", source: "#foo/[email protected]", expected: []GFMEmailMatch{emailMatch(5, 24, "[email protected]")}},
{name: "punctuation starts a new local part", source: "#[email protected]", expected: []GFMEmailMatch{emailMatch(5, 20, "[email protected]")}},
{name: "hash starts a text-node email", source: "#[email protected]", expected: []GFMEmailMatch{emailMatch(1, 16, "[email protected]")}},
{name: "Unicode starts a new local part", source: "#中[email protected]", expected: []GFMEmailMatch{emailMatch(4, 19, "[email protected]")}},
{name: "emoji starts a new local part", source: "😀[email protected]", expected: []GFMEmailMatch{emailMatch(4, 19, "[email protected]")}},
{name: "trailing dot excluded", source: "[email protected].", expected: []GFMEmailMatch{emailMatch(0, 16, "[email protected]")}},
{name: "invalid domain suffix", source: "[email protected]_", expected: nil},
{name: "multiple", source: "[email protected] [email protected]", expected: []GFMEmailMatch{emailMatch(0, 6, "[email protected]"), emailMatch(7, 13, "[email protected]")}},
{name: "first email wins", source: "[email protected]@baz.example", expected: []GFMEmailMatch{emailMatch(0, 11, "[email protected]")}},
{
name: "adjacent emails do not overlap",
source: "[email protected][email protected]",
expected: []GFMEmailMatch{emailMatch(0, 11, "[email protected]"), emailMatch(11, 23, "[email protected]")},
},
{name: "escaped local plus", source: `#foo\[email protected]`, expected: []GFMEmailMatch{emailMatch(1, 21, "[email protected]")}},
{name: "escaped local underscore", source: `#next\[email protected]`, expected: []GFMEmailMatch{emailMatch(1, 23, "[email protected]")}},
{name: "escaped at and domain dot", source: `#foo\@example\.com`, expected: []GFMEmailMatch{emailMatch(1, 18, "[email protected]")}},
{name: "numeric dot reference", source: `#foo.[email protected]`, expected: []GFMEmailMatch{emailMatch(1, 24, "[email protected]")}},
{name: "named dot reference", source: `#foo.[email protected]`, expected: []GFMEmailMatch{emailMatch(1, 27, "[email protected]")}},
{name: "numeric at reference", source: `#foo@example.com`, expected: []GFMEmailMatch{emailMatch(1, 20, "[email protected]")}},
{name: "unknown reference stays literal", source: `#foo&bogus;[email protected]`, expected: []GFMEmailMatch{emailMatch(11, 26, "[email protected]")}},
{name: "allowed delimiter", source: "([email protected]", expected: []GFMEmailMatch{emailMatch(1, 17, "[email protected]")}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, FindGFMEmailMatches([]byte(test.source)))
})
}
}
func TestMatchGFMEmailAtPreservesEscapedSourceRange(t *testing.T) {
source := []byte("[email protected]\n#foo\\[email protected]")
lowerBound := bytes.IndexByte(source, '\n') + 1
at := bytes.LastIndexByte(source, '@')
start, end, ok := MatchGFMEmailAt(source, at, lowerBound, len(source))
require.True(t, ok)
assert.Equal(t, lowerBound+1, start)
assert.Equal(t, len(source), end)
assert.Equal(t, `foo\[email protected]`, string(source[start:end]))
}