Skip to content

Commit

Permalink
test: test bullet
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Nov 21, 2023
1 parent 6004a47 commit d71d560
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
16 changes: 10 additions & 6 deletions list/enumerations.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,9 @@ const (
)

func (l *List) bullet(index int) string {
// out of bounds?
if index < 0 || index >= len(l.items) {
return ""
}

switch l.enumeration {
case Alphabet:
return fmt.Sprintf("%c. ", 'A'+index)
return fmt.Sprintf("%s. ", alphabet(index))
case Arabic:
return fmt.Sprintf("%d. ", index+1)
case Roman:
Expand All @@ -69,6 +64,15 @@ func (l *List) bullet(index int) string {
return ""
}

const alphabetLength = 26

func alphabet(index int) string {
if index >= alphabetLength {
return fmt.Sprintf("%c%c", 'A'+index/alphabetLength-1, 'A'+index%alphabetLength)
}
return fmt.Sprintf("%c", 'A'+index%alphabetLength)
}

func roman(n int) string {
var (
roman = []string{"M", "CM", "D", "CD", "C", "XC", "L", "XL",
Expand Down
29 changes: 29 additions & 0 deletions list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,32 @@ III. Baz
}
}
}

func TestBullet(t *testing.T) {
tests := []struct {
enum Enumeration
i int
exp string
}{
{Alphabet, 0, "A. "},
{Alphabet, 25, "Z. "},
{Alphabet, 26, "AA. "},
{Alphabet, 50, "AY. "},
{Alphabet, 100, "CW. "},
{Alphabet, 701, "ZZ. "},
{Roman, 0, "I. "},
{Roman, 25, "XXVI. "},
{Roman, 26, "XXVII. "},
{Roman, 50, "LI. "},
{Roman, 100, "CI. "},
{Roman, 701, "DCCII. "},
{Roman, 1000, "MI. "},
}

for _, test := range tests {
bullet := New().Enumeration(test.enum).bullet(test.i)
if bullet != test.exp {
t.Errorf("expected: %s, got: %s\n", test.exp, bullet)
}
}
}

0 comments on commit d71d560

Please sign in to comment.