1// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import "testing"
18
19func TestJacocoFilterToSpecs(t *testing.T) {
20	testCases := []struct {
21		name, in, out string
22	}{
23		{
24			name: "class",
25			in:   "package.Class",
26			out:  "package/Class.class",
27		},
28		{
29			name: "class wildcard",
30			in:   "package.Class*",
31			out:  "package/Class*.class",
32		},
33		{
34			name: "package wildcard",
35			in:   "package.*",
36			out:  "package/*.class",
37		},
38		{
39			name: "package recursive wildcard",
40			in:   "package.**",
41			out:  "package/**/*.class",
42		},
43		{
44			name: "recursive wildcard only",
45			in:   "**",
46			out:  "**/*.class",
47		},
48		{
49			name: "single wildcard only",
50			in:   "*",
51			out:  "*.class",
52		},
53	}
54
55	for _, testCase := range testCases {
56		t.Run(testCase.name, func(t *testing.T) {
57			got, err := jacocoFilterToSpec(testCase.in)
58			if err != nil {
59				t.Error(err)
60			}
61			if got != testCase.out {
62				t.Errorf("expected %q got %q", testCase.out, got)
63			}
64		})
65	}
66}
67
68func TestJacocoFiltersToZipCommand(t *testing.T) {
69	testCases := []struct {
70		name               string
71		includes, excludes []string
72		out                string
73	}{
74		{
75			name:     "implicit wildcard",
76			includes: []string{},
77			out:      "**/*.class",
78		},
79		{
80			name:     "only include",
81			includes: []string{"package/Class.class"},
82			out:      "package/Class.class",
83		},
84		{
85			name:     "multiple includes",
86			includes: []string{"package/Class.class", "package2/Class.class"},
87			out:      "package/Class.class package2/Class.class",
88		},
89		{
90			name:     "excludes",
91			includes: []string{"package/**/*.class"},
92			excludes: []string{"package/Class.class"},
93			out:      "-x package/Class.class package/**/*.class",
94		},
95	}
96
97	for _, testCase := range testCases {
98		t.Run(testCase.name, func(t *testing.T) {
99			got := jacocoFiltersToZipCommand(testCase.includes, testCase.excludes)
100			if got != testCase.out {
101				t.Errorf("expected %q got %q", testCase.out, got)
102			}
103		})
104	}
105}
106