...

Source file src/gotest.tools/v3/internal/format/diff_test.go

Documentation: gotest.tools/v3/internal/format

     1  package format_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  	"gotest.tools/v3/golden"
     8  	"gotest.tools/v3/internal/format"
     9  )
    10  
    11  func TestUnifiedDiff(t *testing.T) {
    12  	var testcases = []struct {
    13  		name     string
    14  		a        string
    15  		b        string
    16  		expected string
    17  		from     string
    18  		to       string
    19  	}{
    20  		{
    21  			name: "empty diff",
    22  			a:    "a\nb\nc",
    23  			b:    "a\nb\nc",
    24  			from: "from",
    25  			to:   "to",
    26  		},
    27  		{
    28  			name:     "one diff with header",
    29  			a:        "a\nxyz\nc",
    30  			b:        "a\nb\nc",
    31  			from:     "from",
    32  			to:       "to",
    33  			expected: "one-diff-with-header.golden",
    34  		},
    35  		{
    36  			name:     "many diffs",
    37  			a:        "a123\nxyz\nc\nbaba\nz\nt\nj2j2\nok\nok\ndone\n",
    38  			b:        "a123\nxyz\nc\nabab\nz\nt\nj2j2\nok\nok\n",
    39  			expected: "many-diff.golden",
    40  		},
    41  		{
    42  			name:     "no trailing newline",
    43  			a:        "a123\nxyz\nc\nbaba\nz\nt\nj2j2\nok\nok\ndone\n",
    44  			b:        "a123\nxyz\nc\nabab\nz\nt\nj2j2\nok\nok",
    45  			expected: "many-diff-no-trailing-newline.golden",
    46  		},
    47  		{
    48  			name:     "whitespace diff",
    49  			a:        "  something\n      something\n    \v\r\n",
    50  			b:        "  something\n\tsomething\n  \n",
    51  			expected: "whitespace-diff.golden",
    52  		},
    53  	}
    54  
    55  	for _, testcase := range testcases {
    56  		t.Run(testcase.name, func(t *testing.T) {
    57  			diff := format.UnifiedDiff(format.DiffConfig{
    58  				A:    testcase.a,
    59  				B:    testcase.b,
    60  				From: testcase.from,
    61  				To:   testcase.to,
    62  			})
    63  
    64  			if testcase.expected != "" {
    65  				assert.Assert(t, golden.String(diff, testcase.expected))
    66  				return
    67  			}
    68  			assert.Equal(t, diff, "")
    69  		})
    70  	}
    71  }
    72  

View as plain text