github.com/snowflakedb/gosnowflake@v1.9.0/assert_test.go (about)

     1  // Copyright (c) 2023 Snowflake Computing Inc. All rights reserved.
     2  
     3  package gosnowflake
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  	"reflect"
     9  	"runtime"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func assertNilE(t *testing.T, actual any, descriptions ...string) {
    15  	errorOnNonEmpty(t, validateNil(actual, descriptions...))
    16  }
    17  
    18  func assertNilF(t *testing.T, actual any, descriptions ...string) {
    19  	fatalOnNonEmpty(t, validateNil(actual, descriptions...))
    20  }
    21  
    22  func assertNotNilE(t *testing.T, actual any, descriptions ...string) {
    23  	errorOnNonEmpty(t, validateNotNil(actual, descriptions...))
    24  }
    25  
    26  func assertNotNilF(t *testing.T, actual any, descriptions ...string) {
    27  	fatalOnNonEmpty(t, validateNotNil(actual, descriptions...))
    28  }
    29  
    30  func assertEqualE(t *testing.T, actual any, expected any, descriptions ...string) {
    31  	errorOnNonEmpty(t, validateEqual(actual, expected, descriptions...))
    32  }
    33  
    34  func assertEqualF(t *testing.T, actual any, expected any, descriptions ...string) {
    35  	fatalOnNonEmpty(t, validateEqual(actual, expected, descriptions...))
    36  }
    37  
    38  func assertNotEqualF(t *testing.T, actual any, expected any, descriptions ...string) {
    39  	fatalOnNonEmpty(t, validateNotEqual(actual, expected, descriptions...))
    40  }
    41  
    42  func assertBytesEqualE(t *testing.T, actual []byte, expected []byte, descriptions ...string) {
    43  	errorOnNonEmpty(t, validateBytesEqual(actual, expected, descriptions...))
    44  }
    45  
    46  func assertTrueF(t *testing.T, actual bool, descriptions ...string) {
    47  	fatalOnNonEmpty(t, validateEqual(actual, true, descriptions...))
    48  }
    49  
    50  func assertFalseF(t *testing.T, actual bool, descriptions ...string) {
    51  	fatalOnNonEmpty(t, validateEqual(actual, false, descriptions...))
    52  }
    53  
    54  func assertStringContainsE(t *testing.T, actual string, expectedToContain string, descriptions ...string) {
    55  	errorOnNonEmpty(t, validateStringContains(actual, expectedToContain, descriptions...))
    56  }
    57  
    58  func assertStringContainsF(t *testing.T, actual string, expectedToContain string, descriptions ...string) {
    59  	fatalOnNonEmpty(t, validateStringContains(actual, expectedToContain, descriptions...))
    60  }
    61  
    62  func assertHasPrefixE(t *testing.T, actual string, expectedPrefix string, descriptions ...string) {
    63  	errorOnNonEmpty(t, validateHasPrefix(actual, expectedPrefix, descriptions...))
    64  }
    65  
    66  func assertBetweenE(t *testing.T, value float64, min float64, max float64, descriptions ...string) {
    67  	errorOnNonEmpty(t, validateValueBetween(value, min, max, descriptions...))
    68  }
    69  
    70  func assertBetweenInclusiveE(t *testing.T, value float64, min float64, max float64, descriptions ...string) {
    71  	errorOnNonEmpty(t, validateValueBetweenInclusive(value, min, max, descriptions...))
    72  }
    73  
    74  func assertEmptyE[T any](t *testing.T, actual []T, descriptions ...string) {
    75  	errorOnNonEmpty(t, validateEmpty(actual, descriptions...))
    76  }
    77  
    78  func fatalOnNonEmpty(t *testing.T, errMsg string) {
    79  	if errMsg != "" {
    80  		t.Fatal(formatErrorMessage(errMsg))
    81  	}
    82  }
    83  
    84  func errorOnNonEmpty(t *testing.T, errMsg string) {
    85  	if errMsg != "" {
    86  		t.Error(formatErrorMessage(errMsg))
    87  	}
    88  }
    89  
    90  func formatErrorMessage(errMsg string) string {
    91  	return fmt.Sprintf("%s. Thrown from %s", errMsg, thrownFrom())
    92  }
    93  
    94  func validateNil(actual any, descriptions ...string) string {
    95  	if isNil(actual) {
    96  		return ""
    97  	}
    98  	desc := joinDescriptions(descriptions...)
    99  	return fmt.Sprintf("expected \"%s\" to be nil but was not. %s", actual, desc)
   100  }
   101  
   102  func validateNotNil(actual any, descriptions ...string) string {
   103  	if !isNil(actual) {
   104  		return ""
   105  	}
   106  	desc := joinDescriptions(descriptions...)
   107  	return fmt.Sprintf("expected to be not nil but was not. %s", desc)
   108  }
   109  
   110  func validateEqual(actual any, expected any, descriptions ...string) string {
   111  	if expected == actual {
   112  		return ""
   113  	}
   114  	desc := joinDescriptions(descriptions...)
   115  	return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s", actual, expected, desc)
   116  }
   117  
   118  func validateNotEqual(actual any, expected any, descriptions ...string) string {
   119  	if expected != actual {
   120  		return ""
   121  	}
   122  	desc := joinDescriptions(descriptions...)
   123  	return fmt.Sprintf("expected \"%s\" not to be equal to \"%s\" but they were the same. %s", actual, expected, desc)
   124  }
   125  
   126  func validateBytesEqual(actual []byte, expected []byte, descriptions ...string) string {
   127  	if bytes.Equal(actual, expected) {
   128  		return ""
   129  	}
   130  	desc := joinDescriptions(descriptions...)
   131  	return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s", actual, expected, desc)
   132  }
   133  
   134  func validateStringContains(actual string, expectedToContain string, descriptions ...string) string {
   135  	if strings.Contains(actual, expectedToContain) {
   136  		return ""
   137  	}
   138  	desc := joinDescriptions(descriptions...)
   139  	return fmt.Sprintf("expected \"%s\" to contain \"%s\" but did not. %s", actual, expectedToContain, desc)
   140  }
   141  
   142  func validateHasPrefix(actual string, expectedPrefix string, descriptions ...string) string {
   143  	if strings.HasPrefix(actual, expectedPrefix) {
   144  		return ""
   145  	}
   146  	desc := joinDescriptions(descriptions...)
   147  	return fmt.Sprintf("expected \"%s\" to start with \"%s\" but did not. %s", actual, expectedPrefix, desc)
   148  }
   149  
   150  func validateValueBetween(value float64, min float64, max float64, descriptions ...string) string {
   151  	if value > min && value < max {
   152  		return ""
   153  	}
   154  	desc := joinDescriptions(descriptions...)
   155  	return fmt.Sprintf("expected \"%f\" should be between \"%f\" and  \"%f\" but did not. %s", value, min, max, desc)
   156  }
   157  
   158  func validateValueBetweenInclusive(value float64, min float64, max float64, descriptions ...string) string {
   159  	if value >= min && value <= max {
   160  		return ""
   161  	}
   162  	desc := joinDescriptions(descriptions...)
   163  	return fmt.Sprintf("expected \"%f\" should be between \"%f\" and  \"%f\" inclusively but did not. %s", value, min, max, desc)
   164  }
   165  
   166  func validateEmpty[T any](value []T, descriptions ...string) string {
   167  	if len(value) == 0 {
   168  		return ""
   169  	}
   170  	desc := joinDescriptions(descriptions...)
   171  	return fmt.Sprintf("expected \"%v\" to be empty. %s", value, desc)
   172  }
   173  
   174  func joinDescriptions(descriptions ...string) string {
   175  	return strings.Join(descriptions, " ")
   176  }
   177  
   178  func isNil(value any) bool {
   179  	if value == nil {
   180  		return true
   181  	}
   182  	val := reflect.ValueOf(value)
   183  	return val.Kind() == reflect.Pointer && val.IsNil()
   184  }
   185  
   186  func thrownFrom() string {
   187  	buf := make([]byte, 1024)
   188  	size := runtime.Stack(buf, false)
   189  	stack := string(buf[0:size])
   190  	lines := strings.Split(stack, "\n\t")
   191  	for i, line := range lines {
   192  		if i > 0 && !strings.Contains(line, "assert_test.go") {
   193  			return line
   194  		}
   195  	}
   196  	return stack
   197  }