Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Sep 30, 2020
0 parents commit ee4ee2f
Show file tree
Hide file tree
Showing 14 changed files with 788 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Go
on: [push]
jobs:

test:

runs-on: ubuntu-latest

steps:

- uses: actions/setup-go@v2
with:
go-version: 1.15

- uses: actions/checkout@v2

- name: setup
run: (cd && GO111MODULE=on go get github.com/ysmood/kit/cmd/godev)

- run: godev -m 100
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage.txt
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License

Copyright 2020 Yad Smood

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Overview

A tiny test framework for go projects. The test of this lib is its doc.

All functions in this lib are side-effect free.

## Examples

- [simple](example_simple_test.go)

- [basic](example_basic_test.go)
192 changes: 192 additions & 0 deletions assertion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package got

import (
"errors"
"reflect"
"regexp"
"strings"
)

// Assertion is the assertion context
type Assertion struct {
Testable
}

// New assertion helper
func New(t Testable) Assertion {
return Assertion{t}
}

// Eq a == b
func (as Assertion) Eq(a, b interface{}) {
as.Helper()
if compare(a, b) != 0 {
as.err("%s == %s", pp(a), pp(b))
}
}

// Neq a != b
func (as Assertion) Neq(a, b interface{}) {
as.Helper()
if compare(a, b) == 0 {
as.err("%s != %s", pp(a), pp(b))
}
}

// Gt a > b
func (as Assertion) Gt(a, b interface{}) {
as.Helper()
if compare(a, b) <= 0 {
as.err("%s > %s", pp(a), pp(b))
}
}

// Gte a >= b
func (as Assertion) Gte(a, b interface{}) {
as.Helper()
if compare(a, b) < 0 {
as.err("%s >= %s", pp(a), pp(b))
}
}

// Lt a < b
func (as Assertion) Lt(a, b interface{}) {
as.Helper()
if compare(a, b) >= 0 {
as.err("%s < %s", pp(a), pp(b))
}
}

// Lte a <= b
func (as Assertion) Lte(a, b interface{}) {
as.Helper()
if compare(a, b) > 0 {
as.err("%s <= %s", pp(a), pp(b))
}
}

// True a == true
func (as Assertion) True(a bool) {
as.Helper()
if !a {
as.err("should be true")
}
}

// False a == false
func (as Assertion) False(a bool) {
as.Helper()
if a {
as.err("should be false")
}
}

// Nil args[-1] == nil
func (as Assertion) Nil(args ...interface{}) {
as.Helper()
if len(args) == 0 {
as.err("no args received")
return
}
last := args[len(args)-1]
if !isNil(last) {
as.err("%s should be nil", pp(last))
}
}

// NotNil args[-1] != nil
func (as Assertion) NotNil(args ...interface{}) {
as.Helper()
if len(args) == 0 {
as.err("no args received")
return
}
last := args[len(args)-1]
if isNil(last) {
as.err("%s shouldn't be nil", pp(last))
}
}

// Regex matches str
func (as Assertion) Regex(pattern, str string) {
as.Helper()
if !regexp.MustCompile(pattern).MatchString(str) {
as.err("%s <regex should match> %s", pattern, str)
}
}

// Has str in container
func (as Assertion) Has(container, str string) {
as.Helper()
if !strings.Contains(container, str) {
as.err("%s <should has> %s", container, str)
}
}

// Len len(list) == l
func (as Assertion) Len(list interface{}, l int) {
as.Helper()
actual := reflect.ValueOf(list).Len()
if actual != l {
as.err("expect len %d to be %d", actual, l)
}
}

// Err args[-1] is error and not nil
func (as Assertion) Err(args ...interface{}) {
as.Helper()
if len(args) == 0 {
as.err("no args received")
return
}
last := args[len(args)-1]
if err, _ := last.(error); err == nil {
as.err("%s should be error", pp(last))
}
}

// NoErr is alias of Nil
func (as Assertion) NoErr(args ...interface{}) {
as.Helper()
as.Nil(args...)
}

