This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some type safety for film advance mode
Signed-off-by: Igor Shishkin <[email protected]>
- Loading branch information
Showing
5 changed files
with
180 additions
and
21 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,64 @@ | ||
package types | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// FilmAdvanceMode ... | ||
type FilmAdvanceMode string | ||
|
||
const ( | ||
// FilmAdvanceModeSingleFrame ... | ||
FilmAdvanceModeSingleFrame FilmAdvanceMode = "Single-frame" | ||
|
||
// FilmAdvanceModeContinuousBodyOnly ... | ||
FilmAdvanceModeContinuousBodyOnly FilmAdvanceMode = "Continuous (body only)" | ||
|
||
// FilmAdvanceModeLowSpeedContinuous ... | ||
FilmAdvanceModeLowSpeedContinuous FilmAdvanceMode = "Low-speed continuous" | ||
|
||
// FilmAdvanceModeHighSpeedContinuous ... | ||
FilmAdvanceModeHighSpeedContinuous FilmAdvanceMode = "High-speed continuous" | ||
|
||
// FilmAdvanceModeUltraHighSpeedContinuous ... | ||
FilmAdvanceModeUltraHighSpeedContinuous FilmAdvanceMode = "Ultra-high-speed continuous" | ||
|
||
// FilmAdvanceMode2secSelfTimer ... | ||
FilmAdvanceMode2secSelfTimer FilmAdvanceMode = "2-sec. self-timer" | ||
|
||
// FilmAdvanceMode10secSelfTimer ... | ||
FilmAdvanceMode10secSelfTimer FilmAdvanceMode = "10-sec. self-timer" | ||
) | ||
|
||
// FilmAdvanceModeFromString ... | ||
func FilmAdvanceModeFromString(s string) (fam *FilmAdvanceMode, err error) { | ||
s = strings.TrimSpace(s) | ||
|
||
var famv FilmAdvanceMode | ||
switch FilmAdvanceMode(s) { | ||
case FilmAdvanceMode10secSelfTimer: | ||
famv = FilmAdvanceMode10secSelfTimer | ||
case FilmAdvanceMode2secSelfTimer: | ||
famv = FilmAdvanceMode2secSelfTimer | ||
case FilmAdvanceModeContinuousBodyOnly: | ||
famv = FilmAdvanceModeContinuousBodyOnly | ||
case FilmAdvanceModeHighSpeedContinuous: | ||
famv = FilmAdvanceModeHighSpeedContinuous | ||
case FilmAdvanceModeLowSpeedContinuous: | ||
famv = FilmAdvanceModeLowSpeedContinuous | ||
case FilmAdvanceModeSingleFrame: | ||
famv = FilmAdvanceModeSingleFrame | ||
case FilmAdvanceModeUltraHighSpeedContinuous: | ||
famv = FilmAdvanceModeUltraHighSpeedContinuous | ||
default: | ||
err = errors.Errorf("error parsing FilmAdvanceMode: unknown value `%s`", s) | ||
} | ||
|
||
return &famv, err | ||
} | ||
|
||
func (famv *FilmAdvanceMode) String() string { | ||
return string(*famv) | ||
} |
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,90 @@ | ||
package types | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFilmAdvanceMode(t *testing.T) { | ||
r := require.New(t) | ||
|
||
type testCase struct { | ||
name string | ||
input string | ||
expOutput *FilmAdvanceMode | ||
expError error | ||
} | ||
|
||
tcs := []testCase{ | ||
{ | ||
name: "Single-frame", | ||
input: "Single-frame", | ||
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeSingleFrame), | ||
}, | ||
{ | ||
name: "Continuous (body only)", | ||
input: "Continuous (body only)", | ||
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeContinuousBodyOnly), | ||
}, | ||
{ | ||
name: "Low-speed continuous", | ||
input: "Low-speed continuous", | ||
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeLowSpeedContinuous), | ||
}, | ||
{ | ||
name: "High-speed continuous", | ||
input: "High-speed continuous", | ||
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeHighSpeedContinuous), | ||
}, | ||
{ | ||
name: "Ultra-high-speed continuous", | ||
input: "Ultra-high-speed continuous", | ||
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeUltraHighSpeedContinuous), | ||
}, | ||
{ | ||
name: "2-sec. self-timer", | ||
input: "2-sec. self-timer", | ||
expOutput: ptrFilmAdvanceMode(FilmAdvanceMode2secSelfTimer), | ||
}, | ||
{ | ||
name: "10-sec. self-timer", | ||
input: "10-sec. self-timer", | ||
expOutput: ptrFilmAdvanceMode(FilmAdvanceMode10secSelfTimer), | ||
}, | ||
{ | ||
name: "Ultra-high-speed continuous with spaces", | ||
input: " Ultra-high-speed continuous ", | ||
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeUltraHighSpeedContinuous), | ||
}, | ||
{ | ||
name: "some random text", | ||
input: "aksjdfghq3", | ||
expError: errors.New("error parsing FilmAdvanceMode: unknown value `aksjdfghq3`"), | ||
}, | ||
{ | ||
name: "empty string", | ||
input: "", | ||
expError: errors.New("error parsing FilmAdvanceMode: unknown value ``"), | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
sm, err := FilmAdvanceModeFromString(tc.input) | ||
if tc.expError == nil { | ||
r.Equalf(tc.expOutput, sm, tc.name) | ||
r.NoErrorf(err, tc.name) | ||
} else { | ||
r.Errorf(err, tc.name) | ||
r.Equalf(tc.expError.Error(), err.Error(), tc.name) | ||
} | ||
} | ||
} | ||
|
||
func ptrFilmAdvanceMode(fam FilmAdvanceMode) *FilmAdvanceMode { | ||
if fam == "" { | ||
return nil | ||
} | ||
return &fam | ||
} |
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