MCPcopy Index your code
hub / github.com/gavv/httpexpect / TestArray_Find

Function TestArray_Find

array_test.go:1886–2008  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1884}
1885
1886func TestArray_Find(t *testing.T) {
1887 t.Run("elements of same type", func(t *testing.T) {
1888 reporter := newMockReporter(t)
1889 array := NewArray(reporter, []interface{}{1, 2, 3, 4, 5, 6})
1890
1891 foundValue := array.Find(func(index int, value *Value) bool {
1892 return value.Raw() == 2.0
1893 })
1894
1895 assert.Equal(t, 2.0, foundValue.Raw())
1896 assert.Equal(t, array.Raw(), []interface{}{1.0, 2.0, 3.0, 4.0, 5.0, 6.0})
1897
1898 array.chain.assert(t, success)
1899 foundValue.chain.assert(t, success)
1900 })
1901
1902 t.Run("elements of different types", func(t *testing.T) {
1903 reporter := newMockReporter(t)
1904 array := NewArray(reporter, []interface{}{1, "foo", true, "bar"})
1905
1906 foundValue := array.Find(func(index int, value *Value) bool {
1907 stringifiedValue := value.String().NotEmpty().Raw()
1908 return stringifiedValue == "bar"
1909 })
1910
1911 assert.Equal(t, "bar", foundValue.Raw())
1912 assert.Equal(t, array.Raw(), []interface{}{1.0, "foo", true, "bar"})
1913
1914 array.chain.assert(t, success)
1915 foundValue.chain.assert(t, success)
1916 })
1917
1918 t.Run("first match", func(t *testing.T) {
1919 reporter := newMockReporter(t)
1920 array := NewArray(reporter, []interface{}{1, "foo", true, "bar"})
1921
1922 foundValue := array.Find(func(index int, value *Value) bool {
1923 stringifiedValue := value.String().Raw()
1924 return stringifiedValue != ""
1925 })
1926
1927 assert.Equal(t, "foo", foundValue.Raw())
1928 assert.Equal(t, array.Raw(), []interface{}{1.0, "foo", true, "bar"})
1929
1930 array.chain.assert(t, success)
1931 foundValue.chain.assert(t, success)
1932 })
1933
1934 t.Run("no match", func(t *testing.T) {
1935 reporter := newMockReporter(t)
1936 array := NewArray(reporter, []interface{}{1, "foo", true, "bar"})
1937
1938 foundValue := array.Find(func(index int, value *Value) bool {
1939 return value.Raw() == 2.0
1940 })
1941
1942 assert.Equal(t, nil, foundValue.Raw())
1943 assert.Equal(t, array.Raw(), []interface{}{1.0, "foo", true, "bar"})

Callers

nothing calls this directly

Calls 9

FindMethod · 0.95
RawMethod · 0.95
newMockReporterFunction · 0.85
NewArrayFunction · 0.85
assertMethod · 0.80
RawMethod · 0.45
EqualMethod · 0.45
NotEmptyMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…