-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(project): added default container
- Loading branch information
Axel Etcheverry
committed
Oct 16, 2018
1 parent
298809e
commit 994fb17
Showing
5 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2018 Axel Etcheverry. All rights reserved. | ||
// Use of this source code is governed by a MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package service | ||
|
||
var defaultContainer = New() | ||
|
||
// Set service | ||
func Set(name string, f ContainerFunc) { | ||
defaultContainer.Set(name, f) | ||
} | ||
|
||
// Has service exists | ||
func Has(name string) bool { | ||
return defaultContainer.Has(name) | ||
} | ||
|
||
// Get service | ||
func Get(name string) interface{} { | ||
return defaultContainer.Get(name) | ||
} | ||
|
||
// GetKeys of all services | ||
func GetKeys() []string { | ||
return defaultContainer.GetKeys() | ||
} | ||
|
||
// Fill dst | ||
func Fill(name string, dst interface{}) { | ||
defaultContainer.Fill(name, dst) | ||
} | ||
|
||
// Extend service | ||
func Extend(name string, f ExtenderFunc) { | ||
defaultContainer.Extend(name, f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2018 Axel Etcheverry. All rights reserved. | ||
// Use of this source code is governed by a MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package service | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDefaultContainer(t *testing.T) { | ||
assert.False(t, Has("test.bad.service.name")) | ||
|
||
assert.Equal(t, []string{}, GetKeys()) | ||
|
||
Set("my.service", func(c Container) interface{} { | ||
return &MyService{} | ||
}) | ||
|
||
Extend("my.service", func(s *MyService) *MyService { | ||
s.Name = "My Service" | ||
|
||
return s | ||
}) | ||
|
||
assert.True(t, Has("my.service")) | ||
|
||
assert.Equal(t, []string{"my.service"}, GetKeys()) | ||
|
||
Set("my.service", func(c Container) interface{} { | ||
return &MyService{} | ||
}) | ||
|
||
myService1 := Get("my.service").(*MyService) | ||
|
||
myService2 := Get("my.service").(*MyService) | ||
|
||
assert.Equal(t, myService1, myService2) | ||
|
||
assert.Equal(t, "My Service", myService1.Name) | ||
|
||
assert.Panics(t, func() { | ||
Set("my.service", func(c Container) interface{} { | ||
return &MyService{} | ||
}) | ||
}) | ||
|
||
assert.Panics(t, func() { | ||
Extend("my.service", func(s *MyService) *MyService { | ||
s.Name = "My Service 2" | ||
|
||
return s | ||
}) | ||
}) | ||
|
||
assert.Panics(t, func() { | ||
Extend("not.exists.service", func(s *MyService) *MyService { | ||
s.Name = "My Service 3" | ||
|
||
return s | ||
}) | ||
}) | ||
|
||
assert.Panics(t, func() { | ||
Get("test.bad.service.name") | ||
}) | ||
|
||
var myService3 *MyService | ||
|
||
Fill("my.service", &myService3) | ||
|
||
assert.Equal(t, myService2, myService3) | ||
|
||
assert.Panics(t, func() { | ||
var bad string | ||
|
||
Fill("my.service", &bad) | ||
}) | ||
} |