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
31 changes: 14 additions & 17 deletions pkg/fetcher/fetcher_archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"github.com/readium/go-toolkit/pkg/manifest"
"github.com/readium/go-toolkit/pkg/mediatype"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func withArchiveFetcher(t *testing.T, callback func(a *ArchiveFetcher)) {
a, err := NewArchiveFetcherFromPath(t.Context(), "./testdata/epub.epub")
assert.NoError(t, err)
require.NoError(t, err)
callback(a)
}

Expand Down Expand Up @@ -55,7 +56,7 @@ func TestArchiveFetcherLinks(t *testing.T) {

withArchiveFetcher(t, func(a *ArchiveFetcher) {
links, err := a.Links(t.Context())
assert.Nil(t, err)
require.Nil(t, err)

mustLinks := make([]manifest.Link, len(mustContain))
for i, l := range mustContain {
Expand Down Expand Up @@ -88,39 +89,35 @@ func TestArchiveFetcherRead(t *testing.T) {
withArchiveFetcher(t, func(a *ArchiveFetcher) {
resource := a.Get(t.Context(), manifest.Link{Href: manifest.MustNewHREFFromString("mimetype", false)})
bin, err := resource.Read(t.Context(), 0, 0)
if assert.Nil(t, err) {
assert.Equal(t, "application/epub+zip", string(bin))
}
require.Nil(t, err)
assert.Equal(t, "application/epub+zip", string(bin))
var b bytes.Buffer
n, err := resource.Stream(t.Context(), &b, 0, 0)
if assert.Nil(t, err) {
assert.EqualValues(t, 20, n)
assert.Equal(t, "application/epub+zip", b.String())
}
require.Nil(t, err)
assert.EqualValues(t, 20, n)
assert.Equal(t, "application/epub+zip", b.String())
})
}

func TestArchiveFetcherReadRange(t *testing.T) {
withArchiveFetcher(t, func(a *ArchiveFetcher) {
resource := a.Get(t.Context(), manifest.Link{Href: manifest.MustNewHREFFromString("mimetype", false)})
bin, err := resource.Read(t.Context(), 0, 10)
if assert.Nil(t, err) {
assert.Equal(t, "application", string(bin))
}
require.Nil(t, err)
assert.Equal(t, "application", string(bin))
var b bytes.Buffer
n, err := resource.Stream(t.Context(), &b, 0, 10)
if assert.Nil(t, err) {
assert.EqualValues(t, 11, n)
assert.Equal(t, "application", b.String())
}
require.Nil(t, err)
assert.EqualValues(t, 11, n)
assert.Equal(t, "application", b.String())
})
}

func TestArchiveFetcherComputingLength(t *testing.T) {
withArchiveFetcher(t, func(a *ArchiveFetcher) {
resource := a.Get(t.Context(), manifest.Link{Href: manifest.MustNewHREFFromString("mimetype", false)})
length, err := resource.Length(t.Context())
assert.Nil(t, err)
require.Nil(t, err)
assert.EqualValues(t, 20, length)
})
}
Expand Down
96 changes: 41 additions & 55 deletions pkg/fetcher/fetcher_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/readium/go-toolkit/pkg/manifest"
"github.com/readium/go-toolkit/pkg/mediatype"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var testFileFetcher = &FileFetcher{
Expand All @@ -33,42 +34,37 @@ func TestFileFetcherReadNotFound(t *testing.T) {
func TestFileFetcherHrefInMap(t *testing.T) {
resource := testFileFetcher.Get(t.Context(), manifest.Link{Href: manifest.MustNewHREFFromString("file_href", false)})
bin, err := resource.Read(t.Context(), 0, 0)
if assert.Nil(t, err) {
assert.Equal(t, "text", string(bin))
}
require.Nil(t, err)
assert.Equal(t, "text", string(bin))
var b bytes.Buffer
n, err := resource.Stream(t.Context(), &b, 0, 0)
if assert.Nil(t, err) {
assert.EqualValues(t, 4, n)
assert.Equal(t, "text", b.String())
}
require.Nil(t, err)
assert.EqualValues(t, 4, n)
assert.Equal(t, "text", b.String())
}

func TestFileFetcherDirectoryFile(t *testing.T) {
resource := testFileFetcher.Get(t.Context(), manifest.Link{Href: manifest.MustNewHREFFromString("dir_href/text1.txt", false)})
bin, err := resource.Read(t.Context(), 0, 0)
if assert.Nil(t, err) {
assert.Equal(t, "text1", string(bin))
}
require.Nil(t, err)
assert.Equal(t, "text1", string(bin))
var b bytes.Buffer
n, err := resource.Stream(t.Context(), &b, 0, 0)
if assert.Nil(t, err) {
assert.EqualValues(t, 5, n)
assert.Equal(t, "text1", b.String())
}
require.Nil(t, err)
assert.EqualValues(t, 5, n)
assert.Equal(t, "text1", b.String())
}

func TestFileFetcherSubdirectoryFile(t *testing.T) {
resource := testFileFetcher.Get(t.Context(), manifest.Link{Href: manifest.MustNewHREFFromString("dir_href/subdirectory/text2.txt", false)})
bin, err := resource.Read(t.Context(), 0, 0)
assert.Nil(t, err)
require.Nil(t, err)
assert.Equal(t, "text2", string(bin))
var b bytes.Buffer
n, err := resource.Stream(t.Context(), &b, 0, 0)
if assert.Nil(t, err) {
assert.EqualValues(t, 5, n)
assert.Equal(t, "text2", b.String())
}
require.Nil(t, err)
assert.EqualValues(t, 5, n)
assert.Equal(t, "text2", b.String())
}

func TestFileFetcherDirectoryNotFound(t *testing.T) {
Expand All @@ -90,73 +86,63 @@ func TestFileFetcherDirectoryTraversalNotFound(t *testing.T) {
func TestFileFetcherReadRange(t *testing.T) {
resource := testFileFetcher.Get(t.Context(), manifest.Link{Href: manifest.MustNewHREFFromString("file_href", false)})
bin, err := resource.Read(t.Context(), 0, 2)
if assert.Nil(t, err) {
assert.Equal(t, "tex", string(bin), "read data should be the first three bytes of the file")
}
require.Nil(t, err)
assert.Equal(t, "tex", string(bin), "read data should be the first three bytes of the file")

var b bytes.Buffer
n, err := resource.Stream(t.Context(), &b, 0, 2)
if assert.Nil(t, err) {
assert.EqualValues(t, 3, n)
assert.Equal(t, "tex", b.String(), "read data should be the first three bytes of the file")
}
require.Nil(t, err)
assert.EqualValues(t, 3, n)
assert.Equal(t, "tex", b.String(), "read data should be the first three bytes of the file")
}

func TestFileFetcherTwoRangesSameResource(t *testing.T) {
resource := testFileFetcher.Get(t.Context(), manifest.Link{Href: manifest.MustNewHREFFromString("file_href", false)})
bin, err := resource.Read(t.Context(), 0, 1)
if assert.Nil(t, err) {
assert.Equal(t, "te", string(bin))
}
require.Nil(t, err)
assert.Equal(t, "te", string(bin))
var b bytes.Buffer
n, err := resource.Stream(t.Context(), &b, 0, 1)
if assert.Nil(t, err) {
assert.EqualValues(t, 2, n)
assert.Equal(t, "te", b.String())
}
require.Nil(t, err)
assert.EqualValues(t, 2, n)
assert.Equal(t, "te", b.String())

bin, err = resource.Read(t.Context(), 1, 3)
if assert.Nil(t, err) {
assert.Equal(t, "ext", string(bin))
}
require.Nil(t, err)
assert.Equal(t, "ext", string(bin))
b.Reset()
n, err = resource.Stream(t.Context(), &b, 1, 3)
if assert.Nil(t, err) {
assert.EqualValues(t, 3, n)
assert.Equal(t, "ext", b.String())
}
require.Nil(t, err)
assert.EqualValues(t, 3, n)
assert.Equal(t, "ext", b.String())
}

func TestFileFetcherOutOfRangeClamping(t *testing.T) {
resource := testFileFetcher.Get(t.Context(), manifest.Link{Href: manifest.MustNewHREFFromString("file_href", false)})
bin, err := resource.Read(t.Context(), -5, 60)
if assert.Nil(t, err) {
assert.Equal(t, "text", string(bin))
}
require.Nil(t, err)
assert.Equal(t, "text", string(bin))
var b bytes.Buffer
n, err := resource.Stream(t.Context(), &b, -5, 60)
if assert.Nil(t, err) {
assert.EqualValues(t, 4, n)
assert.Equal(t, "text", b.String())
}
require.Nil(t, err)
assert.EqualValues(t, 4, n)
assert.Equal(t, "text", b.String())
}

func TestFileFetcherDecreasingRange(t *testing.T) {
resource := testFileFetcher.Get(t.Context(), manifest.Link{Href: manifest.MustNewHREFFromString("file_href", false)})
_, err := resource.Read(t.Context(), 60, 20)
if assert.Error(t, err) {
assert.Equal(t, RangeNotSatisfiable(err.Cause), err, "range isn't satisfiable")
}
require.Error(t, err)
assert.Equal(t, RangeNotSatisfiable(err.Cause), err, "range isn't satisfiable")
_, err = resource.Stream(t.Context(), &bytes.Buffer{}, 60, 20)
if assert.Error(t, err) {
assert.Equal(t, RangeNotSatisfiable(err.Cause), err, "range isn't satisfiable")
}
require.Error(t, err)
assert.Equal(t, RangeNotSatisfiable(err.Cause), err, "range isn't satisfiable")
}

func TestFileFetcherComputingLength(t *testing.T) {
resource := testFileFetcher.Get(t.Context(), manifest.Link{Href: manifest.MustNewHREFFromString("file_href", false)})
length, err := resource.Length(t.Context())
assert.Nil(t, err)
require.Nil(t, err)
assert.EqualValues(t, 4, length)
}

Expand All @@ -174,7 +160,7 @@ func TestFileFetcherFileNotFoundLength(t *testing.T) {

func TestFileFetcherLinks(t *testing.T) {
links, err := testFileFetcher.Links(t.Context())
assert.Nil(t, err)
require.Nil(t, err)

mustContain := manifest.LinkList{{
Href: manifest.MustNewHREFFromString("dir_href/subdirectory/hello.mp3", false),
Expand Down
9 changes: 3 additions & 6 deletions pkg/internal/util/css_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/andybalholm/cascadia"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/html"
)

Expand All @@ -30,15 +31,11 @@ const testDoc = `<?xml version="1.0" encoding="UTF-8"?>

func TestCSSSelector(t *testing.T) {
doc, err := html.Parse(strings.NewReader(testDoc))
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)

qf := func(query string) string {
n := cascadia.Query(doc, cascadia.MustCompile(query))
if !assert.NotNil(t, n) {
t.FailNow()
}
require.NotNil(t, n)
return CSSSelector(n)
}

Expand Down
17 changes: 9 additions & 8 deletions pkg/manifest/a11y_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"testing"

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

func TestA11yUnmarshalMinimalJSON(t *testing.T) {
var m A11y
assert.NoError(t, json.Unmarshal([]byte("{}"), &m))
require.NoError(t, json.Unmarshal([]byte("{}"), &m))
assert.Equal(t, NewA11y(), m, "unmarshalled JSON object should be equal to A11y object")
}

func TestA11yUnmarshalFullJSON(t *testing.T) {
var m A11y
assert.NoError(t, json.Unmarshal([]byte(`{
require.NoError(t, json.Unmarshal([]byte(`{
"conformsTo": ["https://profile1", "https://profile2"],
"certification": {
"certifiedBy": "company1",
Expand Down Expand Up @@ -67,21 +68,21 @@ func TestA11yUnmarshalFullJSON(t *testing.T) {

func TestA11yUnmarshalInvalidSummaryIsIgnored(t *testing.T) {
var m A11y
assert.NoError(t, json.Unmarshal([]byte(`{"summary": ["sum1", "sum2"]}`), &m))
require.NoError(t, json.Unmarshal([]byte(`{"summary": ["sum1", "sum2"]}`), &m))
assert.Equal(t, NewA11y(), m, "unmarshalled JSON object should be equal to A11y object")
}

func TestA11yUnmarshalConformsToString(t *testing.T) {
var m A11y
assert.NoError(t, json.Unmarshal([]byte(`{"conformsTo": "http://www.idpf.org/epub/a11y/accessibility-20170105.html#wcag-a"}`), &m))
require.NoError(t, json.Unmarshal([]byte(`{"conformsTo": "http://www.idpf.org/epub/a11y/accessibility-20170105.html#wcag-a"}`), &m))
var e A11y = NewA11y()
e.ConformsTo = []A11yProfile{EPUBA11y10WCAG20A}
assert.Equal(t, e, m, "unmarshalled JSON object should be equal to A11y object")
}

func TestA11yUnmarshalConformsToArray(t *testing.T) {
var m A11y
assert.NoError(t, json.Unmarshal([]byte(`{"conformsTo": ["http://www.idpf.org/epub/a11y/accessibility-20170105.html#wcag-a", "https://profile2"]}`), &m))
require.NoError(t, json.Unmarshal([]byte(`{"conformsTo": ["http://www.idpf.org/epub/a11y/accessibility-20170105.html#wcag-a", "https://profile2"]}`), &m))
var e A11y = NewA11y()
e.ConformsTo = []A11yProfile{EPUBA11y10WCAG20A, "https://profile2"}
assert.Equal(t, e, m, "unmarshalled JSON object should be equal to A11y object")
Expand All @@ -107,7 +108,7 @@ func TestA11ySortConformsTo(t *testing.T) {

func TestA11yUnmarshalAccessModeSufficientContainingBothStringsAndArrays(t *testing.T) {
var m A11y
assert.NoError(t, json.Unmarshal([]byte(`{"accessModeSufficient": ["auditory", ["visual", "tactile"], [], "visual"]}`), &m))
require.NoError(t, json.Unmarshal([]byte(`{"accessModeSufficient": ["auditory", ["visual", "tactile"], [], "visual"]}`), &m))
var e A11y = NewA11y()
e.AccessModesSufficient = [][]A11yPrimaryAccessMode{
{A11yPrimaryAccessModeAuditory},
Expand All @@ -127,7 +128,7 @@ func TestA11yMarshalMinimalJSON(t *testing.T) {
Exemptions: []A11yExemption{},
}
data, err := json.Marshal(m)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, data, []byte(`{}`), "unmarshalled JSON object should be equal to A11y object")
}

Expand Down Expand Up @@ -169,7 +170,7 @@ func TestA11yMarshalFullJSON(t *testing.T) {
},
}
data, err := json.Marshal(m)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(
t,
data,
Expand Down
Loading