-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #217 from PDOK/mapsheets-test
Add test for features as mapsheets
- Loading branch information
Showing
11 changed files
with
395 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package engine | ||
|
||
import ( | ||
htmltemplate "html/template" | ||
"log" | ||
"strconv" | ||
"strings" | ||
texttemplate "text/template" | ||
"time" | ||
|
||
"github.com/docker/go-units" | ||
|
||
sprig "github.com/go-task/slim-sprig" | ||
gomarkdown "github.com/gomarkdown/markdown" | ||
gomarkdownhtml "github.com/gomarkdown/markdown/html" | ||
gomarkdownparser "github.com/gomarkdown/markdown/parser" | ||
stripmd "github.com/writeas/go-strip-markdown/v2" | ||
) | ||
|
||
var ( | ||
globalTemplateFuncs texttemplate.FuncMap | ||
) | ||
|
||
// Initialize functions to be used in html/json/etc templates | ||
func init() { | ||
customFuncs := texttemplate.FuncMap{ | ||
// custom template functions | ||
"markdown": markdown, | ||
"unmarkdown": unmarkdown, | ||
"humansize": humanSize, | ||
"bytessize": bytesSize, | ||
"isdate": isDate, | ||
} | ||
sprigFuncs := sprig.FuncMap() // we also support https://github.com/go-task/slim-sprig functions | ||
globalTemplateFuncs = combineFuncMaps(customFuncs, sprigFuncs) | ||
} | ||
|
||
// combine given FuncMaps | ||
func combineFuncMaps(funcMaps ...map[string]any) map[string]any { | ||
result := make(map[string]any) | ||
for _, funcMap := range funcMaps { | ||
for k, v := range funcMap { | ||
result[k] = v | ||
} | ||
} | ||
return result | ||
} | ||
|
||
// markdown turn Markdown into HTML | ||
func markdown(s *string) htmltemplate.HTML { | ||
if s == nil { | ||
return "" | ||
} | ||
// always normalize newlines, this library only supports Unix LF newlines | ||
md := gomarkdown.NormalizeNewlines([]byte(*s)) | ||
|
||
// create Markdown parser | ||
extensions := gomarkdownparser.CommonExtensions | ||
parser := gomarkdownparser.NewWithExtensions(extensions) | ||
|
||
// parse Markdown into AST tree | ||
doc := parser.Parse(md) | ||
|
||
// create HTML renderer | ||
htmlFlags := gomarkdownhtml.CommonFlags | gomarkdownhtml.HrefTargetBlank | gomarkdownhtml.SkipHTML | ||
renderer := gomarkdownhtml.NewRenderer(gomarkdownhtml.RendererOptions{Flags: htmlFlags}) | ||
|
||
return htmltemplate.HTML(gomarkdown.Render(doc, renderer)) //nolint:gosec | ||
} | ||
|
||
// unmarkdown remove Markdown, so we can use the given string in non-HTML (JSON) output | ||
func unmarkdown(s *string) string { | ||
if s == nil { | ||
return "" | ||
} | ||
withoutMarkdown := stripmd.Strip(*s) | ||
withoutLinebreaks := strings.ReplaceAll(withoutMarkdown, "\n", " ") | ||
return withoutLinebreaks | ||
} | ||
|
||
// humanSize converts size in bytes to a human-readable size | ||
func humanSize(a any) string { | ||
if i, ok := a.(int64); ok { | ||
return units.HumanSize(float64(i)) | ||
} else if f, ok := a.(float64); ok { | ||
return units.HumanSize(f) | ||
} else if s, ok := a.(string); ok { | ||
fs, err := strconv.ParseFloat(s, 64) | ||
if err == nil { | ||
return units.HumanSize(fs) | ||
} | ||
} | ||
log.Printf("cannot convert '%v' to float", a) | ||
return "0" | ||
} | ||
|
||
// bytesSize converts human-readable size to size in bytes (base-10, not base-2) | ||
func bytesSize(s string) int64 { | ||
i, err := units.FromHumanSize(s) | ||
if err != nil { | ||
log.Printf("cannot convert '%s' to bytes", s) | ||
return 0 | ||
} | ||
return i | ||
} | ||
|
||
// isDate true when given input is a date, false otherwise | ||
func isDate(v any) bool { | ||
if _, ok := v.(time.Time); ok { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package engine | ||
|
||
import ( | ||
"html/template" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMarkdown(t *testing.T) { | ||
tests := []struct { | ||
input *string | ||
expected template.HTML | ||
}{ | ||
{nil, ""}, | ||
{ptrTo("**bold**"), "<p><strong>bold</strong></p>\n"}, | ||
{ptrTo("# Heading"), "<h1>Heading</h1>\n"}, | ||
{ptrTo("Some [link](https://example.com)"), "<p>Some <a href=\"https://example.com\" target=\"_blank\">link</a></p>\n"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run("", func(t *testing.T) { | ||
assert.Equal(t, tt.expected, markdown(tt.input)) | ||
}) | ||
} | ||
} | ||
|
||
func TestUnmarkdown(t *testing.T) { | ||
tests := []struct { | ||
input *string | ||
expected string | ||
}{ | ||
{nil, ""}, | ||
{ptrTo("**bold**"), "bold"}, | ||
{ptrTo("# Heading"), "Heading"}, | ||
{ptrTo("Some [link](https://example.com)"), "Some link"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run("", func(t *testing.T) { | ||
assert.Equal(t, tt.expected, unmarkdown(tt.input)) | ||
}) | ||
} | ||
} | ||
|
||
func TestHumanSize(t *testing.T) { | ||
tests := []struct { | ||
input any | ||
expected string | ||
}{ | ||
{int64(1000), "1kB"}, | ||
{float64(1000), "1kB"}, | ||
{1000.00, "1kB"}, | ||
{"1000", "1kB"}, | ||
{"1000000", "1MB"}, | ||
{"invalid", "0"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run("", func(t *testing.T) { | ||
assert.Equal(t, tt.expected, humanSize(tt.input)) | ||
}) | ||
} | ||
} | ||
|
||
func TestBytesSize(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected int64 | ||
}{ | ||
{"1 kB", 1000}, | ||
{"1 MB", 1000000}, | ||
{"1.1 GB", 1100000000}, | ||
{"invalid", 0}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run("", func(t *testing.T) { | ||
assert.Equal(t, tt.expected, bytesSize(tt.input)) | ||
}) | ||
} | ||
} | ||
|
||
func TestIsDate(t *testing.T) { | ||
tests := []struct { | ||
input any | ||
expected bool | ||
}{ | ||
{time.Now(), true}, | ||
{"not a date", false}, | ||
{12345, false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run("", func(t *testing.T) { | ||
assert.Equal(t, tt.expected, isDate(tt.input)) | ||
}) | ||
} | ||
} | ||
|
||
func ptrTo(s string) *string { | ||
return &s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.