assertion

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2024 License: MIT Imports: 1 Imported by: 0

README

Assertion Package

The assertion package provides a set of utility functions for making assertions in Go. These functions are useful for writing tests and ensuring that certain conditions hold true during the execution of your code.



Installation

To install the package, use the following command:

go get github.com/nandlabs/golly/assertion

Usage

The assertion package provides various functions to assert conditions. If an assertion fails, the function will return false with a descriptive error message.

Equal

The Equal function checks if two values are equal.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    a := 5
    b := 5
    if assertion.Equal(a, b) {
        fmt.Println("Values are equal")
    } else {
        fmt.Println("Values are not equal")
    }

    c := 10
    if assertion.Equal(a, c) {
        fmt.Println("Values are equal")
    } else {
        fmt.Println("Values are not equal")
    }
}
NotEqual

The NotEqual function checks if two values are not equal.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    a := 5
    b := 10
    if assertion.NotEqual(a, b) {
        fmt.Println("Values are not equal")
    } else {
        fmt.Println("Values are equal")
    }

    c := 5
    if assertion.NotEqual(a, c) {
        fmt.Println("Values are not equal")
    } else {
        fmt.Println("Values are equal")
    }
}
MapContains

The MapContains function checks if a map contains a key-value pair.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    m := map[string]interface{}{"key1": "value1", "key2": "value2"}
    if assertion.MapContains(m, "key1", "value1") {
        fmt.Println("Map contains the key-value pair")
    } else {
        fmt.Println("Map does not contain the key-value pair")
    }
}
MapMissing

The MapMissing function checks if a map does not contain a key-value pair.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    m := map[string]interface{}{"key1": "value1", "key2": "value2"}
    if assertion.MapMissing(m, "key3", "value3") {
        fmt.Println("Map does not contain the key-value pair")
    } else {
        fmt.Println("Map contains the key-value pair")
    }
}
HasValue

The HasValue function checks if a map contains a value.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    m := map[string]interface{}{"key1": "value1", "key2": "value2"}
    if assertion.HasValue(m, "value1") {
        fmt.Println("Map contains the value")
    } else {
        fmt.Println("Map does not contain the value")
    }
}
ListHas

The ListHas function checks if a list contains a value.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    list := []int{1, 2, 3, 4, 5}
    if assertion.ListHas(3, list) {
        fmt.Println("List contains the value")
    } else {
        fmt.Println("List does not contain the value")
    }
}
ListMissing

The ListMissing function checks if a list does not contain a value.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    list := []int{1, 2, 3, 4, 5}
    if assertion.ListMissing(6, list) {
        fmt.Println("List does not contain the value")
    } else {
        fmt.Println("List contains the value")
    }
}
Empty

The Empty function checks if an object is empty.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    var ptr *int
    if assertion.Empty(ptr) {
        fmt.Println("Value is empty")
    } else {
        fmt.Println("Value is not empty")
    }
}
NotEmpty

The NotEmpty function checks if an object is not empty.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    ptr := new(int)
    if assertion.NotEmpty(ptr) {
        fmt.Println("Value is not empty")
    } else {
        fmt.Println("Value is empty")
    }
}
Len

The Len function checks if the length of an object is equal to the expected length.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    list := []int{1, 2, 3, 4, 5}
    if assertion.Len(list, 5) {
        fmt.Println("Length is equal to expected length")
    } else {
        fmt.Println("Length is not equal to expected length")
    }
}
ElementsMatch

The ElementsMatch function checks if the elements of a list match the expected elements.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    list := []int{1, 2, 3, 4, 5}
    expected := []int{1, 2, 3, 4, 5}
    if assertion.ElementsMatch(list, expected...) {
        fmt.Println("Elements match")
    } else {
        fmt.Println("Elements do not match")
    }
}
True

The True function checks if a condition is true.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    condition := (5 > 3)
    if assertion.True(condition) {
        fmt.Println("Condition is true")
    } else {
        fmt.Println("Condition is false")
    }

    condition = (5 < 3)
    if assertion.True(condition) {
        fmt.Println("Condition is true")
    } else {
        fmt.Println("Condition is false")
    }
}
False

The False function checks if a condition is false.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    condition := (5 < 3)
    if assertion.False(condition) {
        fmt.Println("Condition is false")
    } else {
        fmt.Println("Condition is true")
    }

    condition = (5 > 3)
    if assertion.False(condition) {
        fmt.Println("Condition is false")
    } else {
        fmt.Println("Condition is true")
    }
}
Nil

The Nil function checks if a value is nil.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    var ptr *int
    if assertion.Nil(ptr) {
        fmt.Println("Value is nil")
    } else {
        fmt.Println("Value is not nil")
    }

    ptr = new(int)
    if assertion.Nil(ptr) {
        fmt.Println("Value is nil")
    } else {
        fmt.Println("Value is not nil")
    }
}
NotNil

The NotNil function checks if a value is not nil.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/assertion"
)

func main() {
    ptr := new(int)
    if assertion.NotNil(ptr) {
        fmt.Println("Value is not nil")
    } else {
        fmt.Println("Value is nil")
    }

    ptr = nil
    if assertion.NotNil(ptr) {
        fmt.Println("Value is not nil")
    } else {
        fmt.Println("Value is nil")
    }
}

Documentation

Overview

Package assertion provides a set of utilities for making assertions in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ElementsMatch added in v1.1.0

func ElementsMatch[S ~[]E, E any](list S, expectedElements ...E) bool

ElementsMatch the elements of the list with the expected elements

func Empty

func Empty(obj any) bool

Empty checks if an object is empty

func Equal

func Equal(expected, actual any) bool

Equal compares the expected and actual values and logs an error if they are not equal

func HasValue

func HasValue(m map[string]any, value any) bool

HasValue logs an error if the value is not a member of the map

func Len

func Len(obj any, expected int) bool

Len checks if the length of an object is equal to the expected length

func ListHas

func ListHas[S ~[]E, E any](val any, list S) bool

ListHas logs an error if the list does not contain the value

func ListMissing

func ListMissing[S ~[]E, E any](val any, list S) bool

ListMissing logs an error if the list contains the value

func MapContains

func MapContains(m map[string]any, key string, value any) bool

MapContains logs an error if the map does not contain the key-value pair

func MapMissing

func MapMissing(m map[string]any, key string, value any) bool

MapMissing logs an error if the map contains the key-value pair

func NotEmpty added in v1.0.2

func NotEmpty(obj any) bool

func NotEqual

func NotEqual(expected, actual any) bool

NotEqual compares the expected and actual values and logs an error if they are equal

Types

This section is empty.

Jump to

Keyboard shortcuts

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