-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Included tests for internal/signalio/json.go #306
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2022 Criticality Score Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package signalio | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"testing" | ||
|
||
"github.com/ossf/criticality_score/internal/collector/signal" | ||
) | ||
|
||
type testJSONWriterSet struct { //nolint:govet | ||
UpdatedCount signal.Field[int] | ||
Field string | ||
} | ||
|
||
func (t testJSONWriterSet) Namespace() signal.Namespace { | ||
return "test" | ||
} | ||
|
||
type mockWriterJSON struct{} | ||
|
||
func (m *mockWriterJSON) Write(p []byte) (n int, err error) { | ||
return 0, nil | ||
} | ||
Comment on lines
+34
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a |
||
|
||
func Test_jsonWriter_WriteSignals(t *testing.T) { | ||
type args struct { | ||
signals []signal.Set | ||
extra []Field | ||
} | ||
test := struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May I suggest restructuring these tests into the following the test functions as it isn't really clear what this function is trying to test:
These should ideally not contain the test struct building at the start as it isn't really necessary. |
||
name string | ||
encoder *json.Encoder | ||
args args | ||
wantErr bool | ||
}{ | ||
name: "default", | ||
encoder: json.NewEncoder(io.Writer(&mockWriterJSON{})), | ||
args: args{ | ||
signals: []signal.Set{ | ||
&testJSONWriterSet{}, | ||
}, | ||
extra: []Field{ | ||
{ | ||
Key: "extra", | ||
Value: "value", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
w := JSONWriter(&mockWriterJSON{}) | ||
if err := w.WriteSignals(test.args.signals, test.args.extra...); (err != nil) != test.wantErr { | ||
t.Errorf("WriteSignals() error = %v, wantErr %v", err, test.wantErr) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth extending the comment to say that "if this fails in the future the assignment to nsData below will fail"