Skip to content

Commit

Permalink
Move common test functions to common_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ecc1 committed Sep 20, 2018
1 parent fbb7d23 commit bea5ae3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 78 deletions.
84 changes: 84 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package dexcom

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"time"
)

// Force timezone to match test data.
func init() {
os.Setenv("TZ", "America/New_York")
}

func readBytes(r io.Reader) ([]byte, error) {
var data []byte
for {
var b byte
n, err := fmt.Fscanf(r, "%02x", &b)
if n == 0 {
break
}
if err != nil {
return data, err
}
data = append(data, b)
}
return data, nil
}

func parseTime(s string) time.Time {
t, err := time.ParseInLocation(UserTimeLayout, s, time.Local)
if err != nil {
panic(err)
}
return t
}

func compareJSON(data interface{}, jsonFile string) (bool, string) {
// Write data in JSON format to temporary file.
tmpfile, err := ioutil.TempFile("", "json")
if err != nil {
return false, err.Error()
}
defer func() { _ = os.Remove(tmpfile.Name()) }()
e := json.NewEncoder(tmpfile)
e.SetIndent("", " ")
err = e.Encode(data)
_ = tmpfile.Close()
if err != nil {
return false, err.Error()
}
// Write JSON in canonical form for comparison.
canon1 := canonicalJSON(jsonFile)
canon2 := canonicalJSON(tmpfile.Name())
// Find differences.
cmd := exec.Command("diff", "-u", "--label", jsonFile, "--label", "decoded", canon1, canon2)
diffs, err := cmd.Output()
_ = os.Remove(canon1)
_ = os.Remove(canon2)
return err == nil, string(diffs)
}

// canonicalJSON reads the given file and creates a temporary file
// containing equivalent JSON in canonical form
// (using the "jq" command, which must be on the user's PATH).
// It returns the temporary file name; it is the caller's responsibility
// to remove it when done.
func canonicalJSON(file string) string {
canon, err := exec.Command("jq", "-S", ".", file).Output()
if err != nil {
panic(err)
}
tmpfile, err := ioutil.TempFile("", "json")
if err != nil {
panic(err)
}
_, _ = tmpfile.Write(canon)
_ = tmpfile.Close()
return tmpfile.Name()
}
64 changes: 0 additions & 64 deletions page_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package dexcom

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"testing"
)

Expand Down Expand Up @@ -83,63 +79,3 @@ func checkRecords(t *testing.T, decoded Records, jsonFile string) {
t.Errorf("JSON is different:\n%s\n", msg)
}
}

func readBytes(r io.Reader) ([]byte, error) {
var data []byte
for {
var b byte
n, err := fmt.Fscanf(r, "%02x", &b)
if n == 0 {
break
}
if err != nil {
return data, err
}
data = append(data, b)
}
return data, nil
}

func compareJSON(data interface{}, jsonFile string) (bool, string) {
// Write data in JSON format to temporary file.
tmpfile, err := ioutil.TempFile("", "json")
if err != nil {
return false, err.Error()
}
defer func() { _ = os.Remove(tmpfile.Name()) }()
e := json.NewEncoder(tmpfile)
e.SetIndent("", " ")
err = e.Encode(data)
_ = tmpfile.Close()
if err != nil {
return false, err.Error()
}
// Write JSON in canonical form for comparison.
canon1 := canonicalJSON(jsonFile)
canon2 := canonicalJSON(tmpfile.Name())
// Find differences.
cmd := exec.Command("diff", "-u", "--label", jsonFile, "--label", "decoded", canon1, canon2)
diffs, err := cmd.Output()
_ = os.Remove(canon1)
_ = os.Remove(canon2)
return err == nil, string(diffs)
}

// canonicalJSON reads the given file and creates a temporary file
// containing equivalent JSON in canonical form
// (using the "jq" command, which must be on the user's PATH).
// It returns the temporary file name; it is the caller's responsibility
// to remove it when done.
func canonicalJSON(file string) string {
canon, err := exec.Command("jq", "-S", ".", file).Output()
if err != nil {
panic(err)
}
tmpfile, err := ioutil.TempFile("", "json")
if err != nil {
panic(err)
}
_, _ = tmpfile.Write(canon)
_ = tmpfile.Close()
return tmpfile.Name()
}
14 changes: 0 additions & 14 deletions time_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package dexcom

import (
"os"
"testing"
"time"
)

// Force timezone to match test data.
func init() {
os.Setenv("TZ", "America/New_York")
}

func TestTime(t *testing.T) {
cases := []struct {
n int64
Expand All @@ -35,11 +29,3 @@ func TestTime(t *testing.T) {
})
}
}

func parseTime(s string) time.Time {
t, err := time.ParseInLocation(UserTimeLayout, s, time.Local)
if err != nil {
panic(err)
}
return t
}

0 comments on commit bea5ae3

Please sign in to comment.