github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/repl/format_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package repl
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/terramate-io/tf/lang/marks"
    11  	"github.com/zclconf/go-cty/cty"
    12  )
    13  
    14  func TestFormatValue(t *testing.T) {
    15  	tests := []struct {
    16  		Val  cty.Value
    17  		Want string
    18  	}{
    19  		{
    20  			cty.NullVal(cty.DynamicPseudoType),
    21  			`null`,
    22  		},
    23  		{
    24  			cty.NullVal(cty.String),
    25  			`tostring(null)`,
    26  		},
    27  		{
    28  			cty.NullVal(cty.Number),
    29  			`tonumber(null)`,
    30  		},
    31  		{
    32  			cty.NullVal(cty.Bool),
    33  			`tobool(null)`,
    34  		},
    35  		{
    36  			cty.NullVal(cty.List(cty.String)),
    37  			`tolist(null) /* of string */`,
    38  		},
    39  		{
    40  			cty.NullVal(cty.Set(cty.Number)),
    41  			`toset(null) /* of number */`,
    42  		},
    43  		{
    44  			cty.NullVal(cty.Map(cty.Bool)),
    45  			`tomap(null) /* of bool */`,
    46  		},
    47  		{
    48  			cty.NullVal(cty.Object(map[string]cty.Type{"a": cty.Bool})),
    49  			`null /* object */`, // Ideally this would display the full object type, including its attributes
    50  		},
    51  		{
    52  			cty.UnknownVal(cty.DynamicPseudoType),
    53  			`(known after apply)`,
    54  		},
    55  		{
    56  			cty.StringVal(""),
    57  			`""`,
    58  		},
    59  		{
    60  			cty.StringVal("hello"),
    61  			`"hello"`,
    62  		},
    63  		{
    64  			cty.StringVal("hello\nworld"),
    65  			`<<EOT
    66  hello
    67  world
    68  EOT`,
    69  		},
    70  		{
    71  			cty.StringVal("EOR\nEOS\nEOT\nEOU"),
    72  			`<<EOT_
    73  EOR
    74  EOS
    75  EOT
    76  EOU
    77  EOT_`,
    78  		},
    79  		{
    80  			cty.ObjectVal(map[string]cty.Value{"foo": cty.StringVal("boop\nbeep")}),
    81  			`{
    82    "foo" = <<-EOT
    83    boop
    84    beep
    85    EOT
    86  }`,
    87  		},
    88  		{
    89  			cty.Zero,
    90  			`0`,
    91  		},
    92  		{
    93  			cty.NumberIntVal(5),
    94  			`5`,
    95  		},
    96  		{
    97  			cty.NumberIntVal(1234567890),
    98  			`1234567890`,
    99  		},
   100  		{
   101  			cty.NumberFloatVal(5.2),
   102  			`5.2`,
   103  		},
   104  		{
   105  			cty.NumberFloatVal(123456789.0),
   106  			`123456789`,
   107  		},
   108  		{
   109  			cty.NumberFloatVal(123456789.01),
   110  			`123456789.01`,
   111  		},
   112  		{
   113  			cty.False,
   114  			`false`,
   115  		},
   116  		{
   117  			cty.True,
   118  			`true`,
   119  		},
   120  		{
   121  			cty.EmptyObjectVal,
   122  			`{}`,
   123  		},
   124  		{
   125  			cty.ObjectVal(map[string]cty.Value{
   126  				"a": cty.StringVal("b"),
   127  			}),
   128  			`{
   129    "a" = "b"
   130  }`,
   131  		},
   132  		{
   133  			cty.ObjectVal(map[string]cty.Value{
   134  				"a": cty.StringVal("b"),
   135  				"c": cty.StringVal("d"),
   136  			}),
   137  			`{
   138    "a" = "b"
   139    "c" = "d"
   140  }`,
   141  		},
   142  		{
   143  			cty.MapValEmpty(cty.String),
   144  			`tomap({})`,
   145  		},
   146  		{
   147  			cty.EmptyTupleVal,
   148  			`[]`,
   149  		},
   150  		{
   151  			cty.TupleVal([]cty.Value{
   152  				cty.StringVal("b"),
   153  			}),
   154  			`[
   155    "b",
   156  ]`,
   157  		},
   158  		{
   159  			cty.TupleVal([]cty.Value{
   160  				cty.StringVal("b"),
   161  				cty.StringVal("d"),
   162  			}),
   163  			`[
   164    "b",
   165    "d",
   166  ]`,
   167  		},
   168  		{
   169  			cty.ListValEmpty(cty.String),
   170  			`tolist([])`,
   171  		},
   172  		{
   173  			cty.SetValEmpty(cty.String),
   174  			`toset([])`,
   175  		},
   176  		{
   177  			cty.StringVal("a sensitive value").Mark(marks.Sensitive),
   178  			"(sensitive value)",
   179  		},
   180  	}
   181  
   182  	for _, test := range tests {
   183  		t.Run(fmt.Sprintf("%#v", test.Val), func(t *testing.T) {
   184  			got := FormatValue(test.Val, 0)
   185  			if got != test.Want {
   186  				t.Errorf("wrong result\nvalue: %#v\ngot:   %s\nwant:  %s", test.Val, got, test.Want)
   187  			}
   188  		})
   189  	}
   190  }