Skip to content

Commit

Permalink
Merge context package into the main package
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed May 23, 2020
1 parent eebd38a commit 3ee0647
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 171 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ Feature: math operations
Add a new test `main_test.go`:

```go
func add(t gobdd.StepTest, ctx context.Context, var1, var2 int) context.Context{
func add(t gobdd.StepTest, ctx gobdd.Context, var1, var2 int) {
res := var1 + var2
ctx.Set("sumRes", res)
return ctx
}

func check(t gobdd.StepTest, ctx context.Context, sum int) context.Context{
func check(t gobdd.StepTest, ctx gobdd.Context, sum int) {
received, err := ctx.GetInt("sumRes")
if err != nil {
t.Error(err)
Expand Down
72 changes: 72 additions & 0 deletions context.go
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
}
67 changes: 5 additions & 62 deletions context/context.go
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()
}
12 changes: 0 additions & 12 deletions context/doc.go

This file was deleted.

2 changes: 1 addition & 1 deletion context/get.go → context_get.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3ee0647

Please sign in to comment.