hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

methods_test.go (2601B)

    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 codegen
   15 
   16 import (
   17 	"fmt"
   18 	"net"
   19 	"os"
   20 	"reflect"
   21 	"testing"
   22 
   23 	qt "github.com/frankban/quicktest"
   24 	"github.com/gohugoio/hugo/common/herrors"
   25 )
   26 
   27 func TestMethods(t *testing.T) {
   28 	var (
   29 		zeroIE     = reflect.TypeOf((*IEmbed)(nil)).Elem()
   30 		zeroIEOnly = reflect.TypeOf((*IEOnly)(nil)).Elem()
   31 		zeroI      = reflect.TypeOf((*I)(nil)).Elem()
   32 	)
   33 
   34 	dir, _ := os.Getwd()
   35 	insp := NewInspector(dir)
   36 
   37 	t.Run("MethodsFromTypes", func(t *testing.T) {
   38 		c := qt.New(t)
   39 
   40 		methods := insp.MethodsFromTypes([]reflect.Type{zeroI}, nil)
   41 
   42 		methodsStr := fmt.Sprint(methods)
   43 
   44 		c.Assert(methodsStr, qt.Contains, "Method1(arg0 herrors.ErrorContext)")
   45 		c.Assert(methodsStr, qt.Contains, "Method7() interface {}")
   46 		c.Assert(methodsStr, qt.Contains, "Method0() string\n Method4() string")
   47 		c.Assert(methodsStr, qt.Contains, "MethodEmbed3(arg0 string) string\n MethodEmbed1() string")
   48 
   49 		c.Assert(methods.Imports(), qt.Contains, "github.com/gohugoio/hugo/common/herrors")
   50 	})
   51 
   52 	t.Run("EmbedOnly", func(t *testing.T) {
   53 		c := qt.New(t)
   54 
   55 		methods := insp.MethodsFromTypes([]reflect.Type{zeroIEOnly}, nil)
   56 
   57 		methodsStr := fmt.Sprint(methods)
   58 
   59 		c.Assert(methodsStr, qt.Contains, "MethodEmbed3(arg0 string) string")
   60 	})
   61 
   62 	t.Run("ToMarshalJSON", func(t *testing.T) {
   63 		c := qt.New(t)
   64 
   65 		m, pkg := insp.MethodsFromTypes(
   66 			[]reflect.Type{zeroI},
   67 			[]reflect.Type{zeroIE}).ToMarshalJSON("*page", "page")
   68 
   69 		c.Assert(m, qt.Contains, "method6 := p.Method6()")
   70 		c.Assert(m, qt.Contains, "Method0: method0,")
   71 		c.Assert(m, qt.Contains, "return json.Marshal(&s)")
   72 
   73 		c.Assert(pkg, qt.Contains, "github.com/gohugoio/hugo/common/herrors")
   74 		c.Assert(pkg, qt.Contains, "encoding/json")
   75 
   76 		fmt.Println(pkg)
   77 	})
   78 }
   79 
   80 type I interface {
   81 	IEmbed
   82 	Method0() string
   83 	Method4() string
   84 	Method1(myerr herrors.ErrorContext)
   85 	Method3(myint int, mystring string)
   86 	Method5() (string, error)
   87 	Method6() *net.IP
   88 	Method7() any
   89 	Method8() herrors.ErrorContext
   90 	method2()
   91 	method9() os.FileInfo
   92 }
   93 
   94 type IEOnly interface {
   95 	IEmbed
   96 }