| 112 | } |
| 113 | |
| 114 | func TestDisplay(t *testing.T) { |
| 115 | messages := map[jsonstream.Message][]string{ |
| 116 | // Empty |
| 117 | {}: {"\n", "\n"}, |
| 118 | // Status |
| 119 | { |
| 120 | Status: "status", |
| 121 | }: { |
| 122 | "status\n", |
| 123 | "status\n", |
| 124 | }, |
| 125 | // General |
| 126 | { |
| 127 | ID: "ID", |
| 128 | Status: "status", |
| 129 | }: { |
| 130 | "ID: status\n", |
| 131 | "ID: status\n", |
| 132 | }, |
| 133 | // Stream over status |
| 134 | { |
| 135 | Status: "status", |
| 136 | Stream: "stream", |
| 137 | }: { |
| 138 | "stream", |
| 139 | "stream", |
| 140 | }, |
| 141 | // With progress, stream empty |
| 142 | { |
| 143 | Status: "status", |
| 144 | Stream: "", |
| 145 | Progress: &jsonstream.Progress{Current: 1}, |
| 146 | }: { |
| 147 | "", |
| 148 | fmt.Sprintf("%c[2K\rstatus 1B\r", 27), |
| 149 | }, |
| 150 | } |
| 151 | |
| 152 | // The tests :) |
| 153 | for jsonMessage, expectedMessages := range messages { |
| 154 | // Without terminal |
| 155 | data := bytes.NewBuffer([]byte{}) |
| 156 | if err := Display(jsonMessage, data, false, 0); err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | if data.String() != expectedMessages[0] { |
| 160 | t.Fatalf("Expected %q,got %q", expectedMessages[0], data.String()) |
| 161 | } |
| 162 | // With terminal |
| 163 | data = bytes.NewBuffer([]byte{}) |
| 164 | if err := Display(jsonMessage, data, true, 0); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | if data.String() != expectedMessages[1] { |
| 168 | t.Fatalf("\nExpected %q\n got %q", expectedMessages[1], data.String()) |
| 169 | } |
| 170 | } |
| 171 | } |