|
1 | 1 | package clientgenv2 |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "github.com/99designs/gqlgen/codegen/config" |
| 5 | + gqlgencConfig "github.com/Yamashou/gqlgenc/config" |
4 | 6 | "go/types" |
5 | 7 | "testing" |
6 | 8 | ) |
@@ -235,3 +237,143 @@ func TestMergeFieldsRecursively(t *testing.T) { |
235 | 237 | }) |
236 | 238 | } |
237 | 239 | } |
| 240 | + |
| 241 | +func TestFragmentSpreadExpansionInInlineFragment(t *testing.T) { |
| 242 | + // このテストでは、フラグメントスプレッドがあるインラインフラグメントで |
| 243 | + // `hasFragmentSpread` と `collectFragmentFields` 関数が正しく動作することを確認します |
| 244 | + // |
| 245 | + // This test verifies that `hasFragmentSpread` and `collectFragmentFields` functions |
| 246 | + // work correctly when handling fragment spreads in inline fragments. |
| 247 | + |
| 248 | + // テストデータを作成 |
| 249 | + // Create test data |
| 250 | + // フラグメントスプレッドを含むフィールドのセット |
| 251 | + // A set of fields containing fragment spreads |
| 252 | + responseFields := ResponseFieldList{ |
| 253 | + { |
| 254 | + Name: "languages", |
| 255 | + Type: types.NewPointer(types.NewNamed( |
| 256 | + types.NewTypeName(0, nil, "LanguagesConnection", nil), |
| 257 | + types.NewStruct(nil, nil), |
| 258 | + nil, |
| 259 | + )), |
| 260 | + Tags: []string{`json:"languages,omitempty" graphql:"languages"`}, |
| 261 | + }, |
| 262 | + { |
| 263 | + Name: "RepositoryFragment", |
| 264 | + Type: types.NewPointer(types.NewNamed(types.NewTypeName(0, nil, "RepositoryFragment", nil), types.NewStruct(nil, nil), nil)), |
| 265 | + IsFragmentSpread: true, |
| 266 | + ResponseFields: ResponseFieldList{ |
| 267 | + { |
| 268 | + Name: "id", |
| 269 | + Type: types.Typ[types.String], |
| 270 | + Tags: []string{`json:"id" graphql:"id"`}, |
| 271 | + }, |
| 272 | + { |
| 273 | + Name: "name", |
| 274 | + Type: types.Typ[types.String], |
| 275 | + Tags: []string{`json:"name" graphql:"name"`}, |
| 276 | + }, |
| 277 | + }, |
| 278 | + }, |
| 279 | + } |
| 280 | + |
| 281 | + mockCfg := &config.Config{} |
| 282 | + mockBinder := &config.Binder{} |
| 283 | + mockPackageConfig := config.PackageConfig{} |
| 284 | + mockGenCfg := &gqlgencConfig.GenerateConfig{} |
| 285 | + |
| 286 | + sg := &SourceGenerator{ |
| 287 | + cfg: mockCfg, |
| 288 | + binder: mockBinder, |
| 289 | + client: mockPackageConfig, |
| 290 | + genCfg: mockGenCfg, |
| 291 | + StructSources: make([]*StructSource, 0), |
| 292 | + } |
| 293 | + |
| 294 | + // フラグメントスプレッドの存在を確認 |
| 295 | + // Verify the existence of fragment spreads |
| 296 | + hasFragmentSpread := sg.hasFragmentSpread(responseFields) |
| 297 | + if !hasFragmentSpread { |
| 298 | + t.Errorf("Expected hasFragmentSpread to return true when a fragment spread is present") |
| 299 | + } |
| 300 | + |
| 301 | + // フラグメントフィールドの収集をテスト |
| 302 | + // Test the collection of fragment fields |
| 303 | + fragmentFields := sg.collectFragmentFields(responseFields) |
| 304 | + if len(fragmentFields) != 2 { |
| 305 | + t.Errorf("Expected 2 fields from fragment spread, got %d", len(fragmentFields)) |
| 306 | + } |
| 307 | + |
| 308 | + // フィールド名を検証 |
| 309 | + // Validate field names |
| 310 | + foundID := false |
| 311 | + foundName := false |
| 312 | + |
| 313 | + for _, field := range fragmentFields { |
| 314 | + switch field.Name { |
| 315 | + case "id": |
| 316 | + foundID = true |
| 317 | + case "name": |
| 318 | + foundName = true |
| 319 | + } |
| 320 | + } |
| 321 | + |
| 322 | + if !foundID { |
| 323 | + t.Errorf("Expected to find 'id' field from fragment") |
| 324 | + } |
| 325 | + if !foundName { |
| 326 | + t.Errorf("Expected to find 'name' field from fragment") |
| 327 | + } |
| 328 | + |
| 329 | + // 実際のフラグメントスプレッド展開処理をテスト |
| 330 | + // Test the actual fragment spread expansion process |
| 331 | + // フラグメントスプレッドを含むフィールドから、すべてのフィールドを集める |
| 332 | + // Collect all fields from fields containing fragment spreads |
| 333 | + allFields := make(ResponseFieldList, 0) |
| 334 | + for _, field := range responseFields { |
| 335 | + if !field.IsFragmentSpread { |
| 336 | + allFields = append(allFields, field) |
| 337 | + } |
| 338 | + } |
| 339 | + // フラグメントのフィールドを追加 |
| 340 | + // Add fields from fragments |
| 341 | + allFields = append(allFields, fragmentFields...) |
| 342 | + |
| 343 | + // フィールドの数を検証 |
| 344 | + // Validate the number of fields |
| 345 | + if len(allFields) != 3 { // languages + id + name |
| 346 | + t.Errorf("Expected 3 fields after expansion, got %d", len(allFields)) |
| 347 | + } |
| 348 | + |
| 349 | + // フィールド名を検証 |
| 350 | + // Validate field names |
| 351 | + foundLanguages := false |
| 352 | + foundID = false |
| 353 | + foundName = false |
| 354 | + |
| 355 | + for _, field := range allFields { |
| 356 | + switch field.Name { |
| 357 | + case "languages": |
| 358 | + foundLanguages = true |
| 359 | + case "id": |
| 360 | + foundID = true |
| 361 | + case "name": |
| 362 | + foundName = true |
| 363 | + case "RepositoryFragment": |
| 364 | + // このフィールドは展開されるため、ここには存在しないはず |
| 365 | + // This field should not exist after expansion |
| 366 | + t.Errorf("RepositoryFragment field should not exist after expansion") |
| 367 | + } |
| 368 | + } |
| 369 | + |
| 370 | + if !foundLanguages { |
| 371 | + t.Errorf("Expected to find 'languages' field") |
| 372 | + } |
| 373 | + if !foundID { |
| 374 | + t.Errorf("Expected to find 'id' field from fragment") |
| 375 | + } |
| 376 | + if !foundName { |
| 377 | + t.Errorf("Expected to find 'name' field from fragment") |
| 378 | + } |
| 379 | +} |
0 commit comments