Skip to content

Commit

Permalink
feat add coverage test
Browse files Browse the repository at this point in the history
  • Loading branch information
ThreeDP committed Feb 23, 2024
1 parent 99b5ecf commit 7f13478
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ jobs:
- name: Test
run: make t

- name: Run coverage
run: go test -race -coverprofile=coverage.txt -covermode=atomic
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4-beta
env:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
server.tmp
*.tmp
*.txt
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ all:
# Run tests
t: $(TEST_PATH)

# $(TEST_PATH):
# @echo "[================ RUN TEST $@ ================]"
# @cd $@ && go test
# @echo

$(TEST_PATH):
@echo "[================ RUN TEST $@ ================]"
@cd $@ && go test
@cd $@ && go test -race -coverprofile=$(subst app/,,$@).txt -covermode=atomic
@echo


# $(TEST_PATH):
# @echo "[================ RUN TEST $@ ================]"
# @cd $@ && go test
Expand Down
3 changes: 1 addition & 2 deletions app/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ type Info struct {
}

func (e *Info) mapToBulkString(m map[string]string, section string) string {
// str := fmt.Sprintf("# %s\n\n", section)
str := ""
str := fmt.Sprintf("## %s\n\n", section)
for k, v := range m {
str += fmt.Sprintf("%s:%s\n", k, v)
}
Expand Down
25 changes: 21 additions & 4 deletions app/builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"net"
"time"
"strings"
)

type TTime struct {}
Expand All @@ -24,7 +25,7 @@ func TestInfoBuiltin(t *testing.T) {
}
info := Info{Conn: s.Conn, Infos: i}
params := []string{}
copy(s.Expected, "$26\r\n# Replication\n\nrole:master\r\n")
copy(s.Expected, "$27\r\n## Replication\n\nrole:master\r\n")

info.Cmd(params)

Expand All @@ -40,7 +41,7 @@ func TestInfoBuiltin(t *testing.T) {
}
info := Info{Conn: s.Conn, Infos: i}
params := []string{"RepLication"}
copy(s.Expected, "$25\r\n# Replication\n\nrole:slave\r\n")
copy(s.Expected, "$26\r\n## Replication\n\nrole:slave\r\n")

info.Cmd(params)

Expand All @@ -58,11 +59,17 @@ func TestInfoBuiltin(t *testing.T) {
}
info := Info{Conn: s.Conn, Infos: i}
params := []string{"RepLication"}
copy(s.Expected, "$101\r\n# Replication\n\nrole:slave\nmaster_replid:8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb\nmaster_repl_offset:0\r\n")
expected := []string {
"$102\r\n",
"## Replication\n\n",
"master_repl_offset:0",
"role:slave",
"master_replid:8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb",
}

info.Cmd(params)

compareStrings(t, s.Expected, s.Out)
compareSubStringsInString(t, expected, s.Out)
s.reset()
})
}
Expand Down Expand Up @@ -251,6 +258,16 @@ func compareStrings(t *testing.T, expected, received []byte) {
}
}

func compareSubStringsInString(t *testing.T, expected []string, received []byte) {
t.Helper()

for _, str := range expected {
if !strings.Contains(string(received), str) {
t.Errorf("expected value: '%s' not found in '%s'\n", str, received)
}
}
}

/*
Test Conn struct Mock
*/
Expand Down

0 comments on commit 7f13478

Please sign in to comment.