Skip to content

Commit

Permalink
remove depedency on testify
Browse files Browse the repository at this point in the history
  • Loading branch information
wamuir committed Jul 24, 2021
1 parent 6f0ff7a commit dcd8873
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 53 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/wamuir/go-xslt

go 1.13

require github.com/stretchr/testify v1.7.0
go 1.16
11 changes: 0 additions & 11 deletions go.sum

This file was deleted.

115 changes: 76 additions & 39 deletions xslt_test.go
Original file line number Diff line number Diff line change
@@ -1,83 +1,120 @@
package xslt

import (
"io/ioutil"
"bytes"
_ "embed"
"testing"

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

var (
document = mustReadFile("testdata/document.xml")
style1 = mustReadFile("testdata/style1.xsl")
style2 = mustReadFile("testdata/style2.xsl")
result1 = mustReadFile("testdata/result1.xml")
result2 = mustReadFile("testdata/result2.xhtml")
)
//go:embed testdata/document.xml
var document []byte

func mustReadFile(f string) []byte {
//go:embed testdata/style1.xsl
var style1 []byte

b, err := ioutil.ReadFile(f)
if err != nil {
panic(err)
}
return b
}
//go:embed testdata/style2.xsl
var style2 []byte

//go:embed testdata/result1.xml
var result1 []byte

//go:embed testdata/result2.xhtml
var result2 []byte

func TestNewStylesheet(t *testing.T) {

xs1, err := NewStylesheet(style1)
assert.NotNil(t, xs1)
assert.NoError(t, err)
if err != nil {
t.Errorf("err = %v; want %v", err, nil)
}
if xs1 == nil {
t.Errorf("xs1 = %v, want non-nil", xs1)
}
defer xs1.Close()

xs2, err := NewStylesheet(style2)
assert.NotNil(t, xs2)
assert.NoError(t, err)
if err != nil {
t.Errorf("err = %v; want %v", err, nil)
}
if xs2 == nil {
t.Errorf("xs2 = %v, want non-nil", xs2)
}
defer xs2.Close()

xs3, err := NewStylesheet(nil)
assert.Nil(t, xs3)
assert.Equal(t, ErrXSLParseFailure, err)
if err != ErrXSLParseFailure {
t.Errorf("err = %v; want %v", err, ErrXSLParseFailure)
}
if xs3 != nil {
t.Errorf("xs3 = %v, want %v", xs3, nil)
}
}

func TestStylesheetClose(t *testing.T) {

xs1, err := NewStylesheet(style1)
assert.NotNil(t, xs1)
assert.NoError(t, err)
assert.NotPanics(t, xs1.Close)
if err != nil {
t.Errorf("err = %v; want %v", err, nil)
}
if xs1 == nil {
t.Errorf("xs1 = %v, want non-nil", xs1)
}
xs1.Close()

xs2 := Stylesheet{}
assert.NotPanics(t, xs2.Close)
xs2.Close()
}

func TestStylesheetTransform(t *testing.T) {

xs1, err := NewStylesheet(style1)
assert.NotNil(t, xs1)
assert.NoError(t, err)
if err != nil {
t.Errorf("err = %v; want %v", err, nil)
}
if xs1 == nil {
t.Errorf("xs1 = %v, want non-nil", xs1)
}
defer xs1.Close()

res1, err := xs1.Transform(document)
assert.Equal(t, result1, res1)
assert.NoError(t, err)
if err != nil {
t.Errorf("err = %v; want %v", err, nil)
}
if !bytes.Equal(result1, res1) {
t.FailNow()
}

xs2, err := NewStylesheet(style2)
assert.NotNil(t, xs2)
assert.NoError(t, err)
if err != nil {
t.Errorf("err = %v; want %v", err, nil)
}
if xs2 == nil {
t.Errorf("xs2 = %v, want non-nil", xs2)
}
defer xs2.Close()

res2, err := xs2.Transform(document)
assert.Equal(t, result2, res2)
assert.NoError(t, err)
if err != nil {
t.Errorf("err = %v; want %v", err, nil)
}
if !bytes.Equal(result2, res2) {
t.FailNow()
}

xs3, err := NewStylesheet(style1)
assert.NotNil(t, xs3)
assert.NoError(t, err)
if err != nil {
t.Errorf("err = %v; want %v", err, nil)
}
if xs3 == nil {
t.Errorf("xs3 = %v, want non-nil", xs3)
}
defer xs3.Close()

res3, err := xs3.Transform(nil)
assert.Nil(t, res3)
assert.Equal(t, ErrXSLTFailure, err)
if err != ErrXSLTFailure {
t.Errorf("err = %v; want %v", err, ErrXSLTFailure)
}
if res3 != nil {
t.Errorf("xs3 = %v, want %v", res3, nil)
}
}

0 comments on commit dcd8873

Please sign in to comment.