1. package lexers
    
  2. 
    
  3. import (
    
  4. 	"testing"
    
  5. 
    
  6. 	assert "github.com/alecthomas/assert/v2"
    
  7. 	"github.com/alecthomas/chroma/v2"
    
  8. )
    
  9. 
    
  10. func TestGoHTMLTemplateIssue126(t *testing.T) {
    
  11. 	for _, source := range []string{
    
  12. 		`<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    
  13.   <channel>
    
  14.     <title>{{ if eq  .Title  .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }}{{ end }}</title>
    
  15.     <link>{{ .Permalink }}</link>
    
  16.     <description>Recent content {{ if ne  .Title  .Site.Title }}{{ with .Title }}in {{.}} {{ end }}{{ end }}on {{ .Site.Title }}</description>
    
  17.     <generator>Hugo -- gohugo.io</generator>{{ with .Site.LanguageCode }}
    
  18.     <language>{{.}}</language>{{end}}{{ with .Site.Author.email }}
    
  19.     <managingEditor>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</managingEditor>{{end}}{{ with .Site.Author.email }}
    
  20.     <webMaster>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</webMaster>{{end}}{{ with .Site.Copyright }}
    
  21.     <copyright>{{.}}</copyright>{{end}}{{ if not .Date.IsZero }}
    
  22.     <lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
    
  23.     {{ with .OutputFormats.Get "RSS" }}
    
  24.         {{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
    
  25.     {{ end }}
    
  26. 	{{/*
    
  27. 		Print all pages
    
  28. 	*/}}
    
  29.     {{ range .Data.Pages }}
    
  30.     <item>
    
  31.       <title>{{ .Title }}</title>
    
  32.       <link>{{ .Permalink }}</link>
    
  33.       <pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
    
  34.       {{ with .Site.Author.email }}<author>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</author>{{end}}
    
  35.       <guid>{{ .Permalink }}</guid>
    
  36.       <description>{{ .Summary | html }}</description>
    
  37.     </item>
    
  38.     {{ end }}
    
  39.   </channel>
    
  40. </rss>
    
  41. `,
    
  42. 		`{{ $headless := .Site.GetPage "page" "some-headless-bundle" }}
    
  43. {{ $reusablePages := $headless.Resources.Match "author*" }}
    
  44. <h2>Authors</h2>
    
  45. {{ range $reusablePages }}
    
  46.     <h3>{{ .Title }}</h3>
    
  47.     {{ .Content }}
    
  48. {{ end }}`} {
    
  49. 		tokens, err := chroma.Tokenise(GoHTMLTemplate, nil, source)
    
  50. 		assert.NoError(t, err)
    
  51. 		assert.Equal(t, source, chroma.Stringify(tokens...))
    
  52. 	}
    
  53. }
    
  54. 
    
  55. func TestGoHTMLTemplateMultilineComments(t *testing.T) {
    
  56. 	for _, source := range []string{
    
  57. 		`
    
  58. {{/*
    
  59. 	This is a multiline comment
    
  60. */}}
    
  61. `,
    
  62. 		`
    
  63. {{- /*
    
  64. 	This is a multiline comment
    
  65. */}}
    
  66. `,
    
  67. 		`
    
  68. {{/*
    
  69. 	This is a multiline comment
    
  70. */ -}}
    
  71. `,
    
  72. 		`
    
  73. {{- /*
    
  74. 	This is a multiline comment
    
  75. */ -}}
    
  76. `,
    
  77. 	} {
    
  78. 		tokens, err := chroma.Tokenise(GoHTMLTemplate, nil, source)
    
  79. 		assert.NoError(t, err)
    
  80. 		assert.Equal(t, source, chroma.Stringify(tokens...))
    
  81. 
    
  82. 		// Make sure that there are no errors
    
  83. 		for _, token := range tokens {
    
  84. 			assert.NotEqual(t, chroma.Error, token.Type)
    
  85. 		}
    
  86. 
    
  87. 		// Make sure that multiline comments are printed
    
  88. 		found := false
    
  89. 		for _, token := range tokens {
    
  90. 			if token.Type == chroma.CommentMultiline {
    
  91. 				found = true
    
  92. 			}
    
  93. 		}
    
  94. 		assert.True(t, found)
    
  95. 	}
    
  96. }
    
  97. 
    
  98. func TestGoHTMLTemplateNegativeNumber(t *testing.T) {
    
  99. 	for _, source := range []string{
    
  100. 		`
    
  101. {{ fn -3 }}
    
  102. `,
    
  103. 	} {
    
  104. 		tokens, err := chroma.Tokenise(GoHTMLTemplate, nil, source)
    
  105. 		assert.NoError(t, err)
    
  106. 		assert.Equal(t, source, chroma.Stringify(tokens...))
    
  107. 
    
  108. 		// Make sure that there are no errors
    
  109. 		for _, token := range tokens {
    
  110. 			assert.NotEqual(t, chroma.Error, token.Type)
    
  111. 		}
    
  112. 
    
  113. 		// Make sure that negative number is found
    
  114. 		found := false
    
  115. 		for _, token := range tokens {
    
  116. 			if token.Type == chroma.LiteralNumberInteger {
    
  117. 				found = true
    
  118. 			}
    
  119. 		}
    
  120. 		assert.True(t, found)
    
  121. 	}
    
  122. }