File: cover_test.go

package info (click to toggle)
goawk 1.29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,560 kB
  • sloc: awk: 3,060; yacc: 198; fortran: 189; python: 131; sh: 58; makefile: 12
file content (68 lines) | stat: -rw-r--r-- 1,643 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cover

import (
	"bytes"
	"fmt"
	"os"
	"strings"
	"testing"

	"github.com/benhoyt/goawk/internal/parseutil"
	"github.com/benhoyt/goawk/parser"
)

func TestAnnotatingLogicCorrectness(t *testing.T) {
	tests := []struct {
		awkPath   string
		checkPath string
	}{
		{"a1.awk", "a1_annotation_data.txt"},
		{"a2.awk", "a2_annotation_data.txt"},
		{"a3.awk", "a3_annotation_data.txt"},
	}

	for _, test := range tests {
		t.Run(test.awkPath, func(t *testing.T) {
			awkPath := "../../testdata/cover/" + test.awkPath
			checkPath := "../../testdata/cover/" + test.checkPath
			f, err := os.Open(awkPath)
			if err != nil {
				panic(err)
			}
			fileReader := &parseutil.FileReader{}
			err = fileReader.AddFile(test.awkPath, f)
			if err != nil {
				panic(err)
			}
			coverage := New(ModeSet, false, fileReader)
			prog, err := parser.ParseProgram(fileReader.Source(), nil)
			if err != nil {
				panic(err)
			}
			coverage.Annotate(&prog.ResolvedProgram.Program)

			var actualAnnotationData strings.Builder

			for i, block := range coverage.trackedBlocks {
				actualAnnotationData.WriteString(fmt.Sprintf("%d %s %s-%s %d\n", i+1,
					block.path, block.start, block.end,
					block.numStmts))
			}

			result := strings.TrimSpace(actualAnnotationData.String())

			expected, err := os.ReadFile(checkPath)
			if err != nil {
				panic(err)
			}

			if strings.TrimSpace(string(normalizeNewlines(expected))) != result {
				t.Errorf("Annotation data is wrong:\n\nactual:\n\n%s\n\nexpected:\n\n%s", result, expected)
			}
		})
	}
}

func normalizeNewlines(b []byte) []byte {
	return bytes.Replace(b, []byte("\r\n"), []byte{'\n'}, -1)
}