Skip to content

Commit

Permalink
Add tests for creating comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vocksel committed Feb 4, 2024
1 parent cbf408b commit 7a66544
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 42 deletions.
23 changes: 23 additions & 0 deletions server/src/openapi/createLuauComment.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local types = require("@server/openapi/types")

type Object<T> = types.Object<T>

local function createLuauComment(object: Object<any>): string?
local comment = {}
if object.summary then
table.insert(comment, `-- {object.summary}`)
end
if object.summary and object.description then
table.insert(comment, "--")
end
if object.description then
table.insert(comment, `-- {object.description}`)
end
if #comment > 0 then
return table.concat(comment, "\n")
else
return nil
end
end

return createLuauComment
31 changes: 31 additions & 0 deletions server/src/openapi/createLuauComment.spec.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local fs = require("@lune/fs")
local serde = require("@lune/serde")
local openapiTypes = require("@server/openapi/openapi31")
local createLuauComment = require("./createLuauComment")

type OpenAPIObject = openapiTypes.OpenAPIObject

local api: OpenAPIObject = serde.decode("yaml", fs.readFile("server/src/openapi/mocks/commented-schema.yml"))

local comment = createLuauComment(api.components.schemas.SummaryAndDescription)
local expected = [[-- A short summary
--
-- A long-form description of the schema]]

assert(comment, "no comment created")
assert(comment == expected, "comment doesn't match expectation\ncomment: {comment}\nexpected: {expected}")

comment = createLuauComment(api.components.schemas.SummaryOnly)
expected = "-- A short summary"

assert(comment, "no comment created")
assert(comment == expected, "comment doesn't match expectation\ncomment: {comment}\nexpected: {expected}")

comment = createLuauComment(api.components.schemas.DescriptionOnly)
expected = "-- A long-form description of the schema"

assert(comment, "no comment created")
assert(comment == expected, "comment doesn't match expectation\ncomment: {comment}\nexpected: {expected}")

comment = createLuauComment(api.components.schemas.Empty)
assert(comment == nil, "a comment was created when it shouldn't have been")
65 changes: 30 additions & 35 deletions server/src/openapi/createLuauTypeFromOpenAPIObject.luau
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
local function createTypeCommentFromObject(object: Object<any>): string?
local comment = {}
if object.summary then
table.insert(comment, `-- {object.summary}`)
end
if object.summary and object.description then
table.insert(comment, "--")
end
if object.description then
table.insert(comment, `-- {object.description}`)
end
if #comment > 0 then
return table.concat(comment, "\n")
else
return nil
end
end
local types = require("@server/openapi/types")
local createLuauComment = require("@server/openapi/createLuauComment")

type Object<T> = { [string]: T }
type Object<T> = types.Object<T>

local function createTypePropertiesFromObject(object: Object<any>): Object<string | Object<any>>
local inner = {}
Expand Down Expand Up @@ -45,39 +30,49 @@ local function createTypePropertiesFromObject(object: Object<any>): Object<strin
return inner
end

type LuauType = {
comment: string?,
source: Object<any>,
}

local function createLuauTypeFromOpenAPIObject(object: Object<any>): LuauType
local comment = createTypeCommentFromObject(object)
local function createLuauTypeFromOpenAPIObject(object: Object<any>): types.LuauType
local comment = createLuauComment(object)

print(object)
if object.type == "object" then
local properties = {}

if object.properties then
for key, value in object.properties do
properties[key] = createTypePropertiesFromObject(value)
-- inner ..= `{INDENT}{key}: {createTypePropertiesFromObject(value)}?,\n`
print(key, value)
if typeof(value) == "table" then
value = createTypePropertiesFromObject(value)
else
value = typeof(value)
end
properties[key] = value
end
-- inner
end
local inner = table.concat(properties, "\n")

print(properties)
print("properties", properties)

local typeString = `export type {name} = \{{inner}\}`
if comment then
return `{comment}\n{typeString}`
else
return typeString
end
return properties

-- local typeString = `export type {name} = \{{inner}\}`
-- if comment then
-- return `{comment}\n{typeString}`
-- else
-- return typeString
-- end
elseif object.type == "integer" then
return "number"
else
return {
comment = comment,
source = typeof(object),
}
end

return {
name = name,
comment = createLuauComment(object),
}
end

return createLuauTypeFromOpenAPIObject
23 changes: 16 additions & 7 deletions server/src/openapi/createLuauTypeFromOpenAPIObject.spec.luau
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ local petstore: OpenAPIObject = serde.decode("yaml", fs.readFile("server/src/ope

assert(petstore.components.schemas, "no schemas found in mock API")

local schemas = createLuauTypeFromOpenAPIObject(petstore.components.schemas)

print("schemas", schemas)

assert(schemas.Pet, "missing type Pet")
assert(schemas.Pets, "missing type Pets")
assert(schemas.Error, "missing type Error")
-- print("schemas", petstore.components.schemas)

local Pet = createLuauTypeFromOpenAPIObject(petstore.components.schemas.Pet)
assert(Pet ~= nil, `missing "Pet" schema`)
assert(Pet.id == "number", `bad Pet.id (expected number, got {Pet.id})`)
assert(Pet.name == "string", `bad Pet.name (expected string, got {Pet.name})`)
assert(Pet.tag == "string?", `bad Pet.tag (expected string?, got {Pet.tag})`)

local Pets = createLuauTypeFromOpenAPIObject(petstore.components.schemas.Pets)
assert(Pets ~= nil, `missing "Pets" schema`)
assert(Pets.id == "number", `bad Pets.id (expected number, got {Pets.id})`)
assert(Pets.name == "string", `bad Pets.name (expected string, got {Pets.name})`)
assert(Pets.tag == "string?", `bad Pets.tag (expected string?, got {Pets.tag})`)

local Error = createLuauTypeFromOpenAPIObject(petstore.components.schemas.Error)
assert(Error, `missing "Error" schema`)

-- assert(nil, "summary is not included in type comment")
-- assert(nil, "description is not included in type comment")
Expand Down
22 changes: 22 additions & 0 deletions server/src/openapi/mocks/commented-schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: 3.1.0

info:
title: Title
description: Description
version: 1.0.0
basePath: /v1

components:
schemas:
SummaryAndDescription:
summary: "A short summary"
description: "A long-form description of the schema"

SummaryOnly:
summary: "A short summary"

DescriptionOnly:
description: "A long-form description of the schema"

Empty:
foo: true
12 changes: 12 additions & 0 deletions server/src/openapi/types.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type Object<T> = { [string]: T }

export type LuauType = {
name: string,
body: Primitive | { [string]: Primitive },
comment: string?,
unions: { string }?,
isExported: boolean?,
isRequired: boolean?,
}

return {}

0 comments on commit 7a66544

Please sign in to comment.