github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/cmd/digraph/digraph_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"reflect"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func TestDigraph(t *testing.T) {
    12  	const g1 = `
    13  socks shoes
    14  shorts pants
    15  pants belt shoes
    16  shirt tie sweater
    17  sweater jacket
    18  hat
    19  `
    20  
    21  	const g2 = `
    22  a b c
    23  b d
    24  c d
    25  d c
    26  `
    27  
    28  	for _, test := range []struct {
    29  		input string
    30  		cmd   string
    31  		args  []string
    32  		want  string
    33  	}{
    34  		{g1, "nodes", nil, "belt\nhat\njacket\npants\nshirt\nshoes\nshorts\nsocks\nsweater\ntie\n"},
    35  		{g1, "reverse", []string{"jacket"}, "jacket\nshirt\nsweater\n"},
    36  		{g1, "forward", []string{"socks"}, "shoes\nsocks\n"},
    37  		{g1, "forward", []string{"socks", "sweater"}, "jacket\nshoes\nsocks\nsweater\n"},
    38  
    39  		{g2, "allpaths", []string{"a", "d"}, "a\nb\nc\nd\n"},
    40  
    41  		{g2, "sccs", nil, "a\nb\nc d\n"},
    42  		{g2, "scc", []string{"d"}, "c\nd\n"},
    43  		{g2, "succs", []string{"a"}, "b\nc\n"},
    44  		{g2, "preds", []string{"c"}, "a\nd\n"},
    45  		{g2, "preds", []string{"c", "d"}, "a\nb\nc\nd\n"},
    46  	} {
    47  		stdin = strings.NewReader(test.input)
    48  		stdout = new(bytes.Buffer)
    49  		if err := digraph(test.cmd, test.args); err != nil {
    50  			t.Error(err)
    51  			continue
    52  		}
    53  
    54  		got := stdout.(fmt.Stringer).String()
    55  		if got != test.want {
    56  			t.Errorf("digraph(%s, %s) = %q, want %q", test.cmd, test.args, got, test.want)
    57  		}
    58  	}
    59  
    60  	// TODO(adonovan):
    61  	// - test somepath (it's nondeterministic).
    62  	// - test errors
    63  }
    64  
    65  func TestSplit(t *testing.T) {
    66  	for _, test := range []struct {
    67  		line string
    68  		want []string
    69  	}{
    70  		{`one "2a 2b" three`, []string{"one", "2a 2b", "three"}},
    71  		{`one tw"\n\x0a\u000a\012"o three`, []string{"one", "tw\n\n\n\no", "three"}},
    72  	} {
    73  		got, err := split(test.line)
    74  		if err != nil {
    75  			t.Errorf("split(%s) failed: %v", test.line, err)
    76  		}
    77  		if !reflect.DeepEqual(got, test.want) {
    78  			t.Errorf("split(%s) = %v, want %v", test.line, got, test.want)
    79  		}
    80  	}
    81  }
    82  
    83  func TestQuotedLength(t *testing.T) {
    84  	for _, test := range []struct {
    85  		input string
    86  		want  int
    87  	}{
    88  		{`"abc"`, 5},
    89  		{`"abc"def`, 5},
    90  		{`"abc\"d"ef`, 8}, // "abc\"d" is consumed, ef is residue
    91  		{`"\012\n\x0a\u000a\U0000000a"`, 28},
    92  		{"\"\xff\"", 3}, // bad UTF-8 is ok
    93  		{`"\xff"`, 6},   // hex escape for bad UTF-8 is ok
    94  	} {
    95  		got, ok := quotedLength(test.input)
    96  		if !ok {
    97  			got = 0
    98  		}
    99  		if got != test.want {
   100  			t.Errorf("quotedLength(%s) = %d, want %d", test.input, got, test.want)
   101  		}
   102  	}
   103  
   104  	// errors
   105  	for _, input := range []string{
   106  		``,            // not a quotation
   107  		`a`,           // not a quotation
   108  		`'a'`,         // not a quotation
   109  		`"a`,          // not terminated
   110  		`"\0"`,        // short octal escape
   111  		`"\x1"`,       // short hex escape
   112  		`"\u000"`,     // short \u escape
   113  		`"\U0000000"`, // short \U escape
   114  		`"\k"`,        // invalid escape
   115  		"\"ab\nc\"",   // newline
   116  	} {
   117  		if n, ok := quotedLength(input); ok {
   118  			t.Errorf("quotedLength(%s) = %d, want !ok", input, n)
   119  		}
   120  	}
   121  }