Files
upstream-mattermost/server/public/shared/markdown/emoji_test.go
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

204 lines
6.3 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package markdown
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseEmoji(t *testing.T) {
for name, tc := range map[string]struct {
Input string
Position int
ExpectedOk bool
ExpectedPosition int
ExpectedEmojiName string
}{
"just a colon": {
Input: ":",
Position: 0,
ExpectedOk: false,
ExpectedPosition: 0,
},
"no closing colon": {
Input: ":emoji",
Position: 0,
ExpectedOk: false,
ExpectedPosition: 0,
},
"no closing colon before whitespace": {
Input: ":emoji example",
Position: 0,
ExpectedOk: false,
ExpectedPosition: 0,
},
"valid emoji": {
Input: ":emoji:",
Position: 0,
ExpectedOk: true,
ExpectedPosition: 7,
ExpectedEmojiName: "emoji",
},
"valid emoji with punctuation": {
Input: ":valid-emoji:",
Position: 0,
ExpectedOk: true,
ExpectedPosition: 13,
ExpectedEmojiName: "valid-emoji",
},
"valid emoji with text before": {
Input: "this is an :emoji:",
Position: 11,
ExpectedOk: true,
ExpectedPosition: 18,
ExpectedEmojiName: "emoji",
},
"invalid emoji with text before": {
Input: "this is not an :emoji",
Position: 15,
ExpectedOk: false,
ExpectedPosition: 15,
},
"valid emoji with text after": {
Input: ":emoji: before some text",
Position: 0,
ExpectedOk: true,
ExpectedPosition: 7,
ExpectedEmojiName: "emoji",
},
"valid emoji with text before and after": {
Input: "this is an :emoji: in a sentence",
Position: 11,
ExpectedOk: true,
ExpectedPosition: 18,
ExpectedEmojiName: "emoji",
},
"multiple emojis 1": {
Input: ":multiple: :emojis:",
Position: 0,
ExpectedOk: true,
ExpectedPosition: 10,
ExpectedEmojiName: "multiple",
},
"multiple emojis 2": {
Input: ":multiple: :emojis:",
Position: 11,
ExpectedOk: true,
ExpectedPosition: 19,
ExpectedEmojiName: "emojis",
},
} {
t.Run(name, func(t *testing.T) {
p := newInlineParser(tc.Input, []Range{}, []*ReferenceDefinition{})
p.raw = tc.Input
p.position = tc.Position
ok := p.parseEmoji()
assert.Equal(t, tc.ExpectedOk, ok)
assert.Equal(t, tc.ExpectedPosition, p.position)
if tc.ExpectedOk {
require.True(t, len(p.inlines) > 0)
require.IsType(t, &Emoji{}, p.inlines[len(p.inlines)-1])
assert.Equal(t, tc.ExpectedEmojiName, p.inlines[len(p.inlines)-1].(*Emoji).Name)
}
})
}
}
func TestParseEmojiFull(t *testing.T) {
// These tests are based on https://github.com/mattermost/commonmark.js/blob/master/test/mattermost.txt
for name, tc := range map[string]struct {
Markdown string
ExpectedHTML string
}{
// Valid emojis
"emoji": {
Markdown: "This is an :emoji:",
ExpectedHTML: `<p>This is an <span data-emoji-name="emoji" data-literal=":emoji:" /></p>`,
},
"emoji with underscore": {
Markdown: "This is an :emo_ji:",
ExpectedHTML: `<p>This is an <span data-emoji-name="emo_ji" data-literal=":emo_ji:" /></p>`,
},
"emoji with hyphen": {
Markdown: "This is an :emo-ji:",
ExpectedHTML: `<p>This is an <span data-emoji-name="emo-ji" data-literal=":emo-ji:" /></p>`,
},
"emoji with numbers": {
Markdown: "This is an :emoji123:",
ExpectedHTML: `<p>This is an <span data-emoji-name="emoji123" data-literal=":emoji123:" /></p>`,
},
"emoji in brackets": {
Markdown: "This is an (:emoji:)",
ExpectedHTML: `<p>This is an (<span data-emoji-name="emoji" data-literal=":emoji:" />)</p>`,
},
"two emojis without space between": {
Markdown: "These are some :emoji1::emoji2:",
ExpectedHTML: `<p>These are some <span data-emoji-name="emoji1" data-literal=":emoji1:" /><span data-emoji-name="emoji2" data-literal=":emoji2:" /></p>`,
},
"two emojis separated by a slash": {
Markdown: "These are some :emoji1:/:emoji2:",
ExpectedHTML: `<p>These are some <span data-emoji-name="emoji1" data-literal=":emoji1:" />/<span data-emoji-name="emoji2" data-literal=":emoji2:" /></p>`,
},
"+1 emoji": {
Markdown: "This is an :+1:",
ExpectedHTML: `<p>This is an <span data-emoji-name="+1" data-literal=":+1:" /></p>`,
},
"-1 emoji": {
Markdown: "This is an :-1:",
ExpectedHTML: `<p>This is an <span data-emoji-name="-1" data-literal=":-1:" /></p>`,
},
"emoji with surrounding words": {
Markdown: "This is an :emoji: in a sentence.",
ExpectedHTML: `<p>This is an <span data-emoji-name="emoji" data-literal=":emoji:" /> in a sentence.</p>`,
},
// Invalid emojis
"incomplete emoji 1": {
Markdown: "This is not an :emoji",
ExpectedHTML: `<p>This is not an :emoji</p>`,
},
"incomplete emoji 2": {
Markdown: "This is not an emoji:",
ExpectedHTML: `<p>This is not an emoji:</p>`,
},
"invalid emoji with whitespace": {
Markdown: "This is not an :emo ji:",
ExpectedHTML: `<p>This is not an :emo ji:</p>`,
},
"invalid emoji with other punctuation": {
Markdown: "This is not an :emo'ji:",
ExpectedHTML: `<p>This is not an :emo'ji:</p>`,
},
"invalid emoji due to adjacent text 1": {
Markdown: "Thisisnotan:emoji:",
// This differs slightly from our commonmark.js implementation because it doesn't require :// when autolinking
ExpectedHTML: `<p>Thisisnotan:emoji:</p>`,
},
"invalid emoji due to adjacent text 2": {
Markdown: "This is not an :emoji:isit",
// This differs slightly from our commonmark.js implementation because it doesn't require :// when autolinking
ExpectedHTML: `<p>This is not an :emoji:isit</p>`,
},
"invalid emoji due to adjacent text 3": {
Markdown: "This is not an:emoji:isit",
// This differs slightly from our commonmark.js implementation because it doesn't require :// when autolinking
ExpectedHTML: `<p>This is not an:emoji:isit</p>`,
},
} {
t.Run(name, func(t *testing.T) {
actual := RenderHTML(tc.Markdown)
assert.Equal(t, tc.ExpectedHTML, actual)
})
}
}