// Panic fn should panic
func (as Assertion) Panic(fn func()) {
as.Helper()

defer func() {
as.Helper()

val := recover()
if val == nil {
as.err("should panic")
}
}()

fn()
}

// Is a a kind of b
func (as Assertion) Is(a, b interface{}) {
as.Helper()

if ae, ok := a.(error); ok {
if be, ok := b.(error); ok {
if ae == be {
return
}

if errors.Is(ae, be) {
return
}
as.err("%s <not in err chain> %s", pp(a), pp(b))
}
}

at := reflect.TypeOf(a)
bt := reflect.TypeOf(b)
if at.Kind() != bt.Kind() {
as.err("%s <not kind of> %s", pp(a), pp(b))
}
}
120 changes: 120 additions & 0 deletions assertion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package got_test

import (
"errors"
"fmt"
"os"
"testing"
"time"

"github.com/ysmood/got"
)

func TestAssertion(t *testing.T) {
as := got.New(t)

as.Eq(1, 1)
as.Eq(1.0, 1)
as.Eq([]int{1, 3}, []int{1, 3})
as.Eq(map[int]int{1: 2, 3: 4}, map[int]int{3: 4, 1: 2})

as.Neq(1.1, 1)
as.Neq([]int{1, 2}, []int{2, 1})

as.Lt(time.Millisecond, time.Second)
as.Lte(1, 1)

as.Gt(2, 1.5)
as.Gte(2, 2.0)

a := time.Now()
as.Lt(a, a.Add(time.Second))

as.True(true)
as.False(false)

as.Nil(nil)
as.Nil((*int)(nil))
as.Nil(os.Stat("go.mod"))
as.NotNil(1)

as.Regex(`\d\d`, "10")
as.Has(`test`, "es")

as.Len([]int{1, 2}, 2)

as.Err(1, 2, errors.New("err"))
as.NoErr(1, 2, nil)
as.Panic(func() { panic(1) })

as.Is(1, 2)
err := errors.New("err")
as.Is(err, err)
as.Is(fmt.Errorf("%w", err), err)
}

func TestAssertionErr(t *testing.T) {
m := &mock{t: t}
as := got.New(m)

type data struct {
A int
S string
}

as.Eq(1, 2.0)
m.check("1 (int) == 2 (float64)")
as.Eq(data{1, "a"}, data{1, "b"})
m.check("{1 a} (got_test.data) == {1 b} (got_test.data)")

as.Neq(1, 1)
m.check("1 (int) != 1 (int)")

as.Lt(1, 1)
m.check("1 (int) < 1 (int)")
as.Lte(2, 1)

m.check("2 (int) <= 1 (int)")
as.Gt(1, 1)
m.check("1 (int) > 1 (int)")
as.Gte(1, 2)
m.check("1 (int) >= 2 (int)")

as.True(false)
m.check("should be true")
as.False(true)
m.check("should be false")

as.Nil(1)
m.check("1 (int) should be nil")
as.Nil()
m.check("no args received")
as.NotNil(nil)
m.check("<nil> (<nil>) shouldn't be nil")
as.NotNil((*int)(nil))
m.check("<nil> (*int) shouldn't be nil")
as.NotNil()
m.check("no args received")

as.Regex(`\d\d`, "aaa")
m.check(`\d\d <regex should match> aaa`)
as.Has(`test`, "x")
m.check("test <should has> x")

as.Len([]int{1, 2}, 3)
m.check("expect len 2 to be 3")

as.Err(nil)
m.check("<nil> (<nil>) should be error")
as.Panic(func() {})
m.check("should panic")
as.Err()
m.check("no args received")
as.Err(1)
m.check("1 (int) should be error")

as.Is(1, 2.2)
m.check("1 (int) <not kind of> 2.2 (float64)")
as.Is(errors.New("a"), errors.New("b"))
m.check("a (*errors.errorString) <not in err chain> b (*errors.errorString)")
}
Loading

0 comments on commit ee4ee2f

Please sign in to comment.