github.com/searKing/golang/go@v1.2.74/util/class/class_test.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package class_test
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/searKing/golang/go/util/class"
    12  )
    13  
    14  type Peter interface {
    15  	Name() string
    16  	VirtualUpperName() string
    17  	UpperName() string
    18  }
    19  
    20  type Pet struct {
    21  	class.Class
    22  }
    23  
    24  func (pet *Pet) Name() string {
    25  	return "pet"
    26  }
    27  
    28  func (pet *Pet) VirtualUpperName() string {
    29  	p := pet.GetDerivedElse(pet).(Peter)
    30  	return strings.ToUpper(p.Name())
    31  }
    32  
    33  func (pet *Pet) UpperName() string {
    34  	return strings.ToUpper(pet.Name())
    35  }
    36  
    37  //Dog derived from Pet
    38  type Dog struct {
    39  	Pet
    40  }
    41  
    42  func NewDogEmbedded() *Dog {
    43  	return &Dog{}
    44  }
    45  
    46  func NewDogDerived() *Dog {
    47  	dog := &Dog{}
    48  	dog.SetDerived(dog)
    49  	return dog
    50  }
    51  
    52  func (dog *Dog) Name() string {
    53  	return "dog"
    54  }
    55  
    56  type ClassTests struct {
    57  	input  Peter
    58  	output []string
    59  }
    60  
    61  var classTests = []ClassTests{
    62  	{
    63  		input:  NewDogEmbedded(),
    64  		output: []string{"dog", "PET", "PET"},
    65  	},
    66  	{
    67  		input:  NewDogDerived(),
    68  		output: []string{"dog", "DOG", "PET"},
    69  	},
    70  }
    71  
    72  func TestClass(t *testing.T) {
    73  	for n, test := range classTests {
    74  		gots := []string{test.input.Name(), test.input.VirtualUpperName(), test.input.UpperName()}
    75  		for i, got := range gots {
    76  			if got != test.output[i] {
    77  				t.Errorf("#%d[%d]: %v: got %s runs; expected %s", n, i, test.input, got, test.output[i])
    78  			}
    79  		}
    80  	}
    81  }