Skip to content

Commit

Permalink
extract tests to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
metalim committed Sep 2, 2023
1 parent a78296d commit 6bcaa24
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/gotest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
go-version: "1.20"

- name: Test
run: go test -v ./test/ -race -coverprofile=coverage.txt -covermode=atomic
run: go test ./test -v -race -coverpkg=.,./simplemap -coverprofile=coverage.txt -covermode=atomic

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
Expand All @@ -37,4 +37,4 @@ jobs:
go-version: "1.20"

- name: Benchmark
run: go test ./test/ -bench . -benchmem
run: go test ./test -bench=. -benchmem
2 changes: 1 addition & 1 deletion test/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jsonmap_test
package test_test

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion test/interface_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jsonmap_test
package test_test

import (
"github.com/metalim/jsonmap"
Expand Down
21 changes: 11 additions & 10 deletions test/json_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jsonmap_test
package test_test

import (
"encoding/json"
Expand Down Expand Up @@ -29,21 +29,22 @@ type IMapShort interface {
Keys() []Key
}

func TestFastMap(t *testing.T) {
testMap(t, jsonmap.New)
func TestSerialization(t *testing.T) {
t.Run("JSONMap", func(t *testing.T) {
testSerialization(t, jsonmap.New)
})
t.Run("SimpleMap", func(t *testing.T) {
testSerialization(t, simplemap.New)
})
}

func TestSimpleMap(t *testing.T) {
testMap(t, simplemap.New)
}

func testMap[T IMapShort](t *testing.T, newMap func() T) {
func testSerialization[T IMapShort](t *testing.T, newMap func() T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// due to random nature of Go map iteration, we need to test it many times
for i := 0; i < TEST_ITERATIONS; i++ {
m := newMap()
testSerialization(t, test.json, m)
testSerializationOnce(t, test.json, m)
if test.name == "PlainJSON" {
verifyPlainJSON(t, m)
}
Expand All @@ -52,7 +53,7 @@ func testMap[T IMapShort](t *testing.T, newMap func() T) {
}
}

func testSerialization(t *testing.T, testData string, m IMapShort) {
func testSerializationOnce(t *testing.T, testData string, m IMapShort) {
err := json.Unmarshal([]byte(testData), &m)
assert.Equal(t, err, nil)

Expand Down
8 changes: 4 additions & 4 deletions test/jsonmap_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jsonmap_test
package test_test

import (
"testing"
Expand All @@ -12,11 +12,11 @@ const TEST_ITERATIONS = 100

func TestJSONMap(t *testing.T) {
for i := 0; i < TEST_ITERATIONS; i++ {
testJSONMap(t, i)
testJSONMapOnce(t, i)
}
}

func testJSONMap(t *testing.T, i int) {
func testJSONMapOnce(t *testing.T, i int) {
m := jsonmap.New()
m.Set("a", 1)
m.Set("d", "2")
Expand All @@ -29,7 +29,7 @@ func testJSONMap(t *testing.T, i int) {
m.Delete("e") // test for index bug: delete "e" and not "b"
assert.Equal(t, m.Len(), 3)

// keys
// keys in insertion order
assert.Equal(t, len(m.Keys()), 3)
assert.Equal(t, m.Keys()[0], "a")
assert.Equal(t, m.Keys()[1], "d")
Expand Down

0 comments on commit 6bcaa24

Please sign in to comment.