summaryrefslogtreecommitdiffstats
path: root/modules/highlight/highlight_test.go
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2022-07-30 21:17:43 +0200
committerGitHub <noreply@github.com>2022-07-30 21:17:43 +0200
commitae3dde1c873320abba49c0cf58c6c87c78cfe334 (patch)
treeddd3daded843a229e23f0cabbee244da7a4ed249 /modules/highlight/highlight_test.go
parent0e61a74e5a3fc14ff26d1c85065ba336d3f3994b (diff)
downloadgitea-ae3dde1c873320abba49c0cf58c6c87c78cfe334.tar.gz
gitea-ae3dde1c873320abba49c0cf58c6c87c78cfe334.zip
Rework file highlight rendering and fix yaml copy-paste (#19967)
* Rework file highlight rendering and fix yaml copy-paste * use Split+Trim to replace tag parser * remove unnecessary bytes.Count * remove newLineInHTML = "&#10;" Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'modules/highlight/highlight_test.go')
-rw-r--r--modules/highlight/highlight_test.go197
1 files changed, 123 insertions, 74 deletions
diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go
index e5dfedd2b3..8f83f4a2f6 100644
--- a/modules/highlight/highlight_test.go
+++ b/modules/highlight/highlight_test.go
@@ -8,97 +8,146 @@ import (
"strings"
"testing"
- "code.gitea.io/gitea/modules/setting"
- "code.gitea.io/gitea/modules/util"
-
"github.com/stretchr/testify/assert"
- "gopkg.in/ini.v1"
)
+func lines(s string) []string {
+ return strings.Split(strings.ReplaceAll(strings.TrimSpace(s), `\n`, "\n"), "\n")
+}
+
func TestFile(t *testing.T) {
- setting.Cfg = ini.Empty()
tests := []struct {
- name string
- numLines int
- fileName string
- code string
- want string
+ name string
+ code string
+ want []string
}{
{
- name: ".drone.yml",
- numLines: 12,
- fileName: ".drone.yml",
- code: util.Dedent(`
- kind: pipeline
- name: default
+ name: "empty.py",
+ code: "",
+ want: lines(""),
+ },
+ {
+ name: "tags.txt",
+ code: "<>",
+ want: lines("&lt;&gt;"),
+ },
+ {
+ name: "tags.py",
+ code: "<>",
+ want: lines(`<span class="o">&lt;</span><span class="o">&gt;</span>`),
+ },
+ {
+ name: "eol-no.py",
+ code: "a=1",
+ want: lines(`<span class="n">a</span><span class="o">=</span><span class="mi">1</span>`),
+ },
+ {
+ name: "eol-newline1.py",
+ code: "a=1\n",
+ want: lines(`<span class="n">a</span><span class="o">=</span><span class="mi">1</span>\n`),
+ },
+ {
+ name: "eol-newline2.py",
+ code: "a=1\n\n",
+ want: lines(`
+<span class="n">a</span><span class="o">=</span><span class="mi">1</span>\n
+\n
+ `,
+ ),
+ },
+ {
+ name: "empty-line-with-space.py",
+ code: strings.ReplaceAll(strings.TrimSpace(`
+def:
+ a=1
- steps:
- - name: test
- image: golang:1.13
- environment:
- GOPROXY: https://goproxy.cn
- commands:
- - go get -u
- - go build -v
- - go test -v -race -coverprofile=coverage.txt -covermode=atomic
- `),
- want: util.Dedent(`
- <span class="line"><span class="cl"><span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>
- </span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default</span>
- </span></span><span class="line"><span class="cl">
- </span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">steps</span><span class="p">:</span>
- </span></span><span class="line"><span class="cl"><span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span>
- </span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span>
- </span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">environment</span><span class="p">:</span>
- </span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span>
- </span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">commands</span><span class="p">:</span>
- </span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>
- </span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go build -v</span>
- </span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span></span></span>
+b=''
+{space}
+c=2
+ `), "{space}", " "),
+ want: lines(`
+<span class="n">def</span><span class="p">:</span>\n
+ <span class="n">a</span><span class="o">=</span><span class="mi">1</span>\n
+\n
+<span class="n">b</span><span class="o">=</span><span class="sa"></span><span class="s1">&#39;</span><span class="s1">&#39;</span>\n
+ \n
+<span class="n">c</span><span class="o">=</span><span class="mi">2</span>`,
+ ),
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ out, err := File(tt.name, "", []byte(tt.code))
+ assert.NoError(t, err)
+ expected := strings.Join(tt.want, "\n")
+ actual := strings.Join(out, "\n")
+ assert.Equal(t, strings.Count(actual, "<span"), strings.Count(actual, "</span>"))
+ assert.EqualValues(t, expected, actual)
+ })
+ }
+}
+
+func TestPlainText(t *testing.T) {
+ tests := []struct {
+ name string
+ code string
+ want []string
+ }{
+ {
+ name: "empty.py",
+ code: "",
+ want: lines(""),
+ },
+ {
+ name: "tags.py",
+ code: "<>",
+ want: lines("&lt;&gt;"),
+ },
+ {
+ name: "eol-no.py",
+ code: "a=1",
+ want: lines(`a=1`),
+ },
+ {
+ name: "eol-newline1.py",
+ code: "a=1\n",
+ want: lines(`a=1\n`),
+ },
+ {
+ name: "eol-newline2.py",
+ code: "a=1\n\n",
+ want: lines(`
+a=1\n
+\n
`),
},
{
- name: ".drone.yml - trailing space",
- numLines: 13,
- fileName: ".drone.yml",
- code: strings.Replace(util.Dedent(`
- kind: pipeline
- name: default
+ name: "empty-line-with-space.py",
+ code: strings.ReplaceAll(strings.TrimSpace(`
+def:
+ a=1
- steps:
- - name: test
- image: golang:1.13
- environment:
- GOPROXY: https://goproxy.cn
- commands:
- - go get -u
- - go build -v
- - go test -v -race -coverprofile=coverage.txt -covermode=atomic
- `)+"\n", "name: default", "name: default ", 1),
- want: util.Dedent(`
- <span class="line"><span class="cl"><span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>
- </span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default </span>
- </span></span><span class="line"><span class="cl">
- </span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">steps</span><span class="p">:</span>
- </span></span><span class="line"><span class="cl"><span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span>
- </span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span>
- </span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">environment</span><span class="p">:</span>
- </span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span>
- </span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">commands</span><span class="p">:</span>
- </span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>
- </span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go build -v</span>
- </span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span>
- </span></span>
- <span class="w">
- </span>
- `),
+b=''
+{space}
+c=2
+ `), "{space}", " "),
+ want: lines(`
+def:\n
+ a=1\n
+\n
+b=&#39;&#39;\n
+ \n
+c=2`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- got := strings.Join(File(tt.numLines, tt.fileName, "", []byte(tt.code)), "\n")
- assert.Equal(t, tt.want, got)
+ out := PlainText([]byte(tt.code))
+ expected := strings.Join(tt.want, "\n")
+ actual := strings.Join(out, "\n")
+ assert.EqualValues(t, expected, actual)
})
}
}