本文整理汇总了Golang中github.com/cheekybits/is.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestWithReview
func TestWithReview(t *testing.T) {
is := is.New(t)
freelancerRepositoryMock := &FreelancerRepositoryMock{}
var freelancerContext = &ApplicationContext{
FreelancerRepository: freelancerRepositoryMock,
}
w := httptest.NewRecorder()
r := getRequest(freelancerContext, `
{
"title": "title",
"clientID": 2,
"content": "content",
"freelancerID": 3,
"jobID": 4,
"rating": 5.6
}`)
next := func(review *Review) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
is.Equal(review.Title, "title")
is.Equal(review.ClientID, 2)
is.Equal(review.Content, "content")
is.Equal(review.FreelancerID, 3)
is.Equal(review.JobID, 4)
is.Equal(review.Rating, 5.6)
})
}
withReview{next}.ServeHTTP(w, r)
}
示例2: TestIndexProject
func TestIndexProject(t *testing.T) {
is := is.New(t)
projectRepositoryMock := &ProjectRepositoryMock{}
projectRepositoryMock.GetAllProjectsCall.Returns.Projects = []Project{
Project{
Model: Model{
ID: 1,
},
},
Project{
Model: Model{
ID: 2,
},
},
}
userContext := &ApplicationContext{
ProjectRepository: projectRepositoryMock,
}
r := getRequest(userContext, ``)
w := httptest.NewRecorder()
IndexProject(w, r)
is.Equal(w.Code, http.StatusOK)
var body []Project
is.NoErr(json.Unmarshal(w.Body.Bytes(), &body))
is.Equal(body[0].Model.ID, 1)
is.Equal(body[1].Model.ID, 2)
}
示例3: TestValueEqual
func TestValueEqual(t *testing.T) {
is := is.New(t)
v := ParseValue([]byte("something"))
is.True(v.Equal("something"))
is.False(v.Equal("else"))
is.Equal("string", v.Type())
v = ParseValue([]byte("123"))
is.Equal("float64", v.Type())
v = ParseValue([]byte("/^2.{2}$/"))
is.True(v.Equal(200))
is.True(v.Equal(201))
is.False(v.Equal(404))
is.Equal("regex", v.Type())
v = ParseValue([]byte("/application/json/"))
is.True(v.Equal("application/json"))
is.True(v.Equal("application/json; charset=utf-8"))
is.True(v.Equal("text/xml; application/json; charset=utf-8"))
is.False(v.Equal("text/xml; charset=utf-8"))
is.Equal("regex", v.Type())
is.Equal(`/application/json/`, v.String())
v = ParseValue([]byte("/Silk/"))
is.True(v.Equal("My name is Silk."))
is.True(v.Equal("Silk is my name."))
is.False(v.Equal("I don't contain that word!"))
}
示例4: TestWithError
func TestWithError(t *testing.T) {
is := is.New(t)
w := httptest.NewRecorder()
r := newTestRequest()
err := errors.New("something went wrong")
opts := &respond.Options{
Before: func(w http.ResponseWriter, r *http.Request, status int, data interface{}) (int, interface{}) {
if err, ok := data.(error); ok {
return status, map[string]interface{}{"error": err.Error()}
}
return status, data
},
}
testHandler := &testHandler{
status: http.StatusInternalServerError,
data: err,
}
handler := opts.Handler(testHandler)
handler.ServeHTTP(w, r)
is.Equal(http.StatusInternalServerError, w.Code)
var data map[string]interface{}
is.NoErr(json.Unmarshal(w.Body.Bytes(), &data))
is.Equal(data, map[string]interface{}{"error": err.Error()})
is.Equal(w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
}
示例5: TestFileList
func TestFileList(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
list, err := s.FileList(fmt.Sprintf("/ipfs/%s", examplesHash))
is.Nil(err)
is.Equal(list.Type, "Directory")
is.Equal(list.Size, 0)
is.Equal(len(list.Links), 6)
// TODO: document difference in sice betwen 'ipfs ls' and 'ipfs file ls -v'. additional object encoding in data block?
expected := map[string]UnixLsLink{
"about": {Type: "File", Hash: "QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V", Name: "about", Size: 1677},
"contact": {Type: "File", Hash: "QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y", Name: "contact", Size: 189},
"help": {Type: "File", Hash: "QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7", Name: "help", Size: 311},
"quick-start": {Type: "File", Hash: "QmUzLxaXnM8RYCPEqLDX5foToi5aNZHqfYr285w2BKhkft", Name: "quick-start", Size: 1686},
"readme": {Type: "File", Hash: "QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB", Name: "readme", Size: 1091},
"security-notes": {Type: "File", Hash: "QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ", Name: "security-notes", Size: 1016},
}
for _, l := range list.Links {
el, ok := expected[l.Name]
is.True(ok)
is.NotNil(el)
is.Equal(*l, el)
}
}
示例6: TestAfter
func TestAfter(t *testing.T) {
is := is.New(t)
options := &respond.Options{}
var aftercall map[string]interface{}
options.After = func(w http.ResponseWriter, r *http.Request, status int, data interface{}) {
aftercall = map[string]interface{}{
"w": w, "r": r, "status": status, "data": data,
}
}
testHandler := &testHandler{
status: http.StatusOK, data: testdata,
}
handler := options.Handler(testHandler)
w := httptest.NewRecorder()
r := newTestRequest()
handler.ServeHTTP(w, r)
is.OK(aftercall)
is.Equal(aftercall["w"], w)
is.Equal(aftercall["r"], r)
is.Equal(aftercall["status"], testHandler.status)
is.Equal(aftercall["data"], testHandler.data)
is.Equal(http.StatusOK, w.Code)
var data map[string]interface{}
is.NoErr(json.Unmarshal(w.Body.Bytes(), &data))
is.Equal(data, testdata)
is.Equal(w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
}
示例7: TestEncoderOnErr
func TestEncoderOnErr(t *testing.T) {
is := is.New(t)
var onErrCall map[string]interface{}
options := &respond.Options{
OnErr: func(err error) {
onErrCall = map[string]interface{}{"err": err}
},
}
encoderErr := errors.New("something went wrong while encoding")
options.Encoder = func(w http.ResponseWriter, r *http.Request) respond.Encoder {
return &testEncoder{
err: encoderErr,
}
}
testHandler := &testHandler{
status: http.StatusOK, data: testdata,
}
handler := options.Handler(testHandler)
w := httptest.NewRecorder()
r := newTestRequest()
handler.ServeHTTP(w, r)
is.OK(onErrCall)
is.Equal(onErrCall["err"], encoderErr)
}
示例8: TestIDLess
func TestIDLess(t *testing.T) {
is := is.New(t)
id1 := encode(strings.NewReader(`1`)) // c42yrSHMvUcscrQBssLhrRE28YpGUv9Gf95uH8KnwTiBv4odDbVqNnCYFs3xpsLrgVZfHebSaQQsvxgDGmw5CX1fVy
id2 := encode(strings.NewReader(`2`)) // c42i2hTBA9Ej4nqEo9iUy3pJRRE53KAH9RwwMSWjmfaQN7LxCymVz1zL9hEjqeFYzxtxXz2wRK7CBtt71AFkRfHodu
is.Equal(id1.Less(id2), false)
}
示例9: TestCompareIDs
func TestCompareIDs(t *testing.T) {
is := is.New(t)
for _, test := range []struct {
Id_A *asset.ID
Id_B *asset.ID
Exp int
}{
{
Id_A: encode(strings.NewReader("Test string")),
Id_B: encode(strings.NewReader("Test string")),
Exp: 0,
},
{
Id_A: encode(strings.NewReader("Test string A")),
Id_B: encode(strings.NewReader("Test string B")),
Exp: -1,
},
{
Id_A: encode(strings.NewReader("Test string B")),
Id_B: encode(strings.NewReader("Test string A")),
Exp: 1,
},
{
Id_A: encode(strings.NewReader("Test string")),
Id_B: nil,
Exp: -1,
},
} {
is.Equal(test.Id_A.Cmp(test.Id_B), test.Exp)
}
}
示例10: TestAddFreelancer
func TestAddFreelancer(t *testing.T) {
is := is.New(t)
freelancerRepositoryMock := &FreelancerRepositoryMock{}
freelancerContext := &ApplicationContext{
FreelancerRepository: freelancerRepositoryMock,
}
r := getRequest(freelancerContext, ``)
w := httptest.NewRecorder()
user := &User{
Model: Model{
ID: 1,
},
FirstName: "first",
LastName: "last",
Email: "[email protected]",
}
AddFreelancer(user).ServeHTTP(w, r)
is.Equal(w.Code, http.StatusOK)
is.Equal(freelancerRepositoryMock.AddFreelancerCall.Receives.Freelancer.User.Model.ID, 1)
is.Equal(freelancerRepositoryMock.AddFreelancerCall.Receives.Freelancer.User.FirstName, "first")
is.Equal(freelancerRepositoryMock.AddFreelancerCall.Receives.Freelancer.User.LastName, "last")
is.Equal(freelancerRepositoryMock.AddFreelancerCall.Receives.Freelancer.User.Email, "[email protected]")
}
示例11: TestLogin
func TestLogin(t *testing.T) {
is := is.New(t)
userRepositoryMock := &UserRepositoryMock{}
userRepositoryMock.CheckCredentialsCall.Returns.UserType = "freelancer"
userRepositoryMock.CheckCredentialsCall.Returns.User = User{
Model: Model{
ID: 1,
},
FirstName: "firstname",
LastName: "lastname",
Password: "password",
Email: "[email protected]",
}
userContext := &ApplicationContext{
UserRepository: userRepositoryMock,
}
r := getRequest(userContext, `
{
"email": "[email protected]",
"password": "notimportant"
}
`)
w := httptest.NewRecorder()
Login(w, r)
is.Equal(w.Code, http.StatusOK)
var body map[string]interface{}
is.NoErr(json.Unmarshal(w.Body.Bytes(), &body))
is.Equal(body["id"], 1)
is.OK(body["token"])
is.Equal(body["type"], "freelancer")
}
示例12: TestAddFreelancerReferenceByID
func TestAddFreelancerReferenceByID(t *testing.T) {
is := is.New(t)
referenceRepositoryMock := &ReferenceRepositoryMock{}
var freelancerContext = &ApplicationContext{
ReferenceRepository: referenceRepositoryMock,
}
w := httptest.NewRecorder()
r := getRequest(freelancerContext, ``)
reference := Reference{
Title: "title",
Content: "content",
Media: Media{
Image: "image",
Video: "video",
},
}
addFreelancerReferenceByID{3, &reference}.ServeHTTP(w, r)
receivedReference := referenceRepositoryMock.AddReferenceCall.Receives.Reference
is.Equal(receivedReference.Title, "title")
is.Equal(receivedReference.Content, "content")
is.Equal(receivedReference.Media.Image, "image")
is.Equal(receivedReference.Media.Video, "video")
is.Equal(referenceRepositoryMock.AddReferenceCall.Receives.ID, 3)
}
示例13: TestWithReference
func TestWithReference(t *testing.T) {
is := is.New(t)
referenceRepositoryMock := &ReferenceRepositoryMock{}
var freelancerContext = &ApplicationContext{
ReferenceRepository: referenceRepositoryMock,
}
w := httptest.NewRecorder()
r := getRequest(freelancerContext, `
{
"content": "content",
"title": "title",
"media": {
"image": "image",
"video": "video"
}
}`)
next := func(reference *Reference) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
is.Equal(reference.Title, "title")
is.Equal(reference.Content, "content")
is.Equal(reference.Media.Image, "image")
is.Equal(reference.Media.Video, "video")
})
}
withReference{next}.ServeHTTP(w, r)
}
示例14: TestAddFreelancerReviewByID
func TestAddFreelancerReviewByID(t *testing.T) {
is := is.New(t)
freelancerRepositoryMock := &FreelancerRepositoryMock{}
var freelancerContext = &ApplicationContext{
FreelancerRepository: freelancerRepositoryMock,
}
w := httptest.NewRecorder()
r := getRequest(freelancerContext, ``)
review := Review{
Title: "title",
ClientID: 2,
Content: "content",
JobID: 4,
Rating: 5.6,
}
addFreelancerReviewByID{3, &review}.ServeHTTP(w, r)
receivedReview := freelancerRepositoryMock.AddReviewCall.Receives.Review
is.Equal(receivedReview.Title, "title")
is.Equal(receivedReview.Content, "content")
is.Equal(receivedReview.Rating, 5.6)
is.Equal(receivedReview.ClientID, 2)
is.Equal(receivedReview.JobID, 4)
is.Equal(freelancerRepositoryMock.AddReviewCall.Receives.ID, 3)
}
示例15: TestAddClient
func TestAddClient(t *testing.T) {
is := is.New(t)
clientRepositoryMock := &ClientRepositoryMock{}
userContext := &ApplicationContext{
ClientRepository: clientRepositoryMock,
}
r := getRequest(userContext, ``)
w := httptest.NewRecorder()
user := &User{
Model: Model{
ID: 1,
},
}
AddClient(user).ServeHTTP(w, r)
is.Equal(w.Code, http.StatusOK)
is.Equal(clientRepositoryMock.AddClientCall.Receives.Client.User.ID, 1)
var body map[string]interface{}
is.NoErr(json.Unmarshal(w.Body.Bytes(), &body))
userMap := body["user"].(map[string]interface{})
is.Equal(userMap["id"], 1)
is.Equal(body["type"], "client")
}