Skip to content

Commit 3c8da0a

Browse files
suwakeindeloof
authored andcommitted
Add test of json.go
Signed-off-by: keitosuwahara <[email protected]>
1 parent 1b12c86 commit 3c8da0a

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

pkg/progress/json_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Copyright 2024 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package progress
18+
19+
import (
20+
"bytes"
21+
"context"
22+
"encoding/json"
23+
"testing"
24+
25+
"gotest.tools/v3/assert"
26+
)
27+
28+
func TestJsonWriter_Event(t *testing.T) {
29+
var out bytes.Buffer
30+
w := &jsonWriter{
31+
out: &out,
32+
done: make(chan bool),
33+
dryRun: true,
34+
}
35+
36+
event := Event{
37+
ID: "service1",
38+
ParentID: "project",
39+
Text: "Creating",
40+
StatusText: "Working",
41+
Current: 50,
42+
Total: 100,
43+
Percent: 50,
44+
}
45+
w.Event(event)
46+
47+
var msg jsonMessage
48+
err := json.Unmarshal(out.Bytes(), &msg)
49+
assert.NilError(t, err)
50+
51+
assert.Equal(t, true, msg.DryRun)
52+
assert.Equal(t, false, msg.Tail)
53+
assert.Equal(t, "service1", msg.ID)
54+
assert.Equal(t, "project", msg.ParentID)
55+
assert.Equal(t, "Creating", msg.Text)
56+
assert.Equal(t, "Working", msg.Status)
57+
assert.Equal(t, int64(50), msg.Current)
58+
assert.Equal(t, int64(100), msg.Total)
59+
assert.Equal(t, 50, msg.Percent)
60+
}
61+
62+
func TestJsonWriter_TailMsgf(t *testing.T) {
63+
var out bytes.Buffer
64+
w := &jsonWriter{
65+
out: &out,
66+
done: make(chan bool),
67+
dryRun: false,
68+
}
69+
70+
go func() {
71+
_ = w.Start(context.Background())
72+
}()
73+
74+
w.TailMsgf("hello %s", "world")
75+
76+
w.Stop()
77+
78+
var msg jsonMessage
79+
err := json.Unmarshal(out.Bytes(), &msg)
80+
assert.NilError(t, err)
81+
82+
assert.Equal(t, false, msg.DryRun)
83+
assert.Equal(t, true, msg.Tail)
84+
assert.Equal(t, "hello world", msg.Text)
85+
assert.Equal(t, "", msg.ID)
86+
assert.Equal(t, "", msg.Status)
87+
}

0 commit comments

Comments
 (0)