...
Run Format

Source file src/go/format/format_test.go

Documentation: go/format

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package format
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"go/parser"
    11  	"go/token"
    12  	"io/ioutil"
    13  	"log"
    14  	"strings"
    15  	"testing"
    16  )
    17  
    18  const testfile = "format_test.go"
    19  
    20  func diff(t *testing.T, dst, src []byte) {
    21  	line := 1
    22  	offs := 0 // line offset
    23  	for i := 0; i < len(dst) && i < len(src); i++ {
    24  		d := dst[i]
    25  		s := src[i]
    26  		if d != s {
    27  			t.Errorf("dst:%d: %s\n", line, dst[offs:i+1])
    28  			t.Errorf("src:%d: %s\n", line, src[offs:i+1])
    29  			return
    30  		}
    31  		if s == '\n' {
    32  			line++
    33  			offs = i + 1
    34  		}
    35  	}
    36  	if len(dst) != len(src) {
    37  		t.Errorf("len(dst) = %d, len(src) = %d\nsrc = %q", len(dst), len(src), src)
    38  	}
    39  }
    40  
    41  func TestNode(t *testing.T) {
    42  	src, err := ioutil.ReadFile(testfile)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	fset := token.NewFileSet()
    48  	file, err := parser.ParseFile(fset, testfile, src, parser.ParseComments)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	var buf bytes.Buffer
    54  
    55  	if err = Node(&buf, fset, file); err != nil {
    56  		t.Fatal("Node failed:", err)
    57  	}
    58  
    59  	diff(t, buf.Bytes(), src)
    60  }
    61  
    62  func TestSource(t *testing.T) {
    63  	src, err := ioutil.ReadFile(testfile)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  
    68  	res, err := Source(src)
    69  	if err != nil {
    70  		t.Fatal("Source failed:", err)
    71  	}
    72  
    73  	diff(t, res, src)
    74  }
    75  
    76  // Test cases that are expected to fail are marked by the prefix "ERROR".
    77  // The formatted result must look the same as the input for successful tests.
    78  var tests = []string{
    79  	// declaration lists
    80  	`import "go/format"`,
    81  	"var x int",
    82  	"var x int\n\ntype T struct{}",
    83  
    84  	// statement lists
    85  	"x := 0",
    86  	"f(a, b, c)\nvar x int = f(1, 2, 3)",
    87  
    88  	// indentation, leading and trailing space
    89  	"\tx := 0\n\tgo f()",
    90  	"\tx := 0\n\tgo f()\n\n\n",
    91  	"\n\t\t\n\n\tx := 0\n\tgo f()\n\n\n",
    92  	"\n\t\t\n\n\t\t\tx := 0\n\t\t\tgo f()\n\n\n",
    93  	"\n\t\t\n\n\t\t\tx := 0\n\t\t\tconst s = `\nfoo\n`\n\n\n",     // no indentation added inside raw strings
    94  	"\n\t\t\n\n\t\t\tx := 0\n\t\t\tconst s = `\n\t\tfoo\n`\n\n\n", // no indentation removed inside raw strings
    95  
    96  	// comments
    97  	"/* Comment */",
    98  	"\t/* Comment */ ",
    99  	"\n/* Comment */ ",
   100  	"i := 5 /* Comment */",         // issue #5551
   101  	"\ta()\n//line :1",             // issue #11276
   102  	"\t//xxx\n\ta()\n//line :2",    // issue #11276
   103  	"\ta() //line :1\n\tb()\n",     // issue #11276
   104  	"x := 0\n//line :1\n//line :2", // issue #11276
   105  
   106  	// whitespace
   107  	"",     // issue #11275
   108  	" ",    // issue #11275
   109  	"\t",   // issue #11275
   110  	"\t\t", // issue #11275
   111  	"\n",   // issue #11275
   112  	"\n\n", // issue #11275
   113  	"\t\n", // issue #11275
   114  
   115  	// erroneous programs
   116  	"ERROR1 + 2 +",
   117  	"ERRORx :=  0",
   118  }
   119  
   120  func String(s string) (string, error) {
   121  	res, err := Source([]byte(s))
   122  	if err != nil {
   123  		return "", err
   124  	}
   125  	return string(res), nil
   126  }
   127  
   128  func TestPartial(t *testing.T) {
   129  	for _, src := range tests {
   130  		if strings.HasPrefix(src, "ERROR") {
   131  			// test expected to fail
   132  			src = src[5:] // remove ERROR prefix
   133  			res, err := String(src)
   134  			if err == nil && res == src {
   135  				t.Errorf("formatting succeeded but was expected to fail:\n%q", src)
   136  			}
   137  		} else {
   138  			// test expected to succeed
   139  			res, err := String(src)
   140  			if err != nil {
   141  				t.Errorf("formatting failed (%s):\n%q", err, src)
   142  			} else if res != src {
   143  				t.Errorf("formatting incorrect:\nsource: %q\nresult: %q", src, res)
   144  			}
   145  		}
   146  	}
   147  }
   148  
   149  func ExampleNode() {
   150  	const expr = "(6+2*3)/4"
   151  
   152  	// parser.ParseExpr parses the argument and returns the
   153  	// corresponding ast.Node.
   154  	node, err := parser.ParseExpr(expr)
   155  	if err != nil {
   156  		log.Fatal(err)
   157  	}
   158  
   159  	// Create a FileSet for node. Since the node does not come
   160  	// from a real source file, fset will be empty.
   161  	fset := token.NewFileSet()
   162  
   163  	var buf bytes.Buffer
   164  	err = Node(&buf, fset, node)
   165  	if err != nil {
   166  		log.Fatal(err)
   167  	}
   168  
   169  	fmt.Println(buf.String())
   170  
   171  	// Output: (6 + 2*3) / 4
   172  }
   173  

View as plain text