Skip to content
Open
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
82 changes: 82 additions & 0 deletions packages/go/src/components/function/function.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,88 @@ it("applies camelCase naming policy when not exported", () => {
`);
});

it("applies explicit public prop - public=true with lowercase name", () => {
expect(
<TestPackage>
<FunctionDeclaration name="myFunction" public={true} />
</TestPackage>,
).toRenderTo(`
package alloy

func MyFunction() {}
`);
});

it("applies explicit public prop - public=false with uppercase name", () => {
expect(
<TestPackage>
<FunctionDeclaration name="MyFunction" public={false} />
</TestPackage>,
).toRenderTo(`
package alloy

func myFunction() {}
`);
});

it("preserves original case when public prop is not specified", () => {
expect(
<TestPackage>
<FunctionDeclaration name="MixedCaseFunction" />
</TestPackage>,
).toRenderTo(`
package alloy

func MixedCaseFunction() {}
`);
});

it("handles reserved words with public prop", () => {
expect(
<TestPackage>
<FunctionDeclaration name="func" public={true} />
</TestPackage>,
).toRenderTo(`
package alloy

func Func_() {}
`);
});

it("handles single character names with public prop", () => {
expect(
<TestPackage>
<FunctionDeclaration name="x" public={true} />
</TestPackage>,
).toRenderTo(`
package alloy

func X() {}
`);
});

it("handles empty string names with public prop", () => {
expect(() => {
render(
<TestPackage>
<FunctionDeclaration name="" public={true} />
</TestPackage>,
);
}).toThrow();
});

it("handles special characters in names with public prop", () => {
expect(
<TestPackage>
<FunctionDeclaration name="function_name_with_underscores" public={true} />
</TestPackage>,
).toRenderTo(`
package alloy

func Function_name_with_underscores() {}
`);
});

it("defines single-line params and return type", () => {
const params = [
{
Expand Down
4 changes: 4 additions & 0 deletions packages/go/src/components/function/function.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export interface FunctionProps {
*/
singleLine?: boolean;

/** Whether the function should be public (exported) or private (unexported) */
public?: boolean;

children?: Children;
}

Expand All @@ -73,6 +76,7 @@ export function FunctionDeclaration(props: FunctionProps) {

const functionSymbol = createFunctionSymbol(props.name, !!props.receiver, {
refkeys: props.refkey,
public: props.public,
});

// scope for function declaration
Expand Down
3 changes: 3 additions & 0 deletions packages/go/src/components/interface/declaration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,14 @@ export interface InterfaceFunctionProps {
refkey?: Refkey;
/** Doc comment */
doc?: Children;
/** Whether function should be public (exported) or private (unexported) */
public?: boolean;
}

export function InterfaceFunction(props: InterfaceFunctionProps) {
const symbol = createInterfaceMemberSymbol(props.name, {
refkeys: props.refkey,
public: props.public,
});
const functionScope = createFunctionScope();

Expand Down
64 changes: 64 additions & 0 deletions packages/go/src/components/interface/interface.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,70 @@ describe("naming", () => {
}
`);
});

it("applies explicit public prop - public=true with lowercase name for interface functions", () => {
expect(
<Wrapper>
<List>
<InterfaceFunction name="myFunc" returns="string" public={true} />
</List>
</Wrapper>,
).toRenderTo(`
package alloy

type TestInterface interface {
func MyFunc() string
}
`);
});

it("applies explicit public prop - public=false with uppercase name for interface functions", () => {
expect(
<Wrapper>
<List>
<InterfaceFunction name="MyFunc" returns="string" public={false} />
</List>
</Wrapper>,
).toRenderTo(`
package alloy

type TestInterface interface {
func myFunc() string
}
`);
});

it("preserves original case when public prop is not specified for interface functions", () => {
expect(
<Wrapper>
<List>
<InterfaceFunction name="mixedCaseFunc" returns="string" />
</List>
</Wrapper>,
).toRenderTo(`
package alloy

type TestInterface interface {
func mixedCaseFunc() string
}
`);
});

it("handles reserved words with public prop for interface functions", () => {
expect(
<Wrapper>
<List>
<InterfaceFunction name="func" returns="string" public={true} />
</List>
</Wrapper>,
).toRenderTo(`
package alloy

type TestInterface interface {
func Func_() string
}
`);
});
});

