Skip to content
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

Refactor formatValue #202

Merged
merged 13 commits into from
Dec 29, 2022
156 changes: 156 additions & 0 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,159 @@ func TestFormatDiff(t *testing.T) {
checkOK(map[string]interface{}{"a": 1}, map[string]interface{}{})
checkOK([]interface{}{"a"}, []interface{}{})
}

func TestFormatFailure_Actual(t *testing.T) {
gavv marked this conversation as resolved.
Show resolved Hide resolved
df := &DefaultFormatter{}
ctx := &AssertionContext{}

t.Run("assert type integer", func(t *testing.T) {
fl := &AssertionFailure{
Type: AssertType,
Actual: &AssertionValue{
Value: int(1_000_000),
},
}
tmpl := df.FormatFailure(ctx, fl)
assert.Contains(t, tmpl, "int(1000000)")
})

t.Run("assert type float32", func(t *testing.T) {
fl := &AssertionFailure{
Type: AssertType,
Actual: &AssertionValue{
Value: float32(1_000_000),
},
}
tmpl := df.FormatFailure(ctx, fl)
assert.Contains(t, tmpl, "float32(1e+06)")
})

t.Run("assert type float64", func(t *testing.T) {
fl := &AssertionFailure{
Type: AssertType,
Actual: &AssertionValue{
Value: float64(1_000_000),
},
}
tmpl := df.FormatFailure(ctx, fl)
assert.Contains(t, tmpl, "float64(1e+06)")
})

t.Run("assert type string", func(t *testing.T) {
fl := &AssertionFailure{
Type: AssertType,
Actual: &AssertionValue{
Value: "value string",
},
}
tmpl := df.FormatFailure(ctx, fl)
assert.Contains(t, tmpl, `string("value string")`)
})
}

func TestFormatFailure_Expected(t *testing.T) {
df := &DefaultFormatter{}
ctx := &AssertionContext{}

t.Run("assert in range integer", func(t *testing.T) {
fl := &AssertionFailure{
Type: AssertInRange,
Expected: &AssertionValue{
AssertionRange{
Min: int(1_000_000),
Max: int(2_000_000),
},
},
}
tmpl := df.FormatFailure(ctx, fl)
assert.Contains(t, tmpl, "[1000000; 2000000]")
})

t.Run("assert in range float32", func(t *testing.T) {
fl := &AssertionFailure{
Type: AssertInRange,
Expected: &AssertionValue{
AssertionRange{
Min: float32(1_000_000),
Max: float32(2_000_000),
},
},
}
tmpl := df.FormatFailure(ctx, fl)
assert.Contains(t, tmpl, "[1e+06; 2e+06]")
})

t.Run("assert in range float64", func(t *testing.T) {
fl := &AssertionFailure{
Type: AssertInRange,
Expected: &AssertionValue{
AssertionRange{
Min: float64(1_000_000),
Max: float64(2_000_000),
},
},
}
tmpl := df.FormatFailure(ctx, fl)
assert.Contains(t, tmpl, "[1e+06; 2e+06]")
})

t.Run("assert in range string", func(t *testing.T) {
fl := &AssertionFailure{
Type: AssertInRange,
Expected: &AssertionValue{
AssertionRange{
Min: "min string",
Max: "max string",
},
},
}
tmpl := df.FormatFailure(ctx, fl)
assert.Contains(t, tmpl, "min string")
assert.Contains(t, tmpl, "max string")
})
}

func TestFormatFailure_Delta(t *testing.T) {
df := &DefaultFormatter{}
ctx := &AssertionContext{}

t.Run("integer", func(t *testing.T) {
fl := &AssertionFailure{
Delta: &AssertionValue{
Value: int(1_000_000),
},
}
tmpl := df.FormatFailure(ctx, fl)
assert.Contains(t, tmpl, "1000000")
})

t.Run("float32", func(t *testing.T) {
fl := &AssertionFailure{
Delta: &AssertionValue{
Value: float32(1_000_000),
},
}
tmpl := df.FormatFailure(ctx, fl)
assert.Contains(t, tmpl, "1000000.000000")
})

t.Run("float64", func(t *testing.T) {
fl := &AssertionFailure{
Delta: &AssertionValue{
Value: float64(1_000_000),
},
}
tmpl := df.FormatFailure(ctx, fl)
assert.Contains(t, tmpl, "1000000.000000")
})

t.Run("string", func(t *testing.T) {
fl := &AssertionFailure{
Delta: &AssertionValue{
Value: "delta string",
},
}
tmpl := df.FormatFailure(ctx, fl)
assert.Contains(t, tmpl, "delta string")
})
}