#Create an []interface{} for unit test

9 messages · Page 1 of 1 (latest)

dusk escarp
#

Hi all,
I've got a function I am writing a test for with the signature func EnsureMinMax(input []interface{}) [2]float32 The function takes in some crazy data and tries to type it. Here is a simplified playground example: https://play.golang.com/p/Tf4keVEkmBn

I am wondering if the solution in that link is the simplest one? I have to write a number of tests for this function, and I am trying to limit the logic in the test.

(the solution being)

fs := []float64{0.0, 0.0}
islice := make([]interface{}, len(fs))
for i, f := range fs {
  islice[i] = f
}    

I get why test := make([2]interface{}, 1) could never work, but is there something simple like that I don't know about?

Thanks for any help.

#

Oh my, thanks for that. I guess I had a brain skip and thought that was going to make 2 []interfaces not [2]interfaces{}

hot folio
#

I'm not 100% sure what you're even asking for specifically.
You can do this: x := []interface{}{0.0, 0.0}, or what polyscone said.
It's not very surprising that making a slice of length 1 and then accessing the second element gives you a panic.
An array (e.g [2]interface{}) does not need to be made with make

dusk escarp
#

@teal stream it's much more complicated than this, but basically something like ``` test1 := make([]interface{}, 2)
test1[0] = 0.0
test1[1] = 0.0

test2 := make([]interface{}, 2)
test2[0] = "0.0"
test2[1] = "3.2"

fmt.Println(EnsureMinMax(test1))
fmt.Println(EnsureMinMax(test2))``` where the inputs can be a range of things. I will look into `any` oddly, never seen that before
hot folio
willow copperBOT
#

    type any = interface{}

any is an alias for interface{} and is equivalent to interface{} in all ways.

cloud yoke
#

any is tired people-proof for real

dusk escarp
#

Thanks y'all