github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/python/utilities_test.go (about)

     1  package python
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/blang/semver"
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/hashicorp/hcl/v2"
    12  	"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
    13  	"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
    14  	"github.com/pulumi/pulumi/pkg/v3/codegen/testing/utils"
    15  )
    16  
    17  var testdataPath = filepath.Join("..", "testing", "test", "testdata")
    18  
    19  func parseAndBindProgram(t *testing.T, text, name string, options ...pcl.BindOption) (*pcl.Program, hcl.Diagnostics) {
    20  	parser := syntax.NewParser()
    21  	err := parser.ParseFile(strings.NewReader(text), name)
    22  	if err != nil {
    23  		t.Fatalf("could not read %v: %v", name, err)
    24  	}
    25  	if parser.Diagnostics.HasErrors() {
    26  		t.Fatalf("failed to parse files: %v", parser.Diagnostics)
    27  	}
    28  
    29  	options = append(options, pcl.PluginHost(utils.NewHost(testdataPath)))
    30  
    31  	program, diags, err := pcl.BindProgram(parser.Files, options...)
    32  	if err != nil {
    33  		t.Fatalf("could not bind program: %v", err)
    34  	}
    35  	return program, diags
    36  }
    37  
    38  func TestMakeSafeEnumName(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	tests := []struct {
    42  		input    string
    43  		expected string
    44  		wantErr  bool
    45  	}{
    46  		{"red", "RED", false},
    47  		{"snake_cased_name", "SNAKE_CASED_NAME", false},
    48  		{"+", "", true},
    49  		{"*", "ASTERISK", false},
    50  		{"0", "ZERO", false},
    51  		{"8.3", "TYPE_NAME_8_3", false},
    52  		{"11", "TYPE_NAME_11", false},
    53  		{"Microsoft-Windows-Shell-Startup", "MICROSOFT_WINDOWS_SHELL_STARTUP", false},
    54  		{"Microsoft.Batch", "MICROSOFT_BATCH", false},
    55  		{"readonly", "READONLY", false},
    56  		{"SystemAssigned, UserAssigned", "SYSTEM_ASSIGNED_USER_ASSIGNED", false},
    57  		{"Dev(NoSLA)_Standard_D11_v2", "DEV_NO_SL_A_STANDARD_D11_V2", false},
    58  		{"Standard_E8as_v4+1TB_PS", "STANDARD_E8AS_V4_1_T_B_PS", false},
    59  		{"Plants'R'Us", "PLANTS_R_US", false},
    60  		{"Pulumi Planters Inc.", "PULUMI_PLANTERS_INC_", false},
    61  		{"ZeroPointOne", "ZERO_POINT_ONE", false},
    62  	}
    63  	for _, tt := range tests {
    64  		tt := tt
    65  		t.Run(tt.input, func(t *testing.T) {
    66  			t.Parallel()
    67  
    68  			got, err := makeSafeEnumName(tt.input, "TypeName")
    69  			if (err != nil) != tt.wantErr {
    70  				t.Errorf("makeSafeEnumName() error = %v, wantErr %v", err, tt.wantErr)
    71  				return
    72  			}
    73  			if got != tt.expected {
    74  				t.Errorf("makeSafeEnumName() got = %v, want %v", got, tt.expected)
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  func TestMakePyPiVersion(t *testing.T) {
    81  	t.Parallel()
    82  
    83  	tests := []struct {
    84  		input    string
    85  		expected string
    86  	}{
    87  		{"1.2.3", "1.2.3"},
    88  		{"1.2.3+dirty", "1.2.3+dirty"},
    89  		{"1.2.3-alpha123+beta123", "1.2.3a123+beta123"},
    90  		{"1.2.3-rc789", "1.2.3rc789"},
    91  		{"1.2.3-dev321", "1.2.3.dev321"},
    92  		{"1.2.3-post456", "1.2.3.post456"},
    93  		{"1.2.3-posttt456", "1.2.3+posttt456"},
    94  	}
    95  	for _, tt := range tests {
    96  		tt := tt
    97  		t.Run(tt.input, func(t *testing.T) {
    98  			t.Parallel()
    99  
   100  			v := semver.MustParse(tt.input)
   101  			actual := pypiVersion(v)
   102  			if tt.expected != actual {
   103  				t.Errorf("expected %q != actual %q", tt.expected, actual)
   104  			}
   105  		})
   106  	}
   107  }
   108  
   109  func TestPythonCase(t *testing.T) {
   110  	t.Parallel()
   111  
   112  	tests := []struct{ input, expected string }{
   113  		{"FOOBarInput", "FOOBarInput"},
   114  		{"foo-bar", "FooBar"},
   115  	}
   116  
   117  	for _, tt := range tests {
   118  		tt := tt
   119  		t.Run(tt.input, func(t *testing.T) {
   120  			t.Parallel()
   121  			assert.Equal(t, tt.expected, pythonCase(tt.input))
   122  		})
   123  	}
   124  }