Skip to content

Commit fe92fe1

Browse files
committed
feat: add tests for internal packages
1 parent a0c0f46 commit fe92fe1

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

internal/inputs/inputs.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,5 @@ func ExtractIntPairs(input string) ([]int, []int, error) {
4141
right = append(right, rightNum)
4242
}
4343

44-
// Check whether the number of elements in left and right slices are the same
45-
// This check is probably not needed
46-
if len(left) != len(right) {
47-
return nil, nil, fmt.Errorf("Length of left and right slices differ: %d, %d", len(left), len(right))
48-
}
49-
5044
return left, right, nil
5145
}

internal/inputs/inputs_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package inputs
2+
3+
import (
4+
"slices"
5+
"testing"
6+
)
7+
8+
func TestExtractIntPairs(t *testing.T) {
9+
t.Run("empty input", func(t *testing.T) {
10+
input := ``
11+
_, _, err := ExtractIntPairs(input)
12+
13+
if err == nil {
14+
t.Errorf("expected an error, didnt get one")
15+
}
16+
})
17+
18+
t.Run("invalid line", func(t *testing.T) {
19+
input := `123 456 789`
20+
_, _, err := ExtractIntPairs(input)
21+
22+
if err == nil {
23+
t.Errorf("expected an error, didnt get one")
24+
}
25+
})
26+
27+
t.Run("invalid left column", func(t *testing.T) {
28+
input := `abc 123`
29+
_, _, err := ExtractIntPairs(input)
30+
31+
if err == nil {
32+
t.Errorf("expected an error, didnt get one")
33+
}
34+
})
35+
36+
t.Run("invalid right column", func(t *testing.T) {
37+
input := `123 abc`
38+
_, _, err := ExtractIntPairs(input)
39+
40+
if err == nil {
41+
t.Errorf("expected an error, didnt get one")
42+
}
43+
})
44+
45+
t.Run("test slice content", func(t *testing.T) {
46+
input := "111 222\n333 444"
47+
leftWant := []int{111, 333}
48+
rightWant := []int{222, 444}
49+
leftGot, rightGot, _ := ExtractIntPairs(input)
50+
51+
if !slices.Equal(leftGot, leftWant) || !slices.Equal(rightGot, rightWant) {
52+
t.Errorf("Expected %v, %v, got %v, %v", leftWant, leftGot, rightWant, rightGot)
53+
}
54+
})
55+
}

internal/math/math_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package math
2+
3+
import "testing"
4+
5+
func TestAbs(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
input int
9+
want int
10+
}{
11+
{name: "positive number", input: 7, want: 7},
12+
{name: "negative number", input: -5, want: 5},
13+
{name: "zero", input: 0, want: 0},
14+
{name: "minus zero", input: -0, want: 0},
15+
}
16+
17+
for _, tt := range tests {
18+
t.Run(tt.name, func(t *testing.T) {
19+
got := Abs(tt.input)
20+
if got != tt.want {
21+
t.Errorf("got %d, want %d", got, tt.want)
22+
}
23+
})
24+
}
25+
}

0 commit comments

Comments
 (0)