github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/resources/images/exif/exif_test.go (about)

     1  // Copyright 2019 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package exif
    15  
    16  import (
    17  	"encoding/json"
    18  	"math/big"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/gohugoio/hugo/htesting/hqt"
    25  	"github.com/google/go-cmp/cmp"
    26  
    27  	qt "github.com/frankban/quicktest"
    28  )
    29  
    30  func TestExif(t *testing.T) {
    31  	c := qt.New(t)
    32  	f, err := os.Open(filepath.FromSlash("../../testdata/sunset.jpg"))
    33  	c.Assert(err, qt.IsNil)
    34  	defer f.Close()
    35  
    36  	d, err := NewDecoder(IncludeFields("Lens|Date"))
    37  	c.Assert(err, qt.IsNil)
    38  	x, err := d.Decode(f)
    39  	c.Assert(err, qt.IsNil)
    40  	c.Assert(x.Date.Format("2006-01-02"), qt.Equals, "2017-10-27")
    41  
    42  	// Malaga: https://goo.gl/taazZy
    43  	c.Assert(x.Lat, qt.Equals, float64(36.59744166666667))
    44  	c.Assert(x.Long, qt.Equals, float64(-4.50846))
    45  
    46  	v, found := x.Tags["LensModel"]
    47  	c.Assert(found, qt.Equals, true)
    48  	lensModel, ok := v.(string)
    49  	c.Assert(ok, qt.Equals, true)
    50  	c.Assert(lensModel, qt.Equals, "smc PENTAX-DA* 16-50mm F2.8 ED AL [IF] SDM")
    51  
    52  	v, found = x.Tags["DateTime"]
    53  	c.Assert(found, qt.Equals, true)
    54  	c.Assert(v, hqt.IsSameType, time.Time{})
    55  
    56  	// Verify that it survives a round-trip to JSON and back.
    57  	data, err := json.Marshal(x)
    58  	c.Assert(err, qt.IsNil)
    59  	x2 := &Exif{}
    60  	err = json.Unmarshal(data, x2)
    61  
    62  	c.Assert(x2, eq, x)
    63  }
    64  
    65  func TestExifPNG(t *testing.T) {
    66  	c := qt.New(t)
    67  
    68  	f, err := os.Open(filepath.FromSlash("../../testdata/gohugoio.png"))
    69  	c.Assert(err, qt.IsNil)
    70  	defer f.Close()
    71  
    72  	d, err := NewDecoder()
    73  	c.Assert(err, qt.IsNil)
    74  	_, err = d.Decode(f)
    75  	c.Assert(err, qt.Not(qt.IsNil))
    76  }
    77  
    78  func TestIssue8079(t *testing.T) {
    79  	c := qt.New(t)
    80  
    81  	f, err := os.Open(filepath.FromSlash("../../testdata/iss8079.jpg"))
    82  	c.Assert(err, qt.IsNil)
    83  	defer f.Close()
    84  
    85  	d, err := NewDecoder()
    86  	c.Assert(err, qt.IsNil)
    87  	x, err := d.Decode(f)
    88  	c.Assert(err, qt.IsNil)
    89  	c.Assert(x.Tags["ImageDescription"], qt.Equals, "Città del Vaticano #nanoblock #vatican #vaticancity")
    90  }
    91  
    92  func TestNullString(t *testing.T) {
    93  	c := qt.New(t)
    94  
    95  	for _, test := range []struct {
    96  		in     string
    97  		expect string
    98  	}{
    99  		{"foo", "foo"},
   100  		{"\x20", "\x20"},
   101  		{"\xc4\x81", "\xc4\x81"}, // \u0101
   102  		{"\u0160", "\u0160"},     // non-breaking space
   103  	} {
   104  		res := nullString([]byte(test.in))
   105  		c.Assert(res, qt.Equals, test.expect)
   106  	}
   107  }
   108  
   109  func BenchmarkDecodeExif(b *testing.B) {
   110  	c := qt.New(b)
   111  	f, err := os.Open(filepath.FromSlash("../../testdata/sunset.jpg"))
   112  	c.Assert(err, qt.IsNil)
   113  	defer f.Close()
   114  
   115  	d, err := NewDecoder()
   116  	c.Assert(err, qt.IsNil)
   117  
   118  	b.ResetTimer()
   119  	for i := 0; i < b.N; i++ {
   120  		_, err = d.Decode(f)
   121  		c.Assert(err, qt.IsNil)
   122  		f.Seek(0, 0)
   123  	}
   124  }
   125  
   126  var eq = qt.CmpEquals(
   127  	cmp.Comparer(
   128  		func(v1, v2 *big.Rat) bool {
   129  			return v1.RatString() == v2.RatString()
   130  		},
   131  	),
   132  	cmp.Comparer(func(v1, v2 time.Time) bool {
   133  		return v1.Unix() == v2.Unix()
   134  	}),
   135  )