From 24d099ad97906a5c7d5f0e81ae7ee7860b790a72 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Mon, 28 Oct 2024 11:27:25 +0200 Subject: [PATCH] refactor: add tests for isGenerated in the lint package --- lint/linter_test.go | 31 +++++++++++++++++++++++++++++ test/utils_test.go | 48 --------------------------------------------- 2 files changed, 31 insertions(+), 48 deletions(-) create mode 100644 lint/linter_test.go diff --git a/lint/linter_test.go b/lint/linter_test.go new file mode 100644 index 000000000..4c1f95dd1 --- /dev/null +++ b/lint/linter_test.go @@ -0,0 +1,31 @@ +package lint + +import "testing" + +// TestIsGenerated tests isGenerated function. +func TestIsGenerated(t *testing.T) { //revive:disable-line:exported + tests := []struct { + source string + generated bool + }{ + {"// Code Generated by some tool. DO NOT EDIT.", false}, + {"// Code generated by some tool. DO NOT EDIT.", true}, + {"// Code generated by some tool. DO NOT EDIT", false}, + {"// Code generated DO NOT EDIT.", true}, + {"// Code generated DO NOT EDIT.", false}, + {"\t\t// Code generated by some tool. DO NOT EDIT.\npackage foo\n", false}, + {"// Code generated by some tool. DO NOT EDIT.\npackage foo\n", true}, + {"package foo\n// Code generated by some tool. DO NOT EDIT.\ntype foo int\n", true}, + {"package foo\n // Code generated by some tool. DO NOT EDIT.\ntype foo int\n", false}, + {"package foo\n// Code generated by some tool. DO NOT EDIT. \ntype foo int\n", false}, + {"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.\n", true}, + {"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.", true}, + } + + for i, test := range tests { + got := isGenerated([]byte(test.source)) + if got != test.generated { + t.Errorf("test %d, isGenerated() = %v, want %v", i, got, test.generated) + } + } +} diff --git a/test/utils_test.go b/test/utils_test.go index 56af84219..15a3e2d92 100644 --- a/test/utils_test.go +++ b/test/utils_test.go @@ -1,8 +1,6 @@ package test import ( - "bufio" - "bytes" "encoding/json" "fmt" "go/ast" @@ -387,49 +385,3 @@ func TestExportedType(t *testing.T) { //revive:disable-line:exported } } } - -var ( - genHdr = []byte("// Code generated ") - genFtr = []byte(" DO NOT EDIT.") -) - -// isGenerated reports whether the source file is generated code -// according the rules from https://golang.org/s/generatedcode. -func isGenerated(src []byte) bool { - sc := bufio.NewScanner(bytes.NewReader(src)) - for sc.Scan() { - b := sc.Bytes() - if bytes.HasPrefix(b, genHdr) && bytes.HasSuffix(b, genFtr) && len(b) >= len(genHdr)+len(genFtr) { - return true - } - } - return false -} - -// TestIsGenerated tests isGenerated function -func TestIsGenerated(t *testing.T) { //revive:disable-line:exported - tests := []struct { - source string - generated bool - }{ - {"// Code Generated by some tool. DO NOT EDIT.", false}, - {"// Code generated by some tool. DO NOT EDIT.", true}, - {"// Code generated by some tool. DO NOT EDIT", false}, - {"// Code generated DO NOT EDIT.", true}, - {"// Code generated DO NOT EDIT.", false}, - {"\t\t// Code generated by some tool. DO NOT EDIT.\npackage foo\n", false}, - {"// Code generated by some tool. DO NOT EDIT.\npackage foo\n", true}, - {"package foo\n// Code generated by some tool. DO NOT EDIT.\ntype foo int\n", true}, - {"package foo\n // Code generated by some tool. DO NOT EDIT.\ntype foo int\n", false}, - {"package foo\n// Code generated by some tool. DO NOT EDIT. \ntype foo int\n", false}, - {"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.\n", true}, - {"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.", true}, - } - - for i, test := range tests { - got := isGenerated([]byte(test.source)) - if got != test.generated { - t.Errorf("test %d, isGenerated() = %v, want %v", i, got, test.generated) - } - } -}