• Stars
    star
    150
  • Rank 247,323 (Top 5 %)
  • Language
    Go
  • License
    BSD 2-Clause "Sim...
  • Created over 10 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PowerAssert library for golang

gopwt

Travis-CI Build Status AppVeyor Build Status Go Report Card codecov

PowerAssert library for golang.

screenshot

logo

Supported go versions

See .travis.yml

Getting Started

Check online demo at first!

Install

$ go get -u github.com/ToQoz/gopwt/...

Try

go to project:

$ mkdir -p $GOPATH/src/github.com/$(whoami)/gopwtexample
$ cd $GOPATH/src/github.com/$(whoami)/gopwtexample

write main_test.go

$ cat <<EOF > main_test.go
package main

import (
	"flag"
	"github.com/ToQoz/gopwt"
	"github.com/ToQoz/gopwt/assert"
	"os"
	"testing"
)

func TestMain(m *testing.M) {
	flag.Parse()
	gopwt.Empower()
	os.Exit(m.Run())
}

func TestFoo(t *testing.T) {
	a := "xxx"
	b := "yyy"
	assert.OK(t, a == b, "a should equal to b")
}
EOF

run tests:

$ go test
--- FAIL: TestFoo (0.00s)
	assert.go:85: FAIL main_test.go:20
		assert.OK(t, a == b, "a should equal to b")
		             | |  |
		             | |  "yyy"
		             | false
		             "xxx"
		
		Assertion messages:
			- a should equal to b
		
		--- [string] b
		+++ [string] a
		@@ -1,1 +1,1@@
		-yyy
		+xxx
		
		
FAIL
exit status 1
FAIL	github.com/gopwter/gopwtexample	0.057s
exit status 1
exit status 1
FAIL	github.com/gopwter/gopwtexample	1.330s

Example

package main

import (
	"database/sql"
	"flag"
	"fmt"
	"os"
	"reflect"
	"sync"
	"testing"

	"github.com/ToQoz/gopwt"
	"github.com/ToQoz/gopwt/assert"
)

func TestMain(m *testing.M) {
	flag.Parse()
	gopwt.Empower() // magic happens
	os.Exit(m.Run())
}

func TestWithMessage(t *testing.T) {
	var receiver *struct{}
	receiver = nil
	assert.OK(t, receiver != nil, "receiver should not be nil")
}

func TestWithTestingRun(t *testing.T) {
	t.Run("A", func(t *testing.T) {
		assert.OK(t, 1 == 2)
	})
	t.Run("B", func(t *testing.T) {
		assert.OK(t, 3 == 4)
	})
}

func TestBasicLit(t *testing.T) {
	assert.OK(t, "a" == "b")
	assert.OK(t, 1 == 2)

	a := 1
	b := 2
	c := 3
	assert.OK(t, a+c == b)
	assert.OK(t, (a+c)+a == b)
	assert.OK(t, `foo
bar` == "bar")
}

func TestUnaryExpr(t *testing.T) {
	assert.OK(t, +1 == -1)

	a := "a"
	b := "a"
	assert.OK(t, &a == &b)

	wg := sync.WaitGroup{}
	ch := make(chan int)

	wg.Add(1)
	go func() {
		assert.OK(t, <-ch == 0)
		wg.Done()
	}()
	ch <- 1
	wg.Wait()
	close(ch)
}

func TestStringDiff(t *testing.T) {
	assert.OK(t, "supersoper" == "supersuper")

	expected := `foo
baz
bar2
bar`
	got := `foo
baz
bar
bar`
	assert.OK(t, got == expected)

	expected = `<div>
<div>
foo
</div>
</div>`
	got = `<div>
bar
</div>`
	assert.OK(t, got == expected)
}

func TestMapType(t *testing.T) {
	k := "b--------key"
	v := "b------value"
	assert.OK(t, reflect.DeepEqual(map[string]string{}, map[string]string{
		"a": "a",
		k:   v,
	}))
}

func TestArrayType(t *testing.T) {
	index := 1
	assert.OK(t, []int{
		1,
		2,
	}[index] == 3)
}

func TestStructType(t *testing.T) {
	foox := "foo------x"
	assert.OK(t, reflect.DeepEqual(struct{ Name string }{foox}, struct{ Name string }{"foo"}))
	assert.OK(t, reflect.DeepEqual(struct{ Name string }{Name: foox}, struct{ Name string }{Name: "foo"}))
}

func TestNestedCallExpr(t *testing.T) {
	rev := func(a bool) bool {
		return !a
	}

	assert.OK(t, rev(rev(rev(true))))
}

func TestCallWithNonIdempotentFunc(t *testing.T) {
	i := 0
	incl := func() int {
		i++
		return i
	}

	assert.OK(t, incl()+incl() == incl()+incl())
	assert.OK(t, incl() == incl())
	assert.OK(t, incl() == incl())
	assert.OK(t, incl() == incl())
	assert.OK(t, incl() == incl())
	assert.OK(t, (incl() == incl()) != (incl() == incl()))

	i2 := 0
	incl2 := func(i3 int) int {
		i2 += i3
		return i2
	}

	assert.OK(t, incl2(incl2(2)) == 10)
}

func TestPkgValue(t *testing.T) {
	assert.OK(t, sql.ErrNoRows == fmt.Errorf("error"))
}

func TestSelectorExpr(t *testing.T) {
	type user struct{ name string }
	u := user{"gopwt"}
	assert.OK(t, u.name == "no-name")
}
$ go test
--- FAIL: TestWithMessage (0.00s)
	assert.go:85: FAIL main_test.go:25
		assert.OK(t, receiver != nil, "receiver should not be nil")
		             |        |  |
		             |        |  <nil>
		             |        false
		             (*struct {})(nil)
		
		Assertion messages:
			- receiver should not be nil
		
--- FAIL: TestWithTestingRun (0.00s)
    --- FAIL: TestWithTestingRun/A (0.00s)
    	assert.go:85: FAIL main_test.go:30
    		assert.OK(t, 1 == 2)
    		               |
    		               false
    		
    		--- [int] 2
    		+++ [int] 1
    		@@ -1,1 +1,1@@
    		-2
    		+1
    		
    		
    --- FAIL: TestWithTestingRun/B (0.00s)
    	assert.go:85: FAIL main_test.go:33
    		assert.OK(t, 3 == 4)
    		               |
    		               false
    		
    		--- [int] 4
    		+++ [int] 3
    		@@ -1,1 +1,1@@
    		-4
    		+3
    		
    		
--- FAIL: TestBasicLit (0.00s)
	assert.go:85: FAIL main_test.go:38
		assert.OK(t, "a" == "b")
		                 |
		                 false
		
		--- [string] "b"
		+++ [string] "a"
		@@ -1,1 +1,1@@
		-b
		+a
		
		
	assert.go:85: FAIL main_test.go:39
		assert.OK(t, 1 == 2)
		               |
		               false
		
		--- [int] 2
		+++ [int] 1
		@@ -1,1 +1,1@@
		-2
		+1
		
		
	assert.go:85: FAIL main_test.go:44
		assert.OK(t, a+c == b)
		             ||| |  |
		             ||| |  2
		             ||| false
		             ||3
		             |4
		             1
		
		--- [int] b
		+++ [int] a + c
		@@ -1,1 +1,1@@
		-2
		+4
		
		
	assert.go:85: FAIL main_test.go:45
		assert.OK(t, (a+c)+a == b)
		              ||| || |  |
		              ||| || |  2
		              ||| || false
		              ||| |1
		              ||| 5
		              ||3
		              |4
		              1
		
		--- [int] b
		+++ [int] (a + c) + a
		@@ -1,1 +1,1@@
		-2
		+5
		
		
	assert.go:85: FAIL main_test.go:46
		assert.OK(t, "foo\nbar" == "bar")
		             |          |
		             |          false
		             "foo
		             bar"
		
		--- [string] "bar"
		+++ [string] "foo\nbar"
		@@ -1,1 +1,2@@
		+foo
		 bar
		
		
