-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): yomo init with test file (#680)
# Description `yomo init` command now generates `app_test.go` template for testing `app.go`.
- Loading branch information
Showing
5 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/yomorun/yomo/serverless/mock" | ||
) | ||
|
||
func TestHandler(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ctx *mock.MockContext | ||
// want is the expected data and tag that be written by ctx.Write | ||
want []mock.DataAndTag | ||
}{ | ||
{ | ||
name: "upper", | ||
ctx: mock.NewMockContext([]byte("hello"), 0x33), | ||
want: []mock.DataAndTag{ | ||
{Data: []byte("HELLO"), Tag: 0x34}, | ||
}, | ||
}, | ||
// TODO: add more test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
Handler(tt.ctx) | ||
got := tt.ctx.RecordWritten() | ||
|
||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("TestHandler got: %v, want: %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package mock | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/yomorun/yomo/serverless" | ||
"github.com/yomorun/yomo/serverless/guest" | ||
) | ||
|
||
// DataAndTag is a pair of data and tag. | ||
type DataAndTag struct { | ||
Data []byte | ||
Tag uint32 | ||
} | ||
|
||
// MockContext mock context. | ||
type MockContext struct { | ||
data []byte | ||
tag uint32 | ||
|
||
mu sync.Mutex | ||
wrSlice []DataAndTag | ||
} | ||
|
||
// NewMockContext returns the mock context. | ||
// the data is that returned by ctx.Data(), the tag is that returned by ctx.Tag(). | ||
func NewMockContext(data []byte, tag uint32) *MockContext { | ||
return &MockContext{ | ||
data: data, | ||
tag: tag, | ||
} | ||
} | ||
|
||
func (c *MockContext) Data() []byte { | ||
return c.data | ||
} | ||
func (c *MockContext) Tag() uint32 { | ||
return c.tag | ||
} | ||
func (m *MockContext) HTTP() serverless.HTTP { | ||
return &guest.GuestHTTP{} | ||
} | ||
|
||
func (c *MockContext) Write(tag uint32, data []byte) error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
c.wrSlice = append(c.wrSlice, DataAndTag{ | ||
Data: data, | ||
Tag: tag, | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
// RecordWritten returns the data records be written with `ctx.Write`. | ||
func (c *MockContext) RecordWritten() []DataAndTag { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
return c.wrSlice | ||
} |
6d8b913
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
yomo – ./
yomo.run
yomo-git-master-yomorun.vercel.app
yomo.vercel.app
www.yomo.run
yomo-yomorun.vercel.app