github.com/sacloud/iaas-api-go@v1.12.0/internal/dsl/meta/type_test.go (about)

     1  // Copyright 2022-2023 The sacloud/iaas-api-go Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package meta
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/sacloud/iaas-api-go"
    21  	"github.com/sacloud/iaas-api-go/types"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestStaticType_TypeImplements(t *testing.T) {
    26  	expects := []struct {
    27  		caseName                 string
    28  		instance                 interface{}
    29  		goType                   string
    30  		goPkg                    string
    31  		goImportPath             string
    32  		goTypeSourceCode         string
    33  		zeroInitializeSourceCode string
    34  		zeroValueSourceCode      string
    35  		fatal                    bool
    36  	}{
    37  		{
    38  			caseName: "unsupported primitive type",
    39  			instance: int32(0),
    40  			fatal:    true,
    41  		},
    42  		{
    43  			caseName:                 "bool",
    44  			instance:                 true,
    45  			goType:                   "bool",
    46  			goPkg:                    "",
    47  			goImportPath:             "",
    48  			goTypeSourceCode:         "bool",
    49  			zeroInitializeSourceCode: "false",
    50  			zeroValueSourceCode:      "false",
    51  		},
    52  		{
    53  			caseName:                 "int",
    54  			instance:                 0,
    55  			goType:                   "int",
    56  			goPkg:                    "",
    57  			goImportPath:             "",
    58  			goTypeSourceCode:         "int",
    59  			zeroInitializeSourceCode: "0",
    60  			zeroValueSourceCode:      "0",
    61  		},
    62  		{
    63  			caseName:                 "int64",
    64  			instance:                 int64(0),
    65  			goType:                   "int64",
    66  			goPkg:                    "",
    67  			goImportPath:             "",
    68  			goTypeSourceCode:         "int64",
    69  			zeroInitializeSourceCode: "int64(0)",
    70  			zeroValueSourceCode:      "int64(0)",
    71  		},
    72  		{
    73  			caseName:                 "string",
    74  			instance:                 "",
    75  			goType:                   "string",
    76  			goPkg:                    "",
    77  			goImportPath:             "",
    78  			goTypeSourceCode:         "string",
    79  			zeroInitializeSourceCode: `""`,
    80  			zeroValueSourceCode:      `""`,
    81  		},
    82  		{
    83  			caseName:                 "map",
    84  			instance:                 map[string]interface{}{},
    85  			goType:                   "map[string]interface {}",
    86  			goPkg:                    "",
    87  			goImportPath:             "",
    88  			goTypeSourceCode:         "map[string]interface {}",
    89  			zeroInitializeSourceCode: "map[string]interface {}{}",
    90  			zeroValueSourceCode:      "nil",
    91  		},
    92  		{
    93  			caseName:                 "slice",
    94  			instance:                 []string{},
    95  			goType:                   "[]string",
    96  			goPkg:                    "",
    97  			goImportPath:             "",
    98  			goTypeSourceCode:         "[]string",
    99  			zeroInitializeSourceCode: "[]string{}",
   100  			zeroValueSourceCode:      "nil",
   101  		},
   102  		{
   103  			caseName:                 "enum",
   104  			instance:                 types.EAvailability(""),
   105  			goType:                   "types.EAvailability",
   106  			goPkg:                    "types",
   107  			goImportPath:             "github.com/sacloud/iaas-api-go/types",
   108  			goTypeSourceCode:         "types.EAvailability",
   109  			zeroInitializeSourceCode: `types.EAvailability("")`,
   110  			zeroValueSourceCode:      `types.EAvailability("")`,
   111  		},
   112  		{
   113  			caseName:                 "another package struct",
   114  			instance:                 iaas.Client{},
   115  			goType:                   "iaas.Client",
   116  			goPkg:                    "iaas-api-go",
   117  			goImportPath:             "github.com/sacloud/iaas-api-go",
   118  			goTypeSourceCode:         "*iaas.Client",
   119  			zeroInitializeSourceCode: "&iaas.Client{}",
   120  			zeroValueSourceCode:      "nil",
   121  		},
   122  	}
   123  
   124  	for _, expect := range expects {
   125  		t.Run(expect.caseName, func(t *testing.T) {
   126  			defer func() {
   127  				err := recover()
   128  				require.Equal(t, expect.fatal, err != nil)
   129  			}()
   130  			var tp Type = Static(expect.instance)
   131  			require.Equal(t, expect.goType, tp.GoType())
   132  			require.Equal(t, expect.goPkg, tp.GoPkg())
   133  			require.Equal(t, expect.goImportPath, tp.GoImportPath())
   134  			require.Equal(t, expect.goTypeSourceCode, tp.GoTypeSourceCode())
   135  			require.Equal(t, expect.zeroInitializeSourceCode, tp.ZeroInitializeSourceCode())
   136  			require.Equal(t, expect.zeroValueSourceCode, tp.ZeroValueSourceCode())
   137  		})
   138  	}
   139  }