remove old funcs code
This commit is contained in:
parent
d0d85f909a
commit
d999d43483
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -8,6 +8,7 @@ import (
|
|||
homedir "github.com/mitchellh/go-homedir"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/function"
|
||||
"github.com/zclconf/go-cty/cty/function/stdlib"
|
||||
)
|
||||
|
||||
func TestFile(t *testing.T) {
|
||||
|
@ -150,7 +151,7 @@ func TestTemplateFile(t *testing.T) {
|
|||
|
||||
templateFileFn := MakeTemplateFileFunc(".", func() map[string]function.Function {
|
||||
return map[string]function.Function{
|
||||
"join": JoinFunc,
|
||||
"join": stdlib.JoinFunc,
|
||||
"templatefile": MakeFileFunc(".", false), // just a placeholder, since templatefile itself overrides this
|
||||
}
|
||||
})
|
||||
|
|
|
@ -9,44 +9,6 @@ import (
|
|||
"github.com/zclconf/go-cty/cty/gocty"
|
||||
)
|
||||
|
||||
// CeilFunc contructs a function that returns the closest whole number greater
|
||||
// than or equal to the given value.
|
||||
var CeilFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
var val float64
|
||||
if err := gocty.FromCtyValue(args[0], &val); err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
return cty.NumberIntVal(int64(math.Ceil(val))), nil
|
||||
},
|
||||
})
|
||||
|
||||
// FloorFunc contructs a function that returns the closest whole number lesser
|
||||
// than or equal to the given value.
|
||||
var FloorFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
var val float64
|
||||
if err := gocty.FromCtyValue(args[0], &val); err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
return cty.NumberIntVal(int64(math.Floor(val))), nil
|
||||
},
|
||||
})
|
||||
|
||||
// LogFunc contructs a function that returns the logarithm of a given number in a given base.
|
||||
var LogFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
|
@ -185,16 +147,6 @@ var ParseIntFunc = function.New(&function.Spec{
|
|||
},
|
||||
})
|
||||
|
||||
// Ceil returns the closest whole number greater than or equal to the given value.
|
||||
func Ceil(num cty.Value) (cty.Value, error) {
|
||||
return CeilFunc.Call([]cty.Value{num})
|
||||
}
|
||||
|
||||
// Floor returns the closest whole number lesser than or equal to the given value.
|
||||
func Floor(num cty.Value) (cty.Value, error) {
|
||||
return FloorFunc.Call([]cty.Value{num})
|
||||
}
|
||||
|
||||
// Log returns returns the logarithm of a given number in a given base.
|
||||
func Log(num, base cty.Value) (cty.Value, error) {
|
||||
return LogFunc.Call([]cty.Value{num, base})
|
||||
|
|
|
@ -7,82 +7,6 @@ import (
|
|||
"github.com/zclconf/go-cty/cty"
|
||||
)
|
||||
|
||||
func TestCeil(t *testing.T) {
|
||||
tests := []struct {
|
||||
Num cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.NumberFloatVal(-1.8),
|
||||
cty.NumberFloatVal(-1),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.NumberFloatVal(1.2),
|
||||
cty.NumberFloatVal(2),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("ceil(%#v)", test.Num), func(t *testing.T) {
|
||||
got, err := Ceil(test.Num)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloor(t *testing.T) {
|
||||
tests := []struct {
|
||||
Num cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.NumberFloatVal(-1.8),
|
||||
cty.NumberFloatVal(-2),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.NumberFloatVal(1.2),
|
||||
cty.NumberFloatVal(1),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("floor(%#v)", test.Num), func(t *testing.T) {
|
||||
got, err := Floor(test.Num)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLog(t *testing.T) {
|
||||
tests := []struct {
|
||||
Num cty.Value
|
||||
|
|
|
@ -1,168 +1,13 @@
|
|||
package funcs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/function"
|
||||
"github.com/zclconf/go-cty/cty/gocty"
|
||||
)
|
||||
|
||||
var JoinFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "separator",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "lists",
|
||||
Type: cty.List(cty.String),
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
sep := args[0].AsString()
|
||||
listVals := args[1:]
|
||||
if len(listVals) < 1 {
|
||||
return cty.UnknownVal(cty.String), fmt.Errorf("at least one list is required")
|
||||
}
|
||||
|
||||
l := 0
|
||||
for _, list := range listVals {
|
||||
if !list.IsWhollyKnown() {
|
||||
return cty.UnknownVal(cty.String), nil
|
||||
}
|
||||
l += list.LengthInt()
|
||||
}
|
||||
|
||||
items := make([]string, 0, l)
|
||||
for ai, list := range listVals {
|
||||
ei := 0
|
||||
for it := list.ElementIterator(); it.Next(); {
|
||||
_, val := it.Element()
|
||||
if val.IsNull() {
|
||||
if len(listVals) > 1 {
|
||||
return cty.UnknownVal(cty.String), function.NewArgErrorf(ai+1, "element %d of list %d is null; cannot concatenate null values", ei, ai+1)
|
||||
}
|
||||
return cty.UnknownVal(cty.String), function.NewArgErrorf(ai+1, "element %d is null; cannot concatenate null values", ei)
|
||||
}
|
||||
items = append(items, val.AsString())
|
||||
ei++
|
||||
}
|
||||
}
|
||||
|
||||
return cty.StringVal(strings.Join(items, sep)), nil
|
||||
},
|
||||
})
|
||||
|
||||
var SortFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "list",
|
||||
Type: cty.List(cty.String),
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.List(cty.String)),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
listVal := args[0]
|
||||
|
||||
if !listVal.IsWhollyKnown() {
|
||||
// If some of the element values aren't known yet then we
|
||||
// can't yet predict the order of the result.
|
||||
return cty.UnknownVal(retType), nil
|
||||
}
|
||||
if listVal.LengthInt() == 0 { // Easy path
|
||||
return listVal, nil
|
||||
}
|
||||
|
||||
list := make([]string, 0, listVal.LengthInt())
|
||||
for it := listVal.ElementIterator(); it.Next(); {
|
||||
iv, v := it.Element()
|
||||
if v.IsNull() {
|
||||
return cty.UnknownVal(retType), fmt.Errorf("given list element %s is null; a null string cannot be sorted", iv.AsBigFloat().String())
|
||||
}
|
||||
list = append(list, v.AsString())
|
||||
}
|
||||
|
||||
sort.Strings(list)
|
||||
retVals := make([]cty.Value, len(list))
|
||||
for i, s := range list {
|
||||
retVals[i] = cty.StringVal(s)
|
||||
}
|
||||
return cty.ListVal(retVals), nil
|
||||
},
|
||||
})
|
||||
|
||||
var SplitFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "separator",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.List(cty.String)),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
sep := args[0].AsString()
|
||||
str := args[1].AsString()
|
||||
elems := strings.Split(str, sep)
|
||||
elemVals := make([]cty.Value, len(elems))
|
||||
for i, s := range elems {
|
||||
elemVals[i] = cty.StringVal(s)
|
||||
}
|
||||
if len(elemVals) == 0 {
|
||||
return cty.ListValEmpty(cty.String), nil
|
||||
}
|
||||
return cty.ListVal(elemVals), nil
|
||||
},
|
||||
})
|
||||
|
||||
// ChompFunc constructs a function that removes newline characters at the end of a string.
|
||||
var ChompFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
newlines := regexp.MustCompile(`(?:\r\n?|\n)*\z`)
|
||||
return cty.StringVal(newlines.ReplaceAllString(args[0].AsString(), "")), nil
|
||||
},
|
||||
})
|
||||
|
||||
// IndentFunc constructs a function that adds a given number of spaces to the
|
||||
// beginnings of all but the first line in a given multi-line string.
|
||||
var IndentFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "spaces",
|
||||
Type: cty.Number,
|
||||
},
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
var spaces int
|
||||
if err := gocty.FromCtyValue(args[0], &spaces); err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
data := args[1].AsString()
|
||||
pad := strings.Repeat(" ", spaces)
|
||||
return cty.StringVal(strings.Replace(data, "\n", "\n"+pad, -1)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// ReplaceFunc constructs a function that searches a given string for another
|
||||
// given substring, and replaces each occurence with a given replacement string.
|
||||
var ReplaceFunc = function.New(&function.Spec{
|
||||
|
@ -201,158 +46,8 @@ var ReplaceFunc = function.New(&function.Spec{
|
|||
},
|
||||
})
|
||||
|
||||
// TitleFunc constructs a function that converts the first letter of each word
|
||||
// in the given string to uppercase.
|
||||
var TitleFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return cty.StringVal(strings.Title(args[0].AsString())), nil
|
||||
},
|
||||
})
|
||||
|
||||
// TrimSpaceFunc constructs a function that removes any space characters from
|
||||
// the start and end of the given string.
|
||||
var TrimSpaceFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return cty.StringVal(strings.TrimSpace(args[0].AsString())), nil
|
||||
},
|
||||
})
|
||||
|
||||
// TrimFunc constructs a function that removes the specified characters from
|
||||
// the start and end of the given string.
|
||||
var TrimFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "cutset",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
cutset := args[1].AsString()
|
||||
return cty.StringVal(strings.Trim(str, cutset)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// TrimPrefixFunc constructs a function that removes the specified characters from
|
||||
// the start the given string.
|
||||
var TrimPrefixFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "prefix",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
prefix := args[1].AsString()
|
||||
return cty.StringVal(strings.TrimPrefix(str, prefix)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// TrimSuffixFunc constructs a function that removes the specified characters from
|
||||
// the end of the given string.
|
||||
var TrimSuffixFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "suffix",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
cutset := args[1].AsString()
|
||||
return cty.StringVal(strings.TrimSuffix(str, cutset)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// Join concatenates together the string elements of one or more lists with a
|
||||
// given separator.
|
||||
func Join(sep cty.Value, lists ...cty.Value) (cty.Value, error) {
|
||||
args := make([]cty.Value, len(lists)+1)
|
||||
args[0] = sep
|
||||
copy(args[1:], lists)
|
||||
return JoinFunc.Call(args)
|
||||
}
|
||||
|
||||
// Sort re-orders the elements of a given list of strings so that they are
|
||||
// in ascending lexicographical order.
|
||||
func Sort(list cty.Value) (cty.Value, error) {
|
||||
return SortFunc.Call([]cty.Value{list})
|
||||
}
|
||||
|
||||
// Split divides a given string by a given separator, returning a list of
|
||||
// strings containing the characters between the separator sequences.
|
||||
func Split(sep, str cty.Value) (cty.Value, error) {
|
||||
return SplitFunc.Call([]cty.Value{sep, str})
|
||||
}
|
||||
|
||||
// Chomp removes newline characters at the end of a string.
|
||||
func Chomp(str cty.Value) (cty.Value, error) {
|
||||
return ChompFunc.Call([]cty.Value{str})
|
||||
}
|
||||
|
||||
// Indent adds a given number of spaces to the beginnings of all but the first
|
||||
// line in a given multi-line string.
|
||||
func Indent(spaces, str cty.Value) (cty.Value, error) {
|
||||
return IndentFunc.Call([]cty.Value{spaces, str})
|
||||
}
|
||||
|
||||
// Replace searches a given string for another given substring,
|
||||
// and replaces all occurences with a given replacement string.
|
||||
func Replace(str, substr, replace cty.Value) (cty.Value, error) {
|
||||
return ReplaceFunc.Call([]cty.Value{str, substr, replace})
|
||||
}
|
||||
|
||||
// Title converts the first letter of each word in the given string to uppercase.
|
||||
func Title(str cty.Value) (cty.Value, error) {
|
||||
return TitleFunc.Call([]cty.Value{str})
|
||||
}
|
||||
|
||||
// TrimSpace removes any space characters from the start and end of the given string.
|
||||
func TrimSpace(str cty.Value) (cty.Value, error) {
|
||||
return TrimSpaceFunc.Call([]cty.Value{str})
|
||||
}
|
||||
|
||||
// Trim removes the specified characters from the start and end of the given string.
|
||||
func Trim(str, cutset cty.Value) (cty.Value, error) {
|
||||
return TrimFunc.Call([]cty.Value{str, cutset})
|
||||
}
|
||||
|
||||
// TrimPrefix removes the specified prefix from the start of the given string.
|
||||
func TrimPrefix(str, prefix cty.Value) (cty.Value, error) {
|
||||
return TrimPrefixFunc.Call([]cty.Value{str, prefix})
|
||||
}
|
||||
|
||||
// TrimSuffix removes the specified suffix from the end of the given string.
|
||||
func TrimSuffix(str, suffix cty.Value) (cty.Value, error) {
|
||||
return TrimSuffixFunc.Call([]cty.Value{str, suffix})
|
||||
}
|
||||
|
|
|
@ -7,360 +7,6 @@ import (
|
|||
"github.com/zclconf/go-cty/cty"
|
||||
)
|
||||
|
||||
func TestJoin(t *testing.T) {
|
||||
tests := []struct {
|
||||
Sep cty.Value
|
||||
Lists []cty.Value
|
||||
Want cty.Value
|
||||
}{
|
||||
{
|
||||
cty.StringVal(" "),
|
||||
[]cty.Value{
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("Hello"),
|
||||
cty.StringVal("World"),
|
||||
}),
|
||||
},
|
||||
cty.StringVal("Hello World"),
|
||||
},
|
||||
{
|
||||
cty.StringVal(" "),
|
||||
[]cty.Value{
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("Hello"),
|
||||
cty.StringVal("World"),
|
||||
}),
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("Foo"),
|
||||
cty.StringVal("Bar"),
|
||||
}),
|
||||
},
|
||||
cty.StringVal("Hello World Foo Bar"),
|
||||
},
|
||||
{
|
||||
cty.StringVal(" "),
|
||||
[]cty.Value{
|
||||
cty.ListValEmpty(cty.String),
|
||||
},
|
||||
cty.StringVal(""),
|
||||
},
|
||||
{
|
||||
cty.StringVal(" "),
|
||||
[]cty.Value{
|
||||
cty.ListValEmpty(cty.String),
|
||||
cty.ListValEmpty(cty.String),
|
||||
cty.ListValEmpty(cty.String),
|
||||
},
|
||||
cty.StringVal(""),
|
||||
},
|
||||
{
|
||||
cty.StringVal(" "),
|
||||
[]cty.Value{
|
||||
cty.ListValEmpty(cty.String),
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("Foo"),
|
||||
cty.StringVal("Bar"),
|
||||
}),
|
||||
},
|
||||
cty.StringVal("Foo Bar"),
|
||||
},
|
||||
{
|
||||
cty.UnknownVal(cty.String),
|
||||
[]cty.Value{
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("Hello"),
|
||||
cty.StringVal("World"),
|
||||
}),
|
||||
},
|
||||
cty.UnknownVal(cty.String),
|
||||
},
|
||||
{
|
||||
cty.StringVal(" "),
|
||||
[]cty.Value{
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("Hello"),
|
||||
cty.UnknownVal(cty.String),
|
||||
}),
|
||||
},
|
||||
cty.UnknownVal(cty.String),
|
||||
},
|
||||
{
|
||||
cty.StringVal(" "),
|
||||
[]cty.Value{
|
||||
cty.UnknownVal(cty.List(cty.String)),
|
||||
},
|
||||
cty.UnknownVal(cty.String),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("Join(%#v, %#v...)", test.Sep, test.Lists), func(t *testing.T) {
|
||||
got, err := Join(test.Sep, test.Lists...)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSort(t *testing.T) {
|
||||
tests := []struct {
|
||||
List cty.Value
|
||||
Want cty.Value
|
||||
}{
|
||||
{
|
||||
cty.ListValEmpty(cty.String),
|
||||
cty.ListValEmpty(cty.String),
|
||||
},
|
||||
{
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("banana"),
|
||||
}),
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("banana"),
|
||||
}),
|
||||
},
|
||||
{
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("banana"),
|
||||
cty.StringVal("apple"),
|
||||
}),
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("apple"),
|
||||
cty.StringVal("banana"),
|
||||
}),
|
||||
},
|
||||
{
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("8"),
|
||||
cty.StringVal("9"),
|
||||
cty.StringVal("10"),
|
||||
}),
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("10"), // lexicographical sort, not numeric sort
|
||||
cty.StringVal("8"),
|
||||
cty.StringVal("9"),
|
||||
}),
|
||||
},
|
||||
{
|
||||
cty.UnknownVal(cty.List(cty.String)),
|
||||
cty.UnknownVal(cty.List(cty.String)),
|
||||
},
|
||||
{
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.UnknownVal(cty.String),
|
||||
}),
|
||||
cty.UnknownVal(cty.List(cty.String)),
|
||||
},
|
||||
{
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.UnknownVal(cty.String),
|
||||
cty.StringVal("banana"),
|
||||
}),
|
||||
cty.UnknownVal(cty.List(cty.String)),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("Sort(%#v)", test.List), func(t *testing.T) {
|
||||
got, err := Sort(test.List)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestSplit(t *testing.T) {
|
||||
tests := []struct {
|
||||
Sep cty.Value
|
||||
Str cty.Value
|
||||
Want cty.Value
|
||||
}{
|
||||
{
|
||||
cty.StringVal(" "),
|
||||
cty.StringVal("Hello World"),
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("Hello"),
|
||||
cty.StringVal("World"),
|
||||
}),
|
||||
},
|
||||
{
|
||||
cty.StringVal(" "),
|
||||
cty.StringVal("Hello"),
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("Hello"),
|
||||
}),
|
||||
},
|
||||
{
|
||||
cty.StringVal(" "),
|
||||
cty.StringVal(""),
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal(""),
|
||||
}),
|
||||
},
|
||||
{
|
||||
cty.StringVal(""),
|
||||
cty.StringVal(""),
|
||||
cty.ListValEmpty(cty.String),
|
||||
},
|
||||
{
|
||||
cty.UnknownVal(cty.String),
|
||||
cty.StringVal("Hello World"),
|
||||
cty.UnknownVal(cty.List(cty.String)),
|
||||
},
|
||||
{
|
||||
cty.StringVal(" "),
|
||||
cty.UnknownVal(cty.String),
|
||||
cty.UnknownVal(cty.List(cty.String)),
|
||||
},
|
||||
{
|
||||
cty.UnknownVal(cty.String),
|
||||
cty.UnknownVal(cty.String),
|
||||
cty.UnknownVal(cty.List(cty.String)),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("Split(%#v, %#v)", test.Sep, test.Str), func(t *testing.T) {
|
||||
got, err := Split(test.Sep, test.Str)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChomp(t *testing.T) {
|
||||
tests := []struct {
|
||||
String cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.StringVal("hello world"),
|
||||
cty.StringVal("hello world"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("goodbye\ncruel\nworld"),
|
||||
cty.StringVal("goodbye\ncruel\nworld"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("goodbye\r\nwindows\r\nworld"),
|
||||
cty.StringVal("goodbye\r\nwindows\r\nworld"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("goodbye\ncruel\nworld\n"),
|
||||
cty.StringVal("goodbye\ncruel\nworld"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("goodbye\ncruel\nworld\n\n\n\n"),
|
||||
cty.StringVal("goodbye\ncruel\nworld"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("goodbye\r\nwindows\r\nworld\r\n"),
|
||||
cty.StringVal("goodbye\r\nwindows\r\nworld"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("goodbye\r\nwindows\r\nworld\r\n\r\n\r\n\r\n"),
|
||||
cty.StringVal("goodbye\r\nwindows\r\nworld"),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("chomp(%#v)", test.String), func(t *testing.T) {
|
||||
got, err := Chomp(test.String)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndent(t *testing.T) {
|
||||
tests := []struct {
|
||||
String cty.Value
|
||||
Spaces cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.StringVal(`Fleas:
|
||||
Adam
|
||||
Had'em
|
||||
|
||||
E.E. Cummings`),
|
||||
cty.NumberIntVal(4),
|
||||
cty.StringVal("Fleas:\n Adam\n Had'em\n \n E.E. Cummings"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("oneliner"),
|
||||
cty.NumberIntVal(4),
|
||||
cty.StringVal("oneliner"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal(`#!/usr/bin/env bash
|
||||
date
|
||||
pwd`),
|
||||
cty.NumberIntVal(4),
|
||||
cty.StringVal("#!/usr/bin/env bash\n date\n pwd"),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("indent(%#v, %#v)", test.Spaces, test.String), func(t *testing.T) {
|
||||
got, err := Indent(test.Spaces, test.String)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplace(t *testing.T) {
|
||||
tests := []struct {
|
||||
String cty.Value
|
||||
|
@ -425,207 +71,3 @@ func TestReplace(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTitle(t *testing.T) {
|
||||
tests := []struct {
|
||||
String cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.StringVal("hello"),
|
||||
cty.StringVal("Hello"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("hello world"),
|
||||
cty.StringVal("Hello World"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal(""),
|
||||
cty.StringVal(""),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("title(%#v)", test.String), func(t *testing.T) {
|
||||
got, err := Title(test.String)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrimSpace(t *testing.T) {
|
||||
tests := []struct {
|
||||
String cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.StringVal(" hello "),
|
||||
cty.StringVal("hello"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal(""),
|
||||
cty.StringVal(""),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("trimspace(%#v)", test.String), func(t *testing.T) {
|
||||
got, err := TrimSpace(test.String)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrim(t *testing.T) {
|
||||
tests := []struct {
|
||||
String cty.Value
|
||||
Cutset cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.StringVal("!? test ?!"),
|
||||
cty.StringVal("?!"),
|
||||
cty.StringVal(" test "),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("...test..."),
|
||||
cty.StringVal("."),
|
||||
cty.StringVal("test"),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("trim(%#v, %#v)", test.String, test.Cutset), func(t *testing.T) {
|
||||
got, err := Trim(test.String, test.Cutset)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrimPrefix(t *testing.T) {
|
||||
tests := []struct {
|
||||
String cty.Value
|
||||
Prefix cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.StringVal("helloworld"),
|
||||
cty.StringVal("hello"),
|
||||
cty.StringVal("world"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("...test..."),
|
||||
cty.StringVal("."),
|
||||
cty.StringVal("..test..."),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("trimprefix(%#v, %#v)", test.String, test.Prefix), func(t *testing.T) {
|
||||
got, err := TrimPrefix(test.String, test.Prefix)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrimSuffix(t *testing.T) {
|
||||
tests := []struct {
|
||||
String cty.Value
|
||||
Suffix cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.StringVal("helloworld"),
|
||||
cty.StringVal("world"),
|
||||
cty.StringVal("hello"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("...test..."),
|
||||
cty.StringVal("."),
|
||||
cty.StringVal("...test.."),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("trimright(%#v, %#v)", test.String, test.Suffix), func(t *testing.T) {
|
||||
got, err := TrimSuffix(test.String, test.Suffix)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue