...

Source file src/github.com/pelletier/go-toml/v2/internal/danger/danger_test.go

Documentation: github.com/pelletier/go-toml/v2/internal/danger

     1  package danger_test
     2  
     3  import (
     4  	"testing"
     5  	"unsafe"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/pelletier/go-toml/v2/internal/danger"
    11  )
    12  
    13  func TestSubsliceOffsetValid(t *testing.T) {
    14  	examples := []struct {
    15  		desc   string
    16  		test   func() ([]byte, []byte)
    17  		offset int
    18  	}{
    19  		{
    20  			desc: "simple",
    21  			test: func() ([]byte, []byte) {
    22  				data := []byte("hello")
    23  				return data, data[1:]
    24  			},
    25  			offset: 1,
    26  		},
    27  	}
    28  
    29  	for _, e := range examples {
    30  		t.Run(e.desc, func(t *testing.T) {
    31  			d, s := e.test()
    32  			offset := danger.SubsliceOffset(d, s)
    33  			assert.Equal(t, e.offset, offset)
    34  		})
    35  	}
    36  }
    37  
    38  func TestSubsliceOffsetInvalid(t *testing.T) {
    39  	examples := []struct {
    40  		desc string
    41  		test func() ([]byte, []byte)
    42  	}{
    43  		{
    44  			desc: "unrelated arrays",
    45  			test: func() ([]byte, []byte) {
    46  				return []byte("one"), []byte("two")
    47  			},
    48  		},
    49  		{
    50  			desc: "slice starts before data",
    51  			test: func() ([]byte, []byte) {
    52  				full := []byte("hello world")
    53  				return full[5:], full[1:]
    54  			},
    55  		},
    56  		{
    57  			desc: "slice starts after data",
    58  			test: func() ([]byte, []byte) {
    59  				full := []byte("hello world")
    60  				return full[:3], full[5:]
    61  			},
    62  		},
    63  		{
    64  			desc: "slice ends after data",
    65  			test: func() ([]byte, []byte) {
    66  				full := []byte("hello world")
    67  				return full[:5], full[3:8]
    68  			},
    69  		},
    70  	}
    71  
    72  	for _, e := range examples {
    73  		t.Run(e.desc, func(t *testing.T) {
    74  			d, s := e.test()
    75  			require.Panics(t, func() {
    76  				danger.SubsliceOffset(d, s)
    77  			})
    78  		})
    79  	}
    80  }
    81  
    82  func TestStride(t *testing.T) {
    83  	a := []byte{1, 2, 3, 4}
    84  	x := &a[1]
    85  	n := (*byte)(danger.Stride(unsafe.Pointer(x), unsafe.Sizeof(byte(0)), 1))
    86  	require.Equal(t, &a[2], n)
    87  	n = (*byte)(danger.Stride(unsafe.Pointer(x), unsafe.Sizeof(byte(0)), -1))
    88  	require.Equal(t, &a[0], n)
    89  }
    90  
    91  func TestBytesRange(t *testing.T) {
    92  	type fn = func() ([]byte, []byte)
    93  	examples := []struct {
    94  		desc     string
    95  		test     fn
    96  		expected []byte
    97  	}{
    98  		{
    99  			desc: "simple",
   100  			test: func() ([]byte, []byte) {
   101  				full := []byte("hello world")
   102  				return full[1:3], full[6:8]
   103  			},
   104  			expected: []byte("ello wo"),
   105  		},
   106  		{
   107  			desc: "full",
   108  			test: func() ([]byte, []byte) {
   109  				full := []byte("hello world")
   110  				return full[0:1], full[len(full)-1:]
   111  			},
   112  			expected: []byte("hello world"),
   113  		},
   114  		{
   115  			desc: "end before start",
   116  			test: func() ([]byte, []byte) {
   117  				full := []byte("hello world")
   118  				return full[len(full)-1:], full[0:1]
   119  			},
   120  		},
   121  		{
   122  			desc: "nils",
   123  			test: func() ([]byte, []byte) {
   124  				return nil, nil
   125  			},
   126  		},
   127  		{
   128  			desc: "nils start",
   129  			test: func() ([]byte, []byte) {
   130  				return nil, []byte("foo")
   131  			},
   132  		},
   133  		{
   134  			desc: "nils end",
   135  			test: func() ([]byte, []byte) {
   136  				return []byte("foo"), nil
   137  			},
   138  		},
   139  		{
   140  			desc: "start is end",
   141  			test: func() ([]byte, []byte) {
   142  				full := []byte("hello world")
   143  				return full[1:3], full[1:3]
   144  			},
   145  			expected: []byte("el"),
   146  		},
   147  		{
   148  			desc: "end contained in start",
   149  			test: func() ([]byte, []byte) {
   150  				full := []byte("hello world")
   151  				return full[1:7], full[2:4]
   152  			},
   153  			expected: []byte("ello w"),
   154  		},
   155  		{
   156  			desc: "different backing arrays",
   157  			test: func() ([]byte, []byte) {
   158  				one := []byte("hello world")
   159  				two := []byte("hello world")
   160  				return one, two
   161  			},
   162  		},
   163  	}
   164  
   165  	for _, e := range examples {
   166  		t.Run(e.desc, func(t *testing.T) {
   167  			start, end := e.test()
   168  			if e.expected == nil {
   169  				require.Panics(t, func() {
   170  					danger.BytesRange(start, end)
   171  				})
   172  			} else {
   173  				res := danger.BytesRange(start, end)
   174  				require.Equal(t, e.expected, res)
   175  			}
   176  		})
   177  	}
   178  }
   179  

View as plain text