--- FAIL: TestUnaryExpr (0.00s)
	assert.go:85: FAIL main_test.go:51
		assert.OK(t, +1 == -1)
		             |  |  |
		             |  |  -1
		             |  false
		             1
		
		--- [int] -1
		+++ [int] +1
		@@ -1,1 +1,1@@
		--1
		+1
		
		
	assert.go:85: FAIL main_test.go:55
		assert.OK(t, &a == &b)
		             || |  ||
		             || |  |"a"
		             || |  (*string)(0xc42008f450)
		             || false
		             |"a"
		             (*string)(0xc42008f440)
		
		--- [*string] &b
		+++ [*string] &a
		@@ -1,1 +1,1@@
		&"a"
		
	assert.go:85: FAIL main_test.go:62
		assert.OK(t, <-ch == 0)
		             | |  |
		             | |  false
		             | (chan int)(0xc42008c8a0)
		             1
		
		--- [int] 0
		+++ [int] <-ch
		@@ -1,1 +1,1@@
		-0
		+1
		
		
--- FAIL: TestStringDiff (0.00s)
	assert.go:85: FAIL main_test.go:71
		assert.OK(t, "supersoper" == "supersuper")
		                          |
		                          false
		
		--- [string] "supersuper"
		+++ [string] "supersoper"
		@@ -1,1 +1,1@@
		supers[-u-]{+o+}per
		
	assert.go:85: FAIL main_test.go:81
		assert.OK(t, got == expected)
		             |   |  |
		             |   |  "foo
		             |   |  baz
		             |   |  bar2
		             |   |  bar"
		             |   false
		             "foo
		             baz
		             bar
		             bar"
		
		--- [string] expected
		+++ [string] got
		@@ -1,4 +1,4@@
		foo
		baz
		bar[-2-]
		bar
		
	assert.go:85: FAIL main_test.go:91
		assert.OK(t, got == expected)
		             |   |  |
		             |   |  "<div>
		             |   |  <div>
		             |   |  foo
		             |   |  </div>
		             |   |  </div>"
		             |   false
		             "<div>
		             bar
		             </div>"
		
		--- [string] expected
		+++ [string] got
		@@ -1,5 +1,3@@
		 <div>
		-<div>
		-foo
		-</div>
		+bar
		 </div>
		
		
--- FAIL: TestMapType (0.00s)
	assert.go:85: FAIL main_test.go:97
		assert.OK(t, reflect.DeepEqual(map[string]string{}, map[string]string{"a": "a", k: v}))
		             |                                                                  |  |
		             |                                                                  |  "b------value"
		             |                                                                  "b--------key"
		             false
		
		--- [map[string]string] map[string]string{"a": "a", k: v}
		+++ [map[string]string] map[string]string{}
		@@ -1,3 +1,1@@
		-map[string]string{ "a":            "a",
		-                   "b--------key": "b------value",
		-}
		+map[string]string{}
		
		
--- FAIL: TestArrayType (0.00s)
	assert.go:85: FAIL main_test.go:105
		assert.OK(t, []int{1, 2}[index] == 3)
		                        ||      |
		                        ||      false
		                        |1
		                        2
		
		--- [int] 3
		+++ [int] []int{1, 2}[index]
		@@ -1,1 +1,1@@
		-3
		+2
		
		
--- FAIL: TestStructType (0.00s)
	assert.go:85: FAIL main_test.go:113
		assert.OK(t, reflect.DeepEqual(struct{ Name string }{foox}, struct{ Name string }{"foo"}))
		             |                                       |
		             |                                       "foo------x"
		             false
		
		--- [struct { Name string }] struct{ Name string }{"foo"}
		+++ [struct { Name string }] struct{ Name string }{foox}
		@@ -1,3 +1,3@@
		struct { Name string }{
		  Name: "foo{+------x+}",
		}
		
	assert.go:85: FAIL main_test.go:114
		assert.OK(t, reflect.DeepEqual(struct{ Name string }{Name: foox}, struct{ Name string }{Name: "foo"}))
		             |                                             |
		             |                                             "foo------x"
		             false
		
		--- [struct { Name string }] struct{ Name string }{Name: "foo"}
		+++ [struct { Name string }] struct{ Name string }{Name: foox}
		@@ -1,3 +1,3@@
		struct { Name string }{
		  Name: "foo{+------x+}",
		}
		
--- FAIL: TestNestedCallExpr (0.00s)
	assert.go:85: FAIL main_test.go:122
		assert.OK(t, rev(rev(rev(true))))
		             |   |   |   |
		             |   |   |   true
		             |   |   false
		             |   true
		             false
		
--- FAIL: TestCallWithNonIdempotentFunc (0.00s)
	assert.go:85: FAIL main_test.go:132
		assert.OK(t, incl()+incl() == incl()+incl())
		             |     ||      |  |     ||
		             |     ||      |  |     |4
		             |     ||      |  |     7
		             |     ||      |  3
		             |     ||      false
		             |     |2
		             |     3
		             1
		
		--- [int] incl() + incl()
		+++ [int] incl() + incl()
		@@ -1,1 +1,1@@
		-7
		+3
		
		
	assert.go:85: FAIL main_test.go:133
		assert.OK(t, incl() == incl())
		             |      |  |
		             |      |  6
		             |      false
		             5
		
		--- [int] incl()
		+++ [int] incl()
		@@ -1,1 +1,1@@
		-6
		+5
		
		
	assert.go:85: FAIL main_test.go:134
		assert.OK(t, incl() == incl())
		             |      |  |
		             |      |  8
		             |      false
		             7
		
		--- [int] incl()
		+++ [int] incl()
		@@ -1,1 +1,1@@
		-8
		+7
		
		
	assert.go:85: FAIL main_test.go:135
		assert.OK(t, incl() == incl())
		             |      |  |
		             |      |  10
		             |      false
		             9
		
		--- [int] incl()
		+++ [int] incl()
		@@ -1,1 +1,1@@
		-10
		+9
		
		
	assert.go:85: FAIL main_test.go:136
		assert.OK(t, incl() == incl())
		             |      |  |
		             |      |  12
		             |      false
		             11
		
		--- [int] incl()
		+++ [int] incl()
		@@ -1,1 +1,1@@
		-12
		+11
		
		
	assert.go:85: FAIL main_test.go:137
		assert.OK(t, (incl() == incl()) != (incl() == incl()))
		              |      |  |       |   |      |  |
		              |      |  |       |   |      |  16
		              |      |  |       |   |      false
		              |      |  |       |   15
		              |      |  |       false
		              |      |  14
		              |      false
		              13
		
	assert.go:85: FAIL main_test.go:145
		assert.OK(t, incl2(incl2(2)) == 10)
		             |     |         |
		             |     |         false
		             |     2
		             4
		
		--- [int] 10
		+++ [int] incl2(incl2(2))
		@@ -1,1 +1,1@@
		-10
		+4
		
		
--- FAIL: TestPkgValue (0.00s)
	assert.go:85: FAIL main_test.go:149
		assert.OK(t, sql.ErrNoRows == fmt.Errorf("error"))
		                 |         |  |
		                 |         |  &errors.errorString{s:"error"}
		                 |         false
		                 &errors.errorString{s:"sql: no rows in result set"}
		
		--- [*errors.errorString] fmt.Errorf("error")
		+++ [*errors.errorString] sql.ErrNoRows
		@@ -1,3 +1,3@@
		 &errors.errorString{
		-  s: "error",
		+  s: "sql: no rows in result set",
		 }
		
		
--- FAIL: TestSelectorExpr (0.00s)
	assert.go:85: FAIL main_test.go:155
		assert.OK(t, u.name == "no-name")
		             | |    |
		             | |    false
		             | "gopwt"
		             main.user{name:"gopwt"}
		
		--- [string] "no-name"
		+++ [string] u.name
		@@ -1,1 +1,1@@
		-no-name
		+gopwt
		
		
