terraform/internal/lang/funcs/number_test.go

385 lines
7.3 KiB
Go
Raw Normal View History

2018-05-23 21:41:55 +02:00
package funcs
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/internal/lang/marks"
2018-05-23 21:41:55 +02:00
"github.com/zclconf/go-cty/cty"
)
2018-05-23 22:57:37 +02:00
func TestLog(t *testing.T) {
tests := []struct {
Num cty.Value
Base cty.Value
Want cty.Value
Err bool
}{
{
cty.NumberFloatVal(1),
cty.NumberFloatVal(10),
cty.NumberFloatVal(0),
false,
},
{
cty.NumberFloatVal(10),
cty.NumberFloatVal(10),
cty.NumberFloatVal(1),
false,
},
{
cty.NumberFloatVal(0),
cty.NumberFloatVal(10),
cty.NegativeInfinity,
false,
},
{
cty.NumberFloatVal(10),
cty.NumberFloatVal(0),
cty.NumberFloatVal(-0),
false,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("log(%#v, %#v)", test.Num, test.Base), func(t *testing.T) {
got, err := Log(test.Num, test.Base)
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 TestPow(t *testing.T) {
tests := []struct {
Num cty.Value
Power cty.Value
Want cty.Value
Err bool
}{
{
cty.NumberFloatVal(1),
cty.NumberFloatVal(0),
cty.NumberFloatVal(1),
false,
},
{
cty.NumberFloatVal(1),
cty.NumberFloatVal(1),
cty.NumberFloatVal(1),
false,
},
{
cty.NumberFloatVal(2),
cty.NumberFloatVal(0),
cty.NumberFloatVal(1),
false,
},
{
cty.NumberFloatVal(2),
cty.NumberFloatVal(1),
cty.NumberFloatVal(2),
false,
},
{
cty.NumberFloatVal(3),
cty.NumberFloatVal(2),
cty.NumberFloatVal(9),
false,
},
{
cty.NumberFloatVal(-3),
cty.NumberFloatVal(2),
cty.NumberFloatVal(9),
false,
},
{
cty.NumberFloatVal(2),
cty.NumberFloatVal(-2),
cty.NumberFloatVal(0.25),
false,
},
{
cty.NumberFloatVal(0),
cty.NumberFloatVal(2),
cty.NumberFloatVal(0),
false,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("pow(%#v, %#v)", test.Num, test.Power), func(t *testing.T) {
got, err := Pow(test.Num, test.Power)
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 TestSignum(t *testing.T) {
tests := []struct {
Num cty.Value
Want cty.Value
Err bool
}{
{
cty.NumberFloatVal(0),
cty.NumberFloatVal(0),
false,
},
{
cty.NumberFloatVal(12),
cty.NumberFloatVal(1),
false,
},
{
cty.NumberFloatVal(-29),
cty.NumberFloatVal(-1),
false,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("signum(%#v)", test.Num), func(t *testing.T) {
got, err := Signum(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)
}
})
}
}
2019-09-10 05:42:45 +02:00
func TestParseInt(t *testing.T) {
tests := []struct {
Num cty.Value
Base cty.Value
Want cty.Value
Err string
2019-09-10 05:42:45 +02:00
}{
{
cty.StringVal("128"),
cty.NumberIntVal(10),
cty.NumberIntVal(128),
``,
},
{
cty.StringVal("128").Mark(marks.Sensitive),
cty.NumberIntVal(10),
cty.NumberIntVal(128).Mark(marks.Sensitive),
``,
},
{
cty.StringVal("128"),
cty.NumberIntVal(10).Mark(marks.Sensitive),
cty.NumberIntVal(128).Mark(marks.Sensitive),
``,
},
{
cty.StringVal("128").Mark(marks.Sensitive),
cty.NumberIntVal(10).Mark(marks.Sensitive),
cty.NumberIntVal(128).Mark(marks.Sensitive),
``,
},
{
cty.StringVal("128").Mark("boop"),
cty.NumberIntVal(10).Mark(marks.Sensitive),
cty.NumberIntVal(128).WithMarks(cty.NewValueMarks("boop", marks.Sensitive)),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("-128"),
cty.NumberIntVal(10),
cty.NumberIntVal(-128),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("00128"),
cty.NumberIntVal(10),
cty.NumberIntVal(128),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("-00128"),
cty.NumberIntVal(10),
cty.NumberIntVal(-128),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("FF00"),
cty.NumberIntVal(16),
cty.NumberIntVal(65280),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("ff00"),
cty.NumberIntVal(16),
cty.NumberIntVal(65280),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("-FF00"),
cty.NumberIntVal(16),
cty.NumberIntVal(-65280),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("00FF00"),
cty.NumberIntVal(16),
cty.NumberIntVal(65280),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("-00FF00"),
cty.NumberIntVal(16),
cty.NumberIntVal(-65280),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("1011111011101111"),
cty.NumberIntVal(2),
cty.NumberIntVal(48879),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("aA"),
cty.NumberIntVal(62),
cty.NumberIntVal(656),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("Aa"),
cty.NumberIntVal(62),
cty.NumberIntVal(2242),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("999999999999999999999999999999999999999999999999999999999999"),
cty.NumberIntVal(10),
cty.MustParseNumberVal("999999999999999999999999999999999999999999999999999999999999"),
``,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("FF"),
cty.NumberIntVal(10),
cty.UnknownVal(cty.Number),
`cannot parse "FF" as a base 10 integer`,
},
{
cty.StringVal("FF").Mark(marks.Sensitive),
cty.NumberIntVal(10),
cty.UnknownVal(cty.Number),
`cannot parse (sensitive value) as a base 10 integer`,
},
{
cty.StringVal("FF").Mark(marks.Sensitive),
cty.NumberIntVal(10).Mark(marks.Sensitive),
cty.UnknownVal(cty.Number),
`cannot parse (sensitive value) as a base (sensitive value) integer`,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("00FF"),
cty.NumberIntVal(10),
cty.UnknownVal(cty.Number),
`cannot parse "00FF" as a base 10 integer`,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("-00FF"),
cty.NumberIntVal(10),
cty.UnknownVal(cty.Number),
`cannot parse "-00FF" as a base 10 integer`,
2019-09-10 05:42:45 +02:00
},
{
cty.NumberIntVal(2),
cty.NumberIntVal(10),
cty.UnknownVal(cty.Number),
`first argument must be a string, not number`,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("1"),
cty.NumberIntVal(63),
cty.UnknownVal(cty.Number),
`base must be a whole number between 2 and 62 inclusive`,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("1"),
cty.NumberIntVal(-1),
cty.UnknownVal(cty.Number),
`base must be a whole number between 2 and 62 inclusive`,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("1"),
cty.NumberIntVal(1),
cty.UnknownVal(cty.Number),
`base must be a whole number between 2 and 62 inclusive`,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("1"),
cty.NumberIntVal(0),
cty.UnknownVal(cty.Number),
`base must be a whole number between 2 and 62 inclusive`,
2019-09-10 05:42:45 +02:00
},
{
cty.StringVal("1.2"),
cty.NumberIntVal(10),
cty.UnknownVal(cty.Number),
`cannot parse "1.2" as a base 10 integer`,
2019-09-10 05:42:45 +02:00
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("parseint(%#v, %#v)", test.Num, test.Base), func(t *testing.T) {
got, err := ParseInt(test.Num, test.Base)
if test.Err != "" {
2019-09-10 05:42:45 +02:00
if err == nil {
t.Fatal("succeeded; want error")
}
if got, want := err.Error(), test.Err; got != want {
t.Errorf("wrong error\ngot: %s\nwant: %s", got, want)
}
2019-09-10 05:42:45 +02:00
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)
}
})
}
}