errors_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) 2013 - Max Persson <max@looplab.se>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fsm
  15. import (
  16. "errors"
  17. "testing"
  18. )
  19. func TestInvalidEventError(t *testing.T) {
  20. event := "invalid event"
  21. state := "state"
  22. e := InvalidEventError{Event: event, State: state}
  23. if e.Error() != "event "+e.Event+" inappropriate in current state "+e.State {
  24. t.Error("InvalidEventError string mismatch")
  25. }
  26. }
  27. func TestUnknownEventError(t *testing.T) {
  28. event := "invalid event"
  29. e := UnknownEventError{Event: event}
  30. if e.Error() != "event "+e.Event+" does not exist" {
  31. t.Error("UnknownEventError string mismatch")
  32. }
  33. }
  34. func TestInTransitionError(t *testing.T) {
  35. event := "in transition"
  36. e := InTransitionError{Event: event}
  37. if e.Error() != "event "+e.Event+" inappropriate because previous transition did not complete" {
  38. t.Error("InTransitionError string mismatch")
  39. }
  40. }
  41. func TestNotInTransitionError(t *testing.T) {
  42. e := NotInTransitionError{}
  43. if e.Error() != "transition inappropriate because no state change in progress" {
  44. t.Error("NotInTransitionError string mismatch")
  45. }
  46. }
  47. func TestNoTransitionError(t *testing.T) {
  48. e := NoTransitionError{}
  49. if e.Error() != "no transition" {
  50. t.Error("NoTransitionError string mismatch")
  51. }
  52. e.Err = errors.New("no transition")
  53. if e.Error() != "no transition with error: "+e.Err.Error() {
  54. t.Error("NoTransitionError string mismatch")
  55. }
  56. }
  57. func TestCanceledError(t *testing.T) {
  58. e := CanceledError{}
  59. if e.Error() != "transition canceled" {
  60. t.Error("CanceledError string mismatch")
  61. }
  62. e.Err = errors.New("canceled")
  63. if e.Error() != "transition canceled with error: "+e.Err.Error() {
  64. t.Error("CanceledError string mismatch")
  65. }
  66. }
  67. func TestAsyncError(t *testing.T) {
  68. e := AsyncError{}
  69. if e.Error() != "async started" {
  70. t.Error("AsyncError string mismatch")
  71. }
  72. e.Err = errors.New("async")
  73. if e.Error() != "async started with error: "+e.Err.Error() {
  74. t.Error("AsyncError string mismatch")
  75. }
  76. }
  77. func TestInternalError(t *testing.T) {
  78. e := InternalError{}
  79. if e.Error() != "internal error on state transition" {
  80. t.Error("InternalError string mismatch")
  81. }
  82. }