Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setter declarations and mocks #123

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 16 additions & 20 deletions languages/javascript/src/shared/Prop/MockProps.mjs
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import Mock from "../Transport/MockTransport.mjs"
import router from "./Router.mjs"

const mocks = {}

function mock(module, method, args, def) {
const fullMethod = `${module}.${method}`
if ((args == null) || args.length === 0 || (Object.values(args[0]).length === 0)) {
// get
const rv = mocks[fullMethod] && (mocks[fullMethod].value != null) ? mocks[fullMethod].value : def
return rv
} else {
// set
let mockMethod = mocks[fullMethod]
if (mockMethod == null) {
mockMethod = {
subscribers: []
}
}
mocks[fullMethod] = mockMethod
mockMethod.value = args[0].value
Mock.event(module, method + 'Changed', {
value: args[0].value
})
return {}
function mock(module, method, params, value, contextParameterCount, def) {
const type = router(params, value, contextParameterCount)
const hash = contextParameterCount ? '.' + Object.keys(params).filter(key => key !== 'value').map(key => params[key]).join('.') : ''
const key = `${module}.${method}${hash}`

if (type === "getter") {
const value = mocks.hasOwnProperty(key) ? mocks[key] : def
return value
}
else if (type === "subscriber") {
}
else if (type === "setter") {
mocks[key] = value
Mock.event(module, `${method}Changed`, { value })
return null
}
}

export default {
Expand Down
16 changes: 16 additions & 0 deletions languages/javascript/src/shared/Prop/Router.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default function (params, callbackOrValue, contextParameterCount) {
const numArgs = Object.values(params).length

if (numArgs === contextParameterCount && callbackOrValue === undefined) {
// getter
return "getter"
} else if (numArgs === contextParameterCount && typeof callbackOrValue === 'function') {
// subscribe
return "subscriber"
} else if (numArgs === (contextParameterCount) && callbackOrValue !== undefined) {
// setter
return "setter"
}

return null
}
13 changes: 8 additions & 5 deletions languages/javascript/src/shared/Prop/index.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import Transport from "../Transport/index.mjs"
import Events from "../Events/index.mjs"
import router from "./Router.mjs"

function prop(moduleName, key, params, callbackOrValue = undefined, immutable, readonly, contextParameterCount) {
const numArgs = Object.values(params).length
const type = router(params, callbackOrValue, contextParameterCount)

if (numArgs === contextParameterCount && callbackOrValue === undefined) {
// getter
if (type === "getter") {
return Transport.send(moduleName, key, params)
} else if (numArgs === contextParameterCount && typeof callbackOrValue === 'function') {
// subscribe
}
else if (type === "subscriber") {
// subscriber
if (immutable) {
throw new Error('Cannot subscribe to an immutable property')
}
return Events.listen(moduleName, key + 'Changed', ...Object.values(params), callbackOrValue)
} else if (numArgs === (contextParameterCount) && callbackOrValue !== undefined) {
}
else if (type === "setter") {
// setter
if (immutable) {
throw new Error('Cannot set a value to an immutable property')
Expand Down
2 changes: 1 addition & 1 deletion languages/javascript/templates/codeblocks/setter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
* Setter: ${method.summary}
*
*/
function ${method.setter.for}(value: ${method.result.type}): Promise<void>
function ${method.setter.for}(${method.signature.params}): Promise<void>
4 changes: 3 additions & 1 deletion languages/javascript/templates/defaults/property.mjs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
${method.name}: function () { return MockProps.mock('${info.title}', '${method.name}', arguments, ${method.example.value}) }
${method.name}: function (params) {
return MockProps.mock('${info.title}', '${method.name}', params, undefined, ${method.params.count}, ${method.example.value})
}
5 changes: 5 additions & 0 deletions languages/javascript/templates/defaults/setter.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
${method.name}: function (params) {
const callbackOrValue = params.value
delete params.value
return MockProps.mock('${info.title}', '${method.setter.for}', params, callbackOrValue, ${method.context.count}, ${method.example.value})
}
3 changes: 3 additions & 0 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ function generateDefaults(json = {}, templates) {
reduce((acc, val, i, arr) => {
if (isPropertyMethod(val)) {
acc += insertMethodMacros(getTemplate('/defaults/property', templates), val, json, templates)
} else if (val.tags.find(t => t.name === "setter")) {
acc += insertMethodMacros(getTemplate('/defaults/setter', templates), val, json, templates)
} else {
acc += insertMethodMacros(getTemplate('/defaults/default', templates), val, json, templates)
}
Expand Down Expand Up @@ -955,6 +957,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples={}) {
.replace(/\$\{method\.signature\.params\}/g, types.getMethodSignatureParams(methodObj, json, { destination: state.destination }))
.replace(/\$\{method\.context\}/g, method.context.join(', '))
.replace(/\$\{method\.context\.array\}/g, JSON.stringify(method.context))
.replace(/\$\{method\.context\.count}/g, method.context ? method.context.length : 0)
.replace(/\$\{method\.deprecation\}/g, deprecation)
.replace(/\$\{method\.Name\}/g, method.name[0].toUpperCase() + method.name.substr(1))
.replace(/\$\{event\.name\}/g, method.name.toLowerCase()[2] + method.name.substr(3))
Expand Down