Skip to content

Commit

Permalink
Fixed filter logic & added unit tests for filters.
Browse files Browse the repository at this point in the history
The test helped my fix the filter problem. The problem was that I wasn't
cloning everything. Also, this is the first time I was actually able to
run the program! I have some TODOs now that the program finally runs:
- Remove input "3" from the help menu.
- Fix the new "Edit" bug and add a unit test for it.
- Writing to the file puts a bunch of new lines at the top. That's weird
  and should be fixed.
  • Loading branch information
alanxoc3 committed Sep 28, 2020
1 parent 4d35955 commit 991132e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
6 changes: 6 additions & 0 deletions internal/deck/deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func (d *Deck) FutureLen() int { return d.stack.FutureLen() }
// Clones a deck into this deck.
func (d *Deck) Clone(o *Deck) {
d.stack.Clone(o.stack)

d.cardMap = map[internal.Hash]*card.Card{}
for k, v := range o.cardMap {
d.cardMap[k] = v
}

d.cloneInfo(o)
}

Expand Down
51 changes: 42 additions & 9 deletions internal/deck/deck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,25 @@ func TestNewDeck(t *testing.T) {
assert.Len(t, d.PredictList(), 0)
}

func TestAddCardsTop(t *testing.T) {
func TestAddCardsLen(t *testing.T) {
d := deck.NewDeck(ONE_DATE)
c1, _ := card.NewCards(".", "hi : yo")
d.AddCards(c1...)
assert.Len(t, d.CardList(), 2)
}

func TestAddCardsTop(t *testing.T) {
d := deck.NewDeck(ONE_DATE)
c1, _ := card.NewCards(".", "hi : yo")
d.AddCards(c1...)
assert.Equal(t, c1[0], d.TopCard())
}

func TestAddCardsPredictSameNext(t *testing.T) {
d := deck.NewDeck(ONE_DATE)
c1, _ := card.NewCards(".", "hi : yo")
d.AddCards(c1...)
p := meta.NewPredictFromStrings(c1[0].Hash().String(), "2020-01-01T00:00:00Z")
p := meta.NewPredictFromStrings(c1[0].Hash().String(), "0001-01-01T00:00:00Z")
d.AddPredicts(p)
assert.Equal(t, 2, d.ReviewLen())
assert.Equal(t, 0, d.FutureLen())
Expand All @@ -56,11 +62,38 @@ func TestPredictList(t *testing.T) {
assert.Equal(t, []meta.Predict{*p}, d.PredictList())
}

// TODO! Do this!
func TestFilterMemorize(t *testing.T) {
d := deck.NewDeck(ONE_DATE)
h := internal.NewHash("fad")
p := meta.NewPredictFromStrings(h.String(), "2020-01-01T00:00:00Z")
d.AddPredicts(p)
assert.Equal(t, []meta.Predict{*p}, d.PredictList())
func TestRemove(t *testing.T) {
h1 := internal.NewHash("73a6a403534bcc11b662e3a1d90d31e1")
h2 := internal.NewHash("abad2c2e5be5c33bc319ce038e3f2108")
cards, _ := card.NewCards(".", "hi : yo")

t.Run("Memorize", func(t *testing.T) {
d := deck.NewDeck(ONE_DATE)
d.AddCards(cards...)
d.AddPredicts(meta.NewPredictFromStrings(h1.String()), meta.NewPredictFromStrings(h2.String(), "", "", "1"))
d.RemoveMemorize()

assert.Equal(t, h2, *d.TopHash())
assert.Len(t, d.CardList(), 1)
})

t.Run("Review", func(t *testing.T) {
d := deck.NewDeck(ONE_DATE)
d.AddCards(cards...)
d.AddPredicts(meta.NewPredictFromStrings(h1.String()), meta.NewPredictFromStrings(h2.String(), "", "", "1"))
d.RemoveReview()

assert.Equal(t, h1, *d.TopHash())
assert.Len(t, d.CardList(), 1)
})

t.Run("Done", func(t *testing.T) {
d := deck.NewDeck(ONE_DATE)
d.AddCards(cards...)
d.AddPredicts(meta.NewPredictFromStrings(h1.String()), meta.NewPredictFromStrings(h2.String(), "2020-01-01T00:00:00Z", "", "1"))
d.RemoveDone()

assert.Equal(t, h1, *d.TopHash())
assert.Len(t, d.CardList(), 1)
})
}
3 changes: 1 addition & 2 deletions internal/deck/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
type predicate func(int, internal.Hash) bool

func (d *Deck) cloneInfo(o *Deck) {
d.stack.SetTime(o.stack.Time())
d.predictMap = map[internal.Hash]*meta.Predict{}
for k, v := range o.predictMap {
d.predictMap[k] = v
Expand All @@ -25,7 +24,7 @@ func (d *Deck) cloneInfo(o *Deck) {

func (d *Deck) filter(p predicate) {
hashes := d.stack.List()
n := &Deck{}
n := NewDeck(d.stack.Time())
n.cloneInfo(d)

for i, h := range hashes {
Expand Down

0 comments on commit 991132e

Please sign in to comment.