|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package code |
| 15 | + |
| 16 | +import ( |
| 17 | + "testing" |
| 18 | + |
| 19 | + awssdkmodel "github.com/aws-controllers-k8s/code-generator/pkg/api" |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | +) |
| 22 | + |
| 23 | +func TestSetSDKForScalar(t *testing.T) { |
| 24 | + assert := assert.New(t) |
| 25 | + |
| 26 | + testCases := []struct { |
| 27 | + name string |
| 28 | + targetFieldName string |
| 29 | + targetVarName string |
| 30 | + targetVarType string |
| 31 | + sourceFieldPath string |
| 32 | + sourceVarName string |
| 33 | + isListMember bool |
| 34 | + shapeRef *awssdkmodel.ShapeRef |
| 35 | + indentLevel int |
| 36 | + expected string |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "string scalar", |
| 40 | + targetFieldName: "BucketName", |
| 41 | + targetVarName: "res", |
| 42 | + targetVarType: "structure", |
| 43 | + sourceFieldPath: "Name", |
| 44 | + sourceVarName: "ko.Spec.Name", |
| 45 | + isListMember: false, |
| 46 | + shapeRef: &awssdkmodel.ShapeRef{ |
| 47 | + Shape: &awssdkmodel.Shape{ |
| 48 | + Type: "string", |
| 49 | + }, |
| 50 | + OriginalMemberName: "BucketName", |
| 51 | + }, |
| 52 | + indentLevel: 1, |
| 53 | + expected: "\tres.BucketName = ko.Spec.Name\n", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "boolean scalar", |
| 57 | + targetFieldName: "Enabled", |
| 58 | + targetVarName: "res", |
| 59 | + targetVarType: "structure", |
| 60 | + sourceFieldPath: "Enabled", |
| 61 | + sourceVarName: "ko.Spec.Enabled", |
| 62 | + isListMember: false, |
| 63 | + shapeRef: &awssdkmodel.ShapeRef{ |
| 64 | + Shape: &awssdkmodel.Shape{ |
| 65 | + Type: "boolean", |
| 66 | + }, |
| 67 | + OriginalMemberName: "Enabled", |
| 68 | + }, |
| 69 | + indentLevel: 1, |
| 70 | + expected: "\tres.Enabled = ko.Spec.Enabled\n", |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "integer scalar", |
| 74 | + targetFieldName: "MaxKeys", |
| 75 | + targetVarName: "res", |
| 76 | + targetVarType: "structure", |
| 77 | + sourceFieldPath: "MaxKeys", |
| 78 | + sourceVarName: "ko.Spec.MaxKeys", |
| 79 | + isListMember: false, |
| 80 | + shapeRef: &awssdkmodel.ShapeRef{ |
| 81 | + Shape: &awssdkmodel.Shape{ |
| 82 | + Type: "integer", |
| 83 | + }, |
| 84 | + OriginalMemberName: "MaxKeys", |
| 85 | + }, |
| 86 | + indentLevel: 1, |
| 87 | + expected: ` maxKeysCopy0 := *ko.Spec.MaxKeys |
| 88 | + if maxKeysCopy0 > math.MaxInt32 || maxKeysCopy0 < math.MinInt32 { |
| 89 | + return nil, fmt.Errorf("error: field MaxKeys is of type int32") |
| 90 | + } |
| 91 | + maxKeysCopy := int32(maxKeysCopy0) |
| 92 | + res.MaxKeys = &maxKeysCopy |
| 93 | +`, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "float scalar", |
| 97 | + targetFieldName: "Temperature", |
| 98 | + targetVarName: "res", |
| 99 | + targetVarType: "structure", |
| 100 | + sourceFieldPath: "Temperature", |
| 101 | + sourceVarName: "ko.Spec.Temperature", |
| 102 | + isListMember: false, |
| 103 | + shapeRef: &awssdkmodel.ShapeRef{ |
| 104 | + Shape: &awssdkmodel.Shape{ |
| 105 | + Type: "float", |
| 106 | + }, |
| 107 | + OriginalMemberName: "Temperature", |
| 108 | + }, |
| 109 | + indentLevel: 1, |
| 110 | + expected: ` temperatureCopy0 := *ko.Spec.Temperature |
| 111 | + if temperatureCopy0 > math.MaxFloat32 || temperatureCopy0 < -math.MaxFloat32 || (temperatureCopy0 < math.SmallestNonzeroFloat32 && !(temperatureCopy0 <= 0)) || (temperatureCopy0 > -math.SmallestNonzeroFloat32 && !(temperatureCopy0 >= 0)) { |
| 112 | + return nil, fmt.Errorf("error: field Temperature is of type float32") |
| 113 | + } |
| 114 | + temperatureCopy := float32(temperatureCopy0) |
| 115 | + res.Temperature = &temperatureCopy |
| 116 | +`, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + for _, tc := range testCases { |
| 121 | + t.Run(tc.name, func(t *testing.T) { |
| 122 | + result := setSDKForScalar( |
| 123 | + tc.targetFieldName, |
| 124 | + tc.targetVarName, |
| 125 | + tc.targetVarType, |
| 126 | + tc.sourceFieldPath, |
| 127 | + tc.sourceVarName, |
| 128 | + tc.isListMember, |
| 129 | + tc.shapeRef, |
| 130 | + tc.indentLevel, |
| 131 | + ) |
| 132 | + |
| 133 | + assert.Equal(tc.expected, result, "setSDKForScalar() did not return expected result for %s", tc.name) |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
0 commit comments