github.com/sc0rp1us/gb@v0.4.1-0.20160319180011-4ba8cf1baa5a/depfile/depfile_test.go (about)

     1  package depfile
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestParseKeyVal(t *testing.T) {
    11  	tests := []struct {
    12  		args []string
    13  		want map[string]string
    14  		err  error
    15  	}{{
    16  		args: []string{},          // handled by Parse
    17  		want: map[string]string{}, // expected
    18  	}, {
    19  		args: []string{"version"},
    20  		err:  fmt.Errorf("expected key=value pair, got %q", "version"),
    21  	}, {
    22  		args: []string{"=9.9.9"},
    23  		err:  fmt.Errorf("expected key=value pair, missing key %q", "=9.9.9"),
    24  	}, {
    25  		args: []string{"version="},
    26  		err:  fmt.Errorf("expected key=value pair, missing value %q", "version="),
    27  	}, {
    28  		args: []string{"version=1.2.3"},
    29  		want: map[string]string{
    30  			"version": "1.2.3",
    31  		},
    32  	}, {
    33  		args: []string{"version=1.2.3", "version=2.4.5"},
    34  		err:  fmt.Errorf("duplicate key=value pair, have \"version=1.2.3\" got %q", "version=2.4.5"),
    35  	}, {
    36  		args: []string{"version=1.2.3", "//", "comment"},
    37  		err:  fmt.Errorf("expected key=value pair, got %q", "//"),
    38  	}, {
    39  		args: []string{"vcs=git", "version=1.2.3"},
    40  		want: map[string]string{
    41  			"version": "1.2.3",
    42  			"vcs":     "git",
    43  		},
    44  	}}
    45  
    46  	for _, tt := range tests {
    47  		got, err := parseKeyVal(tt.args)
    48  		if !reflect.DeepEqual(err, tt.err) {
    49  			t.Errorf("parseKeyVal(%v): got %v, expected %v", tt.args, err, tt.err)
    50  			continue
    51  		}
    52  		if err == nil && !reflect.DeepEqual(tt.want, got) {
    53  			t.Errorf("parseKeyVal(%v): got %#v, expected %#v", tt.args, got, tt.want)
    54  		}
    55  	}
    56  }
    57  
    58  func TestParseLine(t *testing.T) {
    59  	tests := []struct {
    60  		line string
    61  		name string
    62  		kv   map[string]string
    63  		err  error
    64  	}{{
    65  		line: "a", // no \n, sc.Text removes it
    66  		err:  fmt.Errorf("a: expected key=value pair after name"),
    67  	}, {
    68  		line: "a\ta",
    69  		err:  fmt.Errorf("a: expected key=value pair, got %q", "a"),
    70  	}, {
    71  		line: "a\tversion=7\t  ",
    72  		name: "a",
    73  		kv: map[string]string{
    74  			"version": "7",
    75  		},
    76  	}}
    77  
    78  	for _, tt := range tests {
    79  		name, kv, err := parseLine(tt.line)
    80  		if !reflect.DeepEqual(err, tt.err) {
    81  			t.Errorf("parseLine(%q): got %v, expected %v", tt.line, err, tt.err)
    82  			continue
    83  		}
    84  		if err == nil && !reflect.DeepEqual(tt.kv, kv) || name != tt.name {
    85  			t.Errorf("parseLine(%q): got %s %#v, expected %s %#v", tt.line, name, kv, tt.name, tt.kv)
    86  		}
    87  	}
    88  }
    89  
    90  func TestParse(t *testing.T) {
    91  	tests := []struct {
    92  		c    string
    93  		want map[string]map[string]string
    94  		err  error
    95  	}{{
    96  		c:    "",                                 // empty
    97  		want: make(map[string]map[string]string), // empty map, not nil
    98  	}, {
    99  		c:    "\n",                               // effectively empty
   100  		want: make(map[string]map[string]string), // empty map, not nil
   101  	}, {
   102  		c:   "github.com/pkg/profile", // no \n
   103  		err: fmt.Errorf("1: github.com/pkg/profile: expected key=value pair after name"),
   104  	}, {
   105  		c:   "github.com/pkg/profile\n",
   106  		err: fmt.Errorf("1: github.com/pkg/profile: expected key=value pair after name"),
   107  	}, {
   108  		c: "github.com/pkg/profile version=1.2.3", // no \n
   109  		want: map[string]map[string]string{
   110  			"github.com/pkg/profile": {
   111  				"version": "1.2.3",
   112  			},
   113  		},
   114  	}, {
   115  		c: "github.com/pkg/profile version=1.2.3\n",
   116  		want: map[string]map[string]string{
   117  			"github.com/pkg/profile": {
   118  				"version": "1.2.3",
   119  			},
   120  		},
   121  	}, {
   122  		c: "// need to pin version\ngithub.com/pkg/profile version=1.2.3\n",
   123  		want: map[string]map[string]string{
   124  			"github.com/pkg/profile": {
   125  				"version": "1.2.3",
   126  			},
   127  		},
   128  	}, {
   129  		c: "github.com/pkg/profile version=1.2.3\ngithub.com/pkg/sftp version=0.0.0",
   130  		want: map[string]map[string]string{
   131  			"github.com/pkg/profile": {
   132  				"version": "1.2.3",
   133  			},
   134  			"github.com/pkg/sftp": {
   135  				"version": "0.0.0",
   136  			},
   137  		},
   138  	}, {
   139  		c: `# some comment
   140  github.com/pkg/profile version=0.1.0
   141  
   142  ; some other comment
   143  // third kind of comment
   144   lines starting with blank lines are also ignored
   145  github.com/pkg/sftp version=0.2.1
   146  `,
   147  		want: map[string]map[string]string{
   148  			"github.com/pkg/profile": {
   149  				"version": "0.1.0",
   150  			},
   151  			"github.com/pkg/sftp": {
   152  				"version": "0.2.1",
   153  			},
   154  		},
   155  	}}
   156  
   157  	for _, tt := range tests {
   158  		r := strings.NewReader(tt.c)
   159  		got, err := Parse(r)
   160  		if !reflect.DeepEqual(err, tt.err) {
   161  			t.Errorf("Parse(%q): got %v, expected %v", tt.c, err, tt.err)
   162  			continue
   163  		}
   164  		if err == nil && !reflect.DeepEqual(tt.want, got) {
   165  			t.Errorf("Parse(%q): got %#v, expected %#v", tt.c, got, tt.want)
   166  		}
   167  	}
   168  }
   169  
   170  func TestSplitLine(t *testing.T) {
   171  	tests := []struct {
   172  		s    string
   173  		want []string
   174  	}{
   175  		{s: "", want: nil},
   176  		{s: "a", want: []string{"a"}},
   177  		{s: " a", want: []string{"a"}},
   178  		{s: "a ", want: []string{"a"}},
   179  		{s: "a b", want: []string{"a", "b"}},
   180  		{s: "a b", want: []string{"a", "b"}},
   181  		{s: "a\tb", want: []string{"a", "b"}},
   182  		{s: "a \tb", want: []string{"a", "b"}},
   183  		{s: "\ta \tb ", want: []string{"a", "b"}},
   184  	}
   185  
   186  	for _, tt := range tests {
   187  		got := splitLine(tt.s)
   188  		if !reflect.DeepEqual(got, tt.want) {
   189  			t.Errorf("splitLine(%q): got %#v, expected %#v", tt.s, got, tt.want)
   190  		}
   191  	}
   192  }