Skip to content

Commit

Permalink
Add tests for AdaptiveJson
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Aug 2, 2022
1 parent eba4360 commit 09c2705
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 4 deletions.
8 changes: 4 additions & 4 deletions twitter/adaptive_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ func (j *AdaptiveJson) FindCursor() (string, error) {
if i.ReplaceEntry.EntryIdToReplace == "sq-cursor-bottom" {
return i.ReplaceEntry.Entry.Content.Operation.Cursor.Value, nil
}
}

for _, e := range j.Timeline.Instructions[0].AddEntries.Entries {
if e.EntryId == "sq-cursor-bottom" {
return e.Content.Operation.Cursor.Value, nil
for _, e := range i.AddEntries.Entries {
if e.EntryId == "sq-cursor-bottom" {
return e.Content.Operation.Cursor.Value, nil
}
}
}

Expand Down
127 changes: 127 additions & 0 deletions twitter/adaptive_json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright 2022 Akiomi Kamakura
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package twitter

import (
"testing"
)

func TestFindCursorWhenReplaceEntryExists(t *testing.T) {
j := AdaptiveJson{
Timeline: Timeline{
Instructions: []Instruction{
Instruction{
ReplaceEntry: ReplaceEntry{
EntryIdToReplace: "sq-cursor-top",
Entry: Entry{
EntryId: "sq-cursor-top",
Content: Content{
Operation: Operation{
Cursor: Cursor{
Value: "refresh:foobar",
},
},
},
},
},
},
Instruction{
ReplaceEntry: ReplaceEntry{
EntryIdToReplace: "sq-cursor-bottom",
Entry: Entry{
EntryId: "sq-cursor-bottom",
Content: Content{
Operation: Operation{
Cursor: Cursor{
Value: "scroll:foobar",
},
},
},
},
},
},
},
},
}

expected := "scroll:foobar"
actual, err := j.FindCursor()
if err != nil {
t.Errorf("Expect AdaptiveJson#FindCursor() not to return error object, but got \"%v\"", err)
return
}

if actual != expected {
t.Errorf("Expect AdaptiveJson#FindCursor() = \"%s\", but got \"%s\"", expected, actual)
return
}
}

func TestFindCursorWhenAddEntriesExist(t *testing.T) {
j := AdaptiveJson{
Timeline: Timeline{
Instructions: []Instruction{
Instruction{
AddEntries: AddEntries{
Entries: []Entry{
Entry{
EntryId: "sq-cursor-top",
Content: Content{
Operation: Operation{
Cursor: Cursor{
Value: "refresh:foobar",
},
},
},
},
Entry{
EntryId: "sq-cursor-bottom",
Content: Content{
Operation: Operation{
Cursor: Cursor{
Value: "scroll:foobar",
},
},
},
},
},
},
},
},
},
}

expected := "scroll:foobar"
actual, err := j.FindCursor()
if err != nil {
t.Errorf("Expect AdaptiveJson#FindCursor() not to return error object, but got \"%v\"", err)
return
}

if actual != expected {
t.Errorf("Expect AdaptiveJson#FindCursor() = \"%s\", but got \"%s\"", expected, actual)
return
}
}

func TestFindCursorWhenNoCursorFound(t *testing.T) {
j := AdaptiveJson{}

_, err := j.FindCursor()
if err == nil {
t.Errorf("Expect AdaptiveJson#FindCursor() to return error object, but got nil")
return
}
}

0 comments on commit 09c2705

Please sign in to comment.