Skip to content

Commit

Permalink
Genereate expected result automatically for each test
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavAqua committed May 26, 2019
1 parent 53ef773 commit d1c3e31
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Check struct {
State `json:"status"`
ActualValue string `json:"actual_value"`
Scored bool `json:"scored"`
ExpectedResult string `json:"expected_result"`
}

// Runner wraps the basic Run method.
Expand Down Expand Up @@ -188,6 +189,7 @@ func (c *Check) run() State {
finalOutput := c.Tests.execute(out.String())
if finalOutput != nil {
c.ActualValue = finalOutput.actualResult
c.ExpectedResult = finalOutput.ExpectedResult
if finalOutput.testResult {
c.State = PASS
} else {
Expand Down
21 changes: 20 additions & 1 deletion check/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type compare struct {
type testOutput struct {
testResult bool
actualResult string
ExpectedResult string
}

func failTestItem(s string) *testOutput {
Expand Down Expand Up @@ -135,8 +136,10 @@ func (t *testItem) execute(s string) *testOutput {
}
}

expectedResultPattern := ""
switch t.Compare.Op {
case "eq":
expectedResultPattern = "'%s' is equal to '%s'"
value := strings.ToLower(flagVal)
// Do case insensitive comparaison for booleans ...
if value == "false" || value == "true" {
Expand All @@ -146,6 +149,7 @@ func (t *testItem) execute(s string) *testOutput {
}

case "noteq":
expectedResultPattern = "'%s' is not equal to '%s'"
value := strings.ToLower(flagVal)
// Do case insensitive comparaison for booleans ...
if value == "false" || value == "true" {
Expand All @@ -155,32 +159,41 @@ func (t *testItem) execute(s string) *testOutput {
}

case "gt":
expectedResultPattern = "%s is greater then %s"
a, b := toNumeric(flagVal, t.Compare.Value)
result.testResult = a > b

case "gte":
expectedResultPattern = "%s is greater or equal to %s"
a, b := toNumeric(flagVal, t.Compare.Value)
result.testResult = a >= b

case "lt":
expectedResultPattern = "%s is lower then %s"
a, b := toNumeric(flagVal, t.Compare.Value)
result.testResult = a < b

case "lte":
expectedResultPattern = "%s is lower or equal to %s"
a, b := toNumeric(flagVal, t.Compare.Value)
result.testResult = a <= b

case "has":
expectedResultPattern = "'%s' has '%s'"
result.testResult = strings.Contains(flagVal, t.Compare.Value)

case "nothave":
expectedResultPattern = " '%s' not have '%s'"
result.testResult = !strings.Contains(flagVal, t.Compare.Value)
}

result.ExpectedResult = fmt.Sprintf(expectedResultPattern, t.Flag, t.Compare.Value)
} else {
result.ExpectedResult = fmt.Sprintf("'%s' is present", t.Flag)
result.testResult = isset
}

} else {
result.ExpectedResult = fmt.Sprintf("'%s' is not present", t.Flag)
notset := !match
result.testResult = notset
}
Expand Down Expand Up @@ -219,13 +232,19 @@ func (ts *tests) execute(s string) *testOutput {
case and, "":
result = true
for i := range res {
finalOutput.ExpectedResult += fmt.Sprintf("%s AND ", res[i].ExpectedResult)
result = result && res[i].testResult
}
// Delete last iteration ' AND '
finalOutput.ExpectedResult = finalOutput.ExpectedResult[:len(finalOutput.ExpectedResult)-5]
case or:
result = false
for i := range res {
finalOutput.ExpectedResult += fmt.Sprintf("%s OR ", res[i].ExpectedResult)
result = result || res[i].testResult
}
// Delete last iteration ' OR '
finalOutput.ExpectedResult = finalOutput.ExpectedResult[:len(finalOutput.ExpectedResult)-4]
}

finalOutput.testResult = result
Expand Down

0 comments on commit d1c3e31

Please sign in to comment.