https://github.com/xybor-x/xycond
A package supports to assert or expect many conditions in Golang
https://github.com/xybor-x/xycond
assert assertion conditions golang-library testing unittest
Last synced: 15 days ago
JSON representation
A package supports to assert or expect many conditions in Golang
- Host: GitHub
- URL: https://github.com/xybor-x/xycond
- Owner: xybor-x
- License: mit
- Created: 2022-09-01T03:54:57.000Z (over 3 years ago)
- Default Branch: dev
- Last Pushed: 2022-10-10T15:46:44.000Z (over 3 years ago)
- Last Synced: 2024-09-23T06:07:26.679Z (over 1 year ago)
- Topics: assert, assertion, conditions, golang-library, testing, unittest
- Language: Go
- Homepage:
- Size: 27.3 KB
- Stars: 6
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://github.com/huykingsofm)
[](https://pkg.go.dev/github.com/xybor-x/xycond)
[](https://github.com/xybor-x/xycond)
[](https://go.dev/)
[](https://go.dev/blog/go1.18)
[](https://github.com/xybor-x/xycond/releases/latest)
[](https://www.codacy.com/gh/xybor-x/xycond/dashboard?utm_source=github.com&utm_medium=referral&utm_content=xyplatform/xyerror&utm_campaign=Badge_Grade)
[](https://www.codacy.com/gh/xybor-x/xycond/dashboard?utm_source=github.com&utm_medium=referral&utm_content=xyplatform/xyerror&utm_campaign=Badge_Coverage)
[](https://goreportcard.com/report/github.com/xybor-x/xycond)
# Introduction
Package xycond supports to assert or expect many conditions.
It makes source code to be shorter and more readable by using inline commands.
# Features
This package has the following features:
- Assert a condition, panic in case condition is false.
- Expect a condition to occur and perform actions on this expectation.
- Panic with an assertion error.
# Benchmark
## ExpectIn
| op | time per op |
| ------------------- | ----------: |
| large-map | 293ns |
| small-map | 209ns |
| large-array | 196507ns |
| small-array | 375ns |
| large-string-string | 115002ns |
| small-string-string | 455ns |
| large-string-rune | 194ns |
| small-string-rune | 192ns |
# Example
1. Assert conditions
```golang
xycond.AssertFalse(1 == 2)
var x int
xycond.AssertZero(x)
xycond.ExpectFalse(true).Assert("this is a custom assertion message")
```
2. Testing
```golang
// Test a condition with *testing.T or *testing.B.
func TestSomething(t *testing.T) {
xycond.ExpectEmpty("").Test(t)
}
```
3. Perform actions on expectation
```golang
// Perform actions on an expectation.
xycond.ExpectEqual(1, 2).
True(func() {
fmt.Printf("1 == 2")
}).
False(func() {
fmt.Printf("1 != 2")
})
// Output:
// 1 != 2
```
4. Panic with formatted string
```golang
func foo() {
xycond.Panicf("foo %s", "bar")
}
func bar() int {
return xycond.Panic("buzzz").(int)
}
```