Skip to content

Commit

Permalink
fixed history for JS builds
Browse files Browse the repository at this point in the history
  • Loading branch information
d4l3k committed Nov 22, 2018
1 parent 3e3af67 commit d5affaa
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 54 deletions.
16 changes: 8 additions & 8 deletions pry/io_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ var readFile = ioutil.ReadFile

var historyFile = ".go-pry_history"

type IOHistory struct {
type ioHistory struct {
FileName string
FilePath string
Records []string
}

// NewHistory constructs IOHistory instance
func NewHistory() (*IOHistory, error) {
h := IOHistory{}
// NewHistory constructs ioHistory instance
func NewHistory() (*ioHistory, error) {
h := ioHistory{}
h.FileName = historyFile

dir, err := homedir.Dir()
Expand All @@ -38,7 +38,7 @@ func NewHistory() (*IOHistory, error) {
}

// Load unmarshal history file into history's records
func (h *IOHistory) Load() error {
func (h *ioHistory) Load() error {
body, err := ioutil.ReadFile(h.FilePath)
if err != nil {
return errors.Wrapf(err, "History file not found")
Expand All @@ -53,7 +53,7 @@ func (h *IOHistory) Load() error {
}

// Save saves marshaled history's records into file
func (h IOHistory) Save() error {
func (h ioHistory) Save() error {
body, err := json.Marshal(h.Records)
if err != nil {
return errors.Wrapf(err, "error marshaling history")
Expand All @@ -66,9 +66,9 @@ func (h IOHistory) Save() error {
}

// Len returns amount of records in history
func (h IOHistory) Len() int { return len(h.Records) }
func (h ioHistory) Len() int { return len(h.Records) }

// Add appends record into history's records
func (h *IOHistory) Add(record string) {
func (h *ioHistory) Add(record string) {
h.Records = append(h.Records, record)
}
48 changes: 48 additions & 0 deletions pry/io_default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// +build !js

package pry

import (
"fmt"
"math/rand"
"os"
"path/filepath"
"reflect"
"testing"
"time"
)

func TestHistory(t *testing.T) {
t.Parallel()

rand.Seed(time.Now().UnixNano())

history := &ioHistory{}
history.FileName = ".go-pry_history_test"
history.FilePath = filepath.Join(os.TempDir(), history.FileName)

expected := []string{
"test",
fmt.Sprintf("rand: %d", rand.Int63()),
}
history.Add(expected[0])
history.Add(expected[1])

if err := history.Save(); err != nil {
t.Error("Failed to save history")
}

if err := history.Load(); err != nil {
t.Error("Failed to load history")
}

if !reflect.DeepEqual(expected, history.Records) {
t.Errorf("history.Load() = %+v; expected %+v", history.Records, expected)
}

// delete test history file
err := os.Remove(history.FilePath)
if err != nil {
t.Error(err)
}
}
16 changes: 8 additions & 8 deletions pry/io_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ func readFile(path string) ([]byte, error) {
return ioutil.ReadAll(r)
}

type BrowserHistory struct {
type browserHistory struct {
Records []string
}

// NewHistory constructs BrowserHistory instance
func NewHistory() (*BrowserHistory, error) {
// NewHistory constructs browserHistory instance
func NewHistory() (*browserHistory, error) {

// FIXME:
// when localStorage is full, can be return an error

return &BrowserHistory{}, nil
return &browserHistory{}, nil
}

// Load unmarshal localStorage data into history's records
func (bh *BrowserHistory) Load() error {
func (bh *browserHistory) Load() error {
hist := js.Global().Get("localStorage").Get("history")
if hist.Type() == js.TypeUndefined {
return nil // nothing to unmarashal
Expand All @@ -60,7 +60,7 @@ func (bh *BrowserHistory) Load() error {
}

// Save saves marshaled history's records into localStorage
func (bh BrowserHistory) Save() error {
func (bh browserHistory) Save() error {
bytes, err := json.Marshal(bh.Records)
if err != nil {
return err
Expand All @@ -71,9 +71,9 @@ func (bh BrowserHistory) Save() error {
}

// Len returns amount of records in history
func (bh BrowserHistory) Len() int { return len(bh.Records) }
func (bh browserHistory) Len() int { return len(bh.Records) }

// Add appends record into history's records
func (bh *BrowserHistory) Add(record string) {
func (bh *browserHistory) Add(record string) {
bh.Records = append(bh.Records, record)
}
38 changes: 0 additions & 38 deletions pry/pry_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package pry

import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path"
"path/filepath"
"reflect"
"testing"
"time"
Expand All @@ -17,41 +14,6 @@ import (
"github.com/pkg/errors"
)

func TestHistory(t *testing.T) {
t.Parallel()

rand.Seed(time.Now().UnixNano())

history := &IOHistory{}
history.FileName = ".go-pry_history_test"
history.FilePath = filepath.Join(os.TempDir(), history.FileName)

expected := []string{
"test",
fmt.Sprintf("rand: %d", rand.Int63()),
}
history.Add(expected[0])
history.Add(expected[1])

if err := history.Save(); err != nil {
t.Error("Failed to save history")
}

if err := history.Load(); err != nil {
t.Error("Failed to load history")
}

if !reflect.DeepEqual(expected, history.Records) {
t.Errorf("history.Load() = %+v; expected %+v", history.Records, expected)
}

// delete test history file
err := os.Remove(history.FilePath)
if err != nil {
t.Error(err)
}
}

func TestCLIBasicStatement(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit d5affaa

Please sign in to comment.