Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions internal/sidekick/swift/generate_deprecated_enum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// 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.

package swift

import (
"os"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/googleapis/librarian/internal/sidekick/api"
"github.com/googleapis/librarian/internal/sidekick/parser"
)

func TestGenerateEnum_Deprecated(t *testing.T) {
for _, test := range []struct {
name string
enumDeprecated bool
valDeprecated bool
wantEnum string
wantCase string
}{
{
name: "deprecated-enum",
enumDeprecated: true,
valDeprecated: false,
wantEnum: "/// -- enum marker --\n@available(*, deprecated)\npublic enum Status",
wantCase: "/// -- case marker --\n case unspecified",
},
{
name: "deprecated-value",
enumDeprecated: false,
valDeprecated: true,
wantEnum: "/// -- enum marker --\npublic enum Status",
wantCase: "/// -- case marker --\n @available(*, deprecated)\n case unspecified",
},
{
name: "both-deprecated",
enumDeprecated: true,
valDeprecated: true,
wantEnum: "/// -- enum marker --\n@available(*, deprecated)\npublic enum Status",
wantCase: "/// -- case marker --\n @available(*, deprecated)\n case unspecified",
},
{
name: "not-deprecated",
enumDeprecated: false,
valDeprecated: false,
wantEnum: "/// -- enum marker --\npublic enum Status",
wantCase: "/// -- case marker --\n case unspecified",
},
} {
t.Run(test.name, func(t *testing.T) {
outDir := t.TempDir()

enum := &api.Enum{
Name: "Status",
Package: "google.cloud.test.v1",
ID: ".google.cloud.test.v1.Status",
Deprecated: test.enumDeprecated,
Documentation: "-- enum marker --",
}
enum.Values = []*api.EnumValue{
{
Name: "STATUS_UNSPECIFIED",
Number: 0,
Parent: enum,
Deprecated: test.valDeprecated,
Documentation: "-- case marker --",
},
}
enum.UniqueNumberValues = enum.Values

model := api.NewTestAPI(nil, []*api.Enum{enum}, nil)
model.PackageName = "google.cloud.test.v1"
cfg := &parser.ModelConfig{}
if err := Generate(t.Context(), model, outDir, cfg, nil); err != nil {
t.Fatal(err)
}

filename := filepath.Join(outDir, "Sources", "GoogleCloudTestV1", "Status.swift")
content, err := os.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
contentStr := string(content)

got := extractBlock(t, contentStr, "/// -- enum marker --", "public enum Status")
if diff := cmp.Diff(test.wantEnum, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
got = extractBlock(t, contentStr, "/// -- case marker --", "case unspecified")
if diff := cmp.Diff(test.wantCase, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}
96 changes: 96 additions & 0 deletions internal/sidekick/swift/generate_deprecated_field_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// 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.

package swift

import (
"os"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/googleapis/librarian/internal/sidekick/api"
"github.com/googleapis/librarian/internal/sidekick/parser"
)

func TestGenerateField_Deprecated(t *testing.T) {
for _, test := range []struct {
name string
deprecated bool
repeated bool
want string
endStr string
}{
{
name: "deprecated",
deprecated: true,
repeated: false,
want: " /// -- field marker --\n @available(*, deprecated)\n public var normalField: Swift.String",
endStr: "public var normalField: Swift.String",
},
{
name: "not-deprecated",
deprecated: false,
repeated: false,
want: " /// -- field marker --\n public var normalField: Swift.String",
endStr: "public var normalField: Swift.String",
},
{
name: "deprecated-repeated",
deprecated: true,
repeated: true,
want: " /// -- field marker --\n @available(*, deprecated)\n public var normalField: [Swift.String]",
endStr: "public var normalField: [Swift.String]",
},
Comment thread
coryan marked this conversation as resolved.
} {
t.Run(test.name, func(t *testing.T) {
outDir := t.TempDir()

field := &api.Field{
Name: "normal_field",
Documentation: "-- field marker --",
ID: ".google.cloud.test.v1.TestMessage.normal_field",
Typez: api.TypezString,
Deprecated: test.deprecated,
Repeated: test.repeated,
}

msg := &api.Message{
Name: "TestMessage",
Package: "google.cloud.test.v1",
ID: ".google.cloud.test.v1.TestMessage",
Fields: []*api.Field{field},
}

model := api.NewTestAPI([]*api.Message{msg}, nil, nil)
model.PackageName = "google.cloud.test.v1"
cfg := &parser.ModelConfig{}
if err := Generate(t.Context(), model, outDir, cfg, nil); err != nil {
t.Fatal(err)
}

filename := filepath.Join(outDir, "Sources", "GoogleCloudTestV1", "TestMessage.swift")
content, err := os.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
contentStr := string(content)

got := extractBlock(t, contentStr, " /// -- field marker --", test.endStr)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}
109 changes: 109 additions & 0 deletions internal/sidekick/swift/generate_deprecated_message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// 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.

package swift

import (
"os"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/googleapis/librarian/internal/sidekick/api"
"github.com/googleapis/librarian/internal/sidekick/parser"
)

func TestGenerateMessage_Deprecated(t *testing.T) {
for _, test := range []struct {
name string
topDeprecated bool
nestedDeprecated bool
wantTop string
wantNested string
}{
{
name: "deprecated-both",
topDeprecated: true,
nestedDeprecated: true,
wantTop: "/// -- top marker --\n@available(*, deprecated)\npublic struct TopMessage",
wantNested: " /// -- nested marker --\n @available(*, deprecated)\n public struct NestedMessage",
},
{
name: "deprecated-top-only",
topDeprecated: true,
nestedDeprecated: false,
wantTop: "/// -- top marker --\n@available(*, deprecated)\npublic struct TopMessage",
wantNested: " /// -- nested marker --\n public struct NestedMessage",
},
{
name: "deprecated-nested-only",
topDeprecated: false,
nestedDeprecated: true,
wantTop: "/// -- top marker --\npublic struct TopMessage",
wantNested: " /// -- nested marker --\n @available(*, deprecated)\n public struct NestedMessage",
},
{
name: "not-deprecated",
topDeprecated: false,
nestedDeprecated: false,
wantTop: "/// -- top marker --\npublic struct TopMessage",
wantNested: " /// -- nested marker --\n public struct NestedMessage",
},
} {
t.Run(test.name, func(t *testing.T) {
outDir := t.TempDir()

nested := &api.Message{
Name: "NestedMessage",
Package: "google.cloud.test.v1",
ID: ".google.cloud.test.v1.TopMessage.NestedMessage",
Deprecated: test.nestedDeprecated,
Documentation: "-- nested marker --",
}

top := &api.Message{
Name: "TopMessage",
Package: "google.cloud.test.v1",
ID: ".google.cloud.test.v1.TopMessage",
Deprecated: test.topDeprecated,
Documentation: "-- top marker --",
Messages: []*api.Message{nested},
}

model := api.NewTestAPI([]*api.Message{top}, nil, nil)
model.PackageName = "google.cloud.test.v1"
cfg := &parser.ModelConfig{}
if err := Generate(t.Context(), model, outDir, cfg, nil); err != nil {
t.Fatal(err)
}

filename := filepath.Join(outDir, "Sources", "GoogleCloudTestV1", "TopMessage.swift")
content, err := os.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
contentStr := string(content)

gotTop := extractBlock(t, contentStr, "/// -- top marker --", "public struct TopMessage")
if diff := cmp.Diff(test.wantTop, gotTop); diff != "" {
t.Errorf("mismatch top (-want +got):\n%s", diff)
}

gotNested := extractBlock(t, contentStr, " /// -- nested marker --", "public struct NestedMessage")
if diff := cmp.Diff(test.wantNested, gotNested); diff != "" {
t.Errorf("mismatch nested (-want +got):\n%s", diff)
}
})
}
}
Loading
Loading