1// Copyright 2022 Google Inc. All rights reserved. 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 15package starlark_fmt 16 17import ( 18 "testing" 19) 20 21func simpleFormat(s string) string { 22 return "%s" 23} 24 25func TestPrintEmptyStringList(t *testing.T) { 26 in := []string{} 27 indentLevel := 0 28 out := PrintStringList(in, indentLevel) 29 expectedOut := "[]" 30 if out != expectedOut { 31 t.Errorf("Expected %q, got %q", expectedOut, out) 32 } 33} 34 35func TestPrintSingleElementStringList(t *testing.T) { 36 in := []string{"a"} 37 indentLevel := 0 38 out := PrintStringList(in, indentLevel) 39 expectedOut := `["a"]` 40 if out != expectedOut { 41 t.Errorf("Expected %q, got %q", expectedOut, out) 42 } 43} 44 45func TestPrintMultiElementStringList(t *testing.T) { 46 in := []string{"a", "b"} 47 indentLevel := 0 48 out := PrintStringList(in, indentLevel) 49 expectedOut := `[ 50 "a", 51 "b", 52]` 53 if out != expectedOut { 54 t.Errorf("Expected %q, got %q", expectedOut, out) 55 } 56} 57 58func TestPrintEmptyList(t *testing.T) { 59 in := []string{} 60 indentLevel := 0 61 out := PrintList(in, indentLevel, simpleFormat) 62 expectedOut := "[]" 63 if out != expectedOut { 64 t.Errorf("Expected %q, got %q", expectedOut, out) 65 } 66} 67 68func TestPrintSingleElementList(t *testing.T) { 69 in := []string{"1"} 70 indentLevel := 0 71 out := PrintList(in, indentLevel, simpleFormat) 72 expectedOut := `[1]` 73 if out != expectedOut { 74 t.Errorf("Expected %q, got %q", expectedOut, out) 75 } 76} 77 78func TestPrintMultiElementList(t *testing.T) { 79 in := []string{"1", "2"} 80 indentLevel := 0 81 out := PrintList(in, indentLevel, simpleFormat) 82 expectedOut := `[ 83 1, 84 2, 85]` 86 if out != expectedOut { 87 t.Errorf("Expected %q, got %q", expectedOut, out) 88 } 89} 90 91func TestListWithNonZeroIndent(t *testing.T) { 92 in := []string{"1", "2"} 93 indentLevel := 1 94 out := PrintList(in, indentLevel, simpleFormat) 95 expectedOut := `[ 96 1, 97 2, 98 ]` 99 if out != expectedOut { 100 t.Errorf("Expected %q, got %q", expectedOut, out) 101 } 102} 103 104func TestStringListDictEmpty(t *testing.T) { 105 in := map[string][]string{} 106 indentLevel := 0 107 out := PrintStringListDict(in, indentLevel) 108 expectedOut := `{}` 109 if out != expectedOut { 110 t.Errorf("Expected %q, got %q", expectedOut, out) 111 } 112} 113 114func TestStringListDict(t *testing.T) { 115 in := map[string][]string{ 116 "key1": []string{}, 117 "key2": []string{"a"}, 118 "key3": []string{"1", "2"}, 119 } 120 indentLevel := 0 121 out := PrintStringListDict(in, indentLevel) 122 expectedOut := `{ 123 "key1": [], 124 "key2": ["a"], 125 "key3": [ 126 "1", 127 "2", 128 ], 129}` 130 if out != expectedOut { 131 t.Errorf("Expected %q, got %q", expectedOut, out) 132 } 133} 134 135func TestPrintDict(t *testing.T) { 136 in := map[string]string{ 137 "key1": `""`, 138 "key2": `"a"`, 139 "key3": `[ 140 1, 141 2, 142 ]`, 143 } 144 indentLevel := 0 145 out := PrintDict(in, indentLevel) 146 expectedOut := `{ 147 "key1": "", 148 "key2": "a", 149 "key3": [ 150 1, 151 2, 152 ], 153}` 154 if out != expectedOut { 155 t.Errorf("Expected %q, got %q", expectedOut, out) 156 } 157} 158 159func TestPrintDictWithIndent(t *testing.T) { 160 in := map[string]string{ 161 "key1": `""`, 162 "key2": `"a"`, 163 } 164 indentLevel := 1 165 out := PrintDict(in, indentLevel) 166 expectedOut := `{ 167 "key1": "", 168 "key2": "a", 169 }` 170 if out != expectedOut { 171 t.Errorf("Expected %q, got %q", expectedOut, out) 172 } 173} 174