当前位置: 首页>>代码示例>>Golang>>正文


Golang assert.Panics函数代码示例

本文整理汇总了Golang中github.com/stretchr/testify/assert.Panics函数的典型用法代码示例。如果您正苦于以下问题:Golang Panics函数的具体用法?Golang Panics怎么用?Golang Panics使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Panics函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestInt

func TestInt(t *testing.T) {
	assert := assert.New(t)

	UnsetKey("envconf_test1")
	UnsetKey("envconf_test2")

	v, ok := GetInt("envconf_test1")
	assert.False(ok)
	assert.Zero(v)
	assert.Panics(func() { MustGetInt("envconf_test1") })

	SetString("envconf_test1", "blahBlah")
	v, ok = GetInt("envconf_test1")
	assert.False(ok)
	assert.Zero(v)
	assert.Panics(func() { MustGetInt("envconf_test1") })

	SetString("envconf_test1", "42")
	v, ok = GetInt("envconf_test1")
	assert.True(ok)
	assert.Equal(42, v)
	assert.Equal(42, MustGetInt("envconf_test1"))

	SetDefaultInt("envconf_test1", -5)
	v, ok = GetInt("envconf_test1")
	assert.True(ok)
	assert.Equal(42, v)
	assert.Equal(42, MustGetInt("envconf_test1"))

	SetDefaultInt("envconf_test2", -33)
	v, ok = GetInt("envconf_test2")
	assert.True(ok)
	assert.Equal(-33, v)
	assert.Equal(-33, MustGetInt("envconf_test2"))
}
开发者ID:sboehmann,项目名称:envconf,代码行数:35,代码来源:envconf_test.go

示例2: TestLoadBlockchainPrivate

func TestLoadBlockchainPrivate(t *testing.T) {
	defer cleanupVisor()
	cleanupVisor()

	// No filename should return fresh blockchain
	bc := loadBlockchain("")
	assert.Equal(t, len(bc.Blocks), 0)

	// Filename with no file should return fresh blockchain
	assertFileNotExists(t, testBlockchainFile)
	bc = loadBlockchain(testBlockchainFile)
	assert.Equal(t, len(bc.Blocks), 0)

	// Loading an empty blockchain should panic
	assert.Nil(t, SaveBlockchain(bc, testBlockchainFile))
	assertFileExists(t, testBlockchainFile)
	assert.Panics(t, func() { loadBlockchain(testBlockchainFile) })

	// Loading a corrupt blockchain should panic
	corruptFile(t, testBlockchainFile)
	assert.Panics(t, func() { loadBlockchain(testBlockchainFile) })
	cleanupVisor()

	// Loading a valid blockchain should be safe
	vc := newMasterVisorConfig(t)
	vc.BlockchainFile = testBlockchainFile
	v := NewVisor(vc)
	assert.Nil(t, transferCoinsToSelf(v, v.Config.MasterKeys.Address))
	assert.Equal(t, len(v.blockchain.Blocks), 2)
	assert.Nil(t, v.SaveBlockchain())
	assertFileExists(t, testBlockchainFile)
	bc = loadBlockchain(testBlockchainFile)
	assert.Equal(t, v.blockchain, bc)
}
开发者ID:keepwalking1234,项目名称:skycoin,代码行数:34,代码来源:blockchain_test.go

示例3: TestPubKeyFromSecKey

func TestPubKeyFromSecKey(t *testing.T) {
	p, s := GenerateKeyPair()
	assert.Equal(t, PubKeyFromSecKey(s), p)
	assert.Panics(t, func() { PubKeyFromSecKey(SecKey{}) })
	assert.Panics(t, func() { PubKeyFromSecKey(NewSecKey(randBytes(t, 99))) })
	assert.Panics(t, func() { PubKeyFromSecKey(NewSecKey(randBytes(t, 31))) })
}
开发者ID:keepwalking1234,项目名称:skycoin,代码行数:7,代码来源:crypto_test.go

示例4: TestTransactionSignInputs

