Skip to content

Commit

Permalink
Add ts tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spinillos committed Jul 24, 2023
1 parent 717140c commit 33693e0
Show file tree
Hide file tree
Showing 27 changed files with 427 additions and 0 deletions.
3 changes: 3 additions & 0 deletions encoding/typescript/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func GenerateTypes(sch thema.Schema, cfg *TypeConfig) (*ast.File, error) {
}
if cfg.CuetsyConfig == nil {
cfg.CuetsyConfig = &cuetsy.Config{
ImportMapper: func(_ string) (string, error) {
return "", nil
},
Export: true,
}
}
Expand Down
62 changes: 62 additions & 0 deletions encoding/typescript/ts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package typescript

import (
"github.com/stretchr/testify/require"
"testing"

"cuelang.org/go/cue/cuecontext"
"github.com/grafana/thema"
"github.com/grafana/thema/internal/txtartest/bindlin"
"github.com/grafana/thema/internal/txtartest/vanilla"
)

func TestGenerate(t *testing.T) {
test := vanilla.TxTarTest{
Root: "../../testdata/lineage",
Name: "encoding/typescript/TestGenerate",
ThemaFS: thema.CueJointFS,
Skip: map[string]string{
"lineage/refexscalar": "bounds constraints are not supported as they lack a direct typescript equivalent",
"lineage/refscalar": "bounds constraints are not supported as they lack a direct typescript equivalent",
},
}

ctx := cuecontext.New()
rt := thema.NewRuntime(ctx)

table := []struct {
name string
cfg *TypeConfig
}{
{
name: "nilcfg",
cfg: nil,
},
{},
}

for _, tb := range table {
t.Run(tb.name, func(t *testing.T) {
testcpy := test
testcpy.Name += "/" + tb.name
testcpy.Run(t, func(tc *vanilla.Test) {
if testing.Short() && tc.HasTag("slow") {
t.Skip("case is tagged #slow, skipping for -short")
}
lin, err := bindlin.BindTxtarLineage(tc, rt)
if err != nil {
tc.Fatal(err)
}

for sch := lin.First(); sch != nil; sch = sch.Successor() {
f, err := GenerateTypes(sch, tb.cfg)
if err != nil {
tc.Fatal(err)
}
_, err = tc.Write([]byte(f.String())) //nolint:gosec,errcheck
require.NoError(t, err)
}
})
})
}
}
56 changes: 56 additions & 0 deletions testdata/lineage/basic-multiversion.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -1840,3 +1840,59 @@ type Basicmultiversion struct {

// BasicmultiversionWithDefault defines model for Basicmultiversion.WithDefault.
type BasicmultiversionWithDefault string

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Basic-Multiversion {
init: string;
}
export interface Basic-Multiversion {
init: string;
optional?: number;
}
export interface Basic-Multiversion {
init: string;
optional?: number;
withDefault?: ('foo' | 'bar');
}

export const defaultBasic-Multiversion: Partial<Basic-Multiversion> = {
withDefault: 'foo',
};
export interface Basic-Multiversion {
init: string;
optional?: number;
withDefault?: ('foo' | 'bar' | 'baz');
}

export const defaultBasic-Multiversion: Partial<Basic-Multiversion> = {
withDefault: 'foo',
};
export interface Basic-Multiversion {
optional?: number;
renamed: string;
withDefault: ('foo' | 'bar' | 'baz');
}

export const defaultBasic-Multiversion: Partial<Basic-Multiversion> = {
withDefault: 'bar',
};
export interface Basic-Multiversion {
optional?: number;
renamed: string;
withDefault: ('foo' | 'bar' | 'baz' | 'bing');
}

export const defaultBasic-Multiversion: Partial<Basic-Multiversion> = {
withDefault: 'bar',
};
export interface Basic-Multiversion {
optional?: number;
toObj: {
init: string;
};
withDefault: ('foo' | 'bar' | 'baz' | 'bing');
}

export const defaultBasic-Multiversion: Partial<Basic-Multiversion> = {
withDefault: 'bar',
};
6 changes: 6 additions & 0 deletions testdata/lineage/embedexref.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,9 @@ type EmbedexrefRefField2 int
Schema count: 1
Schema versions: 0.0
Lenses count: 0

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Embedexref {
refField1: string;
refField2: 42;
}
6 changes: 6 additions & 0 deletions testdata/lineage/embedref.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,9 @@ type Embedref struct {

// EmbedrefRefField2 defines model for Embedref.RefField2.
type EmbedrefRefField2 int

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Embedref {
refField1: string;
refField2: 42;
}
27 changes: 27 additions & 0 deletions testdata/lineage/expand.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -687,3 +687,30 @@ type Expand struct {

// ExpandWithDefault defines model for Expand.WithDefault.
type ExpandWithDefault string

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Expand {
init: string;
}
export interface Expand {
init: string;
optional?: number;
}
export interface Expand {
init: string;
optional?: number;
withDefault?: ('foo' | 'bar');
}

export const defaultExpand: Partial<Expand> = {
withDefault: 'foo',
};
export interface Expand {
init: string;
optional?: number;
withDefault?: ('foo' | 'bar' | 'baz');
}

export const defaultExpand: Partial<Expand> = {
withDefault: 'foo',
};
11 changes: 11 additions & 0 deletions testdata/lineage/go-any.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,14 @@ type Goany struct {
} `json:"structVal"`
Value any `json:"value"`
}

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Go-Any {
emptyMap: Record<string, unknown>;
optional?: (string | boolean);
structVal: {
inner: (string | number);
innerOptional?: unknown;
};
value: (string | boolean);
}
7 changes: 7 additions & 0 deletions testdata/lineage/join/embedref.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,10 @@ type Embedref struct {

// EmbedrefRefField2 defines model for Embedref.RefField2.
type EmbedrefRefField2 int

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Embedref {
foo: string;
refField1: string;
refField2: 42;
}
11 changes: 11 additions & 0 deletions testdata/lineage/join/exref.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,14 @@ type Exref struct {
Ref ExRef `json:"ref"`
Refdef ExRefDef `json:"refdef"`
}

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Exref {
foo: string;
ref: {
normalField: string;
};
refdef: {
defField: string;
};
}
17 changes: 17 additions & 0 deletions testdata/lineage/join/nearoptional.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,20 @@ type Nearoptional struct {
} `json:"astruct,omitempty"`
Notoptional int32 `json:"notoptional"`
}

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Nearoptional {
abool?: boolean;
abytes?: string;
alist?: Array<string>;
anint?: number;
astring?: string;
astruct?: {
nested: string;
};
notoptional: number;
}

export const defaultNearoptional: Partial<Nearoptional> = {
alist: [],
};
5 changes: 5 additions & 0 deletions testdata/lineage/join/onenone.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,8 @@ package onenone

// Foo defines model for foo.
type Foo = string

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Onenone {
foo: string;
}
6 changes: 6 additions & 0 deletions testdata/lineage/join/oneone.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,9 @@ type Oneone struct {
Bar string `json:"bar"`
Foo string `json:"foo"`
}

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Oneone {
bar: string;
foo: string;
}
8 changes: 8 additions & 0 deletions testdata/lineage/join/onestruct.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,11 @@ type AField struct {

// Foo defines model for foo.
type Foo = string

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Onestruct {
aField: {
defLitField: string;
};
foo: string;
}
5 changes: 5 additions & 0 deletions testdata/lineage/join/repeat.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,8 @@ package repeat
type Repeat struct {
Foo string `json:"foo"`
}

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Repeat {
foo: string;
}
21 changes: 21 additions & 0 deletions testdata/lineage/maps.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,24 @@ type ValPrimitive map[string]bool
type ValStruct map[string]struct {
Foo string `json:"foo"`
}

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Maps {
aComplexMap?: {
foo: string;
};
optValList?: Record<string, Array<string>>;
optValPrimitive?: Record<string, boolean>;
optValStruct?: Record<string, {
foo: string,
}>;
refValue: Record<string, {
foo: string,
}>;
someField: Record<string, boolean>;
valList: Record<string, Array<string>>;
valPrimitive: Record<string, boolean>;
valStruct: Record<string, {
foo: string,
}>;
}
17 changes: 17 additions & 0 deletions testdata/lineage/nearoptional.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,20 @@ type Nearoptional struct {
} `json:"astruct,omitempty"`
Notoptional int32 `json:"notoptional"`
}

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Nearoptional {
abool?: boolean;
abytes?: string;
alist?: Array<string>;
anint?: number;
astring?: string;
astruct?: {
nested: string;
};
notoptional: number;
}

export const defaultNearoptional: Partial<Nearoptional> = {
alist: [],
};
5 changes: 5 additions & 0 deletions testdata/lineage/noref.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,8 @@ type Baz struct {

// SomeField defines model for someField.
type SomeField = string

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Noref {
someField: string;
}
5 changes: 5 additions & 0 deletions testdata/lineage/one-schema-versionless.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,8 @@ package oneschemaversionless

// Firstfield defines model for firstfield.
type Firstfield = string

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface One-Schema-Versionless {
firstfield: string;
}
16 changes: 16 additions & 0 deletions testdata/lineage/optional.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ schemas: [{
}
}]
lenses: []

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Optional {
abool?: boolean;
abytes?: string;
alist?: Array<string>;
anint?: number;
astring?: string;
astruct?: {
nested: string;
};
}

export const defaultOptional: Partial<Optional> = {
alist: [],
};
9 changes: 9 additions & 0 deletions testdata/lineage/refexstruct.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,12 @@ type Baz struct {
type Refexstruct struct {
ABaz Baz `json:"aBaz"`
}

-- out/encoding/typescript/TestGenerate/nilcfg --
export interface Refexstruct {
aBaz: {
run: string;
tell: string;
dat: number;
};
}
Loading

0 comments on commit 33693e0

Please sign in to comment.