1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-19 21:28:32 +02:00
ferret/pkg/stdlib/testing/base/assertion.go
Tim Voronov 2643321e0f
Migration to lab (#526)
* Switched to Lab for e2e tests

* Switched to binary

* Updated lab installation

* Updated use of Lab installer

* updates

* Changed lab installation path

* Updated use of installer

* Works

* Added additional functions

* Updated some tests

* Updated go.sum

* Works

* Refactored assertions

* Added tests for testing.True

* Added tests for testing.None

* Added tests for testing.Lte

* Added tests for testing.Lt

* Added generic consturctor

* Added tests for testing.Len

* Added tests for testing.Gte

* Added tests for testing.Gt

* Added tests for testing.False

* Added tests for testing.Empty

* Added tests for testing.Fail

* Added tests for testing.Equal

* Added tests for testing.Include

* Updated urls in static page tests

* Fixed namespace unit tests

* Fixed unit test for testing.Len

* Updated E2E scripts

* Updaes

* Updated Chrome in CI/CD

* Added e2e for example test click.fql

* Added suite cases for example scripts

* Updated examples

* Updated

* Added type assertions

* Updated Chrome opts and disabled headers and cookies related tests

* Fixed iframes example

* Increased timeouts in navigation examples

* Updated value example

* Updated comments

* Disabled cookies examples

* Fixed static url

* Disabled headers examples

* Disabled UA test

* Simplified wait logic

* Added base testing module

* Fixes after codereview

* Disabled failing tests
2020-06-17 17:37:01 -04:00

86 lines
1.9 KiB
Go

package base
import (
"context"
"fmt"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type AssertionFn func(ctx context.Context, args []core.Value) (bool, error)
type MessageFn func(args []core.Value) string
type Assertion struct {
DefaultMessage MessageFn
MinArgs int
MaxArgs int
Fn AssertionFn
}
func NewPositiveAssertion(assertion Assertion) core.Function {
return newInternal(assertion, true)
}
func NewNegativeAssertion(assertion Assertion) core.Function {
return newInternal(assertion, false)
}
func newInternal(assertion Assertion, connotation bool) core.Function {
return func(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, assertion.MinArgs, assertion.MaxArgs)
if err != nil {
return values.None, err
}
res, err := assertion.Fn(ctx, args)
if err != nil {
return values.None, err
}
if res == connotation {
return values.None, nil
}
return values.None, toError(assertion, args, connotation)
}
}
func toError(assertion Assertion, args []core.Value, positive bool) error {
if len(args) != assertion.MaxArgs {
connotation := ""
if !positive {
connotation = "not "
}
if assertion.MaxArgs > 1 {
actual := args[0]
var msg string
if assertion.DefaultMessage != nil {
msg = assertion.DefaultMessage(args)
} else {
if len(args) > 1 {
msg = fmt.Sprintf("be %s", args[1].String())
} else {
msg = "exist"
}
}
return core.Error(ErrAssertion, fmt.Sprintf("expected %s %sto %s", FormatValue(actual), connotation, msg))
}
return core.Error(ErrAssertion, fmt.Sprintf("expected to %s%s", connotation, assertion.DefaultMessage(args)))
}
// Last argument is always is a custom message
msg := args[assertion.MaxArgs-1]
return core.Error(ErrAssertion, msg.String())
}