-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen_test.go
More file actions
94 lines (79 loc) · 2.65 KB
/
codegen_test.go
File metadata and controls
94 lines (79 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"context"
"os"
"path/filepath"
"slices"
"strings"
"testing"
api "dappco.re/go/core/api"
)
// ── SDKGenerator tests ─────────────────────────────────────────────────────
func TestSDKGenerator_Good_SupportedLanguages(t *testing.T) {
langs := api.SupportedLanguages()
if len(langs) == 0 {
t.Fatal("expected at least one supported language")
}
expected := []string{"go", "typescript-fetch", "python", "java", "csharp"}
for _, lang := range expected {
if !slices.Contains(langs, lang) {
t.Errorf("expected %q in supported languages, got %v", lang, langs)
}
}
}
func TestSDKGenerator_Bad_UnsupportedLanguage(t *testing.T) {
gen := &api.SDKGenerator{
SpecPath: "spec.json",
OutputDir: t.TempDir(),
}
err := gen.Generate(context.Background(), "brainfuck")
if err == nil {
t.Fatal("expected error for unsupported language, got nil")
}
if !strings.Contains(err.Error(), "unsupported language") {
t.Fatalf("expected error to contain 'unsupported language', got: %v", err)
}
}
func TestSDKGenerator_Bad_MissingSpec(t *testing.T) {
gen := &api.SDKGenerator{
SpecPath: filepath.Join(t.TempDir(), "nonexistent.json"),
OutputDir: t.TempDir(),
}
err := gen.Generate(context.Background(), "go")
if err == nil {
t.Fatal("expected error for missing spec file, got nil")
}
if !strings.Contains(err.Error(), "spec file not found") {
t.Fatalf("expected error to contain 'spec file not found', got: %v", err)
}
}
func TestSDKGenerator_Good_OutputDirCreated(t *testing.T) {
// Write a minimal spec file so we pass the file-exists check.
specDir := t.TempDir()
specPath := filepath.Join(specDir, "spec.json")
if err := os.WriteFile(specPath, []byte(`{"openapi":"3.1.0"}`), 0o644); err != nil {
t.Fatalf("failed to write spec file: %v", err)
}
outputDir := filepath.Join(t.TempDir(), "nested", "sdk")
gen := &api.SDKGenerator{
SpecPath: specPath,
OutputDir: outputDir,
}
// Generate will fail at the exec step (openapi-generator-cli likely not installed),
// but the output directory should have been created before that.
_ = gen.Generate(context.Background(), "go")
expected := filepath.Join(outputDir, "go")
info, err := os.Stat(expected)
if err != nil {
t.Fatalf("expected output directory %s to exist, got error: %v", expected, err)
}
if !info.IsDir() {
t.Fatalf("expected %s to be a directory", expected)
}
}
func TestSDKGenerator_Good_Available(t *testing.T) {
gen := &api.SDKGenerator{}
// Just verify it returns a bool and does not panic.
_ = gen.Available()
}