Unreviewed changes
1 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:
```
The name of the file: internal/frontend/templates/templates_test.go
Insertions: 9, Deletions: 9.
@@ -24,26 +24,26 @@
}
}
-func TestMakeBoxes(t *testing.T) {
+func TestScoreBoxClasses(t *testing.T) {
for _, test := range []struct {
score, maxScore int
want []string
}{
- {score: 2, maxScore: 5, want: []string{"score-box-marked", "score-box-marked", "score-box-unmarked", "score-box-unmarked", "score-box-unmarked"}},
- {score: 0, maxScore: 3, want: []string{"score-box-unmarked", "score-box-unmarked", "score-box-unmarked"}},
- {score: 3, maxScore: 3, want: []string{"score-box-marked", "score-box-marked", "score-box-marked"}},
- {score: 5, maxScore: 3, want: []string{"score-box-marked", "score-box-marked", "score-box-marked"}},
- {score: -1, maxScore: 3, want: []string{"score-box-unmarked", "score-box-unmarked", "score-box-unmarked"}},
+ {score: 2, maxScore: 5, want: []string{scoreBoxMarked, scoreBoxMarked, scoreBoxUnmarked, scoreBoxUnmarked, scoreBoxUnmarked}},
+ {score: 0, maxScore: 3, want: []string{scoreBoxUnmarked, scoreBoxUnmarked, scoreBoxUnmarked}},
+ {score: 3, maxScore: 3, want: []string{scoreBoxMarked, scoreBoxMarked, scoreBoxMarked}},
+ {score: 5, maxScore: 3, want: []string{scoreBoxMarked, scoreBoxMarked, scoreBoxMarked}},
+ {score: -1, maxScore: 3, want: []string{scoreBoxUnmarked, scoreBoxUnmarked, scoreBoxUnmarked}},
{score: 2, maxScore: 0, want: nil},
} {
- got := makeBoxes(test.score, test.maxScore)
+ got := scoreBoxClasses(test.score, test.maxScore)
if len(got) != len(test.want) {
- t.Errorf("makeBoxes(%d, %d) len = %d, want %d", test.score, test.maxScore, len(got), len(test.want))
+ t.Errorf("scoreBoxClasses(%d, %d) len = %d, want %d", test.score, test.maxScore, len(got), len(test.want))
continue
}
for i := range got {
if got[i] != test.want[i] {
- t.Errorf("makeBoxes(%d, %d)[%d] = %q, want %q", test.score, test.maxScore, i, got[i], test.want[i])
+ t.Errorf("scoreBoxClasses(%d, %d)[%d] = %q, want %q", test.score, test.maxScore, i, got[i], test.want[i])
}
}
}
```
```
The name of the file: internal/frontend/templates/templates.go
Insertions: 19, Deletions: 17.
@@ -27,29 +27,31 @@
"commaseparate": func(s []string) string {
return strings.Join(s, ", ")
},
- "stripscheme": stripScheme,
- "capitalize": cases.Title(language.Und).String,
- "queryescape": url.QueryEscape,
- "makeBoxes": makeBoxes,
+ "stripscheme": stripScheme,
+ "capitalize": cases.Title(language.Und).String,
+ "queryescape": url.QueryEscape,
+ "scoreBoxClasses": scoreBoxClasses,
}
-func makeBoxes(score, maxScore int) []string {
+const (
+ scoreBoxMarked = "score-box-marked"
+ scoreBoxUnmarked = "score-box-unmarked"
+)
+
+// scoreBoxClasses returns a list of maxScore CSS classes
+// for evaluation score boxes. The first score boxes
+// will have a different class from the rest.
+func scoreBoxClasses(score, maxScore int) []string {
if maxScore <= 0 {
return nil
}
- if score > maxScore {
- score = maxScore
+ score = min(maxScore, max(0, score))
+ var classes []string
+ for range score {
+ classes = append(classes, scoreBoxMarked)
}
- if score < 0 {
- score = 0
- }
- classes := make([]string, maxScore)
- for i := range maxScore {
- if i < score {
- classes[i] = "score-box-marked"
- } else {
- classes[i] = "score-box-unmarked"
- }
+ for range maxScore - score {
+ classes = append(classes, scoreBoxUnmarked)
}
return classes
}
```