Skip to content

Commit

Permalink
🌱 Increase test coverage for finding outcomes (#3142)
Browse files Browse the repository at this point in the history
* Increase test coverage for finding outcomes

- Add tests for Outcome UnmarshalYAML function in `finding/finding_test.go`

Signed-off-by: naveensrinivasan <[email protected]>

* Updates based on Codereview

- Update `Outcome` variable in `finding/finding_test.go`
- Add `t.Parallel()` for test parallelization
- Add comparison using `cmp.Diff` to test for mismatches
- Update test cases for various outcomes

Signed-off-by: naveensrinivasan <[email protected]>

---------

Signed-off-by: naveensrinivasan <[email protected]>
  • Loading branch information
naveensrinivasan authored Jun 8, 2023
1 parent 2734a0c commit ab1c515
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions finding/finding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"gopkg.in/yaml.v3"

"github.com/ossf/scorecard/v4/finding/probe"
)
Expand Down Expand Up @@ -198,3 +199,95 @@ func Test_FromBytes(t *testing.T) {
})
}
}

func TestOutcome_UnmarshalYAML(t *testing.T) {
t.Parallel()
type args struct {
n *yaml.Node
}
tests := []struct { //nolint:govet
name string
wantOutcome Outcome
args args
wantErr bool
}{
{
name: "positive outcome",
wantOutcome: OutcomePositive,
args: args{
n: &yaml.Node{
Kind: yaml.ScalarNode,
Value: "Positive",
},
},
wantErr: false,
},
{
name: "negative outcome",
wantOutcome: OutcomeNegative,
args: args{
n: &yaml.Node{
Kind: yaml.ScalarNode,
Value: "Negative",
},
},
wantErr: false,
},
{
name: "NotAvailable outcome",
wantOutcome: OutcomeNotAvailable,
args: args{
n: &yaml.Node{
Kind: yaml.ScalarNode,
Value: "NotAvailable",
},
},
wantErr: false,
},
{
name: "NotSupported outcome",
wantOutcome: OutcomeNotSupported,
args: args{
n: &yaml.Node{
Kind: yaml.ScalarNode,
Value: "NotSupported",
},
},
wantErr: false,
},
{
name: "Unknown error",
wantOutcome: OutcomeError,
args: args{
n: &yaml.Node{
Kind: yaml.ScalarNode,
Value: "Error",
},
},
wantErr: false,
},
{
name: "Unknown outcome",
args: args{
n: &yaml.Node{
Kind: yaml.ScalarNode,
Value: "Unknown",
},
},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt // Re-initializing variable so it is not changed while executing the closure below
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var v Outcome
if err := v.UnmarshalYAML(tt.args.n); (err != nil) != tt.wantErr {
t.Errorf("Outcome.UnmarshalYAML() error = %v, wantErr %v", err, tt.wantErr)
}
if diff := cmp.Diff(tt.wantOutcome, v); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}

0 comments on commit ab1c515

Please sign in to comment.