gitea源码

orgmode_test.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package orgmode_test
  4. import (
  5. "os"
  6. "strings"
  7. "testing"
  8. "code.gitea.io/gitea/modules/markup"
  9. "code.gitea.io/gitea/modules/markup/orgmode"
  10. "code.gitea.io/gitea/modules/setting"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestMain(m *testing.M) {
  14. setting.AppURL = "http://localhost:3000/"
  15. setting.IsInTesting = true
  16. os.Exit(m.Run())
  17. }
  18. func TestRender_StandardLinks(t *testing.T) {
  19. test := func(input, expected string) {
  20. buffer, err := orgmode.RenderString(markup.NewTestRenderContext(), input)
  21. assert.NoError(t, err)
  22. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  23. }
  24. test("[[https://google.com/]]",
  25. `<p><a href="https://google.com/">https://google.com/</a></p>`)
  26. test("[[ImageLink.svg][The Image Desc]]",
  27. `<p><a href="ImageLink.svg">The Image Desc</a></p>`)
  28. }
  29. func TestRender_InternalLinks(t *testing.T) {
  30. test := func(input, expected string) {
  31. buffer, err := orgmode.RenderString(markup.NewTestRenderContext(), input)
  32. assert.NoError(t, err)
  33. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  34. }
  35. test("[[file:test.org][Test]]",
  36. `<p><a href="test.org">Test</a></p>`)
  37. test("[[./test.org][Test]]",
  38. `<p><a href="./test.org">Test</a></p>`)
  39. test("[[test.org][Test]]",
  40. `<p><a href="test.org">Test</a></p>`)
  41. test("[[path/to/test.org][Test]]",
  42. `<p><a href="path/to/test.org">Test</a></p>`)
  43. }
  44. func TestRender_Media(t *testing.T) {
  45. test := func(input, expected string) {
  46. buffer, err := orgmode.RenderString(markup.NewTestRenderContext(), input)
  47. assert.NoError(t, err)
  48. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  49. }
  50. test("[[file:../../.images/src/02/train.jpg]]",
  51. `<p><img src="../../.images/src/02/train.jpg" alt="../../.images/src/02/train.jpg"></p>`)
  52. test("[[file:train.jpg]]",
  53. `<p><img src="train.jpg" alt="train.jpg"></p>`)
  54. // With description.
  55. test("[[https://example.com][https://example.com/example.svg]]",
  56. `<p><a href="https://example.com"><img src="https://example.com/example.svg" alt="https://example.com/example.svg"></a></p>`)
  57. test("[[https://example.com][pre https://example.com/example.svg post]]",
  58. `<p><a href="https://example.com">pre <img src="https://example.com/example.svg" alt="https://example.com/example.svg"> post</a></p>`)
  59. test("[[https://example.com][https://example.com/example.mp4]]",
  60. `<p><a href="https://example.com"><video src="https://example.com/example.mp4">https://example.com/example.mp4</video></a></p>`)
  61. test("[[https://example.com][pre https://example.com/example.mp4 post]]",
  62. `<p><a href="https://example.com">pre <video src="https://example.com/example.mp4">https://example.com/example.mp4</video> post</a></p>`)
  63. // Without description.
  64. test("[[https://example.com/example.svg]]",
  65. `<p><img src="https://example.com/example.svg" alt="https://example.com/example.svg"></p>`)
  66. test("[[https://example.com/example.mp4]]",
  67. `<p><video src="https://example.com/example.mp4">https://example.com/example.mp4</video></p>`)
  68. // test [[LINK][DESCRIPTION]] syntax with "file:" prefix
  69. test(`[[https://example.com/][file:https://example.com/foo%20bar.svg]]`,
  70. `<p><a href="https://example.com/"><img src="https://example.com/foo%20bar.svg" alt="https://example.com/foo%20bar.svg"></a></p>`)
  71. test(`[[file:https://example.com/foo%20bar.svg][Goto Image]]`,
  72. `<p><a href="https://example.com/foo%20bar.svg">Goto Image</a></p>`)
  73. test(`[[file:https://example.com/link][https://example.com/image.jpg]]`,
  74. `<p><a href="https://example.com/link"><img src="https://example.com/image.jpg" alt="https://example.com/image.jpg"></a></p>`)
  75. test(`[[file:https://example.com/link][file:https://example.com/image.jpg]]`,
  76. `<p><a href="https://example.com/link"><img src="https://example.com/image.jpg" alt="https://example.com/image.jpg"></a></p>`)
  77. }
  78. func TestRender_Source(t *testing.T) {
  79. test := func(input, expected string) {
  80. buffer, err := orgmode.RenderString(markup.NewTestRenderContext(), input)
  81. assert.NoError(t, err)
  82. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  83. }
  84. test(`#+begin_src c
  85. int a;
  86. #+end_src
  87. `, `<div class="src src-c">
  88. <pre><code class="chroma language-c"><span class="kt">int</span> <span class="n">a</span><span class="p">;</span></code></pre>
  89. </div>`)
  90. }