github.com/goplus/gop@v1.2.6/x/format/gopstyledir_test.go (about)

     1  /*
     2   * Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package format
    18  
    19  import (
    20  	"io/ioutil"
    21  	"log"
    22  	"os"
    23  	"path"
    24  	"strings"
    25  	"testing"
    26  )
    27  
    28  // -----------------------------------------------------------------------------
    29  
    30  func TestFromTestdata(t *testing.T) {
    31  	sel := ""
    32  	dir, err := os.Getwd()
    33  	if err != nil {
    34  		t.Fatal("Getwd failed:", err)
    35  	}
    36  	dir = path.Join(dir, "./_testdata")
    37  	fis, err := ioutil.ReadDir(dir)
    38  	if err != nil {
    39  		t.Fatal("ReadDir failed:", err)
    40  	}
    41  	for _, fi := range fis {
    42  		name := fi.Name()
    43  		if strings.HasPrefix(name, "_") {
    44  			continue
    45  		}
    46  		t.Run(name, func(t *testing.T) {
    47  			testFrom(t, dir+"/"+name, sel)
    48  		})
    49  	}
    50  }
    51  
    52  func testFrom(t *testing.T, pkgDir, sel string) {
    53  	if sel != "" && !strings.Contains(pkgDir, sel) {
    54  		return
    55  	}
    56  	log.Println("Formatting", pkgDir)
    57  	file := pkgDir + "/index.gop"
    58  	src, err := os.ReadFile(file)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	ret, err := GopstyleSource(src, file)
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	expect, err := os.ReadFile(pkgDir + "/format.expect")
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	diffBytes(t, pkgDir+"/format.result", ret, expect)
    71  }
    72  
    73  func diffBytes(t *testing.T, outfile string, dst, src []byte) {
    74  	line := 1
    75  	offs := 0 // line offset
    76  	for i := 0; i < len(dst) && i < len(src); i++ {
    77  		d := dst[i]
    78  		s := src[i]
    79  		if d != s {
    80  			os.WriteFile(outfile, dst, 0644)
    81  			t.Errorf("dst:%d: %s\n", line, dst[offs:])
    82  			t.Errorf("src:%d: %s\n", line, src[offs:])
    83  			return
    84  		}
    85  		if s == '\n' {
    86  			line++
    87  			offs = i + 1
    88  		}
    89  	}
    90  	if len(dst) != len(src) {
    91  		t.Errorf("len(dst) = %d, len(src) = %d\ndst = %q\nsrc = %q", len(dst), len(src), dst, src)
    92  	}
    93  }
    94  
    95  // -----------------------------------------------------------------------------