unipdf/internal/testutils/testutils.go

12 lines
8.7 KiB
Go
Raw Normal View History

2020-08-27 21:45:09 +00:00
//
// Copyright 2020 FoxyUtils ehf. All rights reserved.
//
// This is a commercial product and requires a license to operate.
// A trial license can be obtained at https://unidoc.io
//
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
//
// Use of this source code is governed by the UniDoc End User License Agreement
// terms that can be accessed at https://unidoc.io/eula/
2020-10-19 10:58:10 +00:00
package testutils ;import (_cg "crypto/md5";_cga "encoding/hex";_ff "errors";_e "fmt";_gc "github.com/unidoc/unipdf/v3/common";_d "github.com/unidoc/unipdf/v3/core";_gd "image";_ga "image/png";_cf "io";_cb "os";_gf "os/exec";_fg "path/filepath";_f "strings";_g "testing";);func ParseIndirectObjects (rawpdf string )(map[int64 ]_d .PdfObject ,error ){_dad :=_d .NewParserFromString (rawpdf );_fbd :=map[int64 ]_d .PdfObject {};for {_fd ,_bff :=_dad .ParseIndirectObject ();if _bff !=nil {if _bff ==_cf .EOF {break ;};return nil ,_bff ;};switch _df :=_fd .(type ){case *_d .PdfIndirectObject :_fbd [_df .ObjectNumber ]=_fd ;case *_d .PdfObjectStream :_fbd [_df .ObjectNumber ]=_fd ;};};for _ ,_fed :=range _fbd {_fbb (_fed ,_fbd );};return _fbd ,nil ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_dg ,_ad :=HashFile (file1 );if _ad !=nil {return false ,_ad ;};_cd ,_ad :=HashFile (file2 );if _ad !=nil {return false ,_ad ;};if _dg ==_cd {return true ,nil ;};_ee ,_ad :=ReadPNG (file1 );if _ad !=nil {return false ,_ad ;};_dbe ,_ad :=ReadPNG (file2 );if _ad !=nil {return false ,_ad ;};if _ee .Bounds ()!=_dbe .Bounds (){return false ,nil ;};return CompareImages (_ee ,_dbe );};func CompareImages (img1 ,img2 _gd .Image )(bool ,error ){_a :=img1 .Bounds ();_aa :=0;for _ag :=0;_ag < _a .Size ().X ;_ag ++{for _bb :=0;_bb < _a .Size ().Y ;_bb ++{_bd ,_agc ,_bg ,_ :=img1 .At (_ag ,_bb ).RGBA ();_gdb ,_ce ,_ac ,_ :=img2 .At (_ag ,_bb ).RGBA ();if _bd !=_gdb ||_agc !=_ce ||_bg !=_ac {_aa ++;};};};_bbc :=float64 (_aa )/float64 (_a .Dx ()*_a .Dy ());if _bbc > 0.0001{_e .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_bbc ,_aa );return false ,nil ;};return true ,nil ;};func RunRenderTest (t *_g .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_da :=_f .TrimSuffix (_fg .Base (pdfPath ),_fg .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_eaf *_g .T ){_gee :=_fg .Join (outputDir ,_da );_ed :=_gee +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _gad :=RenderPDFToPNGs (pdfPath ,0,_ed );_gad !=nil {_eaf .Skip (_gad );};for _fb :=1;true ;_fb ++{_bf :=_e .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_gee ,_fb );_cde :=_fg .Join (baselineRenderPath ,_e .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_da ,_fb ));if _ ,_af :=_cb .Stat (_bf );_af !=nil {break ;};_eaf .Logf ("\u0025\u0073",_cde );if _ ,_fe :=_cb .Stat (_cde );_cb .IsNotExist (_fe ){if saveBaseline {_eaf .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_bf ,_cde );CopyFile (_bf ,_cde );continue ;};break ;};_eaf .Run (_e .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_fb ),func (_fac *_g .T ){_fac .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_bf ,_cde );_bfc ,_fgc :=ComparePNGFiles (_bf ,_cde );if _cb .IsNotExist (_fgc ){_fac .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_bfc {_fac .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");};});};});};func CompareDictionariesDeep (d1 ,d2 *_d .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_gc .Log .Debug ("\u0044\u0069\u0063\u0074\u0020\u0065\u006e\u0074\u0072\u0069\u0065\u0073\u0020\u006d\u0069s\u006da\u0074\u0063\u0068\u0020\u0028\u0025\u0064\u0020\u0021\u003d\u0020\u0025\u0064\u0029",len (d1 .Keys ()),len (d2 .Keys ()));_gc .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_fdd :=range d1 .Keys (){if _fdd =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;};_bgdb :=_d .TraceToDirectObject (d1 .Get (_fdd ));_eed :=_d .TraceToDirectObject (d2 .Get (_fdd ));if _bgdb ==nil {_gc .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _eed ==nil {_gc .Log .Debug ("\u00762\u0020\u0