| // 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" |
| ) |
| |
| var ( |
| testRemoveLines = ` |
| a test line |
| a line with foo and more |
| another line |
| ` |
| wantRemovedLines = ` |
| a test line |
| another line |
| ` |
| ) |
| |
| func TestFlattenString(t *testing.T) { |
| tests := []struct { |
| source string |
| length int |
| want string |
| }{ |
| {"", 0, ""}, |
| {"", -1, ""}, |
| {"abc", 3, "abc"}, |
| {"abc", 2, "ab"}, |
| {"abc", 1, "a"}, |
| {"abc", 0, ""}, |
| {"abc", -1, "abc"}, |
| {"日本語", 3, "日本語"}, |
| {"日本語", 2, "日本"}, |
| {"日本語", 1, "日"}, |
| {"日本語", 0, ""}, |
| {"a\nb", 10, `a\nb`}, |
| {"a\nb", 2, `a\n`}, |
| {"a", 1, "a"}, |
| {"a", 0, ""}, |
| {"a\nb\nc", 10, `a\nb\nc`}, |
| {"a\nb\nc", -1, `a\nb\nc`}, |
| {"日\n本\n語", 10, `日\n本\n語`}, |
| {"日\n本\n語", 5, `日\n本\n語`}, |
| {"日\n本\n語", 4, `日\n本\n`}, |
| {"日\n本\n語", 3, `日\n本`}, |
| {"日\n本\n語", 2, `日\n`}, |
| {"日\n本\n語", 1, "日"}, |
| {"日\n本\n語", 0, ""}, |
| } |
| |
| for i, tt := range tests { |
| testname := fmt.Sprintf("(%q, %d)[%d]", tt.source, tt.length, i) |
| t.Run(testname, func(t *testing.T) { |
| got := flattenString(tt.source, tt.length) |
| if got != tt.want { |
| t.Errorf("want %q, got %q", tt.want, got) |
| } |
| }) |
| } |
| } |
| |
| func TestAddIfNeeded(t *testing.T) { |
| tests := []struct { |
| source, key, loc, text string |
| want string |
| }{ |
| {"", "", "", "", ""}, |
| {"a imp b f1", "f2", "imp", "new", "a imp b f1"}, |
| {"a imp b f1", "f1", "x", "new", "a imp b f1"}, |
| {"a imp b f1", "f1", "imp", "new", "a imp b f1new"}, |
| {"aimpbf1", "f1", "imp", "new", "aimpbf1new"}, // Not assuming word boundaries. |
| {"a imp\n b f1", "f1", "imp", "new", "a imp\nnew b f1"}, |
| {"a \nmimp b \nf1", "f1", "(?m)^imp", "new", "a \nmimp b \nf1"}, |
| {"a \nimp b \nf1", "f1", "(?m)^imp", "new", "a \nimp b \nnewf1"}, // Match line start. |
| {"a imp x y z\n b f1", "f1", "imp", "new", "a imp x y z\nnew b f1"}, |
| {"a imp x y z\n\n b f1", "f1", "imp", "new", "a imp x y z\nnew\n b f1"}, |
| {"a imp x\nimp y\nimp z\n b f1", "f1", "imp", "new", "a imp x\nimp y\nimp z\nnew b f1"}, |
| {"a imp x\nimp y\nimp z\n b f1", "f1", "imp", "new\n", "a imp x\nimp y\nimp z\nnew\n b f1"}, |
| } |
| |
| for i, tt := range tests { |
| testname := fmt.Sprintf("(%q, %q, %q, %q)[%d]", tt.source, tt.key, tt.loc, tt.text, i) |
| t.Run(testname, func(t *testing.T) { |
| got, _ := addIfNeeded(tt.source, tt.key, tt.loc, tt.text) |
| if got != tt.want { |
| t.Errorf("want %q, got %q", tt.want, got) |
| } |
| }) |
| } |
| } |
| |
| func TestAddIfNeededErrors(t *testing.T) { |
| tests := []struct { |
| source, key, loc, text string |
| expError string |
| }{ |
| {"", "", "", "", ""}, |
| {"a imp b f1", "f2", "imp", "new", ""}, |
| {"a imp b f1", "f1", "x", "new", "not found"}, |
| {"a imp b f1", "f1", "imp", "new", ""}, |
| {"aimpbf1", "f1", "imp", "new", ""}, // Not assuming word boundaries. |
| {"a imp\n b f1", "f1", "imp", "new", ""}, |
| {"a \nmimp b \nf1", "f1", "(?m)^imp", "new", "not found"}, |
| {"a \nimp b \nf1", "f1", "(?m)^imp", "new", ""}, // Match line start. |
| {"a imp x y z\n b f1", "f1", "imp", "new", ""}, |
| {"a imp x y z\n\n b f1", "f1", "imp", "new", ""}, |
| {"a imp x\nimp y\nimp z\n b f1", "f1", "imp", "new", ""}, |
| {"a imp x\nimp y\nimp z\n b f1", "f1", "imp", "new\n", ""}, |
| {"a imp x\nimp y\nimp z\n b f1", "f[[{1", "imp", "new\n", "couldn't compile"}, |
| } |
| |
| for i, tt := range tests { |
| testname := fmt.Sprintf("(%q, %q, %q, %q)[%d]", tt.source, tt.key, tt.loc, tt.text, i) |
| t.Run(testname, func(t *testing.T) { |
| _, err := addIfNeeded(tt.source, tt.key, tt.loc, tt.text) |
| if (err == nil && tt.expError != "") || (err != nil && (tt.expError == "" || !strings.Contains(err.Error(), tt.expError))) { |
| t.Errorf("want error like %q, got %v", tt.expError, err) |
| } |
| }) |
| } |
| } |
| |
| func TestRemoveIfNotNeeded(t *testing.T) { |
| tests := []struct { |
| source, key, text string |
| want string |
| }{ |
| {"", "", "", ""}, |
| {"a imp b f1", "f1", "imp", "a imp b f1"}, |
| {"a imp\n b \nxf1", "(?m)^f1", "imp\n", "a b \nxf1"}, |
| {"a imp\n b \nf1", "(?m)^f1", "imp\n", "a imp\n b \nf1"}, |
| {"a imp b f1", "f2", "imp", "a b f1"}, |
| {"a imp b fna", "fn", "imp", "a imp b fna"}, // Substrings match. |
| } |
| |
| for i, tt := range tests { |
| testname := fmt.Sprintf("(%q, %q, %q)[%d]", tt.source, tt.key, tt.text, i) |
| t.Run(testname, func(t *testing.T) { |
| got, _ := removeIfNotNeeded(tt.source, tt.key, tt.text) |
| if got != tt.want { |
| t.Errorf("want %q, got %q", tt.want, got) |
| } |
| }) |
| } |
| } |
| |
| func TestRemoveIfNotNeededErrors(t *testing.T) { |
| tests := []struct { |
| source, key, text string |
| expError string |
| }{ |
| {"", "", "", ""}, |
| {"a imp b f1", "f1", "imp", ""}, |
| {"a imp\n b \nxf1", "(?m)^f1", "imp\n", ""}, |
| {"a imp\n b \nf1", "(?m)^f1", "imp\n", ""}, |
| {"a imp b f1", "f2", "imp", ""}, |
| {"a imp b fna", "fn", "imp", ""}, // Substrings match. |
| {"a imp b fna", "f][}n", "imp", "couldn't compile"}, |
| } |
| |
| for i, tt := range tests { |
| testname := fmt.Sprintf("(%q, %q, %q)[%d]", tt.source, tt.key, tt.text, i) |
| t.Run(testname, func(t *testing.T) { |
| _, err := removeIfNotNeeded(tt.source, tt.key, tt.text) |
| if (err == nil && tt.expError != "") || (err != nil && (tt.expError == "" || !strings.Contains(err.Error(), tt.expError))) { |
| t.Errorf("want error like %q, got %v", tt.expError, err) |
| } |
| }) |
| } |
| } |
| |
| func TestRemoveLineWith(t *testing.T) { |
| tests := []struct { |
| source, key string |
| want string |
| }{ |
| {"", "", ""}, |
| {"a b c", "b", ""}, |
| {"a\nb\nc", "b", "a\nc"}, |
| {"a\nb\nb\nc", "b", "a\nb\nb\nc"}, |
| {"abc \n def\n ghi", "de", "abc \n ghi"}, // Match is not on word boundary. |
| {"\nfoo line \n", "foo", "\n"}, |
| {testRemoveLines, "foo", wantRemovedLines}, |
| } |
| |
| for i, tt := range tests { |
| testname := fmt.Sprintf("(%q, %q)[%d]", tt.source, tt.key, i) |
| t.Run(testname, func(t *testing.T) { |
| got := RemoveLineWith(tt.source, tt.key) |
| if got != tt.want { |
| t.Errorf("want %q, got %q", tt.want, got) |
| } |
| }) |
| } |
| } |