Gut icon indicating copy to clipboard operation
Gut copied to clipboard

Unclear Parameterized Tests example

Open abreathingcorpse opened this issue 4 years ago • 3 comments

While going throughout the Parameterized Tests docs it's uncertain how the following test_foo function works:

extends 'res://addons/gut/test.gd'

var foo_params = [[1, 2, 3], ['a', 'b', 'c']]

func test_foo(params=use_parameters(foo_params)):
  var foo = Foo.new()
  var result = foo.do_something(params[0], params[1])
  assert_eq(result, params[2])

As well as the do_something method within the Foo class. Attempted to recreate this on my end to see if I could get a better understanding of things.

My first attempt was:

# class located at: 'res//scripts/foo.gd'

func do_something(param):
	return param

Which returned the following error: Invalid call to function 'do_something' in base 'Node (foo.gd).' Expected 1 arguments. This makes, sense since I'm passing the arguments params[0] & params[1] to the function that accepts a single argument.

My second attempt was:

# class located at: 'res//scripts/foo.gd'

func do_something(param0, param1):
	return param0

This time it runs but, my tests are failing: (call #1) with paramters: [1, 2, 3] [Failed]: [1] expected to equal [3]

I understand that result is basically foo_params[i][0], on this case but, how can I make the assert_eq with params[2].

My third attempt was:

# class located at: 'res//scripts/foo.gd'

func do_something(param0, param1):
	return param0[2]

Which returned the following error: Invalid get index '2' (on base: 'int'). which makes total sense since GDScript does not know that param0 should be an array. Tried googling how to do it and could not find a way to do it.

My fourth & final attempt was:

# class located at: 'res//scripts/foo.gd'

func do_something(param0, param1, param2):
	return param2

Which returned the following error: Invalid call to function 'do_something' in base 'Node (foo.gd).' Expected 1 arguments. Which is basically the first error. Now I went full circle and is why I'm asking for a clarification here.

Could you please elaborate on the example of Parameterized Tests docs? I'm really not sure how to implement one, given my current knowledge of Gut & Godot.

abreathingcorpse avatar Jul 19 '21 05:07 abreathingcorpse

The example is trying to illustrate how you would include values to be used and an expected result. Indexes 0 and 1 are meant to be used within the test to "do something". Index 2 is the expected result.

This might be a little clearer. Given:

class Foo:
  func add(p1, p2):
    return p1 + p2

Then

var foo_params = [[1, 2, 3], ['a', 'b', 'c']]

func test_foo(params=use_parameters(foo_params)):
  var foo = Foo.new()
  var result = foo.add(params[0], params[1])
  assert_eq(result, params[2])

Which would result in :

  • one passing test (1 + 2 = 3)
  • one failing test ('a' + 'b' != 'c', it actually equals 'ab')

Using the ParameterFactory.named_parameters method makes this a little more clear. It takes an array of names as the first parameter, and values as the second. The returned variable contains all the values paired with their names.

The above example would look like:

var foo_params = ParameterFactory.named_parameters(
  ['p1', 'p2', 'result'], # names
  [[1, 2, 3], ['a', 'b', 'c']]) # values

func test_foo(params=use_parameters(foo_params)):
  var foo = Foo.new()
  var result = foo.add(params.p1, params.p2)
  assert_eq(result, params.result)

bitwes avatar Jul 19 '21 17:07 bitwes

bitwes, first of all. Thank you for your prompt reply. I do believe this to be a better example. Any chance this would be added to the Parameterized Tests docs? Also, how could one help out with the documentation in general? I think that I've read all of it already but, I encountered some typos here and there. Do you need any help weeding those out? For instance, under Requirements you have: "The test must have one and only one paramter." It's not a big issue but, if there's a small thing I could help this amazing project with, I wouldn't mind at all.

abreathingcorpse avatar Jul 20 '21 02:07 abreathingcorpse

You're welcome. Happy to help.

Wiki issues have come up from time to time. There's no easy way to contribute to the wiki. Github does not allow for pull requests against wikis, so any changes pretty much have to be done by me. I've seen other wikis that are external, and it might be time to move to one of those.

Feel free to make any issues, or tack on some comments to this issue. I'm going to leave it open as a documentation issue. Eventually I will tackle a bunch of those at one time.

bitwes avatar Jul 20 '21 05:07 bitwes

Finally added this to wiki.

bitwes avatar Aug 07 '22 19:08 bitwes