assert

package module
v0.0.0-...-782edc0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2024 License: GPL-3.0 Imports: 4 Imported by: 0

README

Assert

A simple asserting library in Go inspired from ThePrimeagen to help checking values in before using them to check if the logic is correct.

It can also simplify error handling.

Example

package main

import (
    "log/slog"
    "os"

    "github.com/Jamlie/assert"
)

func main() {
    val := 4

	logsFile, err := os.OpenFile("logger.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
    assert.NoError(err, "Cannot open file")
    defer logsFile.Close()

	jsonHandler := slog.NewJSONHandler(logsFile, nil)
	logger := slog.New(jsonHandler)

    logger.Info("Some message",
        "user_id", val,
    )
}

Functions

Function Description
AddAssertData Add a key-value pair to a map to log out when the program panics
RemoveAssertData Remove the key from the map
Assert Checks if the condition is true, if not, panics with a specified message
NoError Checks if the given error is nil, if not, logs out the error and panics with a specified message
Equals Checks if two valeus are equal, if not, panics with a specified message
NotEquals Checks if two valeus are not equal, if not, panics with a specified message
GreaterThan Checks if the first value is greater than the second one, if not, panics with a specified message
LessThan Checks if the first value is less than the second one, if not, panics with a specified message
GreaterThanEquals Checks if the first value is greater than or equals the second one, if not, panics with a specified message
LessThanEquals Checks if the first value is less than or equals the second one, if not, panics with a specified message
NotEmptySlice Checks if a slice is not empty, if not, panics with a specified message
NotEmptyMap Checks if a slice is not empty, if not, panics with a specified message

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddAssertData

func AddAssertData(key string, value any)

Add a key-value pair to a map to log out when the program panics

func Assert

func Assert(truth bool, msg string)

Checks if the condition is true, if not, panics with a specified message

func Equals

func Equals(a, b any, msg string)

Checks if two valeus are equal, if not, panics with a specified message

func GreaterThan

func GreaterThan[T cmp.Ordered](a, b T, msg string)

Checks if the first value is greater than the second one, if not, panics with a specified message

func GreaterThanEquals

func GreaterThanEquals[T cmp.Ordered](a, b T, msg string)

Checks if the first value is greater than or equals the second one, if not, panics with a specified message

func LessThan

func LessThan[T cmp.Ordered](a, b T, msg string)

Checks if the first value is less than the second one, if not, panics with a specified message

func LessThanEquals

func LessThanEquals[T cmp.Ordered](a, b T, msg string)

Checks if the first value is less than or equals the second one, if not, panics with a specified message

func NoError

func NoError(err error, msg string)

Checks if the given error is nil, if not, logs out the error and panics with a specified message

func NotEmptyMap

func NotEmptyMap[T ~map[K]V, K comparable, V any](m T, msg string)

Checks if a slice is not empty, if not, panics with a specified message

func NotEmptySlice

func NotEmptySlice[T ~[]E, E any](s T, msg string)

Checks if a slice is not empty, if not, panics with a specified message

func NotEquals

func NotEquals(a, b any, msg string)

Checks if two valeus are not equal, if not, panics with a specified message

func RemoveAssertData

func RemoveAssertData(key string)

Remove the key from the map

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL