Skip to content

Commit

Permalink
Add test for #2690.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Nov 20, 2023
1 parent 7848ec0 commit f6e159d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
3 changes: 0 additions & 3 deletions packages/versioning/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ export function $onValidate(program: Program) {
if (op.interface) {
validateTargetVersionCompatible(program, op.interface, op, { isTargetADependent: true });
}
for (const prop of op.parameters.properties.values()) {
validateReference(program, op, prop.type);
}
validateReference(program, op, op.returnType);
},
interface: (iface) => {
Expand Down
29 changes: 0 additions & 29 deletions packages/versioning/test/versioned-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,35 +809,6 @@ describe("versioning: dependencies", () => {
const [v1] = runProjections(runner.program, MyService);
ok(v1.projectedTypes.get(MyService));
});

it("allows an operation parameter to have versioned properties", async () => {
const { WidgetService } = (await runner.compile(`
@versioned(Versions)
@service({
title: "Widget Service",
})
@test("WidgetService")
namespace WidgetService;
enum Versions {
v1,
v2,
}
model Widget {
@key id: string;
@added(Versions.v2) name: string;
}
@added(Versions.v1)
@post
@route("create")
op create(@body _: Widget): Widget;
`)) as { WidgetService: Namespace };

const [v1, v2] = runProjections(runner.program, WidgetService);
ok(v1.projectedTypes.get(WidgetService));
});
});

function runProjections(program: Program, rootNs: Namespace) {
Expand Down
49 changes: 47 additions & 2 deletions packages/versioning/test/versioning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ import {
Model,
Namespace,
Operation,
Program,
ProjectionApplication,
projectProgram,
Scalar,
Type,
Union,
} from "@typespec/compiler";
import { BasicTestRunner, createTestWrapper, expectDiagnostics } from "@typespec/compiler/testing";
import {
BasicTestRunner,
createTestWrapper,
expectDiagnosticEmpty,
expectDiagnostics,
} from "@typespec/compiler/testing";
import { fail, ok, strictEqual } from "assert";
import { Version } from "../src/types.js";
import { VersioningTimeline } from "../src/versioning-timeline.js";
import { getVersions, indexTimeline } from "../src/versioning.js";
import { buildVersionProjections, getVersions, indexTimeline } from "../src/versioning.js";
import { createVersioningTestHost } from "./test-host.js";
import {
assertHasMembers,
Expand Down Expand Up @@ -873,6 +879,39 @@ describe("versioning: logic", () => {
assertHasProperties(v2.parameters, ["a"]);
});

it("can be added on the same version the underlying model is added", async () => {
const { MyService } = (await runner.compile(
`
@test("MyService")
@versioned(Versions)
namespace MyService;
enum Versions { v1, v2 };
@added(Versions.v1)
op create(body: Widget, @added(Versions.v2) newThing: NewThing): Widget;
model Widget {
@key id: string;
@added(Versions.v2) name: string;
}
@added(Versions.v2)
model NewThing {
name: string;
}
`
)) as { MyService: Namespace };

const [v1, v2] = runProjections(runner.program, MyService);
const w1 = v1.projectedTypes.get(MyService) as Namespace;
const w2 = v2.projectedTypes.get(MyService) as Namespace;
w1.models.get("Widget")?.properties.size === 1;
w1.operations.get("create")?.parameters.properties.size === 1;
w2.models.get("Widget")?.properties.size === 2;
w2.operations.get("create")?.parameters.properties.size === 2;
});

it("can be removed", async () => {
const {
projections: [v1, v2],
Expand Down Expand Up @@ -1780,3 +1819,9 @@ describe("versioning: logic", () => {
return projector.projectedTypes.get(target) as T;
}
});
function runProjections(program: Program, rootNs: Namespace) {
const versions = buildVersionProjections(program, rootNs);
const projectedPrograms = versions.map((x) => projectProgram(program, x.projections));
projectedPrograms.forEach((p) => expectDiagnosticEmpty(p.diagnostics));
return projectedPrograms.map((p) => p.projector);
}

0 comments on commit f6e159d

Please sign in to comment.