github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/repl/format_test.go (about)

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