| 1081 | } |
| 1082 | |
| 1083 | func TestBodyContent(t *testing.T) { |
| 1084 | // We test most of the functionality already in TestBodyPartialContent, so |
| 1085 | // this test focuses on the handling of extraneous attributes. |
| 1086 | tests := []struct { |
| 1087 | src string |
| 1088 | schema *hcl.BodySchema |
| 1089 | diagCount int |
| 1090 | }{ |
| 1091 | { |
| 1092 | `{"unknown": true}`, |
| 1093 | &hcl.BodySchema{}, |
| 1094 | 1, |
| 1095 | }, |
| 1096 | { |
| 1097 | `{"//": "comment that should be ignored"}`, |
| 1098 | &hcl.BodySchema{}, |
| 1099 | 0, |
| 1100 | }, |
| 1101 | { |
| 1102 | `{"unknow": true}`, |
| 1103 | &hcl.BodySchema{ |
| 1104 | Attributes: []hcl.AttributeSchema{ |
| 1105 | { |
| 1106 | Name: "unknown", |
| 1107 | }, |
| 1108 | }, |
| 1109 | }, |
| 1110 | 1, |
| 1111 | }, |
| 1112 | { |
| 1113 | `{"unknow": true, "unnown": true}`, |
| 1114 | &hcl.BodySchema{ |
| 1115 | Attributes: []hcl.AttributeSchema{ |
| 1116 | { |
| 1117 | Name: "unknown", |
| 1118 | }, |
| 1119 | }, |
| 1120 | }, |
| 1121 | 2, |
| 1122 | }, |
| 1123 | } |
| 1124 | |
| 1125 | for i, test := range tests { |
| 1126 | t.Run(fmt.Sprintf("%02d-%s", i, test.src), func(t *testing.T) { |
| 1127 | file, diags := Parse([]byte(test.src), "test.json") |
| 1128 | if len(diags) != 0 { |
| 1129 | t.Fatalf("Parse produced diagnostics: %s", diags) |
| 1130 | } |
| 1131 | _, diags = file.Body.Content(test.schema) |
| 1132 | if len(diags) != test.diagCount { |
| 1133 | t.Errorf("Wrong number of diagnostics %d; want %d", len(diags), test.diagCount) |
| 1134 | for _, diag := range diags { |
| 1135 | t.Logf(" - %s", diag) |
| 1136 | } |
| 1137 | } |
| 1138 | }) |
| 1139 | } |
| 1140 | } |