Skip to content

Commit

Permalink
Remove some duplication in test code
Browse files Browse the repository at this point in the history
  • Loading branch information
geomacy committed Sep 3, 2019
1 parent 111dda9 commit 3101f40
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions cli/commands/catalog-show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,26 @@ func testInApp(t *testing.T, fn func(c *cli.Context) error, args ...string) stri
return out
}

func unmarshalToCatalogEntitySummary(text string) (*models.CatalogEntitySummary, error) {
var summary models.CatalogEntitySummary
actualBytes := []byte(text)
err := json.Unmarshal(actualBytes, &summary)
if err != nil {
return nil, fmt.Errorf("could not unmarshal input to CatalogEntitySummary: %s", err)
}
return &summary, nil
}

func TestCatalogItemSummaryDisplay(t *testing.T) {
expected := testCatalogEntitySummary()
displayOutput := testInApp(t, func(c *cli.Context) error {
return expected.Display(c)
}, "--raw-output", "--json", "$")
var actual models.CatalogItemSummary
actualBytes := []byte(displayOutput)
unmarshalErr := json.Unmarshal(actualBytes, &actual)
assert.Nil(t, unmarshalErr, "result is not JSON: %s", actualBytes)

assertItemSummaryFields(t, expected.CatalogItemSummary, actual)
actual, err := unmarshalToCatalogEntitySummary(displayOutput)
assert.Nil(t, err, "result may be bad JSON: %s", displayOutput)

assertItemSummaryFields(t, expected.CatalogItemSummary, actual.CatalogItemSummary)
}

func TestCatalogEntitySummaryDisplay(t *testing.T) {
Expand All @@ -112,10 +121,8 @@ func TestCatalogEntitySummaryDisplay(t *testing.T) {
return expected.Display(c)
}, "--raw-output", "--json", "$")

var actual models.CatalogEntitySummary
actualBytes := []byte(displayOutput)
unmarshalErr := json.Unmarshal(actualBytes, &actual)
assert.Nil(t, unmarshalErr, "result is not JSON: %s", actualBytes)
actual, err := unmarshalToCatalogEntitySummary(displayOutput)
assert.Nil(t, err, "result may be bad JSON: %s", displayOutput)

assertItemSummaryFields(t, expected.CatalogItemSummary, actual.CatalogItemSummary)
assert.Equal(t, expected.IconUrl, actual.IconUrl, "iconUrl")
Expand Down

0 comments on commit 3101f40

Please sign in to comment.