@@ -40,4 +40,79 @@ func TestMap(t *testing.T) {
4040 assert .NoError (t , err )
4141 assert .Equal (t , expected , out )
4242 })
43+
44+ squared := func (element int ) int {
45+ return element * element
46+ }
47+
48+ t .Run ("should not panic if output is nil" , func (t * testing.T ) {
49+ in := []int {1 , 2 , 3 }
50+ {
51+ var out []int
52+
53+ err := godash .Map (in , out , squared )
54+
55+ assert .EqualError (t , err , "output is nil. Pass a reference to set output" )
56+ }
57+
58+ {
59+ err := godash .Map (in , nil , squared )
60+
61+ assert .EqualError (t , err , "output is nil. Pass a reference to set output" )
62+ }
63+ })
64+
65+ t .Run ("should not accept mapper function that are not functions" , func (t * testing.T ) {
66+ in := []int {1 , 2 , 3 }
67+ var out []int
68+
69+ err := godash .Map (in , & out , 7 )
70+
71+ assert .EqualError (t , err , "mapperFn has to be a function" )
72+ })
73+
74+ t .Run ("should not accept mapper function that do not take exactly one argument" , func (t * testing.T ) {
75+ in := []int {1 , 2 , 3 }
76+ var out []int
77+
78+ {
79+ err := godash .Map (in , & out , func () int { return 0 })
80+ assert .EqualError (t , err , "mapper function has to take only one argument" )
81+ }
82+
83+ {
84+ err := godash .Map (in , & out , func (int , int ) int { return 0 })
85+ assert .EqualError (t , err , "mapper function has to take only one argument" )
86+ }
87+ })
88+
89+ t .Run ("should not accept mapper function that do not return exactly one value" , func (t * testing.T ) {
90+ in := []int {1 , 2 , 3 }
91+ var out []int
92+
93+ {
94+ err := godash .Map (in , & out , func (int ) {})
95+ assert .EqualError (t , err , "mapper function should return only one return value" )
96+ }
97+
98+ {
99+ err := godash .Map (in , & out , func (int ) (int , int ) { return 0 , 0 })
100+ assert .EqualError (t , err , "mapper function should return only one return value" )
101+ }
102+ })
103+
104+ t .Run ("should accept mapper function whose argument's kind should be slice's element kind" , func (t * testing.T ) {
105+ in := []int {1 , 2 , 3 }
106+ var out []int
107+
108+ {
109+ err := godash .Map (in , & out , func (string ) string { return "" })
110+ assert .EqualError (t , err , "mapper function's first argument has to be the type of element of input slice" )
111+ }
112+
113+ {
114+ err := godash .Map (in , & out , func (int ) int { return 0 })
115+ assert .NoError (t , err )
116+ }
117+ })
43118}
0 commit comments