Skip to content

Commit a16ba4d

Browse files
add tests
1 parent 429f525 commit a16ba4d

File tree

2 files changed

+221
-10
lines changed

2 files changed

+221
-10
lines changed

internal/cli/feedback/result/rpc.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,27 @@ import (
2525

2626
// NewPlatformSummary creates a new result.Platform from rpc.PlatformSummary
2727
func NewPlatformSummary(in *rpc.PlatformSummary) *PlatformSummary {
28+
if in == nil {
29+
return nil
30+
}
31+
2832
releases := orderedmap.NewWithConversionFunc[*semver.Version, *PlatformRelease, string]((*semver.Version).String)
29-
for k, v := range in.Releases {
33+
for k, v := range in.GetReleases() {
3034
releases.Set(semver.MustParse(k), NewPlatformRelease(v))
3135
}
3236
releases.SortKeys((*semver.Version).CompareTo)
3337

3438
return &PlatformSummary{
35-
Id: in.Metadata.Id,
36-
Maintainer: in.Metadata.Maintainer,
37-
Website: in.Metadata.Website,
38-
Email: in.Metadata.Email,
39-
ManuallyInstalled: in.Metadata.ManuallyInstalled,
40-
Deprecated: in.Metadata.Deprecated,
41-
Indexed: in.Metadata.Indexed,
39+
Id: in.GetMetadata().GetId(),
40+
Maintainer: in.GetMetadata().GetMaintainer(),
41+
Website: in.GetMetadata().GetWebsite(),
42+
Email: in.GetMetadata().GetEmail(),
43+
ManuallyInstalled: in.GetMetadata().GetManuallyInstalled(),
44+
Deprecated: in.GetMetadata().GetDeprecated(),
45+
Indexed: in.GetMetadata().GetIndexed(),
4246
Releases: releases,
43-
InstalledVersion: semver.MustParse(in.InstalledVersion),
44-
LatestVersion: semver.MustParse(in.LatestVersion),
47+
InstalledVersion: semver.MustParse(in.GetInstalledVersion()),
48+
LatestVersion: semver.MustParse(in.GetLatestVersion()),
4549
}
4650
}
4751

@@ -73,6 +77,9 @@ func (p *PlatformSummary) GetInstalledRelease() *PlatformRelease {
7377

7478
// NewPlatformRelease creates a new result.PlatformRelease from rpc.PlatformRelease
7579
func NewPlatformRelease(in *rpc.PlatformRelease) *PlatformRelease {
80+
if in == nil {
81+
return nil
82+
}
7683
var boards []*Board
7784
for _, board := range in.Boards {
7885
boards = append(boards, &Board{
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2023 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package result_test
17+
18+
import (
19+
"reflect"
20+
"slices"
21+
"strings"
22+
"testing"
23+
24+
"github.com/arduino/arduino-cli/internal/cli/feedback/result"
25+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
26+
"github.com/stretchr/testify/require"
27+
)
28+
29+
func getStructJsonTags(t *testing.T, a any) []string {
30+
tags := []string{}
31+
rt := reflect.TypeOf(a)
32+
if rt.Kind() != reflect.Struct {
33+
rt = rt.Elem()
34+
require.Equal(t, reflect.Struct, rt.Kind())
35+
}
36+
for i := 0; i < rt.NumField(); i++ {
37+
tag := rt.Field(i).Tag.Get("json")
38+
if tag == "" {
39+
continue
40+
}
41+
key, _, _ := strings.Cut(tag, ",")
42+
tags = append(tags, key)
43+
}
44+
return tags
45+
}
46+
47+
func mustContainsAllPropertyOfRpcStruct(t *testing.T, a, b any, excludeFields ...string) {
48+
// must not be the same pointer, a and b struct must be of differnt type
49+
require.NotSame(t, a, b)
50+
rta, rtb := reflect.TypeOf(a), reflect.TypeOf(b)
51+
if rta.Kind() != reflect.Struct {
52+
rta = rta.Elem()
53+
require.Equal(t, reflect.Struct, rta.Kind())
54+
}
55+
if rtb.Kind() != reflect.Struct {
56+
rtb = rtb.Elem()
57+
require.Equal(t, reflect.Struct, rtb.Kind())
58+
}
59+
require.NotEqual(t, rta.String(), rtb.String())
60+
61+
aTags := getStructJsonTags(t, a)
62+
bTags := getStructJsonTags(t, b)
63+
if len(excludeFields) > 0 {
64+
aTags = slices.DeleteFunc(aTags, func(s string) bool { return slices.Contains(excludeFields, s) })
65+
bTags = slices.DeleteFunc(bTags, func(s string) bool { return slices.Contains(excludeFields, s) })
66+
}
67+
require.ElementsMatch(t, aTags, bTags)
68+
}
69+
70+
func TestAllFieldAreMapped(t *testing.T) {
71+
// Our PlatformSummary expands the PlatformMetadata without the need to nest it as the rpc does.
72+
platformSummaryRpc := &rpc.PlatformSummary{
73+
InstalledVersion: "1.0.0",
74+
LatestVersion: "1.0.0",
75+
}
76+
platformSummaryRpcTags := getStructJsonTags(t, platformSummaryRpc)
77+
platformSummaryRpcTags = append(platformSummaryRpcTags, getStructJsonTags(t, platformSummaryRpc.GetMetadata())...)
78+
platformSummaryRpcTags = slices.DeleteFunc(platformSummaryRpcTags, func(s string) bool { return s == "metadata" })
79+
80+
platformSummaryResult := result.NewPlatformSummary(platformSummaryRpc)
81+
platformSummaryResultTags := getStructJsonTags(t, platformSummaryResult)
82+
83+
require.ElementsMatch(t, platformSummaryRpcTags, platformSummaryResultTags)
84+
85+
platformRelease := &rpc.PlatformRelease{}
86+
platformReleaseResult := result.NewPlatformRelease(platformRelease)
87+
mustContainsAllPropertyOfRpcStruct(t, platformRelease, platformReleaseResult)
88+
89+
libraryRpc := &rpc.Library{}
90+
libraryResult := result.NewLibrary(libraryRpc)
91+
mustContainsAllPropertyOfRpcStruct(t, libraryRpc, libraryResult)
92+
93+
libraryReleaseRpc := &rpc.LibraryRelease{}
94+
libraryReleaseResult := result.NewLibraryRelease(libraryReleaseRpc)
95+
mustContainsAllPropertyOfRpcStruct(t, libraryReleaseRpc, libraryReleaseResult)
96+
97+
installedLibrary := &rpc.InstalledLibrary{}
98+
installedLibraryResult := result.NewInstalledLibrary(installedLibrary)
99+
mustContainsAllPropertyOfRpcStruct(t, installedLibrary, installedLibraryResult)
100+
101+
downloadResource := &rpc.DownloadResource{}
102+
downloadResourceResult := result.NewDownloadResource(downloadResource)
103+
mustContainsAllPropertyOfRpcStruct(t, downloadResource, downloadResourceResult)
104+
105+
libraryDependencyRpc := &rpc.LibraryDependency{}
106+
libraryDependencyResult := result.NewLibraryDependency(libraryDependencyRpc)
107+
mustContainsAllPropertyOfRpcStruct(t, libraryDependencyRpc, libraryDependencyResult)
108+
109+
portRpc := &rpc.Port{}
110+
portResult := result.NewPort(portRpc)
111+
mustContainsAllPropertyOfRpcStruct(t, portRpc, portResult)
112+
113+
boardDetailsResponseRpc := &rpc.BoardDetailsResponse{}
114+
boardDetailsResponseResult := result.NewBoardDetailsResponse(boardDetailsResponseRpc)
115+
mustContainsAllPropertyOfRpcStruct(t, boardDetailsResponseRpc, boardDetailsResponseResult)
116+
117+
packageRpc := &rpc.Package{}
118+
packageResult := result.NewPackage(packageRpc)
119+
mustContainsAllPropertyOfRpcStruct(t, packageRpc, packageResult)
120+
121+
helpRpc := &rpc.Help{}
122+
helpResult := result.NewHelp(helpRpc)
123+
mustContainsAllPropertyOfRpcStruct(t, helpRpc, helpResult)
124+
125+
boardPlatformRpc := &rpc.BoardPlatform{}
126+
boardPlatformResult := result.NewBoardPlatform(boardPlatformRpc)
127+
mustContainsAllPropertyOfRpcStruct(t, boardPlatformRpc, boardPlatformResult)
128+
129+
toolsDependencyRpc := &rpc.ToolsDependencies{}
130+
toolsDependencyResult := result.NewToolsDependency(toolsDependencyRpc)
131+
mustContainsAllPropertyOfRpcStruct(t, toolsDependencyRpc, toolsDependencyResult)
132+
133+
systemRpc := &rpc.Systems{}
134+
systemResult := result.NewSystem(systemRpc)
135+
mustContainsAllPropertyOfRpcStruct(t, systemRpc, systemResult)
136+
137+
configOptionRpc := &rpc.ConfigOption{}
138+
configOptionResult := result.NewConfigOption(configOptionRpc)
139+
mustContainsAllPropertyOfRpcStruct(t, configOptionRpc, configOptionResult)
140+
141+
configValueRpc := &rpc.ConfigValue{}
142+
configValueResult := result.NewConfigValue(configValueRpc)
143+
mustContainsAllPropertyOfRpcStruct(t, configValueRpc, configValueResult)
144+
145+
programmerRpc := &rpc.Programmer{}
146+
programmerResult := result.NewProgrammer(programmerRpc)
147+
mustContainsAllPropertyOfRpcStruct(t, programmerRpc, programmerResult)
148+
149+
boardIdentificationPropertiesRpc := &rpc.BoardIdentificationProperties{}
150+
boardIdentificationPropertiesResult := result.NewBoardIndentificationProperty(boardIdentificationPropertiesRpc)
151+
mustContainsAllPropertyOfRpcStruct(t, boardIdentificationPropertiesRpc, boardIdentificationPropertiesResult)
152+
153+
boardListAllResponseRpc := &rpc.BoardListAllResponse{}
154+
boardListAllResponseResult := result.NewBoardListAllResponse(boardListAllResponseRpc)
155+
mustContainsAllPropertyOfRpcStruct(t, boardListAllResponseRpc, boardListAllResponseResult)
156+
157+
boardListItemRpc := &rpc.BoardListItem{}
158+
boardListItemResult := result.NewBoardListItem(boardListItemRpc)
159+
mustContainsAllPropertyOfRpcStruct(t, boardListItemRpc, boardListItemResult)
160+
161+
platformRpc := &rpc.Platform{}
162+
platformResult := result.NewPlatform(platformRpc)
163+
mustContainsAllPropertyOfRpcStruct(t, platformRpc, platformResult)
164+
165+
platformMetadataRpc := &rpc.PlatformMetadata{}
166+
platformMetadataResult := result.NewPlatformMetadata(platformMetadataRpc)
167+
mustContainsAllPropertyOfRpcStruct(t, platformMetadataRpc, platformMetadataResult)
168+
169+
detectedPortRpc := &rpc.DetectedPort{}
170+
detectedPortResult := result.NewDetectedPort(detectedPortRpc)
171+
mustContainsAllPropertyOfRpcStruct(t, detectedPortRpc, detectedPortResult)
172+
173+
libraryResolveDependenciesResponseRpc := &rpc.LibraryResolveDependenciesResponse{}
174+
libraryResolveDependenciesResponseResult := result.NewLibraryResolveDependenciesResponse(libraryResolveDependenciesResponseRpc)
175+
mustContainsAllPropertyOfRpcStruct(t, libraryResolveDependenciesResponseRpc, libraryResolveDependenciesResponseResult)
176+
177+
libraryDependencyStatusRpc := &rpc.LibraryDependencyStatus{}
178+
libraryDependencyStatusResult := result.NewLibraryDependencyStatus(libraryDependencyStatusRpc)
179+
mustContainsAllPropertyOfRpcStruct(t, libraryDependencyStatusRpc, libraryDependencyStatusResult)
180+
181+
librarySearchResponseRpc := &rpc.LibrarySearchResponse{}
182+
librarySearchResponseResult := result.NewLibrarySearchResponse(librarySearchResponseRpc)
183+
mustContainsAllPropertyOfRpcStruct(t, librarySearchResponseRpc, librarySearchResponseResult)
184+
185+
searchedLibraryRpc := &rpc.SearchedLibrary{}
186+
searchedLibraryResult := result.NewSearchedLibrary(searchedLibraryRpc)
187+
mustContainsAllPropertyOfRpcStruct(t, searchedLibraryRpc, searchedLibraryResult)
188+
189+
monitorPortSettingDescriptorRpc := &rpc.MonitorPortSettingDescriptor{}
190+
monitorPortSettingDescriptorResult := result.NewMonitorPortSettingDescriptor(monitorPortSettingDescriptorRpc)
191+
mustContainsAllPropertyOfRpcStruct(t, monitorPortSettingDescriptorRpc, monitorPortSettingDescriptorResult)
192+
193+
compileResponseRpc := &rpc.CompileResponse{}
194+
compileResponseResult := result.NewCompileResponse(compileResponseRpc)
195+
mustContainsAllPropertyOfRpcStruct(t, compileResponseRpc, compileResponseResult, "progress")
196+
197+
executableSectionSizeRpc := &rpc.ExecutableSectionSize{}
198+
executableSectionSizeResult := result.NewExecutableSectionSize(executableSectionSizeRpc)
199+
mustContainsAllPropertyOfRpcStruct(t, executableSectionSizeRpc, executableSectionSizeResult)
200+
201+
installedPlatformReferenceRpc := &rpc.InstalledPlatformReference{}
202+
installedPlatformReferenceResult := result.NewInstalledPlatformReference(installedPlatformReferenceRpc)
203+
mustContainsAllPropertyOfRpcStruct(t, installedPlatformReferenceRpc, installedPlatformReferenceResult)
204+
}

0 commit comments

Comments
 (0)