mirror of
				https://github.com/gohugoio/hugo.git
				synced 2024-05-11 05:54:58 +00:00 
			
		
		
		
	@@ -16,6 +16,7 @@ package helpers
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"github.com/spf13/viper"
 | 
						"github.com/spf13/viper"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -50,9 +51,11 @@ func TestCodeFence(t *testing.T) {
 | 
				
			|||||||
		enabled         bool
 | 
							enabled         bool
 | 
				
			||||||
		input, expected string
 | 
							input, expected string
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Pygments 2.0 and 2.1 have slightly different outputs so only do partial matching
 | 
				
			||||||
	data := []test{
 | 
						data := []test{
 | 
				
			||||||
		{true, "<html></html>", "<div class=\"highlight\"><pre><code class=\"language-html\" data-lang=\"html\"><span class=\"nt\"><html></html></span>\n</code></pre></div>\n"},
 | 
							{true, "<html></html>", `(?s)^<div class="highlight"><pre><code class="language-html" data-lang="html">.*?</code></pre></div>\n$`},
 | 
				
			||||||
		{false, "<html></html>", "<pre><code class=\"language-html\"><html></html></code></pre>\n"},
 | 
							{false, "<html></html>", `(?s)^<pre><code class="language-html">.*?</code></pre>\n$`},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	viper.Reset()
 | 
						viper.Reset()
 | 
				
			||||||
@@ -65,12 +68,21 @@ func TestCodeFence(t *testing.T) {
 | 
				
			|||||||
		viper.Set("PygmentsCodeFences", d.enabled)
 | 
							viper.Set("PygmentsCodeFences", d.enabled)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		result := render(d.input)
 | 
							result := render(d.input)
 | 
				
			||||||
		if result != d.expected {
 | 
					
 | 
				
			||||||
 | 
							expectedRe, err := regexp.Compile(d.expected)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Invalid regexp", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							matched := expectedRe.MatchString(result)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if !matched {
 | 
				
			||||||
			t.Errorf("Test %d failed. BlackFriday enabled:%t, Expected:\n%q got:\n%q", i, d.enabled, d.expected, result)
 | 
								t.Errorf("Test %d failed. BlackFriday enabled:%t, Expected:\n%q got:\n%q", i, d.enabled, d.expected, result)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		result = renderWithMmark(d.input)
 | 
							result = renderWithMmark(d.input)
 | 
				
			||||||
		if result != d.expected {
 | 
							matched = expectedRe.MatchString(result)
 | 
				
			||||||
 | 
							if !matched {
 | 
				
			||||||
			t.Errorf("Test %d failed. Mmark enabled:%t, Expected:\n%q got:\n%q", i, d.enabled, d.expected, result)
 | 
								t.Errorf("Test %d failed. Mmark enabled:%t, Expected:\n%q got:\n%q", i, d.enabled, d.expected, result)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -52,7 +52,7 @@ func CheckShortCodeMatchAndError(t *testing.T, input, expected string, template
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if output != expected {
 | 
						if output != expected {
 | 
				
			||||||
		t.Fatalf("Shortcode render didn't match. got %q but expected %q", output, expected)
 | 
							t.Fatalf("Shortcode render didn't match. got \n%q but expected \n%q", output, expected)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -258,13 +258,28 @@ func TestHighlight(t *testing.T) {
 | 
				
			|||||||
	viper.Set("PygmentsStyle", "bw")
 | 
						viper.Set("PygmentsStyle", "bw")
 | 
				
			||||||
	viper.Set("PygmentsUseClasses", false)
 | 
						viper.Set("PygmentsUseClasses", false)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tem := tpl.New()
 | 
						templ := tpl.New()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	code := `
 | 
						code := `
 | 
				
			||||||
{{< highlight java >}}
 | 
					{{< highlight java >}}
 | 
				
			||||||
void do();
 | 
					void do();
 | 
				
			||||||
{{< /highlight >}}`
 | 
					{{< /highlight >}}`
 | 
				
			||||||
	CheckShortCodeMatch(t, code, "\n<div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span style=\"font-weight: bold\">void</span> do();\n</pre></div>\n", tem)
 | 
					
 | 
				
			||||||
 | 
						p, _ := pageFromString(SIMPLE_PAGE, "simple.md")
 | 
				
			||||||
 | 
						output, err := HandleShortcodes(code, p, templ)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal("Handle shortcode error", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						matched, err := regexp.MatchString("(?s)^\n<div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\">.*?void</span> do().*?</pre></div>\n$", output)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal("Regexp error", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if !matched {
 | 
				
			||||||
 | 
							t.Error("Hightlight mismatch, got\n", output)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const testScPlaceholderRegexp = "{#{#HUGOSHORTCODE-\\d+#}#}"
 | 
					const testScPlaceholderRegexp = "{#{#HUGOSHORTCODE-\\d+#}#}"
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user