From 16d5aeac7c2de010d30cddc90c5755ac5b989b2b Mon Sep 17 00:00:00 2001 From: Duco van Amstel Date: Tue, 13 Nov 2018 22:30:56 +0000 Subject: Make config.Config more unit-test friendly (#586) --- gateway/samechannel/samechannel.go | 6 ++-- gateway/samechannel/samechannel_test.go | 59 ++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 12 deletions(-) (limited to 'gateway/samechannel') diff --git a/gateway/samechannel/samechannel.go b/gateway/samechannel/samechannel.go index 937d769b..ea846e92 100644 --- a/gateway/samechannel/samechannel.go +++ b/gateway/samechannel/samechannel.go @@ -5,17 +5,17 @@ import ( ) type SameChannelGateway struct { - *config.Config + config.Config } -func New(cfg *config.Config) *SameChannelGateway { +func New(cfg config.Config) *SameChannelGateway { return &SameChannelGateway{Config: cfg} } func (sgw *SameChannelGateway) GetConfig() []config.Gateway { var gwconfigs []config.Gateway cfg := sgw.Config - for _, gw := range cfg.SameChannelGateway { + for _, gw := range cfg.ConfigValues().SameChannelGateway { gwconfig := config.Gateway{Name: gw.Name, Enable: gw.Enable} for _, account := range gw.Accounts { for _, channel := range gw.Channels { diff --git a/gateway/samechannel/samechannel_test.go b/gateway/samechannel/samechannel_test.go index 7c75444f..c0e579ae 100644 --- a/gateway/samechannel/samechannel_test.go +++ b/gateway/samechannel/samechannel_test.go @@ -1,16 +1,13 @@ package samechannelgateway import ( - "fmt" - "github.com/42wim/matterbridge/bridge/config" - "github.com/BurntSushi/toml" "github.com/stretchr/testify/assert" "testing" ) -var testconfig = ` +const testConfig = ` [mattermost.test] [slack.test] @@ -21,12 +18,56 @@ var testconfig = ` channels = [ "testing","testing2","testing10"] ` -func TestGetConfig(t *testing.T) { - var cfg *config.Config - if _, err := toml.Decode(testconfig, &cfg); err != nil { - fmt.Println(err) +var ( + expectedConfig = config.Gateway{ + Name: "blah", + Enable: true, + In: []config.Bridge(nil), + Out: []config.Bridge(nil), + InOut: []config.Bridge{ + { + Account: "mattermost.test", + Channel: "testing", + Options: config.ChannelOptions{Key: ""}, + SameChannel: true, + }, + { + Account: "mattermost.test", + Channel: "testing2", + Options: config.ChannelOptions{Key: ""}, + SameChannel: true, + }, + { + Account: "mattermost.test", + Channel: "testing10", + Options: config.ChannelOptions{Key: ""}, + SameChannel: true, + }, + { + Account: "slack.test", + Channel: "testing", + Options: config.ChannelOptions{Key: ""}, + SameChannel: true, + }, + { + Account: "slack.test", + Channel: "testing2", + Options: config.ChannelOptions{Key: ""}, + SameChannel: true, + }, + { + Account: "slack.test", + Channel: "testing10", + Options: config.ChannelOptions{Key: ""}, + SameChannel: true, + }, + }, } +) + +func TestGetConfig(t *testing.T) { + cfg := config.NewConfigFromString([]byte(testConfig)) sgw := New(cfg) configs := sgw.GetConfig() - assert.Equal(t, []config.Gateway{{Name: "blah", Enable: true, In: []config.Bridge(nil), Out: []config.Bridge(nil), InOut: []config.Bridge{{Account: "mattermost.test", Channel: "testing", Options: config.ChannelOptions{Key: ""}, SameChannel: true}, {Account: "mattermost.test", Channel: "testing2", Options: config.ChannelOptions{Key: ""}, SameChannel: true}, {Account: "mattermost.test", Channel: "testing10", Options: config.ChannelOptions{Key: ""}, SameChannel: true}, {Account: "slack.test", Channel: "testing", Options: config.ChannelOptions{Key: ""}, SameChannel: true}, {Account: "slack.test", Channel: "testing2", Options: config.ChannelOptions{Key: ""}, SameChannel: true}, {Account: "slack.test", Channel: "testing10", Options: config.ChannelOptions{Key: ""}, SameChannel: true}}}}, configs) + assert.Equal(t, []config.Gateway{expectedConfig}, configs) } -- cgit v1.2.3