1package cquery
2
3import (
4	"fmt"
5	"reflect"
6	"strings"
7	"testing"
8)
9
10func TestGetOutputFilesParseResults(t *testing.T) {
11	testCases := []struct {
12		description    string
13		input          string
14		expectedOutput []string
15	}{
16		{
17			description:    "no result",
18			input:          "",
19			expectedOutput: []string{},
20		},
21		{
22			description:    "one result",
23			input:          "test",
24			expectedOutput: []string{"test"},
25		},
26		{
27			description:    "splits on comma with space",
28			input:          "foo, bar",
29			expectedOutput: []string{"foo", "bar"},
30		},
31	}
32	for _, tc := range testCases {
33		actualOutput := GetOutputFiles.ParseResult(tc.input)
34		if !reflect.DeepEqual(tc.expectedOutput, actualOutput) {
35			t.Errorf("%q: expected %#v != actual %#v", tc.description, tc.expectedOutput, actualOutput)
36		}
37	}
38}
39
40func TestGetCcInfoParseResults(t *testing.T) {
41	testCases := []struct {
42		description          string
43		input                string
44		expectedOutput       CcInfo
45		expectedErrorMessage string
46	}{
47		{
48			description: "no result",
49			input:       "||||",
50			expectedOutput: CcInfo{
51				OutputFiles:          []string{},
52				CcObjectFiles:        []string{},
53				CcStaticLibraryFiles: []string{},
54				Includes:             []string{},
55				SystemIncludes:       []string{},
56			},
57		},
58		{
59			description: "only output",
60			input:       "test||||",
61			expectedOutput: CcInfo{
62				OutputFiles:          []string{"test"},
63				CcObjectFiles:        []string{},
64				CcStaticLibraryFiles: []string{},
65				Includes:             []string{},
66				SystemIncludes:       []string{},
67			},
68		},
69		{
70			description: "all items set",
71			input:       "out1, out2|static_lib1, static_lib2|object1, object2|., dir/subdir|system/dir, system/other/dir",
72			expectedOutput: CcInfo{
73				OutputFiles:          []string{"out1", "out2"},
74				CcObjectFiles:        []string{"object1", "object2"},
75				CcStaticLibraryFiles: []string{"static_lib1", "static_lib2"},
76				Includes:             []string{".", "dir/subdir"},
77				SystemIncludes:       []string{"system/dir", "system/other/dir"},
78			},
79		},
80		{
81			description:          "too few result splits",
82			input:                "|",
83			expectedOutput:       CcInfo{},
84			expectedErrorMessage: fmt.Sprintf("Expected %d items, got %q", 5, []string{"", ""}),
85		},
86		{
87			description:          "too many result splits",
88			input:                strings.Repeat("|", 8),
89			expectedOutput:       CcInfo{},
90			expectedErrorMessage: fmt.Sprintf("Expected %d items, got %q", 5, make([]string, 9)),
91		},
92	}
93	for _, tc := range testCases {
94		actualOutput, err := GetCcInfo.ParseResult(tc.input)
95		if (err == nil && tc.expectedErrorMessage != "") ||
96			(err != nil && err.Error() != tc.expectedErrorMessage) {
97			t.Errorf("%q: expected Error %s, got %s", tc.description, tc.expectedErrorMessage, err)
98		} else if err == nil && !reflect.DeepEqual(tc.expectedOutput, actualOutput) {
99			t.Errorf("%q: expected %#v != actual %#v", tc.description, tc.expectedOutput, actualOutput)
100		}
101	}
102}
103