page_test.go (2538B)
1 package render 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "git.defalsify.org/vise.git/cache" 9 "git.defalsify.org/vise.git/internal/resourcetest" 10 ) 11 12 13 func TestPageCurrentSize(t *testing.T) { 14 t.Skip("usage is not in use, and it is unclear how it should be calculated") 15 ca := cache.NewCache() 16 pg := NewPage(ca, nil) 17 err := ca.Push() 18 if err != nil { 19 t.Error(err) 20 } 21 err = ca.Add("foo", "inky", 0) 22 if err != nil { 23 t.Error(err) 24 } 25 err = ca.Push() 26 pg.Reset() 27 err = ca.Add("bar", "pinky", 10) 28 if err != nil { 29 t.Error(err) 30 } 31 err = ca.Add("baz", "tinkywinkydipsylalapoo", 51) 32 if err != nil { 33 t.Error(err) 34 } 35 err = pg.Map("foo") 36 if err != nil { 37 t.Error(err) 38 } 39 err = pg.Map("bar") 40 if err != nil { 41 t.Error(err) 42 } 43 err = pg.Map("baz") 44 if err != nil { 45 t.Error(err) 46 } 47 l, c, err := pg.Usage() 48 if err != nil { 49 t.Error(err) 50 } 51 if l != 27 { 52 t.Errorf("expected actual length 27, got %v", l) 53 } 54 if c != 34 { 55 t.Errorf("expected remaining length 34, got %v", c) 56 } 57 58 mn := NewMenu() //.WithOutputSize(32) 59 pg = pg.WithMenu(mn) 60 l, c, err = pg.Usage() 61 if err != nil { 62 t.Error(err) 63 } 64 if l != 59 { 65 t.Errorf("expected actual length 59, got %v", l) 66 } 67 if c != 2 { 68 t.Errorf("expected remaining length 2, got %v", c) 69 } 70 } 71 72 func TestStateMapSink(t *testing.T) { 73 ca := cache.NewCache() 74 pg := NewPage(ca, nil) 75 ca.Push() 76 err := ca.Add("foo", "bar", 0) 77 if err != nil { 78 t.Error(err) 79 } 80 ca.Push() 81 pg.Reset() 82 err = ca.Add("bar", "xyzzy", 6) 83 if err != nil { 84 t.Error(err) 85 } 86 err = ca.Add("baz", "bazbaz", 18) 87 if err != nil { 88 t.Error(err) 89 } 90 err = ca.Add("xyzzy", "plugh", 0) 91 if err != nil { 92 t.Error(err) 93 } 94 err = pg.Map("foo") 95 if err != nil { 96 t.Error(err) 97 } 98 err = pg.Map("xyzzy") 99 if err == nil { 100 t.Errorf("Expected fail on duplicate sink") 101 } 102 err = pg.Map("baz") 103 if err != nil { 104 t.Error(err) 105 } 106 ca.Push() 107 pg.Reset() 108 err = pg.Map("foo") 109 if err != nil { 110 t.Error(err) 111 } 112 ca.Pop() 113 pg.Reset() 114 err = pg.Map("foo") 115 if err != nil { 116 t.Error(err) 117 } 118 } 119 120 func TestWithError(t *testing.T) { 121 ctx := context.Background() 122 ca := cache.NewCache() 123 rs := resourcetest.NewTestResource() 124 rs.AddTemplate(ctx, "foo", "bar") 125 rs.Lock() 126 pg := NewPage(ca, rs) 127 ca.Push() 128 129 mn := NewMenu() 130 err := mn.Put("0", "aiee") 131 if err != nil { 132 t.Fatal(err) 133 } 134 err = fmt.Errorf("my humps") 135 pg = pg.WithMenu(mn).WithError(err) 136 137 r, err := pg.Render(ctx, "foo", 0) 138 if err != nil { 139 t.Fatal(err) 140 } 141 expect := `my humps 142 bar 143 0:aiee` 144 if r != expect { 145 t.Fatalf("expected:\n\t%s\ngot:\n\t%s", expect, r) 146 } 147 } 148