github.com/vugu/vugu@v0.3.6-0.20240430171613-3f6f402e014b/build-env_test.go (about)

     1  package vugu
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestBuildEnvCachedComponent(t *testing.T) {
    10  
    11  	assert := assert.New(t)
    12  
    13  	be, err := NewBuildEnv()
    14  	assert.NoError(err)
    15  	assert.NotNil(be)
    16  
    17  	{ // just double check sane behavior for these keys
    18  		k1 := MakeCompKey(1, 1)
    19  		k2 := MakeCompKey(1, 1)
    20  		assert.Equal(k1, k2)
    21  		k3 := MakeCompKey(1, 2)
    22  		assert.NotEqual(k1, k3)
    23  		k4 := MakeCompKey(1, 1)
    24  		assert.Equal(k1, k4)
    25  	}
    26  
    27  	rb1 := &rootb1{}
    28  
    29  	// first run to intialize
    30  	res := be.RunBuild(rb1)
    31  	assert.NotNil(res)
    32  
    33  	c := be.CachedComponent(MakeCompKey(1, 1))
    34  	assert.Nil(c)
    35  	assert.Nil(be.compCache[MakeCompKey(1, 1)])
    36  
    37  	b1 := &testb1{}
    38  	be.UseComponent(MakeCompKey(1, 1), b1)
    39  	assert.NotNil(be.compUsed[MakeCompKey(1, 1)])
    40  
    41  	// run another one
    42  	res = be.RunBuild(rb1)
    43  	assert.NotNil(res)
    44  
    45  	// we should see b1 in the cache
    46  	assert.NotNil(be.compCache[MakeCompKey(1, 1)])
    47  	assert.Equal(b1, be.compCache[MakeCompKey(1, 1)])
    48  
    49  	// TODO: but not in the used (not used for this pass)
    50  
    51  	// TODO: now try to use it and make sure we can only get it once
    52  
    53  }
    54  
    55  type rootb1 struct{}
    56  
    57  func (b *rootb1) Build(in *BuildIn) (out *BuildOut) {
    58  	return &BuildOut{
    59  		Out: []*VGNode{},
    60  	}
    61  }
    62  
    63  type testb1 struct{}
    64  
    65  func (b *testb1) Build(in *BuildIn) (out *BuildOut) {
    66  	return &BuildOut{
    67  		Out: []*VGNode{},
    68  	}
    69  }