github.com/oNaiPs/go-generate-fast@v0.3.0/src/plugins/plugin_test.go (about) 1 package plugins 2 3 import ( 4 "plugin" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 type TestPlugin struct { 11 plugin.Plugin 12 } 13 14 func (p *TestPlugin) Name() string { 15 return "test" 16 } 17 18 func (p *TestPlugin) Matches(opts GenerateOpts) bool { 19 return opts.ExecutableName == "test" 20 } 21 22 func (p *TestPlugin) ComputeInputOutputFiles(opts GenerateOpts) *InputOutputFiles { 23 ioFiles := InputOutputFiles{} 24 return &ioFiles 25 } 26 27 func TestGenerateOptsFile(t *testing.T) { 28 opts := GenerateOpts{Path: "/a/b/c"} 29 assert.Equal(t, opts.File(), "c") 30 } 31 32 func TestGenerateOptsDir(t *testing.T) { 33 opts := GenerateOpts{Path: "/a/b/c"} 34 assert.Equal(t, opts.Dir(), "/a/b") 35 } 36 37 func TestGenerateOptsCommand(t *testing.T) { 38 opts := GenerateOpts{Words: []string{"command", "arg1"}} 39 assert.Equal(t, opts.Command(), "command arg1") 40 } 41 42 func TestRegister(t *testing.T) { 43 ClearPlugins() 44 45 testPlugin := TestPlugin{} 46 RegisterPlugin(&testPlugin) 47 48 notMatched := MatchPlugin(GenerateOpts{ExecutableName: "unmatched"}) 49 assert.Nil(t, notMatched) 50 51 matched := MatchPlugin(GenerateOpts{ExecutableName: "test"}) 52 assert.NotNil(t, matched) 53 assert.Equal(t, matched.Name(), "test") 54 55 assert.Panics(t, func() { 56 RegisterPlugin(&testPlugin) 57 }) 58 }