github.com/google/yamlfmt@v0.12.2-0.20240514121411-7f77800e2681/formatters/basic/factory_test.go (about)

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package basic_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/yamlfmt"
    21  	"github.com/google/yamlfmt/formatters/basic"
    22  )
    23  
    24  func TestNewWithConfigRetainsDefaultValues(t *testing.T) {
    25  	testCases := []struct {
    26  		name           string
    27  		configMap      map[string]interface{}
    28  		expectedConfig basic.Config
    29  	}{
    30  		{
    31  			name: "only indent specified",
    32  			configMap: map[string]interface{}{
    33  				"indent": 4,
    34  			},
    35  			expectedConfig: basic.Config{
    36  				Indent:               4,
    37  				IncludeDocumentStart: false,
    38  				LineEnding:           yamlfmt.LineBreakStyleLF,
    39  				PadLineComments:      1,
    40  			},
    41  		},
    42  		{
    43  			name: "only include_document_start specified",
    44  			configMap: map[string]interface{}{
    45  				"include_document_start": true,
    46  			},
    47  			expectedConfig: basic.Config{
    48  				Indent:               2,
    49  				IncludeDocumentStart: true,
    50  				LineEnding:           yamlfmt.LineBreakStyleLF,
    51  				PadLineComments:      1,
    52  			},
    53  		},
    54  		{
    55  			name: "only line_ending style specified",
    56  			configMap: map[string]interface{}{
    57  				"line_ending": "crlf",
    58  			},
    59  			expectedConfig: basic.Config{
    60  				Indent:               2,
    61  				IncludeDocumentStart: false,
    62  				LineEnding:           yamlfmt.LineBreakStyleCRLF,
    63  				PadLineComments:      1,
    64  			},
    65  		},
    66  		{
    67  			name: "only pad_line_comments specified",
    68  			configMap: map[string]interface{}{
    69  				"pad_line_comments": 2,
    70  			},
    71  			expectedConfig: basic.Config{
    72  				Indent:               2,
    73  				IncludeDocumentStart: false,
    74  				LineEnding:           yamlfmt.LineBreakStyleLF,
    75  				PadLineComments:      2,
    76  			},
    77  		},
    78  		{
    79  			name: "only indentless_arrays specified",
    80  			configMap: map[string]interface{}{
    81  				"indentless_arrays": true,
    82  			},
    83  			expectedConfig: basic.Config{
    84  				Indent:           2,
    85  				LineEnding:       yamlfmt.LineBreakStyleLF,
    86  				PadLineComments:  1,
    87  				IndentlessArrays: true,
    88  			},
    89  		},
    90  		{
    91  			name: "all specified",
    92  			configMap: map[string]interface{}{
    93  				"indent":                 4,
    94  				"line_ending":            "crlf",
    95  				"include_document_start": true,
    96  				"pad_line_comments":      2,
    97  			},
    98  			expectedConfig: basic.Config{
    99  				Indent:               4,
   100  				IncludeDocumentStart: true,
   101  				LineEnding:           yamlfmt.LineBreakStyleCRLF,
   102  				PadLineComments:      2,
   103  			},
   104  		},
   105  	}
   106  
   107  	factory := basic.BasicFormatterFactory{}
   108  	for _, tc := range testCases {
   109  		t.Run(tc.name, func(t *testing.T) {
   110  			formatter, err := factory.NewFormatter(tc.configMap)
   111  			if err != nil {
   112  				t.Fatalf("expected factory to create config, got error: %v", err)
   113  			}
   114  			basicFormatter, ok := formatter.(*basic.BasicFormatter)
   115  			if !ok {
   116  				t.Fatal("should have been able to cast to basic formatter")
   117  			}
   118  			if *basicFormatter.Config != tc.expectedConfig {
   119  				t.Fatalf("configs differed:\nexpected: %v\ngot: %v", *basicFormatter.Config, tc.expectedConfig)
   120  			}
   121  		})
   122  	}
   123  }