-
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.
feat(challenge1): Add functions for counting bytes, lines, words, and…
… characters
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 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,33 @@ | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestCalculateStats(t *testing.T) { | ||
cases := []struct { | ||
Description string | ||
Input string | ||
Want stats | ||
}{ | ||
{"Empty", "", stats{bytes: 0, words: 0, lines: 0, chars: 0}}, | ||
{"Single char", "s", stats{bytes: 1, words: 1, lines: 0, chars: 1}}, | ||
{"Multibyte chars", "s⌘ f", stats{bytes: 6, words: 2, lines: 0, chars: 4}}, | ||
{"Trailing newline", "this is a sentence\n\nacross multiple\nlines\n", stats{bytes: 42, words: 7, lines: 4, chars: 42}}, | ||
{"No trailing newline", "this is a sentence\n\nacross multiple\nlines", stats{bytes: 41, words: 7, lines: 3, chars: 41}}, | ||
} | ||
|
||
for _, test := range cases { | ||
t.Run(test.Description, func(t *testing.T) { | ||
bufferedString := bufio.NewReader(strings.NewReader(test.Input)) | ||
got := calculateStats(bufferedString, true, true, true, true) | ||
|
||
if got != test.Want { | ||
t.Errorf("got %v, want %v", got, test.Want) | ||
} | ||
}) | ||
} | ||
} |