Skip to content

Commit

Permalink
feat(challenge1): Add functions for counting bytes, lines, words, and…
Browse files Browse the repository at this point in the history
… characters
  • Loading branch information
salma2vec committed Mar 4, 2024
1 parent 3855cd3 commit 719fe49
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions challenge1/stats.go
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)
}
})
}
}

0 comments on commit 719fe49

Please sign in to comment.