github.com/sacloud/iaas-api-go@v1.12.0/internal/dsl/resource_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 dsl
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/sacloud/iaas-api-go"
    21  	"github.com/sacloud/iaas-api-go/internal/dsl/meta"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestResource_LowerName(t *testing.T) {
    26  	expects := []struct {
    27  		resourceName string
    28  		lowerName    string
    29  	}{
    30  		{
    31  			resourceName: "Foo",
    32  			lowerName:    "foo",
    33  		},
    34  		{
    35  			resourceName: "IPAddress",
    36  			lowerName:    "ip_address",
    37  		},
    38  		{
    39  			resourceName: "ProxyLB",
    40  			lowerName:    "proxy_lb",
    41  		},
    42  		{
    43  			resourceName: "MonitorCPU",
    44  			lowerName:    "monitor_cpu",
    45  		},
    46  		{
    47  			resourceName: "SSDDisk",
    48  			lowerName:    "ssd_disk",
    49  		},
    50  	}
    51  
    52  	for _, expect := range expects {
    53  		r := &Resource{
    54  			Name: expect.resourceName,
    55  		}
    56  		require.Equal(t, expect.lowerName, r.FileSafeName())
    57  	}
    58  }
    59  
    60  func TestResource_ImportStatements(t *testing.T) {
    61  	var emptyList []string
    62  
    63  	expects := []struct {
    64  		resource          *Resource
    65  		additionalImports []string
    66  		imports           []string
    67  	}{
    68  		{
    69  			resource:          &Resource{},
    70  			additionalImports: emptyList,
    71  			imports:           emptyList,
    72  		},
    73  		{
    74  			resource: &Resource{
    75  				Operations: []*Operation{
    76  					{
    77  						Arguments: []*Argument{
    78  							{
    79  								Type: meta.Static(iaas.Client{}),
    80  							},
    81  						},
    82  					},
    83  				},
    84  			},
    85  			additionalImports: []string{"context"},
    86  			imports:           wrapByDoubleQuote("context", "github.com/sacloud/iaas-api-go"),
    87  		},
    88  	}
    89  
    90  	for _, expect := range expects {
    91  		require.Equal(t, expect.imports, expect.resource.ImportStatements(expect.additionalImports...))
    92  	}
    93  }
    94  
    95  func TestResources_ImportStatements(t *testing.T) {
    96  	var emptyList []string
    97  
    98  	expects := []struct {
    99  		resources         Resources
   100  		additionalImports []string
   101  		imports           []string
   102  	}{
   103  		{
   104  			resources:         Resources{},
   105  			additionalImports: emptyList,
   106  			imports:           emptyList,
   107  		},
   108  		{
   109  			resources: Resources([]*Resource{
   110  				{
   111  					Operations: []*Operation{
   112  						{
   113  							Arguments: []*Argument{
   114  								{
   115  									Type: meta.Static(iaas.Client{}),
   116  								},
   117  							},
   118  						},
   119  					},
   120  				},
   121  				{
   122  					Operations: []*Operation{
   123  						{
   124  							Arguments: []*Argument{
   125  								{
   126  									Type: meta.Static(iaas.Client{}),
   127  								},
   128  							},
   129  							Results: Results([]*Result{
   130  								{
   131  									Model: &Model{
   132  										Name: "Note",
   133  									},
   134  								},
   135  							}),
   136  						},
   137  					},
   138  				},
   139  			}),
   140  			additionalImports: []string{"context"},
   141  			imports:           wrapByDoubleQuote("context", "github.com/sacloud/iaas-api-go"),
   142  		},
   143  	}
   144  
   145  	for _, expect := range expects {
   146  		require.Equal(t, expect.imports, expect.resources.ImportStatements(expect.additionalImports...))
   147  	}
   148  }