github.com/orijtech/structslop@v0.0.9-0.20230520012622-069644583b8b/structslop_test.go (about)

     1  // Copyright 2020 Orijtech, Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package structslop_test
    16  
    17  import (
    18  	"bytes"
    19  	"os"
    20  	"path/filepath"
    21  	"strings"
    22  	"testing"
    23  
    24  	"golang.org/x/tools/go/analysis/analysistest"
    25  
    26  	"github.com/orijtech/structslop"
    27  )
    28  
    29  func Test(t *testing.T) {
    30  	testdata := analysistest.TestData()
    31  	analysistest.Run(t, testdata, structslop.Analyzer, "struct")
    32  }
    33  
    34  func TestApply(t *testing.T) {
    35  	dir := strings.Join([]string{".", "testdata", "src"}, string(os.PathSeparator))
    36  	tmpdir, err := os.MkdirTemp(dir, "structslop-test-apply-")
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	defer os.RemoveAll(tmpdir)
    41  	fn := filepath.Join(tmpdir, "p.go")
    42  	src, _ := os.ReadFile(filepath.Join(".", "testdata", "src", "struct", "p.go"))
    43  	if err := os.WriteFile(fn, src, 0644); err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	testdata := analysistest.TestData()
    47  	_ = structslop.Analyzer.Flags.Set("apply", "true")
    48  	defer func() {
    49  		_ = structslop.Analyzer.Flags.Set("apply", "false")
    50  	}()
    51  	analysistest.Run(t, testdata, structslop.Analyzer, filepath.Base(tmpdir))
    52  	got, _ := os.ReadFile(fn)
    53  	expected, _ := os.ReadFile(filepath.Join(".", "testdata", "src", "struct", "p.go.golden"))
    54  	if !bytes.Equal(expected, got) {
    55  		t.Errorf("unexpected suggested fix, want:\n%s\ngot:\n%s\n", string(expected), string(got))
    56  	}
    57  }
    58  
    59  func TestIncludeTestFiles(t *testing.T) {
    60  	testdata := analysistest.TestData()
    61  	_ = structslop.Analyzer.Flags.Set("include-test-files", "true")
    62  	defer func() {
    63  		_ = structslop.Analyzer.Flags.Set("include-test-files", "false")
    64  	}()
    65  	analysistest.Run(t, testdata, structslop.Analyzer, "include-test-files")
    66  }
    67  
    68  func TestVerboseMode(t *testing.T) {
    69  	testdata := analysistest.TestData()
    70  	_ = structslop.Analyzer.Flags.Set("verbose", "true")
    71  	defer func() {
    72  		_ = structslop.Analyzer.Flags.Set("verbose", "false")
    73  	}()
    74  	analysistest.Run(t, testdata, structslop.Analyzer, "verbose")
    75  }
    76  
    77  func TestGenerated(t *testing.T) {
    78  	testdata := analysistest.TestData()
    79  	analysistest.Run(t, testdata, structslop.Analyzer, "generated")
    80  }