@@ -5,6 +5,7 @@ package tfjson
55
66import (
77 "encoding/json"
8+ "io"
89 "os"
910 "reflect"
1011 "testing"
@@ -187,7 +188,7 @@ func TestPlan_UnmarshalJSON(t *testing.T) {
187188 t .Fatal (err )
188189 }
189190
190- testCases := map [string ]struct {
191+ numericsTestCases := map [string ]struct {
191192 useJSONNumber bool
192193 expected any
193194 }{
@@ -200,17 +201,15 @@ func TestPlan_UnmarshalJSON(t *testing.T) {
200201 },
201202 }
202203
203- for name , testCase := range testCases {
204- name , testCase := name , testCase
205-
204+ for name , testCase := range numericsTestCases {
206205 t .Run (name , func (t * testing.T ) {
207206 t .Parallel ()
208207
209208 plan := & Plan {}
210209
211210 plan .UseJSONNumber (testCase .useJSONNumber )
212211
213- err = plan . UnmarshalJSON ( b )
212+ err = json . Unmarshal ( b , & plan )
214213 if err != nil {
215214 t .Fatal (err )
216215 }
@@ -232,4 +231,43 @@ func TestPlan_UnmarshalJSON(t *testing.T) {
232231 }
233232 })
234233 }
234+
235+ jsonValidationTestCases := map [string ]struct {
236+ filePath string
237+ expectError bool
238+ }{
239+ "invalid plan JSON" : {
240+ filePath : "testdata/invalid/plan.json" ,
241+ expectError : true ,
242+ },
243+ "valid plan JSON" : {
244+ filePath : "testdata/basic/plan.json" ,
245+ },
246+ }
247+
248+ for tn , tc := range jsonValidationTestCases {
249+ t .Run (tn , func (t * testing.T ) {
250+ f , err := os .Open (tc .filePath )
251+ if err != nil {
252+ t .Fatal (err )
253+ }
254+ defer f .Close ()
255+
256+ b , err := io .ReadAll (f )
257+ if err != nil {
258+ t .Fatal (err )
259+ }
260+
261+ var plan Plan
262+ err = json .Unmarshal (b , & plan )
263+
264+ if tc .expectError && err == nil {
265+ t .Fatalf ("expected error; got none" )
266+ }
267+
268+ if ! tc .expectError && err != nil {
269+ t .Errorf ("expected no error, got %q" , err .Error ())
270+ }
271+ })
272+ }
235273}
0 commit comments