support_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package instructions
  2. import "testing"
  3. type testCase struct {
  4. name string
  5. args []string
  6. attributes map[string]bool
  7. expected []string
  8. }
  9. func initTestCases() []testCase {
  10. testCases := []testCase{}
  11. testCases = append(testCases, testCase{
  12. name: "empty args",
  13. args: []string{},
  14. attributes: make(map[string]bool),
  15. expected: []string{},
  16. })
  17. jsonAttributes := make(map[string]bool)
  18. jsonAttributes["json"] = true
  19. testCases = append(testCases, testCase{
  20. name: "json attribute with one element",
  21. args: []string{"foo"},
  22. attributes: jsonAttributes,
  23. expected: []string{"foo"},
  24. })
  25. testCases = append(testCases, testCase{
  26. name: "json attribute with two elements",
  27. args: []string{"foo", "bar"},
  28. attributes: jsonAttributes,
  29. expected: []string{"foo", "bar"},
  30. })
  31. testCases = append(testCases, testCase{
  32. name: "no attributes",
  33. args: []string{"foo", "bar"},
  34. attributes: nil,
  35. expected: []string{"foo bar"},
  36. })
  37. return testCases
  38. }
  39. func TestHandleJSONArgs(t *testing.T) {
  40. testCases := initTestCases()
  41. for _, test := range testCases {
  42. arguments := handleJSONArgs(test.args, test.attributes)
  43. if len(arguments) != len(test.expected) {
  44. t.Fatalf("In test \"%s\": length of returned slice is incorrect. Expected: %d, got: %d", test.name, len(test.expected), len(arguments))
  45. }
  46. for i := range test.expected {
  47. if arguments[i] != test.expected[i] {
  48. t.Fatalf("In test \"%s\": element as position %d is incorrect. Expected: %s, got: %s", test.name, i, test.expected[i], arguments[i])
  49. }
  50. }
  51. }
  52. }