github.com/hashicorp/packer@v1.14.3/hcl2template/function/length_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package function 5 6 import ( 7 "fmt" 8 "math" 9 "testing" 10 11 "github.com/zclconf/go-cty/cty" 12 ) 13 14 func TestLength(t *testing.T) { 15 tests := []struct { 16 Value cty.Value 17 Want cty.Value 18 }{ 19 { 20 cty.ListValEmpty(cty.Number), 21 cty.NumberIntVal(0), 22 }, 23 { 24 cty.ListVal([]cty.Value{cty.True}), 25 cty.NumberIntVal(1), 26 }, 27 { 28 cty.ListVal([]cty.Value{cty.UnknownVal(cty.Bool)}), 29 cty.NumberIntVal(1), 30 }, 31 { 32 cty.SetValEmpty(cty.Number), 33 cty.NumberIntVal(0), 34 }, 35 { 36 cty.SetVal([]cty.Value{cty.True}), 37 cty.NumberIntVal(1), 38 }, 39 { 40 cty.MapValEmpty(cty.Bool), 41 cty.NumberIntVal(0), 42 }, 43 { 44 cty.MapVal(map[string]cty.Value{"hello": cty.True}), 45 cty.NumberIntVal(1), 46 }, 47 { 48 cty.EmptyTupleVal, 49 cty.NumberIntVal(0), 50 }, 51 { 52 cty.UnknownVal(cty.EmptyTuple), 53 cty.NumberIntVal(0), 54 }, 55 { 56 cty.TupleVal([]cty.Value{cty.True}), 57 cty.NumberIntVal(1), 58 }, 59 { 60 cty.EmptyObjectVal, 61 cty.NumberIntVal(0), 62 }, 63 { 64 cty.UnknownVal(cty.EmptyObject), 65 cty.NumberIntVal(0), 66 }, 67 { 68 cty.ObjectVal(map[string]cty.Value{"true": cty.True}), 69 cty.NumberIntVal(1), 70 }, 71 { 72 cty.UnknownVal(cty.List(cty.Bool)), 73 cty.UnknownVal(cty.Number).Refine(). 74 NotNull(). 75 NumberRangeLowerBound(cty.Zero, true). 76 NumberRangeUpperBound(cty.NumberIntVal(math.MaxInt), true). 77 NewValue(), 78 }, 79 { 80 cty.DynamicVal, 81 cty.UnknownVal(cty.Number).RefineNotNull(), 82 }, 83 { 84 cty.StringVal("hello"), 85 cty.NumberIntVal(5), 86 }, 87 { 88 cty.StringVal(""), 89 cty.NumberIntVal(0), 90 }, 91 { 92 cty.StringVal("1"), 93 cty.NumberIntVal(1), 94 }, 95 { 96 cty.StringVal("Живой Журнал"), 97 cty.NumberIntVal(12), 98 }, 99 { 100 // note that the dieresis here is intentionally a combining 101 // ligature. 102 cty.StringVal("noël"), 103 cty.NumberIntVal(4), 104 }, 105 { 106 // The Es in this string has three combining acute accents. 107 // This tests something that NFC-normalization cannot collapse 108 // into a single precombined codepoint, since otherwise we might 109 // be cheating and relying on the single-codepoint forms. 110 cty.StringVal("wé́́é́́é́́!"), 111 cty.NumberIntVal(5), 112 }, 113 { 114 // Go's normalization forms don't handle this ligature, so we 115 // will produce the wrong result but this is now a compatibility 116 // constraint and so we'll test it. 117 cty.StringVal("baffle"), 118 cty.NumberIntVal(4), 119 }, 120 { 121 cty.StringVal("😸😾"), 122 cty.NumberIntVal(2), 123 }, 124 { 125 cty.UnknownVal(cty.String), 126 cty.UnknownVal(cty.Number).Refine(). 127 NotNull(). 128 NumberRangeLowerBound(cty.Zero, true). 129 NewValue(), 130 }, 131 } 132 133 for _, test := range tests { 134 t.Run(fmt.Sprintf("Length(%#v)", test.Value), func(t *testing.T) { 135 got, err := Length(test.Value) 136 137 if err != nil { 138 t.Fatalf("unexpected error: %s", err) 139 } 140 141 if !got.RawEquals(test.Want) { 142 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 143 } 144 }) 145 } 146 }