|
| 1 | +// Copyright (c) 2023 Snowflake Computing Inc. All rights reserved. |
| 2 | + |
| 3 | +package gosnowflake |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "reflect" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func assertNilF(t *testing.T, actual any, descriptions ...string) { |
| 13 | + fatalOnNonEmpty(t, validateNil(actual, descriptions...)) |
| 14 | +} |
| 15 | + |
| 16 | +func assertNotNilF(t *testing.T, actual any, descriptions ...string) { |
| 17 | + fatalOnNonEmpty(t, validateNotNil(actual, descriptions...)) |
| 18 | +} |
| 19 | + |
| 20 | +func assertEqualE(t *testing.T, actual any, expected any, descriptions ...string) { |
| 21 | + errorOnNonEmpty(t, validateEqual(actual, expected, descriptions...)) |
| 22 | +} |
| 23 | + |
| 24 | +func assertStringContainsE(t *testing.T, actual string, expectedToContain string, descriptions ...string) { |
| 25 | + errorOnNonEmpty(t, validateStringContains(actual, expectedToContain, descriptions...)) |
| 26 | +} |
| 27 | + |
| 28 | +func assertHasPrefixE(t *testing.T, actual string, expectedPrefix string, descriptions ...string) { |
| 29 | + errorOnNonEmpty(t, validateHasPrefix(actual, expectedPrefix, descriptions...)) |
| 30 | +} |
| 31 | + |
| 32 | +func fatalOnNonEmpty(t *testing.T, errMsg string) { |
| 33 | + if errMsg != "" { |
| 34 | + t.Fatal(errMsg) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func errorOnNonEmpty(t *testing.T, errMsg string) { |
| 39 | + if errMsg != "" { |
| 40 | + t.Error(errMsg) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func validateNil(actual any, descriptions ...string) string { |
| 45 | + if isNil(actual) { |
| 46 | + return "" |
| 47 | + } |
| 48 | + desc := joinDescriptions(descriptions...) |
| 49 | + return fmt.Sprintf("expected \"%s\" to be nil but was not. %s", actual, desc) |
| 50 | +} |
| 51 | + |
| 52 | +func validateNotNil(actual any, descriptions ...string) string { |
| 53 | + if !isNil(actual) { |
| 54 | + return "" |
| 55 | + } |
| 56 | + desc := joinDescriptions(descriptions...) |
| 57 | + return fmt.Sprintf("expected to be not nil but was not. %s", desc) |
| 58 | +} |
| 59 | + |
| 60 | +func validateEqual(actual any, expected any, descriptions ...string) string { |
| 61 | + if expected == actual { |
| 62 | + return "" |
| 63 | + } |
| 64 | + desc := joinDescriptions(descriptions...) |
| 65 | + return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s", actual, expected, desc) |
| 66 | +} |
| 67 | + |
| 68 | +func validateStringContains(actual string, expectedToContain string, descriptions ...string) string { |
| 69 | + if strings.Contains(actual, expectedToContain) { |
| 70 | + return "" |
| 71 | + } |
| 72 | + desc := joinDescriptions(descriptions...) |
| 73 | + return fmt.Sprintf("expected \"%s\" to contain \"%s\" but did not. %s", actual, expectedToContain, desc) |
| 74 | +} |
| 75 | + |
| 76 | +func validateHasPrefix(actual string, expectedPrefix string, descriptions ...string) string { |
| 77 | + if strings.HasPrefix(actual, expectedPrefix) { |
| 78 | + return "" |
| 79 | + } |
| 80 | + desc := joinDescriptions(descriptions...) |
| 81 | + return fmt.Sprintf("expected \"%s\" to start with \"%s\" but did not. %s", actual, expectedPrefix, desc) |
| 82 | +} |
| 83 | + |
| 84 | +func joinDescriptions(descriptions ...string) string { |
| 85 | + return strings.Join(descriptions, " ") |
| 86 | +} |
| 87 | + |
| 88 | +func isNil(value any) bool { |
| 89 | + if value == nil { |
| 90 | + return true |
| 91 | + } |
| 92 | + val := reflect.ValueOf(value) |
| 93 | + return val.Kind() == reflect.Pointer && val.IsNil() |
| 94 | +} |
0 commit comments