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