go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

menu_test.go (1377B)


      1 package render
      2 
      3 import (
      4 	"context"
      5 	"testing"
      6 )
      7 
      8 func TestMenuInit(t *testing.T) {
      9 	m := NewMenu()
     10 	err := m.Put("1", "foo")
     11 	if err != nil {
     12 		t.Fatal(err)
     13 	}
     14 	err = m.Put("2", "bar")
     15 	if err != nil {
     16 		t.Fatal(err)
     17 	}
     18 
     19 	ctx := context.TODO()
     20 	r, err := m.Render(ctx, 0)
     21 	if err != nil {
     22 		t.Fatal(err)
     23 	}
     24 	expect := `1:foo
     25 2:bar`
     26 	if r != expect {
     27 		t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect, r)
     28 	}
     29 
     30 	r, err = m.Render(ctx, 1)
     31 	if err == nil {
     32 		t.Fatalf("expected render fail")
     33 	}
     34 
     35 }
     36 
     37 func TestMenuBrowse(t *testing.T) {
     38 	cfg := DefaultBrowseConfig()
     39 	m := NewMenu().WithPageCount(3).WithBrowseConfig(cfg)
     40 	err := m.Put("1", "foo")
     41 	if err != nil {
     42 		t.Fatal(err)
     43 	}
     44 	err = m.Put("2", "bar")
     45 	if err != nil {
     46 		t.Fatal(err)
     47 	}
     48 
     49 	ctx := context.TODO()
     50 	r, err := m.Render(ctx, 0)
     51 	if err != nil {
     52 		t.Fatal(err)
     53 	}
     54 	expect := `1:foo
     55 2:bar
     56 11:next`
     57 	if r != expect {
     58 		t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect, r)
     59 	}
     60 
     61 	r, err = m.Render(ctx, 1)
     62 	if err != nil {
     63 		t.Fatal(err)
     64 	}
     65 	expect = `1:foo
     66 2:bar
     67 11:next
     68 22:previous`
     69 	if r != expect {
     70 		t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect, r)
     71 	}
     72 
     73 	r, err = m.Render(ctx, 2)
     74 	if err != nil {
     75 		t.Fatal(err)
     76 	}
     77 	expect = `1:foo
     78 2:bar
     79 22:previous`
     80 	if r != expect {
     81 		t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect, r)
     82 	}
     83 
     84 	_, err = m.Render(ctx, 3)
     85 	if err == nil {
     86 		t.Fatalf("expected render fail")
     87 	}
     88 }