github.com/vugu/vugu@v0.3.6-0.20240430171613-3f6f402e014b/domrender/renderer-js-instructions_test.go (about)

     1  package domrender
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestWriteSetInnerHTML(t *testing.T) {
    10  	tests := []struct {
    11  		bufferSize   int
    12  		position     int
    13  		htmlString   []byte
    14  		sentBuffers  [][]byte
    15  		outputBuffer []byte
    16  		description  string
    17  	}{
    18  		{
    19  			bufferSize:   10,
    20  			position:     0,
    21  			htmlString:   []byte{1, 2, 3, 4},
    22  			sentBuffers:  nil,
    23  			outputBuffer: []byte{29, 0, 0, 0, 4, 1, 2, 3, 4, 0}, // setInner, len 4, 1,2,3,4 end 0
    24  			description:  "a small message which does not need to be buffered",
    25  		},
    26  		{
    27  			bufferSize:   10,
    28  			position:     3,
    29  			htmlString:   []byte{1, 2, 3, 4},
    30  			sentBuffers:  [][]byte{{255, 255, 255, 37, 0, 0, 0, 1, 1, 0}}, // setBuffer, len 1, 1 end 0
    31  			outputBuffer: []byte{29, 0, 0, 0, 3, 2, 3, 4, 0, 0},           // setInner, len 3, 2,3,4 end 0
    32  			description:  "a small message which does need to be buffered because of offsets",
    33  		},
    34  		{
    35  			bufferSize:   10,
    36  			position:     0,
    37  			htmlString:   []byte{1, 2, 3, 4, 5, 6},
    38  			sentBuffers:  [][]byte{{37, 0, 0, 0, 4, 1, 2, 3, 4, 0}}, // setBuffer, len 4, 1,2,3,4 end 0
    39  			outputBuffer: []byte{29, 0, 0, 0, 2, 5, 6, 0, 0, 0},     // setInner, len 2, 5,6, end 0, empty
    40  			description:  "a message which needs to be split across two buffers",
    41  		},
    42  		{
    43  			bufferSize: 10,
    44  			position:   8,
    45  			htmlString: []byte{1, 2, 3, 4, 5, 6},
    46  			sentBuffers: [][]byte{
    47  				{255, 255, 255, 255, 255, 255, 255, 255, 0, 0}, // previous buffer
    48  				{37, 0, 0, 0, 4, 1, 2, 3, 4, 0},                // setBuffer, len 4, 1,2,3,4 end 0
    49  			},
    50  			outputBuffer: []byte{29, 0, 0, 0, 2, 5, 6, 0, 0, 0}, // setInner, len 2, 5,6, end 0, empty
    51  			description:  "a message which needs to be split across two buffers, that won't fit in the original buffer",
    52  		},
    53  		{
    54  			bufferSize: 10,
    55  			position:   0,
    56  			htmlString: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
    57  			sentBuffers: [][]byte{
    58  				{37, 0, 0, 0, 4, 1, 2, 3, 4, 0},     // setBuffer, len 4, 1,2,3,4 end 0
    59  				{37, 0, 0, 0, 4, 5, 6, 7, 8, 0},     // setBuffer, len 4, 5,6,7,8 end 0
    60  				{37, 0, 0, 0, 4, 9, 10, 11, 12, 0},  // setBuffer, len 4, 9,10,11,12 end 0
    61  				{37, 0, 0, 0, 4, 13, 14, 15, 16, 0}, // setBuffer, len 4, 13,14,15,16 end 0
    62  			},
    63  			outputBuffer: []byte{29, 0, 0, 0, 4, 17, 18, 19, 20, 0}, // setInner, len 2, 5,6, end 0
    64  			description:  "a big message which needs to be split across four buffers",
    65  		},
    66  	}
    67  
    68  	for _, test := range tests {
    69  		buffer := make([]byte, test.bufferSize)
    70  		var sent [][]byte
    71  
    72  		il := newInstructionList(buffer, func(il *instructionList) error {
    73  			// On send, add the ending 0 opCode
    74  			buffer[il.pos] = 0
    75  
    76  			// Save the old buffer as sent
    77  			data := make([]byte, len(buffer))
    78  			copy(data, buffer)
    79  			sent = append(sent, data)
    80  
    81  			// Zero the buffer (not necessary, but makes the tests more readable)
    82  			for i := range buffer {
    83  				buffer[i] = 0
    84  			}
    85  			return nil
    86  		})
    87  
    88  		// Prepend detectable garbage if there is a position set
    89  		for i := 0; i < test.position; i++ {
    90  			buffer[i] = 255
    91  		}
    92  		il.pos = test.position
    93  
    94  		err := il.writeSetInnerHTML(string(test.htmlString))
    95  		assert.NoError(t, err, test.description)
    96  		assert.Equal(t, test.sentBuffers, sent, test.description)
    97  		assert.Equal(t, test.outputBuffer, buffer, test.description)
    98  	}
    99  }