|
| 1 | +// Copyright 2025 DoorDash, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
| 10 | + |
| 11 | +package codegen |
| 12 | + |
| 13 | +import ( |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | +) |
| 18 | + |
| 19 | +func TestGoImports_FiltersHardcodedTemplateImports(t *testing.T) { |
| 20 | + im := importMap{ |
| 21 | + "encoding/json": goImport{Path: "encoding/json"}, |
| 22 | + "net/http": goImport{Path: "net/http"}, |
| 23 | + "custom/pkg": goImport{Path: "custom/pkg"}, |
| 24 | + "another/pkg": goImport{Name: "alias", Path: "another/pkg"}, |
| 25 | + "aliased-json": goImport{Name: "myjson", Path: "encoding/json"}, |
| 26 | + } |
| 27 | + |
| 28 | + result := im.GoImports() |
| 29 | + |
| 30 | + // encoding/json and net/http without aliases are hardcoded in header.tmpl, so they should be filtered out |
| 31 | + assert.NotContains(t, result, `"encoding/json"`) |
| 32 | + assert.NotContains(t, result, `"net/http"`) |
| 33 | + |
| 34 | + // aliased hardcoded imports should still be present (user explicitly wants the alias) |
| 35 | + assert.Contains(t, result, `myjson "encoding/json"`) |
| 36 | + |
| 37 | + // custom imports should still be present |
| 38 | + assert.Contains(t, result, `"custom/pkg"`) |
| 39 | + assert.Contains(t, result, `alias "another/pkg"`) |
| 40 | + assert.Len(t, result, 3) |
| 41 | +} |
| 42 | + |
| 43 | +func TestGoImports_FiltersCurrentPackage(t *testing.T) { |
| 44 | + im := importMap{ |
| 45 | + "-": goImport{Path: importMappingCurrentPackage}, |
| 46 | + "custom/pkg": goImport{Path: "custom/pkg"}, |
| 47 | + } |
| 48 | + |
| 49 | + result := im.GoImports() |
| 50 | + |
| 51 | + assert.Len(t, result, 1) |
| 52 | + assert.Contains(t, result, `"custom/pkg"`) |
| 53 | +} |
| 54 | + |
| 55 | +func TestGoImports_EmptyMap(t *testing.T) { |
| 56 | + im := importMap{} |
| 57 | + result := im.GoImports() |
| 58 | + assert.Empty(t, result) |
| 59 | +} |
0 commit comments