-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge context package into the main package
- Loading branch information
1 parent
eebd38a
commit 3ee0647
Showing
15 changed files
with
208 additions
and
171 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package gobdd | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Holds data from previously executed steps | ||
type Context struct { | ||
values map[interface{}]interface{} | ||
} | ||
|
||
// Creates a new (empty) context struct | ||
func NewContext() Context { | ||
return Context{ | ||
values: map[interface{}]interface{}{}, | ||
} | ||
} | ||
|
||
// Clone creates a copy of the context | ||
func (ctx Context) Clone() Context { | ||
c := Context{ | ||
values: map[interface{}]interface{}{}, | ||
} | ||
|
||
for k, v := range ctx.values { | ||
c.Set(k, v) | ||
} | ||
|
||
return c | ||
} | ||
|
||
// Sets the value under the key | ||
func (ctx Context) Set(key interface{}, value interface{}) { | ||
ctx.values[key] = value | ||
} | ||
|
||
// Returns the data under the key. | ||
// If couldn't find anything but the default value is provided, returns the default value. | ||
// Otherwise, it returns an error. | ||
func (ctx Context) Get(key interface{}, defaultValue ...interface{}) (interface{}, error) { | ||
if _, ok := ctx.values[key]; !ok { | ||
if len(defaultValue) == 1 { | ||
return defaultValue[0], nil | ||
} | ||
|
||
return nil, fmt.Errorf("the key %+v does not exist", key) | ||
} | ||
|
||
return ctx.values[key], nil | ||
} | ||
|
||
// It is a shortcut for getting the value already casted as error. | ||
func (ctx Context) GetError(key interface{}, defaultValue ...error) (error, error) { | ||
if _, ok := ctx.values[key]; !ok { | ||
if len(defaultValue) == 1 { | ||
return defaultValue[0], nil | ||
} | ||
|
||
return nil, fmt.Errorf("the key %+v does not exist", key) | ||
} | ||
|
||
if ctx.values[key] == nil { | ||
return nil, nil | ||
} | ||
|
||
value, ok := ctx.values[key].(error) | ||
if !ok { | ||
return nil, fmt.Errorf("the expected value is not error (%T)", key) | ||
} | ||
|
||
return value, nil | ||
} |
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 |
---|---|---|
@@ -1,72 +1,15 @@ | ||
package context | ||
|
||
import ( | ||
"fmt" | ||
"github.com/go-bdd/gobdd" | ||
) | ||
|
||
// Holds data from previously executed steps | ||
type Context struct { | ||
values map[interface{}]interface{} | ||
} | ||
// Deprecated: use gobdd.Context instead. | ||
type Context = gobdd.Context | ||
|
||
// Creates a new (empty) context struct | ||
// Deprecated: use gobdd.NewContext instead. | ||
func New() Context { | ||
return Context{ | ||
values: map[interface{}]interface{}{}, | ||
} | ||
} | ||
|
||
// Clone creates a copy of the context | ||
func (ctx Context) Clone() Context { | ||
c := Context{ | ||
values: map[interface{}]interface{}{}, | ||
} | ||
|
||
for k, v := range ctx.values { | ||
c.Set(k, v) | ||
} | ||
|
||
return c | ||
} | ||
|
||
// Sets the value under the key | ||
func (ctx Context) Set(key interface{}, value interface{}) { | ||
ctx.values[key] = value | ||
} | ||
|
||
// Returns the data under the key. | ||
// If couldn't find anything but the default value is provided, returns the default value. | ||
// Otherwise, it returns an error. | ||
func (ctx Context) Get(key interface{}, defaultValue ...interface{}) (interface{}, error) { | ||
if _, ok := ctx.values[key]; !ok { | ||
if len(defaultValue) == 1 { | ||
return defaultValue[0], nil | ||
} | ||
|
||
return nil, fmt.Errorf("the key %+v does not exist", key) | ||
} | ||
|
||
return ctx.values[key], nil | ||
} | ||
|
||
// It is a shortcut for getting the value already casted as error. | ||
func (ctx Context) GetError(key interface{}, defaultValue ...error) (error, error) { | ||
if _, ok := ctx.values[key]; !ok { | ||
if len(defaultValue) == 1 { | ||
return defaultValue[0], nil | ||
} | ||
|
||
return nil, fmt.Errorf("the key %+v does not exist", key) | ||
} | ||
|
||
if ctx.values[key] == nil { | ||
return nil, nil | ||
} | ||
|
||
value, ok := ctx.values[key].(error) | ||
if !ok { | ||
return nil, fmt.Errorf("the expected value is not error (%T)", key) | ||
} | ||
|
||
return value, nil | ||
return gobdd.NewContext() | ||
} |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.