Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions internal/gig/section_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package gig

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSectionHeaderText(t *testing.T) {
tests := []struct {
name string
header string
want string
}{
{
name: "heading",
header: "# Set 1\n",
want: "Set 1",
},
{
name: "empty",
header: "",
want: "",
},
{
name: "no heading",
header: "just text\n",
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := Section{Header: []byte(tt.header)}
assert.Equal(t, tt.want, s.HeaderText())
})
}
}
4 changes: 4 additions & 0 deletions internal/html/pdf/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
WithPrintBackground(true).
WithPaperHeight(A4Height).
WithPaperWidth(A4Width).
WithMarginTop(0.2).

Check failure on line 86 in internal/html/pdf/convert.go

View workflow job for this annotation

GitHub Actions / build

Magic number: 0.2, in <argument> detected (mnd)
WithMarginBottom(0.2).

Check failure on line 87 in internal/html/pdf/convert.go

View workflow job for this annotation

GitHub Actions / build

Magic number: 0.2, in <argument> detected (mnd)
WithMarginLeft(0.2).

Check failure on line 88 in internal/html/pdf/convert.go

View workflow job for this annotation

GitHub Actions / build

Magic number: 0.2, in <argument> detected (mnd)
WithMarginRight(0.2).

Check failure on line 89 in internal/html/pdf/convert.go

View workflow job for this annotation

GitHub Actions / build

Magic number: 0.2, in <argument> detected (mnd)
WithLandscape(config.Landscape()).
Do(ctx)
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions internal/html/pdf/convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package convert_test

import (
"testing"

convert "github.com/laenzlinger/setlist/internal/html/pdf"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPageCount(t *testing.T) {
tests := []struct {
name string
pdf string
want int
}{
{
name: "single page",
pdf: "%PDF-1.4\n1 0 obj<</Type /Page>>endobj\n2 0 obj<</Type /Pages /Kids[1 0 R]/Count 1>>endobj\n",
want: 1,
},
{
name: "two pages",
pdf: "%PDF-1.4\n1 0 obj<</Type /Page>>endobj\n" +
"2 0 obj<</Type /Page>>endobj\n" +
"3 0 obj<</Type /Pages /Kids[1 0 R 2 0 R]/Count 2>>endobj\n",
want: 2,
},
{
name: "empty",
pdf: "%PDF-1.4\n",
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := convert.PageCount([]byte(tt.pdf))
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
9 changes: 6 additions & 3 deletions internal/html/template/songsheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
padding: 0;
}
body {
font-size: 28px;
font-size: 36px;
font-family: Tahoma, Geneva, sans-serif;
font-weight: bold;
}
table, th, td {
border: 1px solid;
border-color: DarkGray;
border-collapse: collapse;
font-size: 28px;
font-size: 36px;
font-family: Tahoma, Geneva, sans-serif;
font-weight: bold;
}
Expand All @@ -26,7 +26,10 @@
}
h1 {
font-size: 14px;
text-align: right;
text-align: center;
}
pre {
margin: 0.2em 0;
}

article {
Expand Down
58 changes: 58 additions & 0 deletions internal/nodetext/nodetext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package nodetext_test

import (
"testing"

"github.com/laenzlinger/setlist/internal/nodetext"
"github.com/stretchr/testify/assert"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
)

func firstChild(source []byte, kind ast.NodeKind) ast.Node {
doc := goldmark.New().Parser().Parse(text.NewReader(source))
for n := doc.FirstChild(); n != nil; n = n.NextSibling() {
if n.Kind() == kind {
return n
}
}
return nil
}

func TestExtract(t *testing.T) {
tests := []struct {
name string
source string
want string
kind ast.NodeKind
}{
{
name: "heading",
source: "# Hello World",
kind: ast.KindHeading,
want: "Hello World",
},
{
name: "paragraph",
source: "some text",
kind: ast.KindParagraph,
want: "some text",
},
{
name: "list item",
source: "* item one",
kind: ast.KindList,
want: "item one",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
source := []byte(tt.source)
n := firstChild(source, tt.kind)
assert.NotNil(t, n)
got := nodetext.Extract(n, source)
assert.Equal(t, tt.want, got)
})
}
}
50 changes: 50 additions & 0 deletions internal/setlist/setlist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package setlist_test

import (
"testing"

"github.com/laenzlinger/setlist/internal/setlist"
"github.com/laenzlinger/setlist/internal/song"
"github.com/stretchr/testify/assert"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
east "github.com/yuin/goldmark/extension/ast"
"github.com/yuin/goldmark/text"
)

func songsFromTable(source []byte) (song.Header, []song.Song) {
md := goldmark.New(goldmark.WithExtensions(extension.GFM))
doc := md.Parser().Parse(text.NewReader(source))
table := doc.FirstChild()
var header song.Header
var songs []song.Song
for r := table.FirstChild(); r != nil; r = r.NextSibling() {
if r.Kind() == east.KindTableRow {
songs = append(songs, song.New(r, source))
}
if r.Kind() == east.KindTableHeader {
header = song.NewHeader(&r)
}
}
return header, songs
}

func TestRender(t *testing.T) {
source := []byte("| Title | Year |\n|---|---|\n| Song A | 2020 |\n| Song B | 2021 |\n")
md := goldmark.New(goldmark.WithExtensions(extension.GFM))
header, songs := songsFromTable(source)

sl := setlist.Setlist{
TableHeader: header,
Markdown: md,
Source: source,
Sections: []setlist.Section{
{Songs: songs},
},
}

result := sl.Render()
assert.Contains(t, result, "Song A")
assert.Contains(t, result, "Song B")
assert.Contains(t, result, "<t")
}
56 changes: 56 additions & 0 deletions internal/song/song_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package song_test

import (
"testing"

"github.com/laenzlinger/setlist/internal/song"
"github.com/stretchr/testify/assert"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
east "github.com/yuin/goldmark/extension/ast"
"github.com/yuin/goldmark/text"
)

func tableRow(source []byte) (*east.TableRow, []byte) {
md := goldmark.New(goldmark.WithExtensions(extension.GFM))
doc := md.Parser().Parse(text.NewReader(source))
table := doc.FirstChild()
for r := table.FirstChild(); r != nil; r = r.NextSibling() {
if row, ok := r.(*east.TableRow); ok {
return row, source
}
}
return nil, source
}

func TestNew(t *testing.T) {
source := []byte("| Title | Year |\n|---|---|\n| My Song | 2024 |\n")
row, src := tableRow(source)
assert.NotNil(t, row)

s := song.New(row, src)
assert.Equal(t, "My Song", s.Title)
assert.Equal(t, "My Song", s.String())
}

func TestRemoveCols(t *testing.T) {
source := []byte("| A | B | C |\n|---|---|---|\n| 1 | 2 | 3 |\n")
row, _ := tableRow(source)
assert.NotNil(t, row)

// Count children before.
countBefore := 0
for c := row.FirstChild(); c != nil; c = c.NextSibling() {
countBefore++
}
assert.Equal(t, 3, countBefore)

// Remove middle column.
song.RemoveCols(song.Indexes{1: true}, row)

countAfter := 0
for c := row.FirstChild(); c != nil; c = c.NextSibling() {
countAfter++
}
assert.Equal(t, 2, countAfter)
}
Loading