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

doesNotUseRecursion fix #947

Merged
merged 8 commits into from
Apr 13, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions app/utils/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const multiExpect = (...expectations) => (element) =>

// DECLARATION EXPECTATIONS
export const doSomething = (declaration) =>
newExpectation(`within ${toEDLString(declaration)} count(calls) >= 1`, 'do_something', { declaration })
newExpectation(`${countCallsWithin(declaration)} >= 1`, 'do_something', { declaration })

export const isUsed = (declaration) =>
newExpectation(`calls ${toEDLString(declaration)}`, 'is_used', { declaration })
Expand All @@ -21,15 +21,20 @@ export const isUsedFromMain = (declaration) =>
newExpectation(`through ${toEDLString(entryPointType)} calls ${toEDLString(declaration)}`, 'is_used_from_main', { declaration })

export const notTooLong = (limit = 7) => (declaration) =>
newExpectation(`within ${toEDLString(declaration)} count(calls) <= ${limit - 1}`, 'too_long', { declaration, limit })
newExpectation(`${countCallsWithin(declaration)} <= ${limit - 1}`, 'too_long', { declaration, limit })

export const doesNotUseRecursion = (declaration) =>
newExpectation(`through ${toEDLString(declaration)} ! calls ${toEDLString(declaration)}`, doesNotUseRecursionId, { declaration })
newExpectation(`not (through ${toEDLString(declaration)} calls ${toEDLString(declaration)})`, doesNotUseRecursionId, { declaration })

// UTILS
const newExpectation = (expect, id, opts = {}) =>
export const newExpectation = (expect, id, opts = {}) =>
`expectation "${stringify(id, opts)}": ${expect};`

// Use this to count number of calls inside a procedure, including recursive calls
// Mulang count does not count recursive calls
export const countCallsWithin = (declaration) =>
`within ${toEDLString(declaration)} count(calls) + count(calls ${toEDLString(declaration)})`

export const stringify = (id, opts) =>
`${expectationName(id)}|${Object.entries(opts).map(([key, value]) => `${key}=${value}`).join(';')}`

Expand Down
60 changes: 45 additions & 15 deletions tests/unit/services/mulang-expectations-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { module, test } from 'qunit'
import { entryPointType } from '../../../utils/blocks'
import { declaresAnyProcedure, doSomething, isUsed, isUsedFromMain, notTooLong, parseExpect, doesNotUseRecursion, stringify, expectationId, isCritical, doesNotUseRecursionId } from '../../../utils/expectations'
import { declaresAnyProcedure, doSomething, isUsed, isUsedFromMain, notTooLong, parseExpect, doesNotUseRecursion, stringify, expectationId, isCritical, doesNotUseRecursionId, newExpectation, countCallsWithin } from '../../../utils/expectations'
import { procedure, entryPoint, rawSequence, application } from '../../helpers/astFactories'
import { setupPBUnitTest, setUpTestWorkspace } from '../../helpers/utils'

Expand Down Expand Up @@ -28,7 +28,13 @@ module('Unit | Service | Mulang | Expectations', function (hooks) {
application('PRIMITIVE')
)
])


expectationTestOk('doSomething', doSomething(declaration), [
procedure(declaration, [],
application(declaration)
)
], 'Recursion should count as doing something')

expectationTestFail('doSomething', doSomething('EMPTY'), [
procedure('EMPTY', [])
])
Expand Down Expand Up @@ -84,42 +90,66 @@ module('Unit | Service | Mulang | Expectations', function (hooks) {
application('PRIMITIVE'),
)
])


expectationTestFail('notTooLong', notTooLong(limit)(declaration), [
procedure(declaration, [],
application(declaration),
application(declaration),
application(declaration)
)
], 'Recursive calls should count as being too long ')

expectationTestOk('doesNotUseRecursion', doesNotUseRecursion(declaration), [
procedure(declaration, [],
application("PROCEDURE2")
application("PROCEDURE2")
),
procedure("PROCEDURE2", [])
])

// Direct recursion
expectationTestFail('doesNotUseRecursion', doesNotUseRecursion(declaration), [
procedure(declaration, [],
application(declaration)
application(declaration)
)
])
/*

// Indirect recursion
expectationTestFail('doesNotUseRecursion', doesNotUseRecursion(declaration), [
procedure(declaration, [],
application("PROCEDURE2")
application("PROCEDURE2")
),
procedure("PROCEDURE2", [],
application(declaration)
)
])
*/
], 'Indirect recursion should count as recursion')

expectationTestFail('doesNotUseRecursion', doesNotUseRecursion(declaration), [
procedure(declaration, [],
application(declaration),
application("PROCEDURE2")
),
procedure("PROCEDURE2", [],
application('PRIMITIVE'))
], 'Direct recursion with another procedure call should count as recursion')

expectationTestOk('countCallsWithin', newExpectation(`${countCallsWithin(declaration)} = 2`, 'counts', { declaration }), [
procedure(declaration, [],
application("PROCEDURE2"),
application(declaration)
),
procedure("PROCEDURE2", [])
], 'countCallsWithin includes recursive calls')

function expectationTestOk(expectationName, expectation, astNodes) {
expectationTest(expectationName, expectation, astNodes, true)
function expectationTestOk(expectationName, expectation, astNodes, testName) {
expectationTest(expectationName, expectation, astNodes, true, testName)
}

function expectationTestFail(expectationName, expectation, astNodes) {
expectationTest(expectationName, expectation, astNodes, false)
function expectationTestFail(expectationName, expectation, astNodes, testName) {
expectationTest(expectationName, expectation, astNodes, false, testName)
}

function expectationTest(expectationName, edl, astNodes, shouldPass) {
test(`Expectation ${expectationName} - ${shouldPass ? 'ok' : 'fail'}`, function (assert) {
function expectationTest(expectationName, edl, astNodes, shouldPass, testName = '') {
test(`Expectation ${expectationName} - ${testName || (shouldPass ? 'ok' : 'fail')}`, function (assert) {
const mulangResult = mulang
.astCode(rawSequence(astNodes))
.customExpect(edl)
Expand Down