func TestTransactionSignInputs(t *testing.T) {
	tx := &Transaction{}
	// Panics if txns already signed
	tx.Head.Sigs = append(tx.Head.Sigs, Sig{})
	assert.Panics(t, func() { tx.SignInputs([]SecKey{}) })
	// Panics if not enough keys
	tx = &Transaction{}
	ux, s := makeUxOutWithSecret(t)
	tx.PushInput(ux.Hash())
	ux2, s2 := makeUxOutWithSecret(t)
	tx.PushInput(ux2.Hash())
	tx.PushOutput(makeAddress(), 40, 80)
	assert.Equal(t, len(tx.Head.Sigs), 0)
	assert.Panics(t, func() { tx.SignInputs([]SecKey{s}) })
	assert.Equal(t, len(tx.Head.Sigs), 0)
	// Valid signing
	h := tx.hashInner()
	assert.NotPanics(t, func() { tx.SignInputs([]SecKey{s, s2}) })
	assert.Equal(t, len(tx.Head.Sigs), 2)
	assert.Equal(t, tx.hashInner(), h)
	p := PubKeyFromSecKey(s)
	a := AddressFromPubKey(p)
	p = PubKeyFromSecKey(s2)
	a2 := AddressFromPubKey(p)
	assert.Nil(t, ChkSig(a, h, tx.Head.Sigs[0]))
	assert.Nil(t, ChkSig(a2, h, tx.Head.Sigs[1]))
	assert.NotNil(t, ChkSig(a, h, tx.Head.Sigs[1]))
	assert.NotNil(t, ChkSig(a2, h, tx.Head.Sigs[0]))
}
开发者ID:up4k,项目名称:skycoin,代码行数:29,代码来源:transactions_test.go

示例5: TestListSlice

func TestListSlice(t *testing.T) {
	assert := assert.New(t)

	l1 := NewList()
	l1 = l1.Append(Int32(0), Int32(1), Int32(2), Int32(3))
	l2 := l1.Slice(1, 3)
	assert.Equal(uint64(4), l1.Len())
	assert.Equal(uint64(2), l2.Len())
	assert.Equal(Int32(1), l2.Get(0))
	assert.Equal(Int32(2), l2.Get(1))

	l3 := l1.Slice(0, 0)
	assert.Equal(uint64(0), l3.Len())
	l3 = l1.Slice(1, 1)
	assert.Equal(uint64(0), l3.Len())
	l3 = l1.Slice(1, 2)
	assert.Equal(uint64(1), l3.Len())
	assert.Equal(Int32(1), l3.Get(0))
	l3 = l1.Slice(0, l1.Len())
	assert.True(l1.Equals(l3))

	assert.Panics(func() {
		l3 = l1.Slice(0, l1.Len()+1)
	})
	assert.Panics(func() {
		l3 = l1.Slice(l1.Len()+1, l1.Len()+2)
	})
}
开发者ID:arv,项目名称:noms-old,代码行数:28,代码来源:list_test.go

示例6: TestUnpad

func TestUnpad(t *testing.T) {
	// We've tested the OK decoding in TestPad, now test the error cases
	for _, test := range []struct {
		n   int
		in  string
		err error
	}{
		{8, "", ErrorPaddingNotFound},
		{8, "1", ErrorPaddingNotAMultiple},
		{8, "12", ErrorPaddingNotAMultiple},
		{8, "123", ErrorPaddingNotAMultiple},
		{8, "1234", ErrorPaddingNotAMultiple},
		{8, "12345", ErrorPaddingNotAMultiple},
		{8, "123456", ErrorPaddingNotAMultiple},
		{8, "1234567", ErrorPaddingNotAMultiple},
		{8, "1234567\xFF", ErrorPaddingTooLong},
		{8, "1234567\x09", ErrorPaddingTooLong},
		{8, "1234567\x00", ErrorPaddingTooShort},
		{8, "123456\x01\x02", ErrorPaddingNotAllTheSame},
		{8, "\x07\x08\x08\x08\x08\x08\x08\x08", ErrorPaddingNotAllTheSame},
	} {
		result, actualErr := Unpad(test.n, []byte(test.in))
		assert.Equal(t, test.err, actualErr, fmt.Sprintf("Unpad %d %q", test.n, test.in))
		assert.Equal(t, result, []byte(nil))
	}
	assert.Panics(t, func() { _, _ = Unpad(1, []byte("")) }, "bad multiple")
	assert.Panics(t, func() { _, _ = Unpad(256, []byte("")) }, "bad multiple")
}
开发者ID:marcopaganini,项目名称:rclone,代码行数:28,代码来源:pkcs7_test.go

