Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/a7103/assert

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/a7103/assert

  • v0.0.0-20211113074139-cf4417937248
  • Source
  • Go
  • Socket score

Version published
Created
Source

Usage

package assert_test

import (
	"testing"

	"github.com/go-repo/assert"
)

type StructA struct {
	A int64
	B string
}

func TestEqual(t *testing.T) {
	assert.Equal(t,
		&StructA{
			A: 1,
			B: "str",
		},
		&StructA{
			A: 2,
			B: "str",
		},
	)
}

Run the test and output:

=== RUN   TestEqual
    main_test.go:15: Actual (-) and expected (+) are not equal:
          &assert_test.StructA{
        -     A: int64(1)
        +     A: int64(2)
          }

--- FAIL: TestEqual (0.00s)
FAIL

errorassert

Useful for table test, you can test all cases even if one of them is failed, for example:

package errorassert_test

import (
	"testing"

	"github.com/go-repo/assert/errorassert"
)

type StructA struct {
	A int64
	B string
}

func TestEqual(t *testing.T) {
	tests := []struct {
		name     string
		actual   interface{}
		expected interface{}
	}{
		{
			name:     "equal",
			actual:   "123str",
			expected: "123str",
		},

		{
			name:     "not equal for int type",
			actual:   1,
			expected: 2,
		},

		{
			name:     "not equal for struct type",
			actual:   StructA{B: "abc"},
			expected: StructA{B: "def"},
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			errorassert.Equal(t, test.actual, test.expected)
		})
	}
}

Run the test and output:

=== RUN   TestEqual
=== RUN   TestEqual/equal
=== RUN   TestEqual/not_equal_for_int_type
    main_test.go:40: Actual (-) and expected (+) are not equal:
        - int(1)
        + int(2)

=== RUN   TestEqual/not_equal_for_struct_type
    main_test.go:40: Actual (-) and expected (+) are not equal:
          errorassert_test.StructA{
        -     B: string("abc")
        +     B: string("def")
          }

--- FAIL: TestEqual (0.00s)
    --- PASS: TestEqual/equal (0.00s)
    --- FAIL: TestEqual/not_equal_for_int_type (0.00s)
    --- FAIL: TestEqual/not_equal_for_struct_type (0.00s)
FAIL

FAQs

Package last updated on 13 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc