github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/tiltfile/value/value_test.go (about)

     1  package value
     2  
     3  import (
     4  	"math"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  	"go.starlark.net/starlark"
    11  )
    12  
    13  func stringSeq(v ...string) starlark.Sequence {
    14  	var values []starlark.Value
    15  	for _, x := range v {
    16  		values = append(values, starlark.String(x))
    17  	}
    18  	return starlark.NewList(values)
    19  }
    20  
    21  func TestStringSequence(t *testing.T) {
    22  	type tc struct {
    23  		input    starlark.Value
    24  		expected []string
    25  		err      string
    26  	}
    27  	tcs := []tc{
    28  		{nil, nil, ""},
    29  		{stringSeq(), nil, ""},
    30  		{starlark.NewList([]starlark.Value{}), nil, ""},
    31  		{stringSeq("abc123", "def456"), []string{"abc123", "def456"}, ""},
    32  		{starlark.NewList([]starlark.Value{starlark.MakeInt(35)}), nil, "'35' is a starlark.Int, not a string"},
    33  	}
    34  	for i, tc := range tcs {
    35  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    36  			// underlying, StringSequence.Unpack() uses SequenceToStringSlice(); however, since the latter is also
    37  			// exported, it's also tested explicitly here to ensure consistent behavior between the two
    38  			var v StringSequence
    39  			err := v.Unpack(tc.input)
    40  			if tc.err != "" {
    41  				require.EqualError(t, err, tc.err)
    42  			} else {
    43  				assert.Equal(t, tc.expected, []string(v))
    44  			}
    45  
    46  			inputSeq, ok := tc.input.(starlark.Sequence)
    47  			if ok {
    48  				v, err = SequenceToStringSlice(inputSeq)
    49  				if tc.err != "" {
    50  					require.EqualError(t, err, tc.err)
    51  				} else {
    52  					assert.Equal(t, tc.expected, []string(v))
    53  					// test the round-trip (note: we have to test with iterators as direct
    54  					// equality can't be guaranteed due to difference in semantics around
    55  					// empty vs nil slices)
    56  					expectedSeq := tc.input.(starlark.Sequence)
    57  					actualSeq := v.Sequence()
    58  					if assert.Equal(t, expectedSeq.Len(), actualSeq.Len()) {
    59  						expectedIt := expectedSeq.Iterate()
    60  						actualIt := v.Sequence().Iterate()
    61  						var expectedVal starlark.Value
    62  						for expectedIt.Next(&expectedVal) {
    63  							var actualVal starlark.Value
    64  							require.True(t, actualIt.Next(&actualVal))
    65  							assert.Equal(t, expectedVal, actualVal)
    66  						}
    67  					}
    68  				}
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func TestInt32Value_Unpack(t *testing.T) {
    75  	type tc struct {
    76  		input    starlark.Value
    77  		expected int32
    78  		err      string
    79  	}
    80  	tcs := []tc{
    81  		{nil, 0, "got NoneType, want int"},
    82  		{starlark.MakeInt(0), 0, ""},
    83  		{starlark.MakeInt(-123), -123, ""},
    84  		{starlark.MakeInt(456), 456, ""},
    85  		{starlark.MakeInt64(math.MaxInt32 + 1), 0, "value out of range for int32: 2147483648"},
    86  		{starlark.MakeInt64(math.MinInt32 - 1), 0, "value out of range for int32: -2147483649"},
    87  	}
    88  	for _, tc := range tcs {
    89  		var name string
    90  		if tc.input != nil {
    91  			name = tc.input.String()
    92  		} else {
    93  			name = "nil"
    94  		}
    95  
    96  		t.Run(name, func(t *testing.T) {
    97  			var v Int32
    98  			err := v.Unpack(tc.input)
    99  			if tc.err != "" {
   100  				require.EqualError(t, err, tc.err)
   101  			} else {
   102  				assert.Equal(t, tc.expected, v.Int32())
   103  			}
   104  		})
   105  	}
   106  }