示例7: TestBadConfig

func TestBadConfig(t *testing.T) {
	assert.Panics(t, func() { New(Options{}) })
	assert.Panics(t, func() {
		New(Config{
			AllowAllOrigins: true,
			AllowedOrigins:  []string{"http://google.com"},
		})
	})
	assert.Panics(t, func() {
		New(Config{
			AllowAllOrigins: true,
			AllowOriginFunc: func(origin string) bool { return false },
		})
	})
	assert.Panics(t, func() {
		New(Config{
			AllowedOrigins:  []string{"http://google.com"},
			AllowOriginFunc: func(origin string) bool { return false },
		})
	})
	assert.Panics(t, func() {
		New(Config{
			AllowedOrigins: []string{"google.com"},
		})
	})
}
开发者ID:globalways,项目名称:contrib,代码行数:26,代码来源:cors_test.go

示例8: TestPromiseRejected

func TestPromiseRejected(t *testing.T) {
	defer time.AfterFunc(time.Second, t.FailNow).Stop() // limit test to 1 second running time.

	done1, done2, done3 := make(incrementor, 1), make(incrementor, 1), make(incrementor, 1)
	var a Promise
	a2 := a.Then(panicIfCalled, done1.process)
	a3 := a2.Then(panicIfCalled, done2.process)
	a3.Then(panicIfCalled, done3.process)

	// Validate that nothing happens while it's pending.
	select {
	case <-done1:
		t.Fatal("Wasn't supposed to receive yet!")
	case <-time.After(10 * time.Millisecond):
		// yay!
	}

	// Resolve the promise and trigger the downstream dependencies.
	assert.Equal(t, a.Reject(1), 1)
	assert.Equal(t, <-done1, 1)
	assert.Equal(t, <-done2, 2)
	assert.Equal(t, <-done3, 3)

	// Can't resolve more than once:
	assert.Panics(t, func() { a.Resolve(2) })
	assert.Panics(t, func() { a2.Reject(3) })

	// Subsequent calls to then are immediately queued.
	a.Then(panicIfCalled, done1.process)
	assert.Equal(t, <-done1, 1)
}
开发者ID:augustoroman,项目名称:promise,代码行数:31,代码来源:promisify_test.go

示例9: TestInvalidHandler

func TestInvalidHandler(t *testing.T) {
	router := New(Context{})

	assert.Panics(t, func() {
		router.Get("/action", 1)
	})

	assert.Panics(t, func() {
		router.Get("/action", (*Context).InvalidHandler)
	})

	// Returns a string:
	assert.Panics(t, func() {
		router.Get("/action", (*Context).InvalidHandler2)
	})

	// Two writer inputs:
	assert.Panics(t, func() {
		router.Get("/action", (*Context).InvalidHandler3)
	})

	// Wrong context type:
	assert.Panics(t, func() {
		router.Get("/action", (*invalidSubcontext).Handler)
	})

	//
}
开发者ID:ae6rt,项目名称:goref,代码行数:28,代码来源:invalid_setup_test.go

示例10: TestCommand_makeCommand

func TestCommand_makeCommand(t *testing.T) {

	c := makeCommand(commandString, "", "", HandlerFunc)

	if assert.NotNil(t, c) {
		assert.Equal(t, c.definition, commandString)
		assert.Equal(t, len(c.arguments), 4)
		assert.Equal(t, c.arguments[0].literal, "create")
		assert.Equal(t, c.arguments[1].identifier, "kind")
		assert.Equal(t, c.arguments[1].list[0], "project")
		assert.Equal(t, c.arguments[1].list[1], "account")
		assert.Equal(t, c.arguments[2].identifier, "name")
		assert.Equal(t, c.arguments[2].captureType, "string")
		assert.Equal(t, c.arguments[3].identifier, "description")
		assert.Equal(t, c.arguments[3].captureType, "string")
		assert.True(t, c.arguments[3].isOptional())
		assert.True(t, c.arguments[3].isVariable())
	}

	assert.Panics(t, func() {
		_ = makeCommand(commandStringTwoOptionalVariableBad, "", "", HandlerFunc)
	})

	assert.Panics(t, func() {
		_ = makeCommand(commandStringOptionalBad, "", "", HandlerFunc)
	})

	assert.Panics(t, func() {
		_ = makeCommand(commandString, "", "", nil)
	})

}
开发者ID:jmptrader,项目名称:commander,代码行数:32,代码来源:command_test.go

