Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Case.match/equal #42

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions ytest/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// -----------------------------------------------------------------------------

type caseT interface {
type CaseT interface {
// Name returns the name of the running (sub-) test or benchmark.
//
// The name will include the name of the test along with the names of
Expand Down Expand Up @@ -128,44 +128,49 @@ type testingT struct {

// Errorln is equivalent to Log followed by Fail.
func (p testingT) Errorln(args ...any) {
p.T.Error(args...)
t := p.T
t.Helper()
t.Error(args...)
}

// Run runs f as a subtest of t called name.
//
// Run may be called simultaneously from multiple goroutines, but all such calls
// must return before the outer test function for t returns.
func (p testingT) Run(name string, f func()) bool {
return p.T.Run(name, func(t *testing.T) { f() })
func (p testingT) Run(name string, doSth func()) bool {
return p.T.Run(name, func(t *testing.T) {
doSth()
})
}

// -----------------------------------------------------------------------------

type Case struct {
*Request
*App
caseT
CaseT

DefaultHeader http.Header
}

func NewCase() *Case {
return &Case{}
}

// Gopt_Case_TestMain is required by Go+ compiler as the entry of a YAP test case.
func Gopt_Case_TestMain(c interface{ initCase(*App, caseT) }, t *testing.T) {
func Gopt_Case_TestMain(c interface{ initCase(*App, CaseT) }, t *testing.T) {
app := new(App).initApp()
c.initCase(app, testingT{t})
c.(interface{ Main() }).Main()
}

func (p *Case) initCase(app *App, t caseT) {
func (p *Case) initCase(app *App, t CaseT) {
p.App = app
p.caseT = t
p.CaseT = t
p.DefaultHeader = make(http.Header)
}

// T returns the testing object.
func (p *Case) T() CaseT {
return p.CaseT
}

// Req create a new request given a method and url.
func (p *Case) Req(method, url string) *Request {
req := newRequest(p, method, url)
Expand Down
5 changes: 2 additions & 3 deletions ytest/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package ytest

import (
"io"
"log"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -91,7 +90,7 @@ func Gopt_App_TestMain(app interface{ initApp() *App }, m *testing.M) {
}

// Gopt_App_Main is required by Go+ compiler as the Main entry of a YAP testing project.
func Gopt_App_Main(app interface{ initApp() *App }, workers ...interface{ initCase(*App, caseT) }) {
func Gopt_App_Main(app interface{ initApp() *App }, workers ...interface{ initCase(*App, CaseT) }) {
a := app.initApp()
if me, ok := app.(interface{ MainEntry() }); ok {
me.MainEntry()
Expand All @@ -108,7 +107,7 @@ func Gopt_App_Main(app interface{ initApp() *App }, workers ...interface{ initCa
// host "http://example.com" "http://localhost:8888"
func (p *App) Host(host, real string) {
if !strings.HasPrefix(host, "http") {
log.Panicf("invalid host `%s`: should start with http:// or https://\n", host)
fatalf("invalid host `%s`: should start with http:// or https://\n", host)
}
p.hosts[host] = real
}
Expand Down
Loading