-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
53 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
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 |
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 |
---|---|---|
@@ -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) | ||
} | ||
} |