github.com/status-im/status-go@v1.1.0/cmd/statusd/topics/topics_test.go (about) 1 package topics 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/status-im/status-go/params" 9 ) 10 11 func TestTopicFlags(t *testing.T) { 12 type testCase struct { 13 shortcut string 14 flags []string 15 expected TopicFlag 16 } 17 18 for _, tc := range []testCase{ 19 { 20 shortcut: "single", 21 flags: []string{"whisper"}, 22 expected: TopicFlag{"whisper"}, 23 }, 24 { 25 shortcut: "multiple", 26 flags: []string{"whisper", "les"}, 27 expected: TopicFlag{"whisper", "les"}, 28 }, 29 { 30 shortcut: "corrupted", 31 flags: []string{" whisper ", "les "}, 32 expected: TopicFlag{"whisper", "les"}, 33 }, 34 } { 35 t.Run(tc.shortcut, func(t *testing.T) { 36 result := TopicFlag{} 37 for _, flag := range tc.flags { 38 assert.NoError(t, result.Set(flag)) 39 } 40 assert.Equal(t, tc.expected, result) 41 }) 42 } 43 } 44 45 func TestTopicLimitsFlag(t *testing.T) { 46 type testCase struct { 47 shortcut string 48 flags []string 49 expected TopicLimitsFlag 50 expectErr bool 51 } 52 for _, tc := range []testCase{ 53 { 54 shortcut: "single", 55 flags: []string{"whisper=1,1"}, 56 expected: TopicLimitsFlag{"whisper": params.NewLimits(1, 1)}, 57 }, 58 { 59 shortcut: "multiple", 60 flags: []string{"whisper=1,1", "les=2,3"}, 61 expected: TopicLimitsFlag{"whisper": params.NewLimits(1, 1), "les": params.NewLimits(2, 3)}, 62 }, 63 { 64 shortcut: "corrupted", 65 flags: []string{" whisper=1,1 ", " les=2,3"}, 66 expected: TopicLimitsFlag{"whisper": params.NewLimits(1, 1), "les": params.NewLimits(2, 3)}, 67 }, 68 { 69 shortcut: "badseparator", 70 flags: []string{"whisper==1,1"}, 71 expected: TopicLimitsFlag{}, 72 expectErr: true, 73 }, 74 { 75 shortcut: "singlelimit", 76 flags: []string{"whisper=1"}, 77 expected: TopicLimitsFlag{}, 78 expectErr: true, 79 }, 80 { 81 shortcut: "minnotanumber", 82 flags: []string{"whisper=a,1"}, 83 expected: TopicLimitsFlag{}, 84 expectErr: true, 85 }, 86 { 87 shortcut: "maxnotanumber", 88 flags: []string{"whisper=1,a"}, 89 expected: TopicLimitsFlag{}, 90 expectErr: true, 91 }, 92 } { 93 t.Run(tc.shortcut, func(t *testing.T) { 94 result := TopicLimitsFlag{} 95 for _, flag := range tc.flags { 96 err := result.Set(flag) 97 if tc.expectErr { 98 assert.Error(t, err) 99 } else { 100 assert.NoError(t, err) 101 } 102 } 103 assert.Equal(t, tc.expected, result) 104 }) 105 } 106 }