Skip to content

Commit 052a96c

Browse files
don't truncate numbers for int assertions
1 parent f32c99e commit 052a96c

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

checks/local.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,11 @@ func jsonValOp(test api.HTTPRequestTestJSONValue, jsn string, variables map[stri
248248
case intOk:
249249
v = vInt
250250
case floatOk:
251-
v = int(vFloat)
251+
var ok bool
252+
v, ok = coerceInt(vFloat)
253+
if !ok {
254+
return errors.New("expected int value")
255+
}
252256
default:
253257
return errors.New("expected int value")
254258
}

checks/local_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,26 @@ func TestEvaluateStdoutJqResultTypes(t *testing.T) {
313313
})
314314
}
315315
}
316+
317+
func TestHTTPIntegerAssertions(t *testing.T) {
318+
expected := 3
319+
for _, tt := range []struct {
320+
body string
321+
operator api.OperatorType
322+
pass bool
323+
}{
324+
{`3`, api.OpEquals, true},
325+
{`3.0`, api.OpEquals, true},
326+
{`3.9`, api.OpEquals, false},
327+
{`4`, api.OpGreaterThan, true},
328+
{`4.1`, api.OpGreaterThan, false},
329+
{`"3"`, api.OpEquals, false},
330+
{`1e20`, api.OpGreaterThan, false},
331+
} {
332+
assertion := api.HTTPRequestTestJSONValue{Path: ".", Operator: tt.operator, IntValue: &expected}
333+
err := jsonValOp(assertion, tt.body, nil)
334+
if (err == nil) != tt.pass {
335+
t.Errorf("%s %s %d: error = %v, want pass = %t", tt.body, tt.operator, expected, err, tt.pass)
336+
}
337+
}
338+
}

0 commit comments

Comments
 (0)