flex_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package layout
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestFlexGap(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. opts FlexOptions
  10. items []FlexItem
  11. expected string
  12. }{
  13. {
  14. name: "Row with gap",
  15. opts: FlexOptions{
  16. Direction: Row,
  17. Width: 20,
  18. Height: 1,
  19. Gap: 2,
  20. },
  21. items: []FlexItem{
  22. {View: "A"},
  23. {View: "B"},
  24. {View: "C"},
  25. },
  26. expected: "A B C",
  27. },
  28. {
  29. name: "Column with gap",
  30. opts: FlexOptions{
  31. Direction: Column,
  32. Width: 1,
  33. Height: 5,
  34. Gap: 1,
  35. Align: AlignStart,
  36. },
  37. items: []FlexItem{
  38. {View: "A", FixedSize: 1},
  39. {View: "B", FixedSize: 1},
  40. {View: "C", FixedSize: 1},
  41. },
  42. expected: "A\n \nB\n \nC",
  43. },
  44. {
  45. name: "Row with gap and justify space between",
  46. opts: FlexOptions{
  47. Direction: Row,
  48. Width: 15,
  49. Height: 1,
  50. Gap: 1,
  51. Justify: JustifySpaceBetween,
  52. },
  53. items: []FlexItem{
  54. {View: "A"},
  55. {View: "B"},
  56. {View: "C"},
  57. },
  58. expected: "A B C",
  59. },
  60. {
  61. name: "No gap specified",
  62. opts: FlexOptions{
  63. Direction: Row,
  64. Width: 10,
  65. Height: 1,
  66. },
  67. items: []FlexItem{
  68. {View: "A"},
  69. {View: "B"},
  70. {View: "C"},
  71. },
  72. expected: "ABC",
  73. },
  74. }
  75. for _, tt := range tests {
  76. t.Run(tt.name, func(t *testing.T) {
  77. result := Render(tt.opts, tt.items...)
  78. // Trim any trailing spaces for comparison
  79. result = strings.TrimRight(result, " ")
  80. expected := strings.TrimRight(tt.expected, " ")
  81. if result != expected {
  82. t.Errorf("Render() = %q, want %q", result, expected)
  83. }
  84. })
  85. }
  86. }