示例11: TestVerifyTransactionInputs

func TestVerifyTransactionInputs(t *testing.T) {
	bc := NewBlockchain()
	bc.CreateMasterGenesisBlock(genAddress)
	tx := makeTransactionForChain(t, bc)
	// Valid txn
	uxIn, err := bc.Unspent.GetMultiple(tx.In)
	assert.Nil(t, err)
	assert.Nil(t, verifyTransactionInputs(tx, uxIn))
	// Bad sigs
	sig := tx.Head.Sigs[0]
	tx.Head.Sigs[0] = Sig{}
	assert.NotNil(t, verifyTransactionInputs(tx, uxIn))
	// Too many uxIn
	tx.Head.Sigs[0] = sig
	uxIn, err = bc.Unspent.GetMultiple(tx.In)
	assert.Nil(t, err)
	assert.Equal(t, len(uxIn), len(tx.In))
	uxIn = append(uxIn, makeUxOut(t))
	assert.True(t, DebugLevel2)
	assert.Panics(t, func() { verifyTransactionInputs(tx, uxIn) })
	// ux hash mismatch
	uxIn, err = bc.Unspent.GetMultiple(tx.In)
	assert.Nil(t, err)
	tx.In[0] = SHA256{}
	assert.Panics(t, func() { verifyTransactionInputs(tx, uxIn) })
}
开发者ID:up4k,项目名称:skycoin,代码行数:26,代码来源:blockchain_test.go

示例12: CoordsInsertTestImpl

func CoordsInsertTestImpl(t *testing.T, simpleCoords geom.ReadWriteCoords, pointFactory func(...float64) geom.Point) {
	assert.True(t, simpleCoords.IsEmpty())

	assert.Panics(t, func() { simpleCoords.Insert(1, pointFactory(66, 32)) })
	assert.Equal(t, 0, int(simpleCoords.NumCoords()), "Number of coordinates should be the 0")

	assert.True(t, simpleCoords.IsEmpty())

	simpleCoords.Insert(0, pointFactory(1, 2))
	assert.Equal(t, int(simpleCoords.NumCoords()), 1, "Number of coordinates should be the 1")
	assert.Equal(t, []float64{1, 2}, simpleCoords.Get(0).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))

	assert.False(t, simpleCoords.IsEmpty())

	assert.Panics(t, func() { simpleCoords.Insert(2, pointFactory(66, 32)) })
	assert.Equal(t, 1, int(simpleCoords.NumCoords()), "Number of coordinates should be the 1")
	assert.Equal(t, []float64{1, 2}, simpleCoords.Get(0).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))

	simpleCoords.Insert(0, pointFactory(-1, 0))
	assert.Equal(t, 2, int(simpleCoords.NumCoords()), "Number of coordinates should be the 2")
	assert.Equal(t, []float64{-1, 0}, simpleCoords.Get(0).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{1, 2}, simpleCoords.Get(1).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))

	simpleCoords.Insert(1, pointFactory(1, 1))
	assert.Equal(t, 3, int(simpleCoords.NumCoords()), "Number of coordinates should be the 3")
	assert.Equal(t, []float64{-1, 0}, simpleCoords.Get(0).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{1, 1}, simpleCoords.Get(1).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{1, 2}, simpleCoords.Get(2).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))

	assert.Panics(t, func() { simpleCoords.Insert(1, pointFactory(1, 1, 4)) })
	assert.Equal(t, 3, int(simpleCoords.NumCoords()), "Number of coordinates should be the 3")
	assert.Equal(t, []float64{-1, 0}, simpleCoords.Get(0).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{1, 1}, simpleCoords.Get(1).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{1, 2}, simpleCoords.Get(2).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
}
开发者ID:jesseeichar,项目名称:gotopo,代码行数:35,代码来源:coords_test_functions.go

示例13: CoordsSetTestImpl

func CoordsSetTestImpl(t *testing.T, pointFactory func(...float64) geom.Point, coordsFunc func([]float64) geom.ReadWriteCoords) {
	simpleCoords := coordsFunc([]float64{1, 2, 3, 4, 5, 6})

	assert.False(t, simpleCoords.IsEmpty())

	simpleCoords.Set(1, pointFactory(30, 40))
	assert.Equal(t, 3, int(simpleCoords.NumCoords()), "Number of coordinates should be the 3")
	assert.Equal(t, []float64{1, 2}, simpleCoords.Get(0).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{30, 40}, simpleCoords.Get(1).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{5, 6}, simpleCoords.Get(2).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))

	simpleCoords.Set(0, pointFactory(10, 20))
	assert.Equal(t, int(simpleCoords.NumCoords()), 3, "Number of coordinates should be the 3")
	assert.Equal(t, []float64{10, 20}, simpleCoords.Get(0).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{30, 40}, simpleCoords.Get(1).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{5, 6}, simpleCoords.Get(2).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))

	simpleCoords.Set(2, pointFactory(50, 60))
	assert.Equal(t, int(simpleCoords.NumCoords()), 3, "Number of coordinates should be the 3")
	assert.Equal(t, []float64{10, 20}, simpleCoords.Get(0).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{30, 40}, simpleCoords.Get(1).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{50, 60}, simpleCoords.Get(2).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))

	assert.Panics(t, func() { simpleCoords.Set(3, pointFactory(99, 9)) })
	assert.Equal(t, int(simpleCoords.NumCoords()), 3, "Number of coordinates should be the 3")
	assert.Equal(t, []float64{10, 20}, simpleCoords.Get(0).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{30, 40}, simpleCoords.Get(1).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{50, 60}, simpleCoords.Get(2).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))

	assert.Panics(t, func() { simpleCoords.Set(2, pointFactory(99, 99, 99)) })
	assert.Equal(t, int(simpleCoords.NumCoords()), 3, "Number of coordinates should be the 3")
	assert.Equal(t, []float64{10, 20}, simpleCoords.Get(0).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{30, 40}, simpleCoords.Get(1).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
	assert.Equal(t, []float64{50, 60}, simpleCoords.Get(2).ToArray(), fmt.Sprintf("Incorrect first coordinate in %v", simpleCoords))
}
开发者ID:jesseeichar,项目名称:gotopo,代码行数:35,代码来源:coords_test_functions.go

示例14: TestTracer_TraceWithInvalidLevels

func TestTracer_TraceWithInvalidLevels(t *testing.T) {

	assert.Panics(t, func() {

		tracer := New(LevelDebug)

		tracer.Trace(LevelEverything, "%s", "test")

	}, "Trace LevelEverything")

	assert.Panics(t, func() {

		tracer := New(LevelDebug)

		tracer.Trace(LevelNothing, "%s", "test")

	}, "Trace LevelNothing")

	assert.Panics(t, func() {

		tracer := New(LevelDebug)

		tracer.Trace(LevelEverything-1, "%s", "test")

	}, "Trace too low")

	assert.Panics(t, func() {

		tracer := New(LevelDebug)

		tracer.Trace(LevelNothing+1, "%s", "test")

	}, "Trace too high")

}
开发者ID:jmptrader,项目名称:tracer,代码行数:35,代码来源:tracer_test.go

示例15: TestPanic

func TestPanic(t *testing.T) {

	// only message

	dummy.Reset()

	msg := "panic test message"
	err := errors.New("dummy")
	assert.Panics(t, func() { Panic(msg, err) })

	m := map[string]interface{}{}
	assert.NoError(t, json.NewDecoder(dummy).Decode(&m))

	assert.Equal(t, msg, m["msg"])
	assert.Equal(t, logrus.PanicLevel.String(), m["level"])
	assert.Equal(t, err.Error(), m[logrus.ErrorKey])
	assert.Nil(t, m["id"])

	// with fields

	dummy.Reset()

	id := "vytxeTZskVKR7C7WgdSP3d"
	assert.Panics(t, func() { Panic(msg, err, logrus.Fields{"id": id}) })

	m = map[string]interface{}{}
	assert.NoError(t, json.NewDecoder(dummy).Decode(&m))

	assert.Equal(t, msg, m["msg"])
	assert.Equal(t, logrus.PanicLevel.String(), m["level"])
	assert.Equal(t, err.Error(), m[logrus.ErrorKey])
	assert.Equal(t, id, m["id"])
}
开发者ID:osamingo,项目名称:logrusow,代码行数:33,代码来源:logger_test.go


注:本文中的github.com/stretchr/testify/assert.Panics函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。