This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also cleaned up and commented some code. Year is a little more tricky since they can be invalid when in the past.
- Loading branch information
Showing
12 changed files
with
299 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Common test helpers | ||
package flags | ||
|
||
func equal(a []int, b []int) bool { | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
for i := range a { | ||
if a[i] != b[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
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
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,42 @@ | ||
package flags | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type yearlist struct { | ||
list []int | ||
} | ||
|
||
func (l *yearlist) String() string { | ||
s := make([]string, len(l.list)) | ||
for i := range l.list { | ||
s[i] = strconv.Itoa(l.list[i]) | ||
} | ||
return strings.Join(s, ",") | ||
} | ||
|
||
func (l *yearlist) Set(s string) error { | ||
parts := strings.Split(s, ",") | ||
for i, p := range parts { | ||
x, err := strconv.Atoi(p) | ||
if err != nil { | ||
return fmt.Errorf("no integer at index %d: %s", i, p) | ||
} | ||
l.list = append(l.list, x) | ||
} | ||
return nil | ||
} | ||
|
||
// Yearlist defines a flag for a comma-separated list of integers. | ||
// Call the returned function after flag.Parse to get the value. | ||
func Yearlist(name string) func() []int { | ||
l := &yearlist{} | ||
flag.Var(l, name, "list of years") | ||
return func() []int { | ||
return l.list | ||
} | ||
} |
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,44 @@ | ||
package flags | ||
|
||
import "testing" | ||
|
||
func TestYearlist(t *testing.T) { | ||
tests := []struct { | ||
text string | ||
parsed []int | ||
invalid bool | ||
}{ | ||
{text: "0,2000", parsed: []int{0, 2000}}, | ||
{text: "2017", parsed: []int{2017}}, | ||
{text: "1999,57", parsed: []int{1999, 57}}, | ||
{text: "-2000,2000", parsed: []int{-2000, 2000}}, | ||
{text: "", invalid: true}, | ||
{text: "-1, 2", invalid: true}, | ||
{text: "hello", invalid: true}, | ||
} | ||
|
||
for i, tt := range tests { | ||
l := yearlist{} | ||
if err := l.Set(tt.text); err != nil { | ||
if !tt.invalid { | ||
t.Errorf("parsing %s failed unexpectedly: %v", tt.text, err) | ||
} | ||
continue | ||
} | ||
if tt.invalid { | ||
t.Errorf("parsing %s should have failed", tt.text) | ||
continue | ||
} | ||
if !equal(l.list, tt.parsed) { | ||
t.Errorf(` | ||
%d. | ||
Input: %s | ||
Expected: %v | ||
Got %v`, i, tt.text, tt.parsed, l.list) | ||
} | ||
} | ||
|
||
if (&yearlist{}).String() != "" { | ||
t.Errorf("Non empty String() output: %s", (&yearlist{}).String()) | ||
} | ||
} |
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
Oops, something went wrong.