File tree 2 files changed +37
-0
lines changed
2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ package funk
2
+
3
+ func ShortIf (condition bool , a interface {}, b interface {}) interface {} {
4
+ if condition {
5
+ return a
6
+ }
7
+ return b
8
+ }
Original file line number Diff line number Diff line change
1
+ package funk
2
+
3
+ import (
4
+ "testing"
5
+ "github.com/stretchr/testify/assert"
6
+ )
7
+
8
+ func TestShortIf (t * testing.T ) {
9
+ is := assert .New (t )
10
+
11
+ r := ShortIf (10 > 5 , 10 , 5 )
12
+ is .Equal (r ,10 )
13
+
14
+ r = ShortIf (10.0 == 10 , "yes" , "no" )
15
+ is .Equal (r ,"yes" )
16
+
17
+ r = ShortIf ('a' == 'b' ,"equal chars" ,"unequal chars" )
18
+ is .Equal (r ,"unequal chars" )
19
+
20
+ r = ShortIf ("abc" == "abc" ,"Same string" ,"Different strings" )
21
+ is .Equal (r ,"Same string" )
22
+
23
+ type testStruct struct {}
24
+ a := testStruct {}
25
+ b := testStruct {}
26
+ r = ShortIf (a == b , & a , & b )
27
+ is .Equal (r ,& b )
28
+
29
+ }
You can’t perform that action at this time.
0 commit comments