Skip to content

Commit

Permalink
Fix matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Apr 19, 2023
1 parent 5ee6a45 commit a4acb79
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions runtime/stdlib/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,9 @@ var testExpectFunction = interpreter.NewUnmeteredHostFunctionValue(
)

if !result {
panic(AssertionError{})
panic(AssertionError{
LocationRange: locationRange,
})
}

return interpreter.Void
Expand Down Expand Up @@ -1456,7 +1458,7 @@ var beEmptyMatcherFunction = interpreter.NewUnmeteredHostFunctionValue(
},
)

return newMatcherWithGenericTestFunction(invocation, beEmptyTestFunc)
return newMatcherWithAnyStructTestFunction(invocation, beEmptyTestFunc)
},
)

Expand Down Expand Up @@ -1510,7 +1512,7 @@ var haveElementCountMatcherFunction = interpreter.NewUnmeteredHostFunctionValue(
},
)

return newMatcherWithGenericTestFunction(invocation, haveElementCountTestFunc)
return newMatcherWithAnyStructTestFunction(invocation, haveElementCountTestFunc)
},
)

Expand Down Expand Up @@ -1575,7 +1577,7 @@ var containMatcherFunction = interpreter.NewUnmeteredHostFunctionValue(
},
)

return newMatcherWithGenericTestFunction(invocation, containTestFunc)
return newMatcherWithAnyStructTestFunction(invocation, containTestFunc)
},
)

Expand Down Expand Up @@ -1632,7 +1634,7 @@ var beGreaterThanMatcherFunction = interpreter.NewUnmeteredHostFunctionValue(
},
)

return newMatcherWithGenericTestFunction(invocation, beGreaterThanTestFunc)
return newMatcherWithAnyStructTestFunction(invocation, beGreaterThanTestFunc)
},
)

Expand Down Expand Up @@ -1689,7 +1691,7 @@ var beLessThanMatcherFunction = interpreter.NewUnmeteredHostFunctionValue(
},
)

return newMatcherWithGenericTestFunction(invocation, beLessThanTestFunc)
return newMatcherWithAnyStructTestFunction(invocation, beLessThanTestFunc)
},
)

Expand Down Expand Up @@ -1828,12 +1830,38 @@ func (e TestFailedError) Error() string {
return fmt.Sprintf("test failed: %s", e.Err.Error())
}

func newMatcherWithGenericTestFunction(
// Creates a matcher using a function that accepts an `AnyStruct` typed parameter.
// i.e: invokes `newMatcher(fun (value: AnyStruct): Bool)`.
func newMatcherWithAnyStructTestFunction(
invocation interpreter.Invocation,
testFunc interpreter.FunctionValue,
) interpreter.Value {

inter := invocation.Interpreter
matcherConstructor := getNestedTypeConstructorValue(
*invocation.Self,
matcherTypeName,
)
matcher, err := invocation.Interpreter.InvokeExternally(
matcherConstructor,
matcherConstructor.Type,
[]interpreter.Value{
testFunc,
},
)

if err != nil {
panic(err)
}

return matcher
}

// Creates a matcher using a function that accepts a generic `T` typed parameter.
// NOTE: Use this function only if the matcher function has a generic type.
func newMatcherWithGenericTestFunction(
invocation interpreter.Invocation,
testFunc interpreter.FunctionValue,
) interpreter.Value {

typeParameterPair := invocation.TypeParameterTypes.Oldest()
if typeParameterPair == nil {
Expand Down Expand Up @@ -1882,23 +1910,7 @@ func newMatcherWithGenericTestFunction(
},
)

matcherConstructor := getNestedTypeConstructorValue(
*invocation.Self,
matcherTypeName,
)
matcher, err := inter.InvokeExternally(
matcherConstructor,
matcherConstructor.Type,
[]interpreter.Value{
matcherTestFunction,
},
)

if err != nil {
panic(err)
}

return matcher
return newMatcherWithAnyStructTestFunction(invocation, matcherTestFunction)
}

func TestCheckerContractValueHandler(
Expand Down

0 comments on commit a4acb79

Please sign in to comment.