mirror of
https://github.com/Belphemur/CBZOptimizer.git
synced 2025-10-14 04:28:51 +02:00
test: add more converter tests
This commit is contained in:
@@ -75,6 +75,11 @@ func (converter *Converter) ConvertChapter(chapter *packer2.Chapter, quality uin
|
||||
defer wgConvertedPages.Done()
|
||||
convertedPage, err := converter.convertPage(pageToConvert, quality)
|
||||
if err != nil {
|
||||
if convertedPage == nil {
|
||||
errChan <- err
|
||||
<-guard
|
||||
return
|
||||
}
|
||||
buffer := new(bytes.Buffer)
|
||||
err := png.Encode(buffer, convertedPage.Image)
|
||||
if err != nil {
|
||||
@@ -101,7 +106,7 @@ func (converter *Converter) ConvertChapter(chapter *packer2.Chapter, quality uin
|
||||
|
||||
splitNeeded, img, format, err := converter.checkPageNeedsSplit(page)
|
||||
if err != nil {
|
||||
errChan <- fmt.Errorf("error checking if page %d of chapter %s needs split: %v", page.Index, chapter.FilePath, err)
|
||||
errChan <- fmt.Errorf("error checking if page %d of genTestChapter %s needs split: %v", page.Index, chapter.FilePath, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -112,7 +117,7 @@ func (converter *Converter) ConvertChapter(chapter *packer2.Chapter, quality uin
|
||||
}
|
||||
images, err := converter.cropImage(img)
|
||||
if err != nil {
|
||||
errChan <- fmt.Errorf("error converting page %d of chapter %s to webp: %v", page.Index, chapter.FilePath, err)
|
||||
errChan <- fmt.Errorf("error converting page %d of genTestChapter %s to webp: %v", page.Index, chapter.FilePath, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@@ -11,16 +11,35 @@ import (
|
||||
|
||||
func TestConvertChapter(t *testing.T) {
|
||||
|
||||
// Load test chapter from testdata
|
||||
testCases := []struct {
|
||||
name string
|
||||
genTestChapter func(path string) (*packer.Chapter, error)
|
||||
}{
|
||||
{
|
||||
name: "All split pages",
|
||||
genTestChapter: loadTestChapterSplit,
|
||||
},
|
||||
{
|
||||
name: "No split pages",
|
||||
genTestChapter: loadTestChapterNoSplit,
|
||||
},
|
||||
{
|
||||
name: "Mix of split and no split pages",
|
||||
genTestChapter: loadTestChapterMixSplit,
|
||||
},
|
||||
}
|
||||
// Load test genTestChapter from testdata
|
||||
temp, err := os.CreateTemp("", "test_chapter_*.cbz")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temporary file: %v", err)
|
||||
|
||||
}
|
||||
defer os.Remove(temp.Name())
|
||||
chapter, err := loadTestChapter(temp.Name())
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
chapter, err := tc.genTestChapter(temp.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load test chapter: %v", err)
|
||||
t.Fatalf("failed to load test genTestChapter: %v", err)
|
||||
}
|
||||
|
||||
converter := New()
|
||||
@@ -32,7 +51,7 @@ func TestConvertChapter(t *testing.T) {
|
||||
|
||||
convertedChapter, err := converter.ConvertChapter(chapter, quality, progress)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to convert chapter: %v", err)
|
||||
t.Fatalf("failed to convert genTestChapter: %v", err)
|
||||
}
|
||||
|
||||
if len(convertedChapter.Pages) == 0 {
|
||||
@@ -44,9 +63,11 @@ func TestConvertChapter(t *testing.T) {
|
||||
t.Errorf("page %d was not converted to webp format", page.Index)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func loadTestChapter(path string) (*packer.Chapter, error) {
|
||||
func loadTestChapterSplit(path string) (*packer.Chapter, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -74,3 +95,61 @@ func loadTestChapter(path string) (*packer.Chapter, error) {
|
||||
Pages: pages,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func loadTestChapterNoSplit(path string) (*packer.Chapter, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var pages []*packer.Page
|
||||
for i := 0; i < 5; i++ { // Assuming there are 5 pages for the test
|
||||
img := image.NewRGBA(image.Rect(0, 0, 300, 1000))
|
||||
buf := new(bytes.Buffer)
|
||||
err := jpeg.Encode(buf, img, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
page := &packer.Page{
|
||||
Index: uint16(i),
|
||||
Contents: buf,
|
||||
Extension: ".jpg",
|
||||
}
|
||||
pages = append(pages, page)
|
||||
}
|
||||
|
||||
return &packer.Chapter{
|
||||
FilePath: path,
|
||||
Pages: pages,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func loadTestChapterMixSplit(path string) (*packer.Chapter, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var pages []*packer.Page
|
||||
for i := 0; i < 5; i++ { // Assuming there are 5 pages for the test
|
||||
img := image.NewRGBA(image.Rect(0, 0, 300, 1000*(i+1)))
|
||||
buf := new(bytes.Buffer)
|
||||
err := jpeg.Encode(buf, img, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
page := &packer.Page{
|
||||
Index: uint16(i),
|
||||
Contents: buf,
|
||||
Extension: ".jpg",
|
||||
}
|
||||
pages = append(pages, page)
|
||||
}
|
||||
|
||||
return &packer.Chapter{
|
||||
FilePath: path,
|
||||
Pages: pages,
|
||||
}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user