Skip to content

Commit

Permalink
Included tests for internal/signalio/text
Browse files Browse the repository at this point in the history
- Included test for `internal/signalio/text`

Signed-off-by: nathannaveen <[email protected]>
  • Loading branch information
nathannaveen committed Jan 10, 2023
1 parent 27ad7d2 commit c8b6513
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions internal/signalio/text_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package signalio

import (
"io"
"sync"
"testing"

"github.com/ossf/criticality_score/internal/collector/signal"
)

func Test_textWriter_writeRecord(t *testing.T) {
type fields struct {
w io.Writer
fields []string
firstRecordWritten bool
}
tests := []struct { //nolint:govet
name string
fields fields
values map[string]string
wantErr bool
}{
{
name: "regular",
fields: fields{
w: io.Writer(&mockWriter{
written: []byte{},
}),
fields: []string{"test"},
firstRecordWritten: false,
},
values: map[string]string{
"test": "test",
},
},
{
name: "first record written and w is invalid",
fields: fields{
w: &mockWriter{},
fields: []string{"test"},
firstRecordWritten: true,
},
values: map[string]string{},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
w := &textWriter{
w: test.fields.w,
fields: test.fields.fields,
firstRecordWritten: test.fields.firstRecordWritten,
mu: sync.Mutex{},
}

if err := w.writeRecord(test.values); (err != nil) != test.wantErr {
t.Errorf("writeRecord() error = %v, wantErr %v", err, test.wantErr)
}
})
}
}

type mockWriter struct {
written []byte
}

func (m *mockWriter) Write(p []byte) (n int, err error) {
return 0, nil
}

func Test_textWriter_WriteSignals(t *testing.T) {
type fields struct {
w io.Writer
fields []string
firstRecordWritten bool
}
type args struct {
signals []signal.Set
extra []Field
}
tests := []struct { //nolint:govet
name string
fields fields
args args
wantErr bool
}{
{
name: "regular",
fields: fields{
w: io.Writer(&mockWriter{
written: []byte{},
}),
fields: []string{"test"},
firstRecordWritten: true,
},
args: args{
signals: []signal.Set{},
extra: []Field{},
},
},
{
name: "error while marshaling with extra",
args: args{
signals: []signal.Set{},
extra: []Field{{"test", []string{"invalid"}}},
},
wantErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
w := &textWriter{
w: test.fields.w,
fields: test.fields.fields,
firstRecordWritten: test.fields.firstRecordWritten,
mu: sync.Mutex{},
}
if err := w.WriteSignals(test.args.signals, test.args.extra...); (err != nil) != test.wantErr {
t.Errorf("WriteSignals() error = %v, wantErr %v", err, test.wantErr)
}
})
}
}

0 comments on commit c8b6513

Please sign in to comment.