format_test.go 3.7 KB

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