edge/pkg/bundle/oldzim/zim_test.go

154 lines
2.7 KiB
Go
Raw Normal View History

2023-10-11 11:18:32 +02:00
package zim
import (
"path/filepath"
"reflect"
"runtime"
"testing"
"github.com/pkg/errors"
)
var testCases = []func(t *testing.T, z *ZimReader){
testOpen,
testData,
testDisplayArticle,
testDisplayInfost,
testFavicon,
testListArticles,
testMainPage,
testMetadata,
testMime,
testURLAtIdx,
}
func TestZim(t *testing.T) {
zimFiles, err := filepath.Glob("testdata/*.zim")
if err != nil {
t.Fatalf("%+v", errors.WithStack(err))
}
for _, zf := range zimFiles {
zr, err := NewReader(zf)
if err != nil {
t.Fatalf("%+v", errors.WithStack(err))
}
base := filepath.Base(zf)
t.Run(base, func(t *testing.T) {
for _, fn := range testCases {
testName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
t.Run(testName, func(t *testing.T) {
fn(t, zr)
})
}
})
}
}
func testOpen(t *testing.T, zr *ZimReader) {
if zr.ArticleCount == 0 {
t.Errorf("No article found")
}
}
func testMime(t *testing.T, zr *ZimReader) {
if len(zr.MimeTypes()) == 0 {
t.Errorf("No mime types found")
}
}
func testDisplayInfost(t *testing.T, zr *ZimReader) {
info := zr.String()
if len(info) < 0 {
t.Errorf("Can't read infos")
}
t.Log(info)
}
func testURLAtIdx(t *testing.T, zr *ZimReader) {
// addr 0 is a redirect
p, _ := zr.OffsetAtURLIdx(5)
a, _ := zr.ArticleAt(p)
if a == nil {
t.Errorf("Can't find 1st url")
}
}
func testDisplayArticle(t *testing.T, zr *ZimReader) {
// addr 0 is a redirect
p, _ := zr.OffsetAtURLIdx(5)
a, _ := zr.ArticleAt(p)
if a == nil {
t.Errorf("Can't find 1st url")
}
t.Log(a)
}
func testListArticles(t *testing.T, zr *ZimReader) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
var i uint32
for a := range zr.ListArticles() {
i++
t.Log(a.String())
}
if i == 0 {
t.Errorf("Can't find any urls")
}
if i != zr.ArticleCount-1 {
t.Errorf("Can't find the exact ArticleCount urls %d vs %d", i, zr.ArticleCount)
}
}
func testMainPage(t *testing.T, zr *ZimReader) {
a, _ := zr.MainPage()
if a == nil {
t.Errorf("Can't find the mainpage article")
}
t.Log(a)
}
func testFavicon(t *testing.T, zr *ZimReader) {
favicon, err := zr.Favicon()
if err != nil {
t.Errorf("%+v", errors.WithStack(err))
}
if favicon == nil {
t.Errorf("Can't find the favicon article")
}
}
func testMetadata(t *testing.T, zr *ZimReader) {
metadata, err := zr.Metadata()
if err != nil {
t.Errorf("%+v", errors.WithStack(err))
}
if metadata == nil {
t.Errorf("Can't find the metadata")
}
}
func testData(t *testing.T, zr *ZimReader) {
// addr 0 is a redirect
p, _ := zr.OffsetAtURLIdx(5)
a, _ := zr.ArticleAt(p)
b, _ := a.Data()
data := string(b)
if a.EntryType != RedirectEntry {
if len(data) == 0 {
t.Error("can't read data")
}
}
t.Log(a.String())
t.Log(data)
}