Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wisbery committed Oct 20, 2023
1 parent 2857f5f commit b788b7f
Show file tree
Hide file tree
Showing 24 changed files with 1,011 additions and 1,451 deletions.
77 changes: 23 additions & 54 deletions assert.go → assert/assert.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,65 @@
package oxyde
package assert

import (
"fmt"
"github.com/wisbery/oxyde/common"
"reflect"
)

// AssertNil function asserts that actual value is nil.
// When actual value is not nil, an error is reported.
func AssertNil(actual interface{}) {
if !isNil(actual) {
func Nil(actual interface{}) {
if !common.NilValue(actual) {
displayAssertionError(nil, actual)
}
}

func AssertNotNil(actual interface{}) {
if isNil(actual) {
func NotNil(actual interface{}) {
if reflect.ValueOf(actual).IsNil() {
displayAssertionError("not nil", actual)
}
}

func AssertNilError(e error) {
func NilError(e error) {
if e != nil {
displayAssertionError(nil, e)
}
}

func AssertNilString(actual *string) {
func NilString(actual *string) {
if actual != nil {
displayAssertionError(nil, actual)
}
}

func AssertNotNilString(actual *string) {
func NotNilString(actual *string) {
if actual == nil {
displayAssertionError("not nil", actual)
}
}

func AssertTrue(actual bool) {
func True(actual bool) {
if !actual {
displayAssertionError(true, actual)
}
}

func AssertFalse(actual bool) {
func False(actual bool) {
if actual {
displayAssertionError(false, actual)
}
}

func AssertNotNilId(actual *string) {
AssertNotNilString(actual)
AssertEqualInt(36, len(*actual))
func NotNilId(actual *string) {
NotNilString(actual)
EqualInt(36, len(*actual))
}

func AssertEqualString(expected string, actual string) {
func EqualString(expected string, actual string) {
if !equalString(expected, actual) {
displayAssertionError(expected, actual)
}
}

func AssertEqualStringNullable(expected *string, actual *string) {
func EqualStringNullable(expected *string, actual *string) {
if !equalStringNullable(expected, actual) {
if expected != nil && actual != nil {
displayAssertionError(*expected, *actual)
Expand All @@ -69,19 +68,19 @@ func AssertEqualStringNullable(expected *string, actual *string) {
}
}

func AssertNilInt(actual *int) {
func NilInt(actual *int) {
if actual != nil {
displayAssertionError(nil, actual)
}
}

func AssertEqualInt(expected int, actual int) {
func EqualInt(expected int, actual int) {
if !equalInt(expected, actual) {
displayAssertionError(expected, actual)
}
}

func AssertEqualIntNullable(expected *int, actual *int) {
func EqualIntNullable(expected *int, actual *int) {
if !equalIntNullable(expected, actual) {
if expected != nil && actual != nil {
displayAssertionError(*expected, *actual)
Expand All @@ -90,32 +89,18 @@ func AssertEqualIntNullable(expected *int, actual *int) {
}
}

func AssertEqualInt64Nullable(expected *int64, actual *int64) {
if !equalInt64Nullable(expected, actual) {
if expected != nil && actual != nil {
displayAssertionError(*expected, *actual)
}
displayAssertionError(expected, actual)
}
}

func AssertEqualFloat64(expected float64, actual float64) {
func EqualFloat64(expected float64, actual float64) {
if !equalFloat64(expected, actual) {
displayAssertionError(expected, actual)
}
}

func AssertEqualBool(expected bool, actual bool) {
func EqualBool(expected bool, actual bool) {
if !equalBool(expected, actual) {
displayAssertionError(expected, actual)
}
}

// Function isNil checks if the value specified as parameter is nil.
func isNil(value interface{}) bool {
return value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil())
}

// Function equalString checks if two string values are equal.
func equalString(expected string, actual string) bool {
return expected == actual
Expand Down Expand Up @@ -148,22 +133,6 @@ func equalIntNullable(expected *int, actual *int) bool {
return true
}

// Function equalInt64 checks if two int64 values are equal.
func equalInt64(expected int64, actual int64) bool {
return expected == actual
}

// Function equalInt64Nullable checks if two pointers to int64 values are equal.
func equalInt64Nullable(expected *int64, actual *int64) bool {
if expected != nil && actual != nil {
return equalInt64(*expected, *actual)
}
if expected != nil || actual != nil {
return false
}
return true
}

// Function equalFloat64 checks if two float64 values are equal.
func equalFloat64(expected float64, actual float64) bool {
return expected == actual
Expand All @@ -176,11 +145,11 @@ func equalBool(expected bool, actual bool) bool {

// Function displayAssertionError displays assertion error details.
func displayAssertionError(expected interface{}, actual interface{}) {
separator := makeText("-", 120)
separator := common.MakeString('-', 120)
fmt.Printf("\n\n%s\n> ERROR: assertion error\n> Expected: %+v\n> Actual: %+v\n%s\n\n",
separator,
expected,
actual,
separator)
brexit()
common.BrExit()
}
6 changes: 2 additions & 4 deletions assert_test.go → assert/assert_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package oxyde
package assert

import (
"testing"
)
import "testing"

func TestEqualStrings(t *testing.T) {
if !equalString("string", "string") {
Expand Down
103 changes: 103 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package common

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"github.com/google/uuid"
"os"
"reflect"
"regexp"
"runtime"
)

const (
ApiTagName = "api" // Name of the tag in which documentation details are stored.
JsonTagName = "json" // Name of the tag in which JSON details are stored.
OptionalPrefix = "?" // Prefix used to mark th field as optional.
)

// Function MakeString creates a string of length 'len' containing the same character 'ch'.
func MakeString(ch byte, len int) string {
b := make([]byte, len)
for i := 0; i < len; i++ {
b[i] = ch
}
return string(b)
}

// Function NilValue checks if the value specified as parameter is nil.
func NilValue(value interface{}) bool {
return value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil())
}

// Function PrettyPrint takes JSON string as an argument
// and returns the same JSON but pretty-printed.
func PrettyPrint(in []byte) string {
var out bytes.Buffer
err := json.Indent(&out, in, "", " ")
if err != nil {
return string(in)
}
return out.String()
}

// Function PanicOnError panics when the error passed as argument is not nil.
func PanicOnError(err error) {
if err != nil {
panic(err)
}
}

// Function GenerateId generated unique identifier in form of UUID string.
func GenerateId() string {
id, err := uuid.NewRandom()
PanicOnError(err)
return id.String()
}

// Function TypeOfValue returns the type of value specified as parameter.
// If specified value is a pointer, then is first dereferenced.
func TypeOfValue(value interface{}) reflect.Type {
t := reflect.TypeOf(value)
if t.Kind().String() == "ptr" {
v := reflect.ValueOf(value)
t = reflect.Indirect(v).Type()
}
return t
}

// Function ValueOfValue returns the value of specified parameter.
// If specified value is a pointer, then is first dereferenced.
func ValueOfValue(value interface{}) reflect.Value {
t := reflect.TypeOf(value)
if t.Kind().String() == "ptr" {
v := reflect.ValueOf(value)
return reflect.Indirect(v)
}
return reflect.ValueOf(value)
}

// Function BrExit breaks the execution of test and displays stack trace.
// After breaking the execution flow, application returns exit code -1
// that can be utilized by test automation tools.
func BrExit() {
fmt.Printf("Stack trace:\n------------\n")
reDeepCalls := regexp.MustCompile(`(^goroutine[^:]*:$)|(^.*/oxyde/.*$)`)
reFuncParams := regexp.MustCompile(`([a-zA-Z_0-9]+)\([^\)]+\)`)
reFuncOffset := regexp.MustCompile(`\s+\+.*$`)
b := make([]byte, 100000)
runtime.Stack(b, false)
scanner := bufio.NewScanner(bytes.NewBuffer(b))
for scanner.Scan() {
line := scanner.Text()
if reDeepCalls.MatchString(line) {
continue
}
line = reFuncParams.ReplaceAllString(line, "$1()")
line = reFuncOffset.ReplaceAllString(line, "")
fmt.Println(line)
}
os.Exit(-1)
}
Loading

0 comments on commit b788b7f

Please sign in to comment.