| // Copyright 2020 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package codifier |
| |
| import ( |
| "fmt" |
| "strings" |
| "testing" |
| |
| "github.com/google/go-cmp/cmp" |
| ) |
| |
| func TestListContains(t *testing.T) { |
| tests := []struct { |
| list orderedStrings |
| item string |
| want bool |
| }{ |
| {orderedStrings{}, "", false}, |
| {orderedStrings{"a"}, "", false}, |
| {orderedStrings{"a"}, "b", false}, |
| {orderedStrings{"a"}, "a", true}, |
| {orderedStrings{"a", "b"}, "", false}, |
| {orderedStrings{"a", "b"}, "a", true}, |
| } |
| |
| for i, tt := range tests { |
| testname := fmt.Sprintf("(%q, %q)[%d]", tt.list, tt.item, i) |
| t.Run(testname, func(t *testing.T) { |
| got := tt.list.contains(tt.item) |
| if got != tt.want { |
| t.Errorf("want %v, got %v", tt.want, got) |
| } |
| }) |
| } |
| } |
| |
| func TestListAddItem(t *testing.T) { |
| tests := []struct { |
| list orderedStrings |
| items []string |
| wantList orderedStrings |
| wantRes bool |
| }{ |
| {nil, nil, nil, false}, |
| {nil, orderedStrings{}, nil, false}, |
| {orderedStrings{}, nil, orderedStrings{}, false}, |
| {orderedStrings{}, orderedStrings{}, orderedStrings{}, false}, |
| {orderedStrings{}, []string{""}, orderedStrings{}, false}, |
| {orderedStrings{}, []string{""}, orderedStrings{}, false}, // #5 |
| {orderedStrings{"a"}, []string{""}, orderedStrings{"a"}, false}, |
| {orderedStrings{"a"}, []string{"a"}, orderedStrings{"a"}, false}, |
| {orderedStrings{"a"}, []string{"b"}, orderedStrings{"a", "b"}, true}, |
| {orderedStrings{"a", "b"}, []string{""}, orderedStrings{"a", "b"}, false}, |
| {orderedStrings{"a", "b"}, []string{"a"}, orderedStrings{"a", "b"}, false}, // #10 |
| {orderedStrings{"a", "b"}, []string{"c"}, orderedStrings{"a", "b", "c"}, true}, |
| {orderedStrings{"a", "b"}, []string{"c", "d"}, orderedStrings{"a", "b", "c", "d"}, true}, |
| } |
| |
| for i, tt := range tests { |
| testname := fmt.Sprintf("(%q, %q)[%d]", tt.list, tt.items, i) |
| t.Run(testname, func(t *testing.T) { |
| got := tt.list.Add(tt.items...) |
| if diff := cmp.Diff(tt.wantList, tt.list); diff != "" { |
| t.Errorf("listAdd() mismatch (-want +got):\n%s", diff) |
| } |
| if got != tt.wantRes { |
| t.Errorf("want %v, got %v\n", tt.wantRes, got) |
| } |
| }) |
| } |
| } |
| |
| func TestListOrderedStringsLoad(t *testing.T) { |
| tests := []struct { |
| in string |
| wantList orderedStrings |
| wantErr string |
| }{ |
| {"", nil, ""}, |
| {" \n \n\n", nil, ""}, |
| {" # \n# \n ### \n", nil, ""}, |
| {"foo", orderedStrings{"foo"}, ""}, |
| {" foo \n ", orderedStrings{"foo"}, ""}, |
| {"#foo", nil, ""}, |
| {"#foo\nfoo#ccc", orderedStrings{"foo"}, ""}, |
| {"foo\nfoo#ccc", nil, "duplicate entry"}, |
| {"foo\nbar#ccc#ddd#eee", orderedStrings{"foo", "bar"}, ""}, |
| {"foo\nbar\nbaz#ddd\n#eee", orderedStrings{"foo", "bar", "baz"}, ""}, |
| {"foo\n #ccc\nbar\nbaz#ddd\n#eee", orderedStrings{"foo", "bar", "baz"}, ""}, |
| } |
| |
| for i, tt := range tests { |
| testname := fmt.Sprintf("(%q)[%d]", tt.in, i) |
| t.Run(testname, func(t *testing.T) { |
| got, err := orderedStringsLoad(strings.NewReader(tt.in)) |
| if diff := cmp.Diff(tt.wantList, got); diff != "" { |
| t.Errorf("orderedStringsLoad() mismatch (-want +got):\n%s", diff) |
| } |
| if (err != nil && tt.wantErr == "") || (tt.wantErr != "" && !strings.Contains(err.Error(), tt.wantErr)) { |
| t.Errorf("want %v, got %v\n", tt.wantErr, err) |
| } |
| }) |
| } |
| } |