gitlab.com/evatix-go/core@v1.3.55/coretests/BaseTestCase.go (about)

     1  package coretests
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/smartystreets/goconvey/convey"
    10  	"gitlab.com/evatix-go/core/constants"
    11  	"gitlab.com/evatix-go/core/errcore"
    12  	"gitlab.com/evatix-go/core/internal/reflectinternal"
    13  )
    14  
    15  type BaseTestCase struct {
    16  	Title                                                           string // consider as header
    17  	ArrangeInput, ActualInput, ExpectedInput                        interface{}
    18  	ArrangeExpectedType, ActualExpectedType, ExpectedTypeOfExpected reflect.Type
    19  	HasError                                                        bool
    20  	IsValidateError                                                 bool
    21  }
    22  
    23  func (it *BaseTestCase) CaseTitle() string {
    24  	return it.Title
    25  }
    26  
    27  func (it *BaseTestCase) TypesValidationMustPasses(t *testing.T) {
    28  	err := it.TypeValidationError()
    29  
    30  	if err != nil {
    31  		t.Error(
    32  			"any one of the type validation failed",
    33  			err.Error())
    34  	}
    35  }
    36  
    37  func (it *BaseTestCase) TypeValidationError() error {
    38  	var sliceErr []string
    39  	arrangeInputActualType := reflect.TypeOf(it.ArrangeInput)
    40  	actualInputActualType := reflect.TypeOf(it.ActualInput)
    41  	expectedInputActualType := reflect.TypeOf(it.ExpectedInput)
    42  
    43  	if reflectinternal.IsNotNull(it.ArrangeInput) && arrangeInputActualType != it.ArrangeExpectedType {
    44  		sliceErr = append(
    45  			sliceErr,
    46  			errcore.ExpectingSimpleNoType(
    47  				"Arrange Type Mismatch",
    48  				it.ArrangeExpectedType,
    49  				arrangeInputActualType))
    50  	}
    51  
    52  	if reflectinternal.IsNotNull(it.ActualInput) && actualInputActualType != it.ActualExpectedType {
    53  		sliceErr = append(
    54  			sliceErr,
    55  			errcore.ExpectingSimpleNoType(
    56  				"Actual Type Mismatch",
    57  				it.ActualExpectedType,
    58  				actualInputActualType))
    59  	}
    60  
    61  	if reflectinternal.IsNotNull(it.ExpectedInput) && expectedInputActualType != it.ExpectedTypeOfExpected {
    62  		sliceErr = append(
    63  			sliceErr,
    64  			errcore.ExpectingSimpleNoType(
    65  				"Expected Type Mismatch",
    66  				it.ExpectedTypeOfExpected,
    67  				expectedInputActualType))
    68  	}
    69  
    70  	return errcore.SliceToError(sliceErr)
    71  }
    72  
    73  func (it *BaseTestCase) ArrangeString() string {
    74  	return fmt.Sprintf(
    75  		constants.SprintValueFormat,
    76  		it.ArrangeInput)
    77  }
    78  
    79  func (it *BaseTestCase) Input() interface{} {
    80  	return it.ArrangeInput
    81  }
    82  
    83  func (it *BaseTestCase) Expected() interface{} {
    84  	return it.ExpectedInput
    85  }
    86  
    87  func (it *BaseTestCase) ExpectedString() string {
    88  	return fmt.Sprintf(
    89  		constants.SprintValueFormat,
    90  		it.ExpectedInput)
    91  }
    92  
    93  func (it *BaseTestCase) Actual() interface{} {
    94  	return it.ActualInput
    95  }
    96  
    97  func (it *BaseTestCase) ActualString() string {
    98  	return fmt.Sprintf(
    99  		constants.SprintValueFormat,
   100  		it.ActualInput)
   101  }
   102  
   103  func (it *BaseTestCase) SetActual(actual interface{}) {
   104  	it.ActualInput = actual
   105  }
   106  
   107  func (it *BaseTestCase) String(caseIndex int) string {
   108  	return GetAssertMessageUsingSimpleTestCaseWrapper(
   109  		caseIndex, it)
   110  }
   111  
   112  func (it *BaseTestCase) ShouldBe(
   113  	caseIndex int,
   114  	t *testing.T,
   115  	assert convey.Assertion,
   116  	actual interface{},
   117  ) {
   118  	it.ShouldBeExplicit(
   119  		true,
   120  		caseIndex,
   121  		t,
   122  		it.Title,
   123  		actual,
   124  		assert,
   125  		it.Expected())
   126  }
   127  
   128  func (it *BaseTestCase) ShouldBeExplicit(
   129  	isValidateType bool,
   130  	caseIndex int,
   131  	t *testing.T,
   132  	title string,
   133  	actual interface{},
   134  	assert convey.Assertion,
   135  	expected interface{},
   136  ) {
   137  	it.SetActual(actual)
   138  
   139  	convey.Convey(title, t, func() {
   140  		convey.SoMsg(it.String(caseIndex), actual, assert, expected)
   141  	})
   142  
   143  	if !isValidateType {
   144  		return
   145  	}
   146  
   147  	err := it.TypeValidationError()
   148  	errHeader := fmt.Sprintf(
   149  		"case %d : test case type validation must passes",
   150  		caseIndex)
   151  
   152  	if err != nil {
   153  		err = errors.New(errHeader + err.Error() + ", case title : " + title)
   154  	}
   155  
   156  	convey.Convey(errHeader, t, func() {
   157  		convey.So(err, convey.ShouldBeNil)
   158  	})
   159  }
   160  
   161  func (it *BaseTestCase) AsSimpleTestCaseWrapper() SimpleTestCaseWrapper {
   162  	return it
   163  }