| // 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 TestFindMatchingDelimiters(t *testing.T) { |
| testList := []struct { |
| source string |
| od, cd rune |
| want string |
| }{ |
| {"", '{', '}', ""}, |
| {"{", '{', '}', ""}, |
| {"}", '{', '}', ""}, |
| {"}{", '{', '}', ""}, |
| {"{}", '{', '}', "{}"}, |
| {"[]", '[', ']', "[]"}, |
| {"s{ s} aaaaa", '{', '}', "s{ s}"}, // Includes the string start. |
| {"s{ \ns}aa aa aaa", '{', '}', "s{ \ns}"}, |
| {" s{日本語} aaa }", '{', '}', " s{日本語}"}, |
| {" s{日[本♬]語} aaa }", '[', ']', " s{日[本♬]"}, |
| {" s{日[本♬語} aaa }", '[', ']', ""}, |
| {"{♛,♬{日{}本語}♠, ♧}, ♡, ", '{', '}', "{♛,♬{日{}本語}♠, ♧}"}, |
| {"{♛,♬{日{}本語}♠, ♧}, ♡,} ", '{', '}', "{♛,♬{日{}本語}♠, ♧}"}, |
| {"{♛,♬{日{}{{{本語}♠, ♧}, ♡,} ", '{', '}', ""}, |
| {"⌘⌘{{♛,♬}{[日}{}{{{本語}♠, ♧}}, ♡,} ", '{', '}', "⌘⌘{{♛,♬}{[日}{}{{{本語}♠, ♧}}, ♡,}"}, |
| } |
| |
| for i, tt := range testList { |
| testname := fmt.Sprintf("(%q, %q, %q)[%d]", i, tt.source, tt.od, tt.cd) |
| t.Run(testname, func(t *testing.T) { |
| got := findMatchingDelimiters(tt.source, tt.od, tt.cd) |
| if got != tt.want { |
| t.Errorf("want %q, got %q", tt.want, got) |
| } |
| }) |
| } |
| } |
| |
| func TestFindIndicesForAll(t *testing.T) { |
| testList := []struct { |
| source, atype, name string |
| delimiter rune |
| want [][]int |
| wantErr string |
| }{ |
| {"", "", "", rune(0), nil, "blank source"}, |
| {`group("test"){`, "", "", '{', nil, "blank sectionType"}, |
| {`xx group("tests") {xx`, "gro", "", '{', nil, "blank sectionName"}, |
| {`xx group("tests") {xx`, "gro", "[[[", '{', nil, "could not compile regex"}, |
| {`xx group("tests") {xx`, "gro", "test", '{', nil, ""}, |
| {`xx group("tests") {xx`, "group", "test", '{', nil, ""}, |
| // 00000000001111111111222 |
| // 01234567890123456789012 |
| {`group("tests") [xx`, "group", "tests", '[', [][]int{{0, 17}}, ""}, |
| {`xx group("tests") [xx`, "group", "tests", '[', [][]int{{2, 21}}, ""}, |
| {`xx group("tests") {xx`, "group", "tests", '{', [][]int{{2, 21}}, ""}, |
| {`xx group("tests") {{x{x`, "group", "tests", '{', [][]int{{2, 21}}, ""}, |
| {`xx group("tests") {{x{x`, "tests", "group", '{', nil, ""}, |
| {`xx group("tests") [xx`, "", "tests", '{', nil, ""}, |
| {`xx group("tests") [xx`, "", "tests", '[', nil, ""}, |
| {`xx group("tests") [xx`, "group", "", '[', nil, ""}, |
| {`xx group("tests") [xx`, "group", "tests", rune(0), nil, ""}, |
| } |
| |
| for i, tt := range testList { |
| testname := fmt.Sprintf("(%q, %q, %q, %q)[%d]", tt.source, tt.atype, tt.name, tt.delimiter, i) |
| t.Run(testname, func(t *testing.T) { |
| got, err := findIndicesForAll(tt.source, tt.atype, tt.name, tt.delimiter) |
| if err == nil && tt.wantErr != "" || err != nil && tt.wantErr != "" && !strings.Contains(err.Error(), tt.wantErr) { |
| t.Errorf("want err like %v; got err: %q", tt.wantErr, err) |
| } |
| if diff := cmp.Diff(tt.want, got); diff != "" { |
| t.Errorf("findIndicesForAll() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| } |
| |
| func TestFindIndicesForAllSectionsOfType(t *testing.T) { |
| testList := []struct { |
| source, atype string |
| delimiter rune |
| want [][]int |
| wantErr string |
| }{ |
| {"", "", rune(0), nil, "blank source"}, |
| {`group("test"){`, "", '{', nil, "blank sectionType"}, |
| {`group("test"){`, "[]((", '{', nil, "could not compile regex"}, |
| {`xx group("tests") {xx`, "gro", '{', nil, ""}, |
| {`xx group("tests") {xx`, "tests", '{', nil, ""}, |
| // 00000000001111111111222 |
| // 01234567890123456789012 |
| {`test("foo") [xx`, "test", '[', [][]int{{0, 14, 6, 9}}, ""}, |
| {`foo-test("foo") [xx`, "test", '[', nil, ""}, |
| {`foo_test("foo") [xx`, "test", '[', nil, ""}, |
| {`foo!test("foo") [xx`, "test", '[', nil, ""}, |
| {`foo=test("foo") [xx`, "test", '[', nil, ""}, |
| {`foo&test("foo") [xx`, "test", '[', nil, ""}, |
| {`fooдtest("foo") [xx`, "test", '[', nil, ""}, |
| // 00000000001111111111222 |
| // 01234567890123456789012 |
| {`group("tests") [xx`, "group", '[', [][]int{{0, 17, 7, 12}}, ""}, |
| {`xx group("tests") [xx`, "group", '[', [][]int{{4, 21, 11, 16}}, ""}, |
| {`xx group("tests") {xx`, "group", '{', [][]int{{4, 21, 11, 16}}, ""}, |
| {`)group("tests") {{x{x`, "group", '{', [][]int{{0, 18, 8, 13}}, ""}, |
| {`xx group("tests") {{x{x`, "group", '{', [][]int{{4, 21, 11, 16}}, ""}, |
| {`xx group("tests") {{x{x`, "tests", '{', nil, ""}, |
| {`xx group("tests") [xx`, "", '[', nil, "blank sectionType"}, |
| {`xx group("tests") [xx`, "group", rune(0), nil, ""}, |
| // 000000000011111111112222222222333333333344444 |
| // 012345678901234567890123456789012345678901234 |
| { |
| `xx group("tests") {xx}\n group("foo") {xx}`, "group", '{', |
| [][]int{{4, 21, 11, 16}, {27, 42, 34, 37}}, |
| "", |
| }, |
| // 000000000011111111112222222222333333333344444444445 |
| // 012345678901234567890123456789012345678901234567890 |
| { |
| `x("bb")z("bb")g("b"){abbc}g("c"){abbc}g("d"){abbc}`, "g", '{', |
| [][]int{{14, 21, 17, 18}, {26, 33, 29, 30}, {38, 45, 41, 42}}, |
| "", |
| }, |
| } |
| |
| for i, tt := range testList { |
| testname := fmt.Sprintf("(%q, %q, %q)[%d]", tt.source, tt.atype, tt.delimiter, i) |
| t.Run(testname, func(t *testing.T) { |
| got, err := findIndicesForAllSectionsOfType(tt.source, tt.atype, tt.delimiter) |
| if err == nil && tt.wantErr != "" || err != nil && tt.wantErr != "" && !strings.Contains(err.Error(), tt.wantErr) { |
| t.Errorf("want err like %v; got err: %q", tt.wantErr, err) |
| } |
| if diff := cmp.Diff(tt.want, got); diff != "" { |
| t.Errorf("findIndicesForAllSectionsOfType() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| } |
| |
| func TestFindIndicesForAllSectionsNamed(t *testing.T) { |
| testList := []struct { |
| source, name string |
| delimiter rune |
| want [][]int |
| wantErr string |
| }{ |
| {"", "", rune(0), nil, "blank source"}, |
| {`group("test"){`, "", '{', nil, "blank sectionName"}, |
| {`group("test"){`, "[([", '{', nil, "could not compile"}, |
| {`xx group("tests") {xx`, "test", '{', nil, ""}, |
| {`xx group("tests") {xx`, "test", '{', nil, ""}, |
| // 00000000001111111111222 |
| // 01234567890123456789012 |
| {`test("foo") [xx`, "foo", '[', [][]int{{0, 14, 0, 4}}, ""}, |
| {`-test("foo") [xx`, "foo", '[', nil, ""}, |
| {"\n\ntest(\"foo\") [xx", "foo", '[', [][]int{{2, 16, 2, 6}}, ""}, |
| {`group("tests") [xx`, "tests", '[', [][]int{{0, 17, 0, 5}}, ""}, |
| {`xx group("tests") [xx`, "tests", '[', [][]int{{4, 21, 4, 9}}, ""}, |
| {`xx group("tests") {xx`, "tests", '{', [][]int{{4, 21, 4, 9}}, ""}, |
| {`xx group("tests") {{x{x`, "tests", '{', [][]int{{4, 21, 4, 9}}, ""}, |
| {`xx group("tests") {{x{x`, "group", '{', nil, ""}, |
| {`xx group("tests") [xx`, "tests", '{', nil, ""}, |
| {`xx group("tests") [xx`, "", '[', nil, "blank sectionName"}, |
| {`xx group("tests") [xx`, "tests", rune(0), nil, ""}, |
| // 000000000011111111112222222222333333333344444 |
| // 012345678901234567890123456789012345678901234 |
| { |
| `xx group("tests") {xx}\n foo("tests") {xx}`, "tests", '{', |
| [][]int{{4, 21, 4, 9}, {27, 42, 27, 30}}, |
| "", |
| }, |
| // 0000000000111111111122222222223333333333444 |
| // 0123456789012345678901234567890123456789012 |
| { |
| `}group("tests") {xx}\n}foo("tests") {xx}`, "tests", '{', |
| [][]int{{0, 18, 1, 6}, {24, 39, 24, 27}}, |
| "", |
| }, |
| } |
| |
| for i, tt := range testList { |
| testname := fmt.Sprintf("(%q, %q, %q)[%d]", tt.source, tt.name, tt.delimiter, i) |
| t.Run(testname, func(t *testing.T) { |
| got, err := findIndicesForAllSectionsNamed(tt.source, tt.name, tt.delimiter) |
| if err == nil && tt.wantErr != "" || err != nil && tt.wantErr != "" && !strings.Contains(err.Error(), tt.wantErr) { |
| t.Errorf("want err like %v; got err: %q", tt.wantErr, err) |
| } |
| if diff := cmp.Diff(tt.want, got); diff != "" { |
| t.Errorf("findIndicesForAllSectionsNamed() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| } |
| |
| func TestSectionsWithTypeAndName(t *testing.T) { |
| if got, _ := sectionsWithTypeAndName("", "", "", rune(0), rune(0)); got != nil { |
| t.Errorf("('','','', rune(0), rune(0)) not nil, but %v", got) |
| } |
| |
| sectionTests := []struct { |
| source string |
| kind, name string |
| od, cd rune |
| want []SectionType |
| wantErr string |
| }{ |
| {"", "group", "tests", rune(0), rune(0), nil, "blank source"}, |
| {`group("tests"){ sdf `, "group", "tests", '{', '}', nil, "matching delimiters"}, |
| {`group("tests")[ sdf `, "group", "tests", '[', ']', nil, "matching delimiters"}, |
| {`group("tests") sdf }`, "group", "tests", '{', '}', nil, "not found"}, |
| {`group("tests"){}`, "group", "tests", '{', '}', []SectionType{{"tests", "group", `group("tests"){}`}}, ""}, |
| {`group("tests"){ } aaaaaaa`, "group", "tests", '{', '}', []SectionType{{"tests", "group", `group("tests"){ }`}}, ""}, |
| {`group("tests"){ sdf }`, "group", "tests", '{', '}', []SectionType{{"tests", "group", `group("tests"){ sdf }`}}, ""}, |
| {` group("tests"){ sdf }`, "group", "tests", '{', '}', []SectionType{{"tests", "group", ` group("tests"){ sdf }`}}, ""}, |
| {` group("tests")[ sdf ]`, "group", "tests", '[', ']', []SectionType{{"tests", "group", ` group("tests")[ sdf ]`}}, ""}, |
| // Find more than one. |
| {`group("tests"){ sdf }\ngroup("tests"){ abc }`, "group", "tests", '{', '}', []SectionType{{"tests", "group", `group("tests"){ sdf }`}, {"tests", "group", `group("tests"){ abc }`}}, ""}, |
| } |
| |
| for i, tt := range sectionTests { |
| testname := fmt.Sprintf("(%q, %q, %q, %q, %q)[%d]", tt.source, tt.kind, tt.name, tt.od, tt.cd, i) |
| t.Run(testname, func(t *testing.T) { |
| got, err := sectionsWithTypeAndName(tt.source, tt.kind, tt.name, tt.od, tt.cd) |
| if err == nil && tt.wantErr != "" || err != nil && tt.wantErr != "" && !strings.Contains(err.Error(), tt.wantErr) { |
| t.Errorf("want err like %v; got err: %q", tt.wantErr, err) |
| } |
| if diff := cmp.Diff(tt.want, got); diff != "" { |
| t.Errorf("sectionsWithTypeAndName() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| } |
| |
| func TestSectionsOfType(t *testing.T) { |
| testList := []struct { |
| source, kind string |
| open, close rune |
| want []SectionType |
| wantErr string |
| }{ |
| {"", "", rune(0), rune(0), nil, "blank source"}, |
| {`group("test"){`, "", '{', '}', nil, "blank section"}, |
| {`xx group("tests") {xx`, "gro", '{', '}', nil, "not found"}, |
| {`xx group("tests") {xx`, "tests", '{', '}', nil, "not found"}, |
| {`group("tests") [xx`, "group", '[', ']', nil, "matching delimiters"}, |
| {`group("tests") [xx]`, "group", '[', ']', []SectionType{{"tests", "group", `group("tests") [xx]`}}, ""}, |
| {`xx group("tests") [xx]`, "group", '[', ']', []SectionType{{"tests", "group", `group("tests") [xx]`}}, ""}, |
| {`xx group("tests") {x{x}}`, "group", '{', '}', []SectionType{{"tests", "group", `group("tests") {x{x}}`}}, ""}, |
| |
| // Note that if there are nested delimiters, they must match. |
| {`xx group("tests") {x{x}`, "group", '{', '}', nil, "matching delimiters"}, |
| {`xx group("tests") {x{{x}`, "group", '{', '}', nil, "matching delimiters"}, |
| {`xx group("tests") {xx{}`, "group", '{', '}', nil, "matching delimiters"}, |
| |
| {`xx group("tests") [xx`, "", '{', '}', nil, "blank section"}, |
| {`xx group("tests") [xx`, "", '[', ']', nil, "blank section"}, |
| {`xx group("tests") [xx`, "group", rune(0), rune(0), nil, "not found"}, |
| // Example of grabbing multiple sections with the same type. |
| { |
| `xx group("tests") {xx}\n group("foo") {yy}`, "group", '{', '}', |
| []SectionType{{"tests", "group", `group("tests") {xx}`}, {"foo", "group", `group("foo") {yy}`}}, |
| "", |
| }, |
| } |
| |
| for i, tt := range testList { |
| testname := fmt.Sprintf("(%q, %q, %q, %q)[%d]", tt.source, tt.kind, tt.open, tt.close, i) |
| t.Run(testname, func(t *testing.T) { |
| got, err := sectionsOfType(tt.source, tt.kind, tt.open, tt.close) |
| if err == nil && tt.wantErr != "" || err != nil && tt.wantErr != "" && !strings.Contains(err.Error(), tt.wantErr) { |
| t.Errorf("want err like %v; got err: %q", tt.wantErr, err) |
| } |
| if diff := cmp.Diff(tt.want, got); diff != "" { |
| t.Errorf("sectionsOfType() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| } |
| |
| func TestSectionsNamed(t *testing.T) { |
| testList := []struct { |
| source, kind string |
| open, close rune |
| want []SectionType |
| wantErr string |
| }{ |
| {"", "", rune(0), rune(0), nil, "blank source"}, |
| {`group("test"){`, "", '{', '}', nil, "blank sectionName"}, |
| {`xx group("tests") {xx`, "tes", '{', '}', nil, "not found"}, |
| {`xx group("tests") {xx`, "tests", '{', '}', nil, "matching delimiters"}, |
| {`group("tests") [xx`, "tests", '[', ']', nil, "matching delimiters"}, |
| {`group("tests") [xx]`, "tests", '[', ']', []SectionType{{"tests", "group", `group("tests") [xx]`}}, ""}, |
| {`xx group("tests") [xx]`, "tests", '[', ']', []SectionType{{"tests", "group", `group("tests") [xx]`}}, ""}, |
| {`xx group("tests") {x{x}}`, "tests", '{', '}', []SectionType{{"tests", "group", `group("tests") {x{x}}`}}, ""}, |
| |
| // Note that if there are nested delimiters, they must match. |
| {`xx group("tests") {x{x}`, "tests", '{', '}', nil, "matching delimiters"}, |
| {`xx group("tests") {x{{x}`, "tests", '{', '}', nil, "matching delimiters"}, |
| {`xx group("tests") {xx{}`, "tests", '{', '}', nil, "matching delimiters"}, |
| |
| {`xx group("tests") [xx`, "", '{', '}', nil, "blank sectionName"}, |
| {`xx group("tests") [xx`, "", '[', ']', nil, "blank sectionName"}, |
| {`xx group("tests") [xx`, "tests", rune(0), rune(0), nil, "not found"}, |
| // Example of grabbing multiple sections with the same name. |
| { |
| `xx thing("foo") {xx}\n group("foo") {yy}`, "foo", '{', '}', |
| []SectionType{{"foo", "thing", `thing("foo") {xx}`}, {"foo", "group", `group("foo") {yy}`}}, |
| "", |
| }, |
| } |
| |
| for i, tt := range testList { |
| testname := fmt.Sprintf("(%q, %q, %q, %q)[%d]", tt.source, tt.kind, tt.open, tt.close, i) |
| t.Run(testname, func(t *testing.T) { |
| got, err := sectionsNamed(tt.source, tt.kind, tt.open, tt.close) |
| if err == nil && tt.wantErr != "" || err != nil && tt.wantErr != "" && !strings.Contains(err.Error(), tt.wantErr) { |
| t.Errorf("want err like %v; got err: %q", tt.wantErr, err) |
| } |
| if diff := cmp.Diff(tt.want, got); diff != "" { |
| t.Errorf("sectionsNamed() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| } |
| |
| // TODO(gboone): "deps" is GN-specific, so generify or move. |
| func TestExtractList(t *testing.T) { |
| testList := []struct { |
| source string |
| name string |
| want []string |
| wantErr string |
| }{ |
| {"", "", nil, ""}, |
| {"", "deps", nil, ""}, |
| {"\ndeps\n", "(([[", nil, "could not compile"}, |
| {"\ndeps\n", "deps", nil, ""}, |
| {"deps = []", "deps", nil, ""}, |
| {"deps= []", "deps", nil, ""}, |
| {"deps=[]", "deps", nil, ""}, |
| {"deps += []", "deps", nil, ""}, |
| {"deps+= []", "deps", nil, ""}, |
| {"deps +=[]", "deps", nil, ""}, |
| {"deps+=[]", "deps", nil, ""}, |
| {"deps = [,]", "deps", nil, ""}, |
| {"deps += [,]", "deps", nil, ""}, |
| {"deps = [a]", "deps", []string{"a"}, ""}, |
| {"deps = [a]", "foo", nil, ""}, |
| {"deps += [a]", "deps", []string{"a"}, ""}, |
| {"deps + = [a]", "deps", nil, ""}, |
| {"deps ++= [a]", "deps", nil, ""}, |
| {"deps +== [a]", "deps", nil, ""}, |
| {"deps+= [a]", "deps", []string{"a"}, ""}, |
| {"deps +=[a]", "deps", []string{"a"}, ""}, |
| {"deps+=[a]", "deps", []string{"a"}, ""}, |
| {"deps = [a ]", "deps", []string{"a"}, ""}, |
| {"deps += [a ]", "deps", []string{"a"}, ""}, |
| {"deps = [a,b]", "deps", []string{"a", "b"}, ""}, |
| {"deps += [a,b]", "deps", []string{"a", "b"}, ""}, |
| {"deps = [a, b]", "deps", []string{"a", "b"}, ""}, |
| {"deps += ['a']", "deps", []string{"a"}, ""}, |
| {"deps = ['a','b']", "deps", []string{"a", "b"}, ""}, |
| {"deps = ['a', 'b']", "deps", []string{"a", "b"}, ""}, |
| {`deps = ["a"]`, "deps", []string{"a"}, ""}, |
| {`deps = ["a","b"]`, "deps", []string{"a", "b"}, ""}, |
| {`deps = ["a", "b"]`, "deps", []string{"a", "b"}, ""}, |
| {`deps = ["a", "b",'c']`, "deps", []string{"a", "b", "c"}, ""}, |
| {`deps += ["a", "b",'c']`, "deps", []string{"a", "b", "c"}, ""}, |
| {"\ndeps \n=\n[\n \"a\", \n\n \"\nb\"\n ]", "deps", []string{"a", "b"}, ""}, |
| } |
| |
| for i, tt := range testList { |
| testname := fmt.Sprintf("(%q, %q)[%d]", tt.source, tt.name, i) |
| t.Run(testname, func(t *testing.T) { |
| got, err := extractList(tt.source, tt.name) |
| if err == nil && tt.wantErr != "" || err != nil && tt.wantErr != "" && !strings.Contains(err.Error(), tt.wantErr) { |
| t.Errorf("want err like %v; got err: %q", tt.wantErr, err) |
| } |
| if diff := cmp.Diff(tt.want, got); diff != "" { |
| t.Errorf("extractList() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| } |
| |
| func TestGetValue(t *testing.T) { |
| testList := []struct { |
| source, key, want string |
| }{ |
| {"", "", ""}, |
| {"a=b", "a", ""}, |
| {`a="b"`, "a", "b"}, |
| {` a="b"`, "a", "b"}, |
| {`a ="b"`, "a", "b"}, |
| {`a= "b"`, "a", "b"}, |
| {`a="b" `, "a", "b"}, |
| {`xyz a="b" abc`, "a", "b"}, |
| {`a="b "`, "a", ""}, // Malformed. No spaces allowed in variables. |
| {`a="b""`, "a", "b"}, // There's no check for extra trailing quotes. |
| {`a=""b""`, "a", ""}, // Malformed. Two opening quotes. |
| {`a="'b'"`, "a", ""}, // Malformed. Two opening quotes. |
| {`a='b'`, "a", "b"}, // Single quotes ok. |
| {`a="b'`, "a", "b"}, // Quotes not checked for match. |
| {"a=\n\"b\"", "a", "b"}, // Linefeed ok. |
| {"♬=\n\"b\"", "♬", "b"}, // Unicode ok. |
| {`a="♬"`, "a", "♬"}, // Symbols ok. |
| {`a="b@"`, "a", ""}, // @ not ok. |
| {`a="@b"`, "a", ""}, // @ not ok. |
| {`a="ě"`, "a", "ě"}, // Unicode ok. |
| {`a="дěसæ"`, "a", "дěसæ"}, // Unicode ok. |
| {`a="д_ě"`, "a", "д_ě"}, // Underscores ok. |
| {`a="__foo__"`, "a", "__foo__"}, // Underscores ok. |
| {`a="--b--"`, "a", "--b--"}, // Dashes ok. |
| {`a="!b!"`, "a", ""}, // Punctuation not ok. |
| {`a="b!"`, "a", ""}, // Punctuation not ok. |
| {`a="%!b"`, "a", ""}, // Punctuation not ok. |
| {`a="%b"`, "a", ""}, // Punctuation not ok. |
| {` a = "b "`, "a", ""}, // No spaces in value. |
| {` a = "b "`, "b", ""}, // No spaces in value. |
| {`a="b5"`, "a", "b5"}, |
| {`foo="b5"`, "foo", "b5"}, |
| {`a="b"`, "=", ""}, |
| {` a = "b" = "c"`, "a", "b"}, // Doesn't check correctness of the source. |
| {` a = b = "c"`, "b", "c"}, // Doesn't check correctness of the source. |
| {` a = "b", a = "c"`, "a", "b"}, // Returns first match. |
| {` a = "b", a = "c"`, "b", ""}, |
| } |
| |
| for i, tt := range testList { |
| testname := fmt.Sprintf("(%q, %q)[%d]", tt.source, tt.key, i) |
| t.Run(testname, func(t *testing.T) { |
| got, _ := getValue(tt.source, tt.key) |
| if got != tt.want { |
| t.Errorf("got %q, want %q", got, tt.want) |
| } |
| }) |
| } |
| } |
| |
| func TestExtractNamedList(t *testing.T) { |
| testList := []struct { |
| source string |
| name string |
| want []string |
| wantErr string |
| }{ |
| {"", "", nil, ""}, |
| {"\ndeps\n", "deps", nil, ""}, |
| {"deps = []", "deps", nil, ""}, |
| {"deps=[]", "deps", nil, ""}, |
| {"deps+=[]", "deps", nil, ""}, |
| {"deps = [][[]]]]]]]]]]]", "deps", nil, ""}, |
| {"deps = [[][]]", "deps", []string{"[][]"}, ""}, // Nested brackets are not recursed. |
| {"deps=[]\n deps+=[]\ndeps+=[]]", "deps", nil, ""}, |
| {"deps = [,]", "deps", nil, ""}, |
| {"deps = [a]", "deps", []string{"a"}, ""}, |
| {"deps = [a]", "foo", nil, ""}, |
| {"deps = [a\n]", "deps", []string{"a"}, ""}, |
| {"deps = [\n a#foo\n]", "deps", []string{"a"}, ""}, // Line comments removed. |
| {"deps = [\n a #foo\n]", "deps", []string{"a"}, ""}, |
| {"deps = [\n a # foo \n]", "deps", []string{"a"}, ""}, |
| {"deps = [a ]", "deps", []string{"a"}, ""}, |
| {"deps = [a,b]", "deps", []string{"a", "b"}, ""}, |
| {"deps = [a, b]", "deps", []string{"a", "b"}, ""}, |
| {"deps = ['a']", "deps", []string{"a"}, ""}, |
| {"deps = ['a','b']", "deps", []string{"a", "b"}, ""}, |
| {"deps = ['a', 'b']", "deps", []string{"a", "b"}, ""}, |
| {`deps = ["a"]`, "deps", []string{"a"}, ""}, |
| {`deps = ["a","b"]`, "deps", []string{"a", "b"}, ""}, |
| {`deps = ["a", "b"]`, "deps", []string{"a", "b"}, ""}, |
| {`deps = ["a", "b",'c']`, "deps", []string{"a", "b", "c"}, ""}, |
| {"deps=[a,b,c]deps+=[]deps+=[]]", "deps", []string{"a", "b", "c"}, ""}, |
| {"\ndeps \n=\n[\n \"a\", \n\n \"\nb\"\n ]", "deps", []string{"a", "b"}, ""}, |
| {`deps=[]\nx\ndeps+=["a"]\n,ababababdeps+=["b",'c']`, "deps", []string{"a", "b", "c"}, ""}, |
| {"deps=[a]\n deps+=[b]\ndeps+=[c]]", "deps", []string{"a", "b", "c"}, ""}, |
| {"deps=[]\n deps+=[a]\ndeps+=[b,c]]", "deps", []string{"a", "b", "c"}, ""}, |
| {"deps=[]deps+=[a]deps+=[b,c]]", "deps", []string{"a", "b", "c"}, ""}, |
| {"deps=[a]\n{} deps+=[b]\nif(foo){deps+=[c]]}", "deps", []string{"a", "b", "c"}, ""}, |
| } |
| |
| for i, tt := range testList { |
| testname := fmt.Sprintf("(%q, %q)[%d]", tt.source, tt.name, i) |
| t.Run(testname, func(t *testing.T) { |
| got, err := extractNamedList(tt.source, tt.name) |
| if err == nil && tt.wantErr != "" || err != nil && tt.wantErr != "" && !strings.Contains(err.Error(), tt.wantErr) { |
| t.Errorf("want err like %v; got err: %q", tt.wantErr, err) |
| } |
| if diff := cmp.Diff(tt.want, got); diff != "" { |
| t.Errorf("extractNamedList() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| } |