format_test.go 3.6 KB

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