|
1 | | -// This file is part of arduino-app-cli. |
2 | | -// |
3 | | -// Copyright 2025 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-app-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 | 1 | package bricks |
17 | 2 |
|
18 | 3 | import ( |
@@ -110,3 +95,83 @@ func TestBrickCreate(t *testing.T) { |
110 | 95 | require.Equal(t, secret, after.Descriptor.Bricks[0].Variables["ARDUINO_SECRET"]) |
111 | 96 | }) |
112 | 97 | } |
| 98 | + |
| 99 | +func TestGetBrickInstanceVariableDetails(t *testing.T) { |
| 100 | + tests := []struct { |
| 101 | + name string |
| 102 | + brick *bricksindex.Brick |
| 103 | + userVariables map[string]string |
| 104 | + expectedInstanceVariable []BrickInstanceVariable |
| 105 | + expectedVariableMap map[string]string |
| 106 | + }{ |
| 107 | + { |
| 108 | + name: "variable is present in the map", |
| 109 | + brick: &bricksindex.Brick{ |
| 110 | + Variables: []bricksindex.BrickVariable{ |
| 111 | + {Name: "VAR1", Description: "desc"}, |
| 112 | + }, |
| 113 | + }, |
| 114 | + userVariables: map[string]string{"VAR1": "value1"}, |
| 115 | + expectedInstanceVariable: []BrickInstanceVariable{ |
| 116 | + {Name: "VAR1", Value: "value1", Description: "desc", Required: true}, |
| 117 | + }, |
| 118 | + expectedVariableMap: map[string]string{"VAR1": "value1"}, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "variable not present in the map", |
| 122 | + brick: &bricksindex.Brick{ |
| 123 | + Variables: []bricksindex.BrickVariable{ |
| 124 | + {Name: "VAR1", Description: "desc"}, |
| 125 | + }, |
| 126 | + }, |
| 127 | + userVariables: map[string]string{}, |
| 128 | + expectedInstanceVariable: []BrickInstanceVariable{ |
| 129 | + {Name: "VAR1", Value: "", Description: "desc", Required: true}, |
| 130 | + }, |
| 131 | + expectedVariableMap: map[string]string{"VAR1": ""}, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "variable with default value", |
| 135 | + brick: &bricksindex.Brick{ |
| 136 | + Variables: []bricksindex.BrickVariable{ |
| 137 | + {Name: "VAR1", DefaultValue: "default", Description: "desc"}, |
| 138 | + }, |
| 139 | + }, |
| 140 | + userVariables: map[string]string{}, |
| 141 | + expectedInstanceVariable: []BrickInstanceVariable{ |
| 142 | + {Name: "VAR1", Value: "default", Description: "desc", Required: false}, |
| 143 | + }, |
| 144 | + expectedVariableMap: map[string]string{"VAR1": "default"}, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "multiple variables", |
| 148 | + brick: &bricksindex.Brick{ |
| 149 | + Variables: []bricksindex.BrickVariable{ |
| 150 | + {Name: "VAR1", Description: "desc1"}, |
| 151 | + {Name: "VAR2", DefaultValue: "def2", Description: "desc2"}, |
| 152 | + }, |
| 153 | + }, |
| 154 | + userVariables: map[string]string{"VAR1": "v1"}, |
| 155 | + expectedInstanceVariable: []BrickInstanceVariable{ |
| 156 | + {Name: "VAR1", Value: "v1", Description: "desc1", Required: true}, |
| 157 | + {Name: "VAR2", Value: "def2", Description: "desc2", Required: false}, |
| 158 | + }, |
| 159 | + expectedVariableMap: map[string]string{"VAR1": "v1", "VAR2": "def2"}, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "no variables", |
| 163 | + brick: &bricksindex.Brick{Variables: []bricksindex.BrickVariable{}}, |
| 164 | + userVariables: map[string]string{}, |
| 165 | + expectedInstanceVariable: []BrickInstanceVariable{}, |
| 166 | + expectedVariableMap: map[string]string{}, |
| 167 | + }, |
| 168 | + } |
| 169 | + |
| 170 | + for _, tt := range tests { |
| 171 | + t.Run(tt.name, func(t *testing.T) { |
| 172 | + actualVariableMap, actualInstanceVariables := getBrickVariableDetails(tt.brick, tt.userVariables) |
| 173 | + require.Equal(t, tt.expectedVariableMap, actualVariableMap) |
| 174 | + require.Equal(t, tt.expectedInstanceVariable, actualInstanceVariables) |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
0 commit comments