describe("embedded", () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/go/src/components/struct/declaration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,14 @@ export interface StructMemberProps {
tag?: string | Record<string, string>;
/** Doc comment */
doc?: Children;
/** Whether member should be public (exported) or private (unexported) */
public?: boolean;
}

export function StructMember(props: StructMemberProps) {
const symbol = createStructMemberSymbol(props.name, {
refkeys: props.refkey,
public: props.public,
});

const tagString = computed(() => {
Expand Down
72 changes: 72 additions & 0 deletions packages/go/src/components/struct/struct.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,78 @@ it("specify doc comment", () => {
`);
});

it("applies explicit public prop - public=true with lowercase name for struct members", () => {
expect(
<TestPackage>
<TypeDeclaration name="TestStruct">
<StructDeclaration>
<StructMember name="myField" type="string" public={true} />
</StructDeclaration>
</TypeDeclaration>
</TestPackage>,
).toRenderTo(`
package alloy

type TestStruct struct{
MyField string
}
`);
});

it("applies explicit public prop - public=false with uppercase name for struct members", () => {
expect(
<TestPackage>
<TypeDeclaration name="TestStruct">
<StructDeclaration>
<StructMember name="MyField" type="string" public={false} />
</StructDeclaration>
</TypeDeclaration>
</TestPackage>,
).toRenderTo(`
package alloy

type TestStruct struct{
myField string
}
`);
});

it("preserves original case when public prop is not specified for struct members", () => {
expect(
<TestPackage>
<TypeDeclaration name="TestStruct">
<StructDeclaration>
<StructMember name="MixedCaseField" type="string" />
</StructDeclaration>
</TypeDeclaration>
</TestPackage>,
).toRenderTo(`
package alloy

type TestStruct struct{
MixedCaseField string
}
`);
});

it("handles reserved words with public prop for struct members", () => {
expect(
<TestPackage>
<TypeDeclaration name="TestStruct">
<StructDeclaration>
<StructMember name="type" type="string" public={true} />
</StructDeclaration>
</TypeDeclaration>
</TestPackage>,
).toRenderTo(`
package alloy

type TestStruct struct{
Type_ string
}
`);
});

it("defines fields", () => {
expect(
<TestPackage>
Expand Down
51 changes: 51 additions & 0 deletions packages/go/src/components/type/declaration.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Children, expect, it } from "vitest";
import { TestPackage } from "../../../test/utils.js";
import { TypeDeclaration } from "./declaration.js";

it("applies explicit public prop - public=true with lowercase name for types", () => {
expect(
<TestPackage>
<TypeDeclaration name="myType" public={true}>string</TypeDeclaration>
</TestPackage>,
).toRenderTo(`
package alloy

type MyType string
`);
});

it("applies explicit public prop - public=false with uppercase name for types", () => {
expect(
<TestPackage>
<TypeDeclaration name="MyType" public={false}>string</TypeDeclaration>
</TestPackage>,
).toRenderTo(`
package alloy

type myType string
`);
});

it("preserves original case when public prop is not specified for types", () => {
expect(
<TestPackage>
<TypeDeclaration name="MixedCaseType">string</TypeDeclaration>
</TestPackage>,
).toRenderTo(`
package alloy

type MixedCaseType string
`);
});

it("handles reserved words with public prop for types", () => {
expect(
<TestPackage>
<TypeDeclaration name="type" public={true}>string</TypeDeclaration>
</TestPackage>,
).toRenderTo(`
package alloy

type Type_ string
`);
});
3 changes: 3 additions & 0 deletions packages/go/src/components/type/declaration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export interface TypeDeclarationProps {
doc?: Children;
/** Whether the type is an alias */
alias?: boolean;
/** Whether the type should be public (exported) or private (unexported) */
public?: boolean;
/** Type expression */
children?: Children;
/** Type parameters */
Expand All @@ -69,6 +71,7 @@ export function TypeDeclaration(props: TypeDeclarationProps) {
if (!symbol) {
symbol = createTypeSymbol(props.name, "type", {
refkeys: props.refkey,
public: props.public,
// TODO: set aliasTarget when alias is true
});

Expand Down
Loading