|
| 1 | +// Copyright 2022 Criticality Score Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package signalio |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "io" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/ossf/criticality_score/internal/collector/signal" |
| 23 | +) |
| 24 | + |
| 25 | +type testJSONWriterSet struct { //nolint:govet |
| 26 | + UpdatedCount signal.Field[int] |
| 27 | + Field string |
| 28 | +} |
| 29 | + |
| 30 | +func (t testJSONWriterSet) Namespace() signal.Namespace { |
| 31 | + return "test" |
| 32 | +} |
| 33 | + |
| 34 | +type mockWriterJSON struct{} |
| 35 | + |
| 36 | +func (m *mockWriterJSON) Write(p []byte) (n int, err error) { |
| 37 | + return 0, nil |
| 38 | +} |
| 39 | + |
| 40 | +func Test_jsonWriter_WriteSignals(t *testing.T) { |
| 41 | + type args struct { |
| 42 | + signals []signal.Set |
| 43 | + extra []Field |
| 44 | + } |
| 45 | + test := struct { |
| 46 | + name string |
| 47 | + encoder *json.Encoder |
| 48 | + args args |
| 49 | + wantErr bool |
| 50 | + }{ |
| 51 | + name: "default", |
| 52 | + encoder: json.NewEncoder(io.Writer(&mockWriterJSON{})), |
| 53 | + args: args{ |
| 54 | + signals: []signal.Set{ |
| 55 | + &testJSONWriterSet{}, |
| 56 | + }, |
| 57 | + extra: []Field{ |
| 58 | + { |
| 59 | + Key: "extra", |
| 60 | + Value: "value", |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + w := JSONWriter(&mockWriterJSON{}) |
| 67 | + if err := w.WriteSignals(test.args.signals, test.args.extra...); (err != nil) != test.wantErr { |
| 68 | + t.Errorf("WriteSignals() error = %v, wantErr %v", err, test.wantErr) |
| 69 | + } |
| 70 | +} |
0 commit comments