本文整理匯總了Golang中github.com/facebookgo/ensure.Nil函數的典型用法代碼示例。如果您正苦於以下問題:Golang Nil函數的具體用法?Golang Nil怎麽用?Golang Nil使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Nil函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestGeneralCase
func TestGeneralCase(t *testing.T) {
fn := new(WalkFn)
root := makeEmptyRoot(t)
defer os.RemoveAll(root)
ensure.Nil(t, os.MkdirAll(filepath.Join(root, "a", "a1"), 0755))
createFile(t, filepath.Join(root, "a", "a1", "1"))
ensure.Nil(t, os.Mkdir(filepath.Join(root, "b"), 0755))
createFile(t, filepath.Join(root, "b", "2"))
ensure.Nil(t, os.Symlink(filepath.Join(root, "b"), filepath.Join(root, "a", "a1", "a2")))
ensure.Nil(t, os.Symlink(filepath.Join(root, "a", "a1", "1"), filepath.Join(root, "top")))
ensure.Nil(t, Walk(root, fn.walkFn))
ensure.DeepEqual(t,
fn.List,
[]string{
root,
filepath.Join(root, "a"),
filepath.Join(root, "a", "a1"),
filepath.Join(root, "a", "a1", "1"),
filepath.Join(root, "a", "a1", "a2"),
filepath.Join(root, "a", "a1", "a2", "2"),
filepath.Join(root, "b"),
filepath.Join(root, "b", "2"),
filepath.Join(root, "top"),
},
)
}
示例2: TestParserEmail
func TestParserEmail(t *testing.T) {
t.Parallel()
h := parsecli.NewTokenHarness(t)
h.MakeEmptyRoot()
defer h.Stop()
ensure.Nil(t, parsecli.CreateConfigWithContent(filepath.Join(h.Env.Root, parsecli.ParseLocal), "{}"))
ensure.Nil(t,
parsecli.CreateConfigWithContent(
filepath.Join(h.Env.Root, parsecli.ParseProject),
`{"project_type": 1}`,
),
)
var c configureCmd
ensure.Nil(t, c.parserEmail(h.Env, []string{"email2"}))
ensure.DeepEqual(
t,
h.Out.String(),
`Successfully configured email for current project to: "email2"
`,
)
ensure.Err(t, c.parserEmail(h.Env, nil), regexp.MustCompile("Invalid args:"))
ensure.Err(t, c.parserEmail(h.Env, []string{"a", "b"}), regexp.MustCompile("Invalid args:"))
}
示例3: TestTriggerHooksUpdate
func TestTriggerHooksUpdate(t *testing.T) {
t.Parallel()
h := newTriggersHarness(t)
var tr triggerHooksCmd
h.env.In =
ioutil.NopCloser(strings.NewReader("foo\nbeforeSave\napi.example.com/_foo/beforeSave\n"))
ensure.Nil(t, tr.triggerHooksUpdate(h.env, nil))
ensure.DeepEqual(t,
h.Out.String(),
`Please enter following details about the trigger webhook
Class name: Trigger name: URL: https://Successfully update the "beforeSave" trigger for class "foo" to point to "https://api.example.com/_foo/beforeSave"
`)
ensure.DeepEqual(t, h.Err.String(), "WARNING: beforeSave trigger already exists for class: foo\n")
h.Out.Reset()
h.Err.Reset()
h.env.In =
ioutil.NopCloser(strings.NewReader("bar\nafterSave\napi.example.com/_bar/afterSave\n"))
ensure.Nil(t, tr.triggerHooksUpdate(h.env, nil))
ensure.DeepEqual(t,
h.Out.String(),
`Please enter following details about the trigger webhook
Class name: Trigger name: URL: https://Successfully update the "afterSave" trigger for class "bar" to point to "https://api.example.com/_bar/afterSave"
`)
ensure.DeepEqual(t, h.Err.String(), "")
}
示例4: TestExistingConnState
func TestExistingConnState(t *testing.T) {
t.Parallel()
hello := []byte("hello")
fin := make(chan struct{})
okHandler := func(w http.ResponseWriter, r *http.Request) {
defer close(fin)
w.Write(hello)
}
var called int32
listener, err := net.Listen("tcp", "127.0.0.1:0")
ensure.Nil(t, err)
server := &http.Server{
Handler: http.HandlerFunc(okHandler),
ConnState: func(c net.Conn, s http.ConnState) {
atomic.AddInt32(&called, 1)
},
}
transport := &http.Transport{}
client := &http.Client{Transport: transport}
down := &httpdown.HTTP{}
s := down.Serve(server, listener)
res, err := client.Get(fmt.Sprintf("http://%s/", listener.Addr().String()))
ensure.Nil(t, err)
actualBody, err := ioutil.ReadAll(res.Body)
ensure.Nil(t, err)
ensure.DeepEqual(t, actualBody, hello)
ensure.Nil(t, res.Body.Close())
ensure.Nil(t, s.Stop())
<-fin
ensure.True(t, atomic.LoadInt32(&called) > 0)
}
示例5: TestMergeOperator
func TestMergeOperator(t *testing.T) {
var (
givenKey = []byte("hello")
givenVal1 = []byte("foo")
givenVal2 = []byte("bar")
givenMerged = []byte("foobar")
)
merger := &mockMergeOperator{
fullMerge: func(key, existingValue []byte, operands [][]byte) ([]byte, bool) {
ensure.DeepEqual(&fatalAsError{t}, key, givenKey)
ensure.DeepEqual(&fatalAsError{t}, existingValue, givenVal1)
ensure.DeepEqual(&fatalAsError{t}, operands, [][]byte{givenVal2})
return givenMerged, true
},
}
db := newTestDB(t, "TestMergeOperator", func(opts *Options) {
opts.SetMergeOperator(merger)
})
defer db.Close()
wo := NewDefaultWriteOptions()
ensure.Nil(t, db.Put(wo, givenKey, givenVal1))
ensure.Nil(t, db.Merge(wo, givenKey, givenVal2))
// trigger a compaction to ensure that a merge is performed
db.CompactRange(Range{nil, nil})
ro := NewDefaultReadOptions()
v1, err := db.Get(ro, givenKey)
defer v1.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v1.Data(), givenMerged)
}
示例6: TestCreateNewApp
func TestCreateNewApp(t *testing.T) {
t.Parallel()
h, _ := newAppHarness(t)
defer h.Stop()
a := defaultApps
h.env.In = ioutil.NopCloser(strings.NewReader("D"))
app, err := a.createApp(h.env)
ensure.Nil(t, err)
ensure.Nil(t, a.printApp(h.env, app))
ensure.DeepEqual(t, h.Out.String(),
`Please choose a name for your Parse app.
Note that this name will appear on the Parse website,
but it does not have to be the same as your mobile app's public name.
Name: Name D
DashboardURL https://api.example.com/dashboard/D
ApplicationID applicationID.D
ClientKey clientKey.D
JavaScriptKey javaScriptKey.D
WindowsKey windowsKey.D
WebhookKey webhookKey.D
RestKey restKey.D
MasterKey masterKey.D
ClientPushEnabled false
ClientClassCreationEnabled true
RequireRevocableSessions true
RevokeSessionOnPasswordChange true
`)
}
示例7: TestDeleteHook
func TestDeleteHook(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
c := &configureCmd{}
_, _, err := c.deleteHook(h.env, nil, "delete")
ensure.Err(t, err, regexp.MustCompile("invalid format"))
_, _, err = c.deleteHook(h.env, nil, "invalid", "1")
ensure.Err(t, err, regexp.MustCompile("invalid format"))
_, _, err = c.deleteHook(h.env, nil, "delete", "1", "2")
ensure.Err(t, err, regexp.MustCompile("invalid trigger name"))
var hooksOps []*hookOperation
_, ops, err := c.deleteHook(h.env, hooksOps, "delete", "call")
ensure.Nil(t, err)
ensure.DeepEqual(t, ops[len(ops)-1].method, "DELETE")
ensure.DeepEqual(
t,
*ops[len(ops)-1].function,
functionHook{FunctionName: "call"},
)
_, ops, err = c.deleteHook(h.env, hooksOps, "delete", "_User", "beforeSave")
ensure.Nil(t, err)
ensure.DeepEqual(t, ops[len(ops)-1].method, "DELETE")
ensure.DeepEqual(
t,
*ops[len(ops)-1].trigger,
triggerHook{ClassName: "_User", TriggerName: "beforeSave"},
)
}
示例8: newServer
func newServer(t *testing.T, handler func(net.Conn)) *testServer {
ln, err := net.Listen("tcp", ":")
ensure.Nil(t, err)
ts := &testServer{
ln: ln,
stop: make(chan struct{}),
}
go func() {
for {
select {
case <-ts.stop:
return
case <-time.After(100 * time.Millisecond):
conn, err := ts.ln.Accept()
if err != nil {
ensure.Nil(t, err)
continue
}
go handler(conn)
}
}
}()
return ts
}
示例9: TestClientDo
func TestClientDo(t *testing.T) {
given := map[string]string{"answer": "42"}
givenJSON, err := json.Marshal(given)
ensure.Nil(t, err)
wrapped := []map[string]interface{}{
{
"code": http.StatusOK,
"body": string(givenJSON),
},
}
c := &Client{
Client: &fbapi.Client{
Transport: fTransport(func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(jsonpipe.Encode(wrapped)),
}, nil
}),
},
}
var actual map[string]string
_, err = c.Do(&http.Request{
Method: "GET",
URL: &url.URL{},
}, &actual)
ensure.Nil(t, err)
ensure.DeepEqual(t, actual, given)
}
示例10: TestApplyForceRemoveExistingWhenNotFound
func TestApplyForceRemoveExistingWhenNotFound(t *testing.T) {
var inspectCalls int
container, err := NewContainer(
ContainerName("x"),
ContainerConfig(&dockerclient.ContainerConfig{Image: "foo"}),
ContainerForceRemoveExisting(),
)
ensure.Nil(t, err)
client := &mockClient{
inspectContainer: func(name string) (*dockerclient.ContainerInfo, error) {
inspectCalls++
switch inspectCalls {
case 1:
return nil, dockerclient.ErrNotFound
case 2:
return &dockerclient.ContainerInfo{Id: "y"}, nil
}
panic("not reached")
},
createContainer: func(config *dockerclient.ContainerConfig, name string) (string, error) {
return "", nil
},
startContainer: func(id string, config *dockerclient.HostConfig) error {
return nil
},
}
ensure.Nil(t, container.Apply(client))
}
示例11: TestGetHostFromURL
func TestGetHostFromURL(t *testing.T) {
urlStr := "https://api.parse.com/1/"
host, err := getHostFromURL(urlStr, "")
ensure.Nil(t, err)
ensure.DeepEqual(t, host, "api.parse.com")
urlStr = "https://api.parse.com/1/"
host, err = getHostFromURL(urlStr, "[email protected]")
ensure.Nil(t, err)
ensure.DeepEqual(t, host, "api.parse.com#[email protected]")
urlStr = "https://api.example.com:8080/1/"
host, err = getHostFromURL(urlStr, "")
ensure.Nil(t, err)
ensure.DeepEqual(t, host, "api.example.com")
urlStr = "https://api.example.com:8080/1/"
host, err = getHostFromURL(urlStr, "[email protected]")
ensure.Nil(t, err)
ensure.DeepEqual(t, host, "api.example.com#[email protected]")
urlStr = "api.example.com:8080:90"
host, err = getHostFromURL(urlStr, "")
ensure.Err(t, err, regexp.MustCompile("not a valid url"))
}
示例12: TestComparator
func TestComparator(t *testing.T) {
db := newTestDB(t, "TestComparator", func(opts *Options) {
opts.SetComparator(&bytesReverseComparator{})
})
defer db.Close()
// insert keys
givenKeys := [][]byte{[]byte("key1"), []byte("key2"), []byte("key3")}
wo := NewDefaultWriteOptions()
for _, k := range givenKeys {
ensure.Nil(t, db.Put(wo, k, []byte("val")))
}
// create a iterator to collect the keys
ro := NewDefaultReadOptions()
iter := db.NewIterator(ro)
defer iter.Close()
// we seek to the last key and iterate in reverse order
// to match given keys
var actualKeys [][]byte
for iter.SeekToLast(); iter.Valid(); iter.Prev() {
key := make([]byte, 4)
copy(key, iter.Key().Data())
actualKeys = append(actualKeys, key)
}
ensure.Nil(t, iter.Err())
// ensure that the order is correct
ensure.DeepEqual(t, actualKeys, givenKeys)
}
示例13: TestSecondUnixListenInherited
func TestSecondUnixListenInherited(t *testing.T) {
var n Net
tmpfile, err := ioutil.TempFile("", "TestSecondUnixListenInherited-")
ensure.Nil(t, err)
ensure.Nil(t, tmpfile.Close())
ensure.Nil(t, os.Remove(tmpfile.Name()))
defer os.Remove(tmpfile.Name())
l1, err := net.Listen("unix", tmpfile.Name())
ensure.Nil(t, err)
file, err := l1.(*net.UnixListener).File()
ensure.Nil(t, err)
n.fdStart = dup(t, int(file.Fd()))
ensure.Nil(t, file.Close())
os.Setenv(envCountKey, "1")
ensure.Nil(t, n.inherit())
ensure.DeepEqual(t, len(n.inherited), 1)
l2, err := n.Listen("unix", tmpfile.Name())
ensure.Nil(t, err)
ensure.DeepEqual(t, len(n.active), 1)
ensure.DeepEqual(t, n.inherited[0], nil)
_, err = n.Listen("unix", tmpfile.Name())
ensure.Err(t, err, regexp.MustCompile("bind: address already in use$"))
ensure.Nil(t, l1.Close())
ensure.Nil(t, l2.Close())
}
示例14: TestNewError
func TestNewError(t *testing.T) {
t.Parallel()
// expected stats
statsDone := make(chan struct{})
hc := &stats.HookClient{
BumpSumHook: func(key string, val float64) {
if key == "acquire.error.new" {
close(statsDone)
}
},
}
expected := errors.New("")
p := Pool{
New: func() (io.Closer, error) { return nil, expected },
Stats: hc,
Max: 1,
MinIdle: 1,
IdleTimeout: time.Hour,
ClosePoolSize: 2,
}
r, actual := p.Acquire()
ensure.Nil(t, r)
ensure.DeepEqual(t, actual, expected)
ensure.Nil(t, p.Close())
<-statsDone
}
示例15: initProject
func initProject(t *testing.T, root string) ([]string, []string) {
dirs := []string{
filepath.Join(root, "tester"),
filepath.Join(root, "tester", "inside"),
filepath.Join(root, "tester", "inside", "test"),
filepath.Join(root, "tester", "inside", "tester"),
}
for _, dir := range dirs {
ensure.Nil(t, os.MkdirAll(dir, 0755))
}
files := []string{
filepath.Join(root, "tester", "test"),
filepath.Join(root, "tester", "inside", "tester", "test"),
}
for _, file := range files {
f, err := os.Create(file)
ensure.Nil(t, err)
defer f.Close()
}
return files, dirs
}