go-vise

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

size_test.go (8659B)


      1 package render
      2 
      3 import (
      4 	"context"
      5 	"fmt"
      6 	"math/rand"
      7 	"testing"
      8 
      9 	"git.defalsify.org/vise.git/state"
     10 	"git.defalsify.org/vise.git/resource"
     11 	"git.defalsify.org/vise.git/internal/resourcetest"
     12 	"git.defalsify.org/vise.git/cache"
     13 )
     14 
     15 type testSizeResource struct {
     16 	*resourcetest.TestResource
     17 }
     18 
     19 func newTestSizeResource() *testSizeResource {
     20 	ctx := context.Background()
     21 	rs := resourcetest.NewTestResource()
     22 	tr := &testSizeResource{
     23 		TestResource: rs,
     24 	}
     25 	rs.AddTemplate(ctx, "small", "one {{.foo}} two {{.bar}} three {{.baz}}")
     26 	rs.AddTemplate(ctx, "toobug", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in mattis lorem. Aliquam erat volutpat. Ut vitae metus.")
     27 	rs.AddTemplate(ctx, "pages", "one {{.foo}} two {{.bar}} three {{.baz}}\n{{.xyzzy}}")
     28 	rs.AddTemplate(ctx, "transparent", "{{.out}}")
     29 	rs.AddLocalFunc("foo", get)
     30 	rs.AddLocalFunc("bar", get)
     31 	rs.AddLocalFunc("baz", get)
     32 	rs.AddLocalFunc("xyzzy", getXyzzy)
     33 	return tr
     34 }
     35 
     36 func get(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     37 	switch sym {
     38 	case "foo":
     39 		return resource.Result{
     40 			Content: "inky",
     41 		}, nil
     42 	case "bar":
     43 		return resource.Result{
     44 			Content: "pinky",
     45 		}, nil
     46 	case "baz":
     47 		return resource.Result{
     48 			Content: "blinky",
     49 		}, nil
     50 	}
     51 	return resource.Result{}, fmt.Errorf("unknown sym: %s", sym)
     52 }
     53 
     54 func getXyzzy(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     55 	r := "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve"
     56 	return resource.Result{
     57 		Content: r,
     58 	}, nil
     59 }
     60 
     61 func TestSizeCheck(t *testing.T) {
     62 	szr := NewSizer(16)
     63 	l, ok := szr.Check("foobar")
     64 	if !ok {
     65 		t.Fatalf("expected ok")
     66 	}
     67 	if l != 10 {
     68 		t.Fatalf("expected 10, got %v", l)
     69 	}
     70 
     71 	l, ok = szr.Check("inkypinkyblinkyclyde")
     72 	if ok {
     73 		t.Fatalf("expected not ok")
     74 	}
     75 	if l != 0 {
     76 		t.Fatalf("expected 0, got %v", l)
     77 	}
     78 }
     79 
     80 func TestSizeLimit(t *testing.T) {
     81 	st := state.NewState(0)
     82 	ca := cache.NewCache()
     83 	mn := NewMenu()
     84 	rs := newTestSizeResource()
     85 	rs.Lock()
     86 	szr := NewSizer(128)
     87 	pg := NewPage(ca, rs).WithMenu(mn).WithSizer(szr)
     88 	ca.Push()
     89 	st.Down("test")
     90 	err := ca.Add("foo", "inky", 4)
     91 	if err != nil {
     92 		t.Fatal(err)
     93 	}
     94 	err = ca.Add("bar", "pinky", 10)
     95 	if err != nil {
     96 		t.Fatal(err)
     97 	}
     98 	err = ca.Add("baz", "blinky", 0)
     99 	if err != nil {
    100 		t.Fatal(err)
    101 	}
    102 	err = pg.Map("foo")
    103 	if err != nil {
    104 		t.Fatal(err)
    105 	}
    106 	err = pg.Map("bar")
    107 	if err != nil {
    108 		t.Fatal(err)
    109 	}
    110 	err = pg.Map("baz")
    111 	if err != nil {
    112 		t.Fatal(err)
    113 	}
    114 
    115 	mn.Put("1", "foo the foo")
    116 	mn.Put("2", "go to bar")
    117 
    118 	ctx := context.Background()
    119 	_, err = pg.Render(ctx, "small", 0)
    120 	if err != nil {
    121 		t.Fatal(err)
    122 	}
    123 
    124 	_, err = pg.Render(ctx, "toobig", 0)
    125 	if err == nil {
    126 		t.Fatalf("expected size exceeded")
    127 	}
    128 }
    129 
    130 func TestSizePages(t *testing.T) {
    131 	st := state.NewState(0)
    132 	ca := cache.NewCache()
    133 	mn := NewMenu()
    134 	rs := newTestSizeResource()
    135 	rs.Lock()
    136 	szr := NewSizer(128)
    137 	pg := NewPage(ca, rs).WithSizer(szr).WithMenu(mn)
    138 	ca.Push()
    139 	st.Down("test")
    140 	ca.Add("foo", "inky", 4)
    141 	ca.Add("bar", "pinky", 10)
    142 	ca.Add("baz", "blinky", 20)
    143 	ca.Add("xyzzy", "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", 0)
    144 	pg.Map("foo")
    145 	pg.Map("bar")
    146 	pg.Map("baz")
    147 	pg.Map("xyzzy")
    148 
    149 	mn.Put("1", "foo the foo")
    150 	mn.Put("2", "go to bar")
    151 
    152 	ctx := context.Background()
    153 	r, err := pg.Render(ctx, "pages",  0)
    154 	if err != nil {
    155 		t.Fatal(err)
    156 	}
    157 
    158 	expect := `one inky two pinky three blinky
    159 inky pinky
    160 blinky clyde sue
    161 tinkywinky dipsy
    162 lala poo
    163 1:foo the foo
    164 2:go to bar`
    165 
    166 
    167 	if r != expect {
    168 		t.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, r)
    169 	}
    170 	r, err = pg.Render(ctx, "pages", 1)
    171 	if err != nil {
    172 		t.Fatal(err)
    173 	}
    174 
    175 	expect = `one inky two pinky three blinky
    176 one two three four five six seven
    177 eight nine ten
    178 eleven twelve
    179 1:foo the foo
    180 2:go to bar`
    181 	if r != expect {
    182 		t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect, r)
    183 	}
    184 
    185 }
    186 
    187 func TestManySizes(t *testing.T) {
    188 	for i := 60; i < 160; i++ {
    189 		st := state.NewState(0)
    190 		ca := cache.NewCache()
    191 		mn := NewMenu() //.WithOutputSize(32)
    192 		rs := newTestSizeResource() //.WithEntryFuncGetter(funcFor).WithTemplateGetter(getTemplate)
    193 		rs.Lock()
    194 		//rs := TestSizeResource{
    195 		//	mrs,	
    196 		//}
    197 		szr := NewSizer(uint32(i))
    198 		pg := NewPage(ca, rs).WithSizer(szr).WithMenu(mn)
    199 		ca.Push()
    200 		st.Down("pages")
    201 		ca.Add("foo", "inky", 10)
    202 		ca.Add("bar", "pinky", 10)
    203 		ca.Add("baz", "blinky", 10)
    204 		ca.Add("xyzzy", "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", 0)
    205 		pg.Map("foo")
    206 		pg.Map("bar")
    207 		pg.Map("baz")
    208 		pg.Map("xyzzy")
    209 
    210 		ctx := context.Background()
    211 		_, err := pg.Render(ctx, "pages", 0)
    212 		if err != nil {
    213 			t.Fatal(err)
    214 		}
    215 	}
    216 }
    217 
    218 func TestManySizesMenued(t *testing.T) {
    219 	for i := 60; i < 160; i++ {
    220 		st := state.NewState(0)
    221 		ca := cache.NewCache()
    222 		mn := NewMenu() //.WithOutputSize(32)
    223 		rs := newTestSizeResource()
    224 		rs.Lock()
    225 		szr := NewSizer(uint32(i))
    226 		pg := NewPage(ca, rs).WithSizer(szr).WithMenu(mn)
    227 		ca.Push()
    228 		st.Down("pages")
    229 		ca.Add("foo", "inky", 10)
    230 		ca.Add("bar", "pinky", 10)
    231 		ca.Add("baz", "blinky", 10)
    232 		ca.Add("xyzzy", "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", 0)
    233 		pg.Map("foo")
    234 		pg.Map("bar")
    235 		pg.Map("baz")
    236 		pg.Map("xyzzy")
    237 		mn.Put("0", "yay")
    238 		mn.Put("12", "nay")
    239 
    240 		ctx := context.Background()
    241 		_, err := pg.Render(ctx, "pages", 0)
    242 		if err != nil {
    243 			t.Fatal(err)
    244 		}
    245 	}
    246 }
    247 
    248 func TestMenuCollideSink(t *testing.T) {
    249 	ctx := context.Background()
    250 	ca := cache.NewCache()
    251 	rs := resourcetest.NewTestResource()
    252 	rs.AddTemplate(ctx, "foo", "bar")
    253 	rs.Lock()
    254 	szr := NewSizer(30)
    255 	pg := NewPage(ca, rs).WithSizer(szr)
    256 	ca.Push()
    257 
    258 	ca.Add("inky", "pinky", 5)
    259 	ca.Add("blinky", "clyde", 0)
    260 	pg.Map("inky")
    261 	
    262 	mn := NewMenu().WithSink()
    263 	pg = pg.WithMenu(mn)
    264 
    265 	var err error
    266 	_, err = pg.Render(ctx, "foo", 0)
    267 	if err != nil {
    268 		t.Fatal(err)
    269 	}
    270 	
    271 	mn = NewMenu().WithSink()
    272 	pg = pg.WithMenu(mn)
    273 	pg.Map("blinky")
    274 	_, err = pg.Render(ctx, "foo", 0)
    275 	if err == nil {
    276 		t.Fatalf("expected error")
    277 	}
    278 }
    279 
    280 func TestMenuSink(t *testing.T) {
    281 	var err error
    282 	ctx := context.Background()
    283 
    284 	ca := cache.NewCache()
    285 	rs := resourcetest.NewTestResource()
    286 	rs.AddTemplate(ctx, "foo", "bar {{.baz}}")
    287 	rs.Lock()
    288 	szr := NewSizer(45)
    289 
    290 	mn := NewMenu().WithSink().WithBrowseConfig(DefaultBrowseConfig())
    291 	mn.Put("0", "inky")
    292 	mn.Put("1", "pinky")
    293 	mn.Put("22", "blinky")
    294 	mn.Put("3", "clyde")
    295 	mn.Put("44", "tinkywinky")
    296 
    297 	pg := NewPage(ca, rs).WithSizer(szr).WithMenu(mn)
    298 	ca.Push()
    299 
    300 	ca.Add("baz", "xyzzy", 5)
    301 	pg.Map("baz")
    302 
    303 	r, err := pg.Render(ctx, "foo", 0)
    304 	if err != nil {
    305 		t.Fatal(err)
    306 	}
    307 	expect := `bar xyzzy
    308 0:inky
    309 1:pinky
    310 22:blinky
    311 11:next`
    312 	if r != expect {
    313 		t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect, r)
    314 	}
    315 
    316 	mn = NewMenu().WithSink().WithBrowseConfig(DefaultBrowseConfig())
    317 	mn.Put("0", "inky")
    318 	mn.Put("1", "pinky")
    319 	mn.Put("22", "blinky")
    320 	mn.Put("3", "clyde")
    321 	mn.Put("44", "tinkywinky")
    322 
    323 	pg = NewPage(ca, rs).WithSizer(szr).WithMenu(mn)
    324 	ca.Push()
    325 
    326 	ca.Add("baz", "xyzzy", 5)
    327 	pg.Map("baz")
    328 
    329 	r, err = pg.Render(ctx, "foo", 1)
    330 	if err != nil {
    331 		t.Fatal(err)
    332 	}
    333 	expect = `bar xyzzy
    334 3:clyde
    335 11:next
    336 22:previous`
    337 	if r != expect {
    338 		t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect, r)
    339 	}
    340 
    341 	mn = NewMenu().WithSink().WithBrowseConfig(DefaultBrowseConfig())
    342 	mn.Put("0", "inky")
    343 	mn.Put("1", "pinky")
    344 	mn.Put("22", "blinky")
    345 	mn.Put("3", "clyde")
    346 	mn.Put("44", "tinkywinky")
    347 
    348 	pg = NewPage(ca, rs).WithSizer(szr).WithMenu(mn)
    349 	ca.Push()
    350 
    351 	ca.Add("baz", "xyzzy", 5)
    352 	pg.Map("baz")
    353 
    354 
    355 	r, err = pg.Render(ctx, "foo", 2)
    356 	if err != nil {
    357 		t.Fatal(err)
    358 	}
    359 	expect = `bar xyzzy
    360 44:tinkywinky
    361 22:previous`
    362 	if r != expect {
    363 		t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect, r)
    364 	}
    365 
    366 }
    367 
    368 func TestMiddlePage(t *testing.T) {
    369 	ctx := context.Background()
    370 	st := state.NewState(0)
    371 	ca := cache.NewCache()
    372 	mn := NewMenu().WithBrowseConfig(DefaultBrowseConfig())
    373 	rs := newTestSizeResource()
    374 	rs.Lock()
    375 		content := ""
    376 	for i := 0; i < 42; i++ {
    377 		v := rand.Intn(26)
    378 		b := make([]byte, 3+(v%3))
    379 		for ii := 0; ii < len(b); ii++ {
    380 			b[ii] = uint8(0x41 + v)
    381 			v = rand.Intn(26)
    382 		}
    383 		content += fmt.Sprintf("%d:%s\n", i, string(b))
    384 	}
    385 	content = content[:len(content)-1]
    386 
    387 	st.Down("test")
    388 
    389 	ca.Push()
    390 	ca.Add("out", content, 0)
    391 	szr := NewSizer(160)
    392 
    393 	mn.Put("x", "exit")
    394 	mn.Put("q", "quit")
    395 	pg := NewPage(ca, rs).WithMenu(mn).WithSizer(szr)
    396 	pg.Map("out")
    397 
    398 	r, err := pg.Render(ctx, "transparent", 2)
    399 	if err != nil {
    400 		t.Fatal(err)
    401 	}
    402 	fmt.Printf("%s\n", r)
    403 }
    404