Skip to content

Use most appropriate test assertion types #151

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

Merged
merged 1 commit into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/result/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,12 @@ func TestWriteReport(t *testing.T) {
require.Nil(t, err)

flags.Set("report-file", reportFilePath.Join("report-file.json").String())
assert.Nil(t, configuration.Initialize(flags, projectPaths))
require.Nil(t, configuration.Initialize(flags, projectPaths))
assert.Error(t, Results.WriteReport(), "Parent folder creation should fail due to a collision with an existing file at that path")

reportFilePath = reportFolderPath.Join("report-file-subfolder", "report-file-subsubfolder", "report-file.json")
flags.Set("report-file", reportFilePath.String())
assert.Nil(t, configuration.Initialize(flags, projectPaths))
require.Nil(t, configuration.Initialize(flags, projectPaths))
assert.NoError(t, Results.WriteReport(), "Creation of multiple levels of parent folders")

reportFile, err := reportFilePath.Open()
Expand Down
93 changes: 47 additions & 46 deletions internal/rule/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/arduino/arduino-lint/internal/rule/schema/testdata"
"github.com/arduino/go-properties-orderedmap"
"github.com/ory/jsonschema/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -50,27 +51,27 @@ func init() {
}

func TestCompile(t *testing.T) {
require.Panics(t, func() {
assert.Panics(t, func() {
Compile("valid-schema-with-references.json", []string{"nonexistent.json"}, testdata.Asset)
})

require.Panics(t, func() {
assert.Panics(t, func() {
Compile("valid-schema-with-references.json", []string{"schema-without-id.json"}, testdata.Asset)
})

require.Panics(t, func() {
assert.Panics(t, func() {
Compile("invalid-schema.json", []string{}, testdata.Asset)
})

require.Panics(t, func() {
assert.Panics(t, func() {
Compile("valid-schema-with-references.json", []string{}, testdata.Asset)
})

require.NotPanics(t, func() {
assert.NotPanics(t, func() {
Compile("valid-schema.json", []string{}, testdata.Asset)
})

require.NotPanics(t, func() {
assert.NotPanics(t, func() {
Compile(
"valid-schema-with-references.json",
[]string{
Expand All @@ -86,134 +87,134 @@ func TestValidate(t *testing.T) {
schemaObject := Compile("valid-schema.json", []string{}, testdata.Asset)
propertiesMap := properties.NewFromHashmap(validMap)
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), schemaObject)
require.Nil(t, validationResult.Result)
assert.Nil(t, validationResult.Result)

validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.Nil(t, validationResult.Result)
assert.Nil(t, validationResult.Result)

propertiesMap.Set("property1", "a")
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), schemaObject)
require.Equal(t, "#/property1", validationResult.Result.InstancePtr)
require.Equal(t, "#/properties/property1/minLength", validationResult.Result.SchemaPtr)
assert.Equal(t, "#/property1", validationResult.Result.InstancePtr)
assert.Equal(t, "#/properties/property1/minLength", validationResult.Result.SchemaPtr)
}

func TestRequiredPropertyMissing(t *testing.T) {
propertiesMap := properties.NewFromHashmap(validMap)
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.False(t, RequiredPropertyMissing("property1", validationResult))
assert.False(t, RequiredPropertyMissing("property1", validationResult))

propertiesMap.Remove("property1")
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.True(t, RequiredPropertyMissing("property1", validationResult))
assert.True(t, RequiredPropertyMissing("property1", validationResult))
}

func TestPropertyPatternMismatch(t *testing.T) {
propertiesMap := properties.NewFromHashmap(validMap)
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.False(t, PropertyPatternMismatch("property2", validationResult))
assert.False(t, PropertyPatternMismatch("property2", validationResult))

propertiesMap.Set("property2", "fOo")
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.True(t, PropertyPatternMismatch("property2", validationResult))
assert.True(t, PropertyPatternMismatch("property2", validationResult))

require.False(t, PropertyPatternMismatch("property1", validationResult))
assert.False(t, PropertyPatternMismatch("property1", validationResult))
}

func TestPropertyLessThanMinLength(t *testing.T) {
propertiesMap := properties.NewFromHashmap(validMap)
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.False(t, PropertyLessThanMinLength("property1", validationResult))
assert.False(t, PropertyLessThanMinLength("property1", validationResult))

propertiesMap.Set("property1", "a")
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.True(t, PropertyLessThanMinLength("property1", validationResult))
assert.True(t, PropertyLessThanMinLength("property1", validationResult))
}

func TestPropertyGreaterThanMaxLength(t *testing.T) {
propertiesMap := properties.NewFromHashmap(validMap)
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.False(t, PropertyGreaterThanMaxLength("property1", validationResult))
assert.False(t, PropertyGreaterThanMaxLength("property1", validationResult))

propertiesMap.Set("property1", "12345")
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.True(t, PropertyGreaterThanMaxLength("property1", validationResult))
assert.True(t, PropertyGreaterThanMaxLength("property1", validationResult))
}

func TestPropertyEnumMismatch(t *testing.T) {
propertiesMap := properties.NewFromHashmap(validMap)
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.False(t, PropertyEnumMismatch("property3", validationResult))
assert.False(t, PropertyEnumMismatch("property3", validationResult))

propertiesMap.Set("property3", "invalid")
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.True(t, PropertyEnumMismatch("property3", validationResult))
assert.True(t, PropertyEnumMismatch("property3", validationResult))
}

func TestPropertyDependenciesMissing(t *testing.T) {
propertiesMap := properties.NewFromHashmap(validMap)
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.False(t, PropertyDependenciesMissing("dependentProperty", validationResult))
assert.False(t, PropertyDependenciesMissing("dependentProperty", validationResult))

propertiesMap.Remove("dependencyProperty")
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.True(t, PropertyDependenciesMissing("dependentProperty", validationResult))
assert.True(t, PropertyDependenciesMissing("dependentProperty", validationResult))
}

func TestMisspelledOptionalPropertyFound(t *testing.T) {
propertiesMap := properties.NewFromHashmap(validMap)
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.False(t, MisspelledOptionalPropertyFound(validationResult))
assert.False(t, MisspelledOptionalPropertyFound(validationResult))

propertiesMap.Set("porperties", "foo")
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.True(t, MisspelledOptionalPropertyFound(validationResult))
assert.True(t, MisspelledOptionalPropertyFound(validationResult))
}

func TestValidationErrorMatch(t *testing.T) {
propertiesMap := properties.NewFromHashmap(validMap)
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.False(t, ValidationErrorMatch("", "", "", "", validationResult))
assert.False(t, ValidationErrorMatch("", "", "", "", validationResult))

propertiesMap.Set("property2", "fOo")
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult))
require.False(t, ValidationErrorMatch("^#/property2$", "nomatch", "nomatch", "nomatch", validationResult))
require.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", "nomatch", "nomatch", validationResult))
require.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^\^\[a-z\]\+\$$`, "nomatch", validationResult))
require.True(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^"\^\[a-z\]\+\$"$`, "", validationResult))
require.True(t, ValidationErrorMatch("", "", "", "", validationResult))
assert.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult))
assert.False(t, ValidationErrorMatch("^#/property2$", "nomatch", "nomatch", "nomatch", validationResult))
assert.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", "nomatch", "nomatch", validationResult))
assert.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^\^\[a-z\]\+\$$`, "nomatch", validationResult))
assert.True(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^"\^\[a-z\]\+\$"$`, "", validationResult))
assert.True(t, ValidationErrorMatch("", "", "", "", validationResult))

propertiesMap.Set("property3", "bAz")
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.True(t, ValidationErrorMatch("^#/property3$", "/pattern$", "", "", validationResult), "Match pointer below logic inversion keyword")
assert.True(t, ValidationErrorMatch("^#/property3$", "/pattern$", "", "", validationResult), "Match pointer below logic inversion keyword")

propertiesMap = properties.NewFromHashmap(validMap)
propertiesMap.Remove("property1")
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
require.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult))
require.True(t, ValidationErrorMatch("", "", "", "^#/property1$", validationResult))
assert.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult))
assert.True(t, ValidationErrorMatch("", "", "", "^#/property1$", validationResult))
}

func Test_loadReferencedSchema(t *testing.T) {
compiler := jsonschema.NewCompiler()

require.Panics(
assert.Panics(
t,
func() {
loadReferencedSchema(compiler, "nonexistent.json", testdata.Asset)
},
)
require.Error(t, loadReferencedSchema(compiler, "schema-without-id.json", testdata.Asset))
require.Nil(t, loadReferencedSchema(compiler, "referenced-schema-2.json", testdata.Asset))
assert.Error(t, loadReferencedSchema(compiler, "schema-without-id.json", testdata.Asset))
assert.Nil(t, loadReferencedSchema(compiler, "referenced-schema-2.json", testdata.Asset))
}

func Test_schemaID(t *testing.T) {
_, err := schemaID("schema-without-id.json", testdata.Asset)
require.NotNil(t, err)
assert.NotNil(t, err)

id, err := schemaID("valid-schema.json", testdata.Asset)
require.Equal(t, "https://raw.githubusercontent.com/arduino/arduino-lint/main/internal/rule/schema/testdata/input/valid-schema.json", id)
require.Nil(t, err)
assert.Equal(t, "https://raw.githubusercontent.com/arduino/arduino-lint/main/internal/rule/schema/testdata/input/valid-schema.json", id)
}

func Test_validationErrorSchemaPointerValue(t *testing.T) {
Expand All @@ -227,17 +228,17 @@ func Test_validationErrorSchemaPointerValue(t *testing.T) {

schemaPointerValueInterface := validationErrorSchemaPointerValue(validationError)
schemaPointerValue, ok := schemaPointerValueInterface.(string)
require.True(t, ok)
require.Equal(t, "^[a-z]+$", schemaPointerValue)
assert.True(t, ok)
assert.Equal(t, "^[a-z]+$", schemaPointerValue)
}

func Test_validationErrorContextMatch(t *testing.T) {
validationError := jsonschema.ValidationError{
Context: nil,
}

require.True(t, validationErrorContextMatch(regexp.MustCompile(".*"), &validationError))
require.False(t, validationErrorContextMatch(regexp.MustCompile("foo"), &validationError))
assert.True(t, validationErrorContextMatch(regexp.MustCompile(".*"), &validationError))
assert.False(t, validationErrorContextMatch(regexp.MustCompile("foo"), &validationError))

validationError.Context = &jsonschema.ValidationErrorContextRequired{
Missing: []string{
Expand All @@ -246,6 +247,6 @@ func Test_validationErrorContextMatch(t *testing.T) {
},
}

require.True(t, validationErrorContextMatch(regexp.MustCompile("^#/bar$"), &validationError))
require.False(t, validationErrorContextMatch(regexp.MustCompile("nomatch"), &validationError))
assert.True(t, validationErrorContextMatch(regexp.MustCompile("^#/bar$"), &validationError))
assert.False(t, validationErrorContextMatch(regexp.MustCompile("nomatch"), &validationError))
}