...

Source file src/github.com/playwright-community/playwright-go/helpers_test.go

Documentation: github.com/playwright-community/playwright-go

     1  package playwright
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  type testOptionsJSONSerialization struct {
    10  	StringPointer  *string `json:"stringPointer"`
    11  	NormalString   string  `json:"normalString"`
    12  	WithoutJSONTag string
    13  	WithJSONTag    string  `json:"withJSONTag"`
    14  	SkipNilPtrs    *string `json:"skipNilPtrs"`
    15  	SkipMe         *int    `json:"skipMe"`
    16  }
    17  
    18  func TestTransformOptions(t *testing.T) {
    19  	// test data
    20  	structVar := &testOptionsJSONSerialization{
    21  		StringPointer:  String("1"),
    22  		NormalString:   "2",
    23  		WithoutJSONTag: "3",
    24  		WithJSONTag:    "4",
    25  	}
    26  	var nilStrPtr *string
    27  	testCases := []struct {
    28  		name           string
    29  		baseMap        map[string]interface{}
    30  		optionalStruct interface{}
    31  		expected       interface{}
    32  	}{
    33  		{
    34  			name: "No options supplied",
    35  			baseMap: map[string]interface{}{
    36  				"1234": nilStrPtr,
    37  				"foo":  "bar",
    38  			},
    39  			expected: map[string]interface{}{
    40  				"foo": "bar",
    41  			},
    42  		},
    43  		{
    44  			name: "Options are nil",
    45  			baseMap: map[string]interface{}{
    46  				"foo": "bar",
    47  			},
    48  			optionalStruct: nil,
    49  			expected: map[string]interface{}{
    50  				"foo": "bar",
    51  			},
    52  		},
    53  		{
    54  			name: "JSON serialization works",
    55  			baseMap: map[string]interface{}{
    56  				"foo": "bar",
    57  			},
    58  			optionalStruct: structVar,
    59  			expected: map[string]interface{}{
    60  				"foo":            "bar",
    61  				"stringPointer":  String("1"),
    62  				"normalString":   "2",
    63  				"WithoutJSONTag": "3",
    64  				"withJSONTag":    "4",
    65  			},
    66  		},
    67  		{
    68  			name: "Second overwrites the first one",
    69  			baseMap: map[string]interface{}{
    70  				"foo": "1",
    71  			},
    72  			optionalStruct: map[string]interface{}{
    73  				"foo": "2",
    74  			},
    75  			expected: map[string]interface{}{
    76  				"foo": "2",
    77  			},
    78  		},
    79  	}
    80  	for _, tc := range testCases {
    81  		t.Run(tc.name, func(t *testing.T) {
    82  			require.Equal(t, tc.expected, transformOptions(tc.baseMap, tc.optionalStruct))
    83  		})
    84  	}
    85  }
    86  
    87  func TestRemapMapToStruct(t *testing.T) {
    88  	ourStruct := struct {
    89  		V1 string `json:"v1"`
    90  	}{}
    91  	inMap := map[string]interface{}{
    92  		"v1": "foobar",
    93  	}
    94  	remapMapToStruct(inMap, &ourStruct)
    95  	require.Equal(t, ourStruct.V1, "foobar")
    96  }
    97  
    98  func TestConvertSelectOptionSet(t *testing.T) {
    99  	testCases := []struct {
   100  		name         string
   101  		optionValues SelectOptionValues
   102  		expected     interface{}
   103  	}{
   104  		{
   105  			name:         "SelectOptionValues is nil",
   106  			optionValues: SelectOptionValues{},
   107  			expected:     make(map[string]interface{}),
   108  		},
   109  		{
   110  			name: "SelectOptionValues is supplied",
   111  			optionValues: SelectOptionValues{
   112  				Values:  StringSlice("a", "b"),
   113  				Indexes: IntSlice(1),
   114  				Labels:  StringSlice("x"),
   115  			},
   116  			expected: map[string]interface{}{
   117  				"options": []map[string]interface{}{
   118  					{"value": "a"}, {"value": "b"}, {"index": 1}, {"label": "x"},
   119  				},
   120  			},
   121  		},
   122  		{
   123  			name: "Only value is supplied",
   124  			optionValues: SelectOptionValues{
   125  				Values: StringSlice("a", "b"),
   126  			},
   127  			expected: map[string]interface{}{
   128  				"options": []map[string]interface{}{
   129  					{"value": "a"}, {"value": "b"},
   130  				},
   131  			},
   132  		},
   133  	}
   134  	for _, tc := range testCases {
   135  		t.Run(tc.name, func(t *testing.T) {
   136  			require.Equal(t, tc.expected, convertSelectOptionSet(tc.optionValues))
   137  		})
   138  	}
   139  }
   140  

View as plain text