-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add string representation of Direction.
Allow to print Direction in human form. Extract Direction struct to a separate file. Add tests to cover Direction to string. Parse direction from string with a new method stream.ParseDirection. It returns direction or error if could not parse.
- Loading branch information
Showing
6 changed files
with
107 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package stream | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
type Direction uint8 | ||
|
||
var ErrInvalidDirectionParameter error = errors.New("stream: invalid direction") | ||
|
||
const ( | ||
Upstream Direction = iota | ||
Downstream | ||
NumDirections | ||
) | ||
|
||
func (d Direction) String() string { | ||
if d >= NumDirections { | ||
return "num_directions" | ||
} | ||
return [...]string{"upstream", "downstream"}[d] | ||
} | ||
|
||
func ParseDirection(value string) (Direction, error) { | ||
switch strings.ToLower(value) { | ||
case "downstream": | ||
return Downstream, nil | ||
case "upstream": | ||
return Upstream, nil | ||
} | ||
|
||
return NumDirections, ErrInvalidDirectionParameter | ||
} |
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,67 @@ | ||
package stream_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Shopify/toxiproxy/v2/stream" | ||
) | ||
|
||
func TestDirection_String(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
direction stream.Direction | ||
expected string | ||
}{ | ||
{"Downstream to string", stream.Downstream, "downstream"}, | ||
{"Upstream to string", stream.Upstream, "upstream"}, | ||
{"NumDirections to string", stream.NumDirections, "num_directions"}, | ||
{"Upstream via number direction to string", stream.Direction(0), "upstream"}, | ||
{"Downstream via number direction to string", stream.Direction(1), "downstream"}, | ||
{"High number direction to string", stream.Direction(5), "num_directions"}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc // capture range variable | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual := tc.direction.String() | ||
if actual != tc.expected { | ||
t.Errorf("got \"%s\"; expected \"%s\"", actual, tc.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestParseDirection(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input string | ||
expected stream.Direction | ||
err error | ||
}{ | ||
{"parse empty", "", stream.NumDirections, stream.ErrInvalidDirectionParameter}, | ||
{"parse upstream", "upstream", stream.Upstream, nil}, | ||
{"parse downstream", "downstream", stream.Downstream, nil}, | ||
{"parse unknown", "unknown", stream.NumDirections, stream.ErrInvalidDirectionParameter}, | ||
{"parse number", "-123", stream.NumDirections, stream.ErrInvalidDirectionParameter}, | ||
{"parse upper case", "DOWNSTREAM", stream.Downstream, nil}, | ||
{"parse camel case", "UpStream", stream.Upstream, nil}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc // capture range variable | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual, err := stream.ParseDirection(tc.input) | ||
if actual != tc.expected { | ||
t.Errorf("got \"%s\"; expected \"%s\"", actual, tc.expected) | ||
} | ||
|
||
if err != tc.err { | ||
t.Errorf("got \"%s\"; expected \"%s\"", err, tc.err) | ||
} | ||
}) | ||
} | ||
} |
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