Skip to content

Commit

Permalink
Merge pull request #7 from gochore/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfogre authored Jun 18, 2020
2 parents dbcf902 + 86ee479 commit 3d6c434
Show file tree
Hide file tree
Showing 19 changed files with 1,031 additions and 411 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ before_install:
- go get -t -v ./...

script:
- go test -race -coverprofile=coverage.txt -covermode=atomic
- go test -race -coverprofile=coverage.txt -covermode=atomic -v ./...
- go vet -v ./...
- gofmt -l . && test -z $(gofmt -l .)

Expand Down
34 changes: 16 additions & 18 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,36 @@ package emailt
import (
"bytes"
"fmt"
"html/template"
"io"
"strings"
"text/template"

"github.com/gochore/emailt/internal/rend"
"github.com/gochore/emailt/style"
)

type Element interface {
Render(writer io.Writer, themes ...Theme) error
}

type StringElement string

func NewStringElement(format string, a ...interface{}) StringElement {
return StringElement(fmt.Sprintf(format, a...))
}

func (e StringElement) Render(writer io.Writer, themes ...Theme) error {
return htmlRender(strings.NewReader(string(e)), writer, mergeThemes(themes))
Render(writer io.Writer, themes ...style.Theme) error
}

type TemplateElement struct {
Data interface{}
Template string
Funcs template.FuncMap
}

func (e TemplateElement) Render(writer io.Writer, themes ...Theme) error {
t, err := template.New("").Parse(e.Template)
func (e TemplateElement) Render(writer io.Writer, themes ...style.Theme) error {
errPrefix := "TemplateElement.Render: "

t, err := template.New("").Funcs(e.Funcs).Parse(e.Template)
if err != nil {
return fmt.Errorf("parse template: %w", err)
return fmt.Errorf(errPrefix+"parse template: %w", err)
}
buffer := &bytes.Buffer{}
if err := t.Execute(buffer, e.Data); err != nil {
return fmt.Errorf("template execute: %w", err)
return fmt.Errorf(errPrefix+"template execute: %w", err)
}
if err := rend.RenderTheme(buffer, writer, rend.MergeThemes(themes)); err != nil {
return fmt.Errorf(errPrefix+"%w", err)
}
return htmlRender(buffer, writer, mergeThemes(themes))
return nil
}
95 changes: 22 additions & 73 deletions element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,16 @@ package emailt

import (
"bytes"
"strings"
"testing"
"time"
"text/template"
)

func TestStringElement_Render(t *testing.T) {
tests := []struct {
name string
e StringElement
style Theme
wantWriter string
wantErr bool
}{
{
name: "regular",
e: "<p>test</p>",
wantWriter: "<p>test</p>",
wantErr: false,
},
{
name: "with_style",
e: "<p>test</p>",
style: MapTheme{
"p": Attributes{
{
Name: "style",
Value: "background-color:#dedede;",
},
},
},
wantWriter: `<p style="background-color:#dedede;">test</p>`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
writer := &bytes.Buffer{}
err := tt.e.Render(writer, tt.style)
if (err != nil) != tt.wantErr {
t.Errorf("Render() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotWriter := writer.String(); gotWriter != tt.wantWriter {
t.Errorf("Render() gotWriter = %v, want %v", gotWriter, tt.wantWriter)
}
})
}
}

func TestTemplateElement_Render(t *testing.T) {
type fields struct {
Data interface{}
Template string
Funcs template.FuncMap
}
tests := []struct {
name string
Expand Down Expand Up @@ -100,12 +58,31 @@ func TestTemplateElement_Render(t *testing.T) {
wantWriter: "",
wantErr: true,
},
{
name: "with_funcs",
fields: fields{
Data: struct {
A string
B int
}{
A: "hello",
B: 1,
},
Template: "A:{{title .A}}, B:{{.B}}",
Funcs: template.FuncMap{
"title": strings.Title,
},
},
wantWriter: "A:Hello, B:1",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := TemplateElement{
Data: tt.fields.Data,
Template: tt.fields.Template,
Funcs: tt.fields.Funcs,
}
writer := &bytes.Buffer{}
err := e.Render(writer)
Expand All @@ -119,31 +96,3 @@ func TestTemplateElement_Render(t *testing.T) {
})
}
}

func TestNewStringElement(t *testing.T) {
type args struct {
format string
a []interface{}
}
tests := []struct {
name string
args args
want StringElement
}{
{
name: "regular",
args: args{
format: "%v %v",
a: []interface{}{1, time.Minute},
},
want: "1 1m0s",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewStringElement(tt.args.format, tt.args.a...); got != tt.want {
t.Errorf("NewStringElement() = %v, want %v", got, tt.want)
}
})
}
}
9 changes: 6 additions & 3 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package emailt
import (
"fmt"
"io"

"github.com/gochore/emailt/internal/rend"
"github.com/gochore/emailt/style"
)

type Email struct {
elements []Element
theme Theme
theme style.Theme
}

type Option func(email *Email)

func WithTheme(theme Theme) Option {
func WithTheme(theme style.Theme) Option {
return func(email *Email) {
email.theme = theme
}
Expand All @@ -34,7 +37,7 @@ func (e *Email) AddElements(element ...Element) *Email {
}

func (e *Email) Render(writer io.Writer) error {
render := newFmtWriter(writer)
render := rend.NewFmtWriter(writer)
render.Print(`
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
Expand Down
18 changes: 0 additions & 18 deletions headline.go

This file was deleted.

53 changes: 0 additions & 53 deletions headline_test.go

This file was deleted.

Loading

0 comments on commit 3d6c434

Please sign in to comment.