Skip to content

[FEATURE] Each with multiple args #30

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

Open
raphaelauv opened this issue Jul 5, 2022 · 2 comments
Open

[FEATURE] Each with multiple args #30

raphaelauv opened this issue Jul 5, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@raphaelauv
Copy link

would you accept tests helpers on this repo ?

like parametrize ?

func parametrize[V any, T any](fn T, allValues [][]V) {
    v := reflect.ValueOf(fn)
    for _, a := range allValues {
        vargs := make([]reflect.Value, len(a))

        for i, b := range a {
            vargs[i] = reflect.ValueOf(b)
        }
        v.Call(vargs)
    }
}

func tutu(a int) int {
    return a + 1
}

func Test_tutu(t *testing.T) {
    testsArgs := [][]any{
        {t, 1, 2}, {t, 3, 4},
    }
    test := func(t *testing.T, input int, expected int) {
        assert.Equal(t, tutu(input), expected)
    }
    parametrize(test, testsArgs)
}
@rjNemo
Copy link
Owner

rjNemo commented Jul 6, 2022

Hi.

Yes, it looks like a good idea.
Feel free to prepare a pull request

@rjNemo rjNemo added the enhancement New feature or request label Jul 6, 2022
@raphaelauv
Copy link
Author

raphaelauv commented Jul 6, 2022

parametrize is just a specific Each

that accepts a list of multiple parameters instead of a list of single parameter

so it could be

func EachMultiple[T any, V any](allValues [][]T, action V) {
	v := reflect.ValueOf(action)
	for _, a := range allValues {
		vargs := make([]reflect.Value, len(a))

		for i, b := range a {
			vargs[i] = reflect.ValueOf(b)
		}
		v.Call(vargs)
	}
}

I have no idea for a good name

But maybe the best would be to introduce a partial struct like in python

from functools import partial

def f(a, b, c, x):
  return 1000*a + 100*b + 10*c + x


g = partial(f, 3, 1, 4)

print(g(5))  # give 3145

@raphaelauv raphaelauv changed the title [FEATURE] tests helper [FEATURE] Each with multiple args Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants