github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/synchronization/watch_mode_test.go (about) 1 package synchronization 2 3 import ( 4 "testing" 5 ) 6 7 // TestWatchModeUnmarshal tests that unmarshaling from a string specification 8 // succeeeds for WatchMode. 9 func TestWatchModeUnmarshal(t *testing.T) { 10 // Set up test cases. 11 testCases := []struct { 12 text string 13 expectedMode WatchMode 14 expectFailure bool 15 }{ 16 {"", WatchMode_WatchModeDefault, true}, 17 {"asdf", WatchMode_WatchModeDefault, true}, 18 {"portable", WatchMode_WatchModePortable, false}, 19 {"force-poll", WatchMode_WatchModeForcePoll, false}, 20 {"no-watch", WatchMode_WatchModeNoWatch, false}, 21 } 22 23 // Process test cases. 24 for _, testCase := range testCases { 25 var mode WatchMode 26 if err := mode.UnmarshalText([]byte(testCase.text)); err != nil { 27 if !testCase.expectFailure { 28 t.Errorf("unable to unmarshal text (%s): %s", testCase.text, err) 29 } 30 } else if testCase.expectFailure { 31 t.Error("unmarshaling succeeded unexpectedly for text:", testCase.text) 32 } else if mode != testCase.expectedMode { 33 t.Errorf( 34 "unmarshaled mode (%s) does not match expected (%s)", 35 mode, 36 testCase.expectedMode, 37 ) 38 } 39 } 40 } 41 42 // TestWatchModeSupported tests that WatchMode support detection works as 43 // expected. 44 func TestWatchModeSupported(t *testing.T) { 45 // Set up test cases. 46 testCases := []struct { 47 mode WatchMode 48 expectSupported bool 49 }{ 50 {WatchMode_WatchModeDefault, false}, 51 {WatchMode_WatchModePortable, true}, 52 {WatchMode_WatchModeForcePoll, true}, 53 {WatchMode_WatchModeNoWatch, true}, 54 {(WatchMode_WatchModeNoWatch + 1), false}, 55 } 56 57 // Process test cases. 58 for _, testCase := range testCases { 59 if supported := testCase.mode.Supported(); supported != testCase.expectSupported { 60 t.Errorf( 61 "mode support status (%t) does not match expected (%t)", 62 supported, 63 testCase.expectSupported, 64 ) 65 } 66 } 67 } 68 69 // TestWatchModeDescription tests that WatchMode description generation works as 70 // expected. 71 func TestWatchModeDescription(t *testing.T) { 72 // Set up test cases. 73 testCases := []struct { 74 mode WatchMode 75 expectedDescription string 76 }{ 77 {WatchMode_WatchModeDefault, "Default"}, 78 {WatchMode_WatchModePortable, "Portable"}, 79 {WatchMode_WatchModeForcePoll, "Force Poll"}, 80 {WatchMode_WatchModeNoWatch, "No Watch"}, 81 {(WatchMode_WatchModeNoWatch + 1), "Unknown"}, 82 } 83 84 // Process test cases. 85 for _, testCase := range testCases { 86 if description := testCase.mode.Description(); description != testCase.expectedDescription { 87 t.Errorf( 88 "mode description (%s) does not match expected (%s)", 89 description, 90 testCase.expectedDescription, 91 ) 92 } 93 } 94 }