FAIL
exit status 1
FAIL	github.com/ToQoz/gopwt/_example	0.074s
exit status 1
exit status 1
FAIL	github.com/ToQoz/gopwt/_example	1.277s

Tips

Off gopwt.Empoer()

If you don't need gopwt or need to stop gopwt, you can!

GOPWT_OFF=1 go test -covermode count
PASS
coverage: 60.8% of statements
ok      github.com/ToQoz/gopwt  1.008s

See Also

More Repositories

1

api-gateway-mapping-template

make AWS API Gateway's Mapping Template testable
JavaScript
40
star
2

Pyblr

Pyblr is a simple Tumblr API v2 library written in python
Python
22
star
3

SandS

SandS on macOS Sierra
Swift
20
star
4

ircmad

Bringing IRC into WebSocket. If you use this gateway, you can assess IRC from browser easily.
Ruby
9
star
5

godeps

github.com/ToQoz/go-deps reveals go pkg dependencies.
Go
9
star
6

api-gateway-localdev

simulate AWS API Gateway for dev in your local
JavaScript
8
star
7

anything-hub

Anything interface for github.
Ruby
7
star
8

dotfiles

personal dotfiles
Shell
7
star
9

ruby-anything

Anything interface for ruby
Ruby
5
star
10

UITalk

UITalk
Ruby
5
star
11

aws-xray-scala

(unstable) AWS XRay + Scala
Scala
4
star
12

Gokuraku

This is Gokuraku in office.(music server)
JavaScript
4
star
13

go-unitypackage

under development. go-unitypackage is utility for *.unitypackage.
Go
4
star
14

scheman

!!! Under development !!! scheman is database schema migration tool.
Go
3
star
15

Dockerfiles

My personal dockerfiles
Dockerfile
3
star
16

pry-pipeline

Pipeline for pry
Ruby
3
star
17

fluent-plugin-weather

Weather output plugin for Fluent
Ruby
3
star
18

uim-tmux

uim-tmux is wrapper for always using tmux session with uim-fep
Shell
3
star
19

trial-ava-puppeteer

For trial & explanation
JavaScript
2
star
20

jquery.simpleModal

JavaScript
2
star
21

tiqbi

text-mode interface for qiita
Ruby
2
star
22

gomain

Write go code in your $EDITOR and run it!
Go
1
star
23

aws-make-patch-operations

makes patchOperations from 2 objects
JavaScript
1
star
24

old-toqoz.net

toqoz.net old version
Ruby
1
star
25

pic-toqoz-net

Gyazoクローン
Ruby
1
star
26

rome

Rome is http router for golang
Go
1
star
27

go-formspec

Package github.com/ToQoz/formspec validates a form. So it will expresses spec for form. This is generally used in http.Handler.
Go
1
star
28

no

supporter for creating null object in Ruby.
Ruby
1
star
29

api-gateway-put-method-response

creates or updates (or deletes) methodResponses of AWS APIGateway
JavaScript
1
star
30

gentle_quitman.vim

This plugin provides :GQ and :GWQ command. it confirm before quit last window.
Vim Script
1
star
31

jquery.i4TableView

JavaScript
1
star
32

unused_view

This is a Rails plugin to see unused views in you controllers
Ruby
1
star
33

twitter-toqoz-net

@ToQozとして発言します. http://twitter.toqoz.net
JavaScript
1
star
34

jquery.goodThumbnail

JavaScript
1
star
35

Migemoge

ローマ字から, 平仮名とかカタカナとかローマ字とかにマッチする正規表現を作ります
JavaScript
1
star
36

goimps

goimps is utility for treating imports
Go
1
star
37

console_command

This Rails plugin is enable to add command easily to rails console even if it is using which of pry and irb.
Ruby
1
star
38

Nopaste

Nopasteクローン
Ruby
1
star
39

godecls

godecls lists declarations in files
Go
1
star
40

isucon12q

Java
1
star