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

github.com/huandu/go-assert

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/huandu/go-assert

  • v1.1.6
  • Source
  • Go
  • Socket score

Version published
Created
Source

Package assert - Magic assert macros for Go

Go Go Doc

Package assert provides developer a way to assert expression and output useful contextual information automatically when a case fails. With this package, we can focus on writing test code without worrying about how to print lots of verbose debug information for debug.

Here is a quick sample.

import "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    str := Foo(42)
    assert.Assert(t, str == "expected")

    // This case fails with following message.
    //
    //     Assertion failed:
    //         str == "expected"
    //     Referenced variables are assigned in following statements:
    //         str := Foo(42)
}

Import

Use go get to install this package.

go get github.com/huandu/go-assert

Current stable version is v1.*. Old versions tagged by v0.* are obsoleted.

Usage

Assertion methods

If we just want to use functions like Assert, Equal or NotEqual, it's recommended to import this package as ..

import "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    a, b := 1, 2
    assert.Assert(t, a > b)

    // This case fails with message:
    //     Assertion failed:
    //         a > b
}

func TestAssertEquality(t *testing.T) {
    assert.Equal(t, map[string]int{
        "foo": 1,
        "bar": -2,
    }, map[string]int{
        "bar": -2,
        "foo": 10000,
    })

    // This case fails with message:
    //     Assertion failed:
    //     The value of following expression should equal.
    //     [1] map[string]int{
    //             "foo": 1,
    //             "bar": -2,
    //         }
    //     [2] map[string]int{
    //             "bar": -2,
    //             "foo": 10000,
    //         }
    //     Values:
    //     [1] -> (map[string]int)map[bar:-2 foo:1]
    //     [2] -> (map[string]int)map[bar:-2 foo:10000]
}

Advanced assertion wrapper: type A

If we want more controls on assertion, it's recommended to wrap t in an A.

There are lots of useful assert methods implemented in A.

  • Assert/Eqaul/NotEqual: Basic assertion methods.
  • NilError/NonNilError: Test if a func/method returns expected error.
  • Use: Track variables. If any assert method fails, all variables tracked by A and related in assert method will be printed out automatically in assertion message.

Here is a sample to demonstrate how to use A#Use to print related variables in assertion message.

import "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    a := assert.New(t)
    v1 := 123
    v2 := []string{"wrong", "right"}
    v3 := v2[0]
    v4 := "not related"
    a.Use(&v1, &v2, &v3, &v4)

    a.Assert(v1 == 123 && v3 == "right")

    // This case fails with following message.
    //
    //     Assertion failed:
    //         v1 == 123 && v3 == "right"
    //     Referenced variables are assigned in following statements:
    //         v1 := 123
    //         v3 := v2[0]
    //     Related variables:
    //         v1 -> (int)123
    //         v2 -> ([]string)[wrong right]
    //         v3 -> (string)wrong
}

FAQs

Package last updated on 28 Jul 2023

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