Skip to content

Commit 9d434fa

Browse files
committed
parser/gotest: Implement active id tracking in output collector
1 parent d6bf223 commit 9d434fa

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

parser/gotest/internal/collector/collector.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ type line struct {
1414
}
1515

1616
// Output stores output lines grouped by id. Output can be retrieved for one or
17-
// more ids and output of different ids can be merged together, all while
18-
// preserving their original order based on the time it was collected.
17+
// more ids and output for different ids can be merged together, while
18+
// preserving their insertion original order based on the time it was
19+
// collected.
20+
// Output also tracks the active id, so you can append output without providing
21+
// an id.
1922
type Output struct {
20-
m map[int][]line
23+
m map[int][]line
24+
id int // active id
2125
}
2226

2327
// New returns a new output collector.
@@ -27,11 +31,17 @@ func New() *Output {
2731

2832
// Clear deletes all output for the given id.
2933
func (o *Output) Clear(id int) {
30-
o.m[id] = nil
34+
delete(o.m, id)
3135
}
3236

33-
// Append appends the given line of text to the output of the specified id.
34-
func (o *Output) Append(id int, text string) {
37+
// Append appends the given line of text to the output of the currently active
38+
// id.
39+
func (o *Output) Append(text string) {
40+
o.m[o.id] = append(o.m[o.id], line{time.Now(), text})
41+
}
42+
43+
// AppendToID appends the given line of text to the output of the given id.
44+
func (o *Output) AppendToID(id int, text string) {
3545
o.m[id] = append(o.m[id], line{time.Now(), text})
3646
}
3747

@@ -79,3 +89,9 @@ func (o *Output) Merge(fromID, intoID int) {
7989
o.m[intoID] = merged
8090
delete(o.m, fromID)
8191
}
92+
93+
// SetActiveID sets the active id. Text appended to this output will be
94+
// associated with the active id.
95+
func (o *Output) SetActiveID(id int) {
96+
o.id = id
97+
}

parser/gotest/internal/collector/collector_test.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
func TestClear(t *testing.T) {
1111
o := New()
12-
o.Append(1, "1")
13-
o.Append(2, "2")
12+
o.AppendToID(1, "1")
13+
o.AppendToID(2, "2")
1414
o.Clear(1)
1515

1616
want := []string(nil)
@@ -28,22 +28,22 @@ func TestClear(t *testing.T) {
2828

2929
func TestAppendAndGet(t *testing.T) {
3030
o := New()
31-
o.Append(1, "1.1")
32-
o.Append(1, "1.2")
33-
o.Append(2, "2")
34-
o.Append(1, "1.3")
31+
o.AppendToID(1, "1.1")
32+
o.AppendToID(1, "1.2")
33+
o.AppendToID(2, "2")
34+
o.AppendToID(1, "1.3")
3535

3636
want := []string{"1.1", "1.2", "1.3"}
3737
got := o.Get(1)
3838
if diff := cmp.Diff(want, got); diff != "" {
39-
t.Errorf("Append() incorrect (-want +got):\n%s", diff)
39+
t.Errorf("AppendToID() incorrect (-want +got):\n%s", diff)
4040
}
4141
}
4242

4343
func TestContains(t *testing.T) {
4444
o := New()
45-
o.Append(1, "1")
46-
o.Append(2, "2")
45+
o.AppendToID(1, "1")
46+
o.AppendToID(2, "2")
4747
o.Clear(1)
4848

4949
if !o.Contains(2) {
@@ -59,7 +59,7 @@ func TestContains(t *testing.T) {
5959
func TestGetAll(t *testing.T) {
6060
o := New()
6161
for i := 1; i <= 10; i++ {
62-
o.Append(i%3, strconv.Itoa(i))
62+
o.AppendToID(i%3, strconv.Itoa(i))
6363
}
6464

6565
want := []string{"1", "2", "4", "5", "7", "8", "10"}
@@ -72,7 +72,7 @@ func TestGetAll(t *testing.T) {
7272
func TestMerge(t *testing.T) {
7373
o := New()
7474
for i := 1; i <= 10; i++ {
75-
o.Append(i%3, strconv.Itoa(i))
75+
o.AppendToID(i%3, strconv.Itoa(i))
7676
}
7777

7878
o.Merge(2, 1)
@@ -89,3 +89,29 @@ func TestMerge(t *testing.T) {
8989
t.Errorf("Get(2) after Merge(2, 1) incorrect (-want +got):\n%s", diff)
9090
}
9191
}
92+
93+
func TestActiveID(t *testing.T) {
94+
o := New()
95+
96+
o.Append("0")
97+
o.SetActiveID(2)
98+
o.Append("2")
99+
o.SetActiveID(1)
100+
o.Append("1")
101+
o.SetActiveID(0)
102+
o.Append("0")
103+
104+
expected := [][]string{
105+
{"0", "0"},
106+
{"1"},
107+
{"2"},
108+
}
109+
for i := 0; i < 2; i++ {
110+
want := expected[i]
111+
got := o.Get(i)
112+
if diff := cmp.Diff(want, got); diff != "" {
113+
t.Errorf("Get(0) after SetActiveID incorrect (-want +got):\n%s", diff)
114+
}
115+
}
116+
117+
}

0 commit comments

Comments
 (0)