Replace AssertPanic with AssertPanicMessageContains
Bug: 182885307 Test: m nothing Change-Id: Idffa314285c90080796cc3df391de9c314eaa422
This commit is contained in:
parent
0387862185
commit
9f4b3bbb7c
|
@ -15,6 +15,7 @@
|
||||||
package android
|
package android
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -162,19 +163,24 @@ func AssertDeepEquals(t *testing.T, message string, expected interface{}, actual
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AssertPanic checks that the supplied function panics as expected.
|
// AssertPanicMessageContains checks that the supplied function panics as expected and the message
|
||||||
func AssertPanic(t *testing.T, message string, funcThatShouldPanic func()) {
|
// obtained by formatting the recovered value as a string contains the expected contents.
|
||||||
|
func AssertPanicMessageContains(t *testing.T, message, expectedMessageContents string, funcThatShouldPanic func()) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
panicked := false
|
panicked := false
|
||||||
|
var recovered interface{}
|
||||||
func() {
|
func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if x := recover(); x != nil {
|
if recovered = recover(); recovered != nil {
|
||||||
panicked = true
|
panicked = true
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
funcThatShouldPanic()
|
funcThatShouldPanic()
|
||||||
}()
|
}()
|
||||||
if !panicked {
|
if !panicked {
|
||||||
t.Error(message)
|
t.Errorf("%s: did not panic", message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
panicMessage := fmt.Sprintf("%s", recovered)
|
||||||
|
AssertStringDoesContain(t, fmt.Sprintf("%s: panic message", message), panicMessage, expectedMessageContents)
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,9 +84,9 @@ func TestAddPropertySimple(t *testing.T) {
|
||||||
set.AddProperty(name, val)
|
set.AddProperty(name, val)
|
||||||
android.AssertDeepEquals(t, "wrong value", val, set.getValue(name))
|
android.AssertDeepEquals(t, "wrong value", val, set.getValue(name))
|
||||||
}
|
}
|
||||||
android.AssertPanic(t, "adding x again should panic",
|
android.AssertPanicMessageContains(t, "adding x again should panic", `Property "x" already exists in property set`,
|
||||||
func() { set.AddProperty("x", "taxi") })
|
func() { set.AddProperty("x", "taxi") })
|
||||||
android.AssertPanic(t, "adding arr again should panic",
|
android.AssertPanicMessageContains(t, "adding arr again should panic", `Property "arr" already exists in property set`,
|
||||||
func() { set.AddProperty("arr", []string{"d"}) })
|
func() { set.AddProperty("arr", []string{"d"}) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,14 +124,14 @@ func TestAddPropertySubset(t *testing.T) {
|
||||||
|
|
||||||
t.Run("add conflicting subset", func(t *testing.T) {
|
t.Run("add conflicting subset", func(t *testing.T) {
|
||||||
set := propertySetFixture().(*bpPropertySet)
|
set := propertySetFixture().(*bpPropertySet)
|
||||||
android.AssertPanic(t, "adding x again should panic",
|
android.AssertPanicMessageContains(t, "adding x again should panic", `Property "x" already exists in property set`,
|
||||||
func() { set.AddProperty("x", propertySetFixture()) })
|
func() { set.AddProperty("x", propertySetFixture()) })
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("add non-pointer struct", func(t *testing.T) {
|
t.Run("add non-pointer struct", func(t *testing.T) {
|
||||||
set := propertySetFixture().(*bpPropertySet)
|
set := propertySetFixture().(*bpPropertySet)
|
||||||
str := propertyStructFixture().(*propertyStruct)
|
str := propertyStructFixture().(*propertyStruct)
|
||||||
android.AssertPanic(t, "adding a non-pointer struct should panic",
|
android.AssertPanicMessageContains(t, "adding a non-pointer struct should panic", "Value is a struct, not a pointer to one:",
|
||||||
func() { set.AddProperty("new", *str) })
|
func() { set.AddProperty("new", *str) })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue