Skip to content

Commit

Permalink
Merge 6d75e81 into e56f417
Browse files Browse the repository at this point in the history
  • Loading branch information
tkanos committed Apr 30, 2018
2 parents e56f417 + 6d75e81 commit c1cc20e
Showing 1 changed file with 141 additions and 0 deletions.
141 changes: 141 additions & 0 deletions tester/variables_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tester

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -226,3 +227,143 @@ func TestExtractValueFromJSONArray(t *testing.T) {
assert.Equal(t, tt.expected, v, tt.description)
}
}

var parseOtt = []struct {
runner *Runner
contract *Contract
body []byte
description string
err bool
expectedKey string
expectedValue string
}{
{
contract: &Contract{
Outputs: make(map[string]string),
},
err: false,
description: "It should not return an error if there are no outputs",
},
{
contract: &Contract{
Outputs: map[string]string{"value": "NOT_MAPPED.whatever"},
},
err: true,
description: "It should return an error if there are outputs that does not begin by what is expected (JSON)",
},
{
contract: &Contract{
Outputs: map[string]string{"value": "JSON.A"},
},
runner: &Runner{test: Test{Globals: make(map[string]string)}},
body: []byte(`{"A": 1 }`),
err: false,
expectedKey: "value",
expectedValue: "1",
description: "Runner should have an value as output",
},
{
contract: &Contract{
Outputs: map[string]string{"value": "JSON.A"},
},
runner: &Runner{test: Test{Globals: make(map[string]string)}},
body: []byte(`OBVIOUSLY NOT A JSON`),
err: true,
description: "It should return an error if the body does not match with what is exected",
},
}

func TestParseOutputs(t *testing.T) {
for _, tt := range parseOtt {
//act
err := parseOutputs(tt.runner, tt.contract, tt.body)

//assert
assert.True(t, (err != nil) == tt.err, tt.description)

if tt.expectedKey != "" && err == nil {
assert.Contains(t, tt.runner.test.Globals, tt.expectedKey, tt.description)
assert.Equal(t, tt.expectedValue, tt.runner.test.Globals[tt.expectedKey], tt.description)
}

}

}

var replaceVartt = []struct {
s string
contract *Contract
runner *Runner
env map[string]string
err bool
expected string
description string
}{
{
s: "NO PATTERN",
err: false,
expected: "NO PATTERN",
description: "If there are no value to replace it should return the string in output",
},
{
s: "::local::",
err: false,
contract: &Contract{Locals: map[string]string{"local": "1"}},
runner: &Runner{test: Test{Globals: map[string]string{}}},
expected: "1",
description: "If should replace the input value by the local one",
},
{
s: "::global::",
err: false,
contract: &Contract{Locals: map[string]string{}},
runner: &Runner{test: Test{Globals: map[string]string{"global": "1"}}},
expected: "1",
description: "If should replace the input value by the global one",
},
{
s: "::env::",
err: false,
contract: &Contract{Locals: map[string]string{}},
runner: &Runner{test: Test{Globals: map[string]string{}}},
env: map[string]string{"ENV": "1"},
expected: "1",
description: "If should replace the input value by the env one",
},
{
s: "::not_found::",
contract: &Contract{Locals: map[string]string{}},
runner: &Runner{test: Test{Globals: map[string]string{}}},
expected: "::not_found::",
err: true,
description: "If should send an error if the value is not on local, global neither env variables",
},
{
s: "::local::_::global::_::env::",
err: false,
contract: &Contract{Locals: map[string]string{"local": "1"}},
runner: &Runner{test: Test{Globals: map[string]string{"global": "2"}}},
env: map[string]string{"ENV": "3"},
expected: "1_2_3",
description: "If should replace all the values if there are many ",
},
}

func TestReplaceVariables(t *testing.T) {
for _, tt := range replaceVartt {
//arrange
if tt.env != nil && len(tt.env) > 0 {
// Define env variables
for key, value := range tt.env {
os.Setenv(key, value)
}
}

//act
s, err := replaceVariables(tt.runner, tt.contract, tt.s)

//assert
assert.True(t, (err != nil) == tt.err, tt.description)
assert.Equal(t, tt.expected, s, tt.description)
}
}

0 comments on commit c1cc20e

Please sign in to comment.