github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/covermerger/lines_matcher_test.go (about)

     1  // Copyright 2024 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package covermerger
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  var (
    13  	textBase = `line1
    14  line2
    15  line3`
    16  	textBaseWithNewLine = `line0
    17  line1
    18  line2
    19  line3`
    20  	textBaseWOLine = `line2
    21  line3`
    22  	textBaseChangedLine = `lineX
    23  line2
    24  line3`
    25  )
    26  
    27  func TestMatching(t *testing.T) {
    28  	type Test struct {
    29  		name     string
    30  		textFrom string
    31  		textTo   string
    32  		lineFrom int
    33  		lineTo   int
    34  	}
    35  	tests := []Test{
    36  		{
    37  			name:     "same text matching",
    38  			textFrom: textBase,
    39  			textTo:   textBase,
    40  			lineFrom: 0,
    41  			lineTo:   0,
    42  		},
    43  		{
    44  			name:     "diff matching with the new line",
    45  			textFrom: textBase,
    46  			textTo:   textBaseWithNewLine,
    47  			lineFrom: 0,
    48  			lineTo:   1,
    49  		},
    50  		{
    51  			name:     "diff matching with the removed line",
    52  			textFrom: textBase,
    53  			textTo:   textBaseWOLine,
    54  			lineFrom: 0,
    55  			lineTo:   -1,
    56  		},
    57  		{
    58  			name:     "diff matching with the changed line",
    59  			textFrom: textBase,
    60  			textTo:   textBaseChangedLine,
    61  			lineFrom: 0,
    62  			lineTo:   -1,
    63  		},
    64  	}
    65  	for _, test := range tests {
    66  		t.Run(test.name, func(t *testing.T) {
    67  			m := makeLineToLineMatcher(test.textFrom, test.textTo)
    68  			assert.NotNil(t, m)
    69  			got := m.SameLinePos(test.lineFrom)
    70  			if got != test.lineTo {
    71  				t.Fatalf("expected to see line %d instread of %d", test.lineTo, got)
    72  			}
    73  		})
    74  	}
    75  }