Skip to content

Commit

Permalink
Add helpers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Nov 6, 2024
1 parent 7aae411 commit a50362a
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export const firstNodeWithProblems = (node: Node): Node | undefined => {

export const isError = (problem: Problem): boolean => problem.level === 'error'

export const parentModule = (node: Node): Module => (node.ancestors.find(ancestor => ancestor.is(Module))) as Module ?? node.environment.objectClass
export const parentModule = (node: Node): Module => getParentModule(node) ?? node.environment.objectClass

export const parentImport = (node: Node): Import | undefined => node.ancestors.find(ancestor => ancestor.is(Import)) as Import

Expand Down Expand Up @@ -446,7 +446,7 @@ export const superMethodDefinition = (superNode: Super, methodModule: Module): M
return methodModule.lookupMethod(currentMethod.name, superNode.args.length, { lookupStartFQN: currentMethod.parent.fullyQualifiedName })
}

const getParentModule = (node: Node) => node.ancestors.find(is(Module)) as Module
const getParentModule = (node: Node): Module => node.ancestors.find(is(Module)) as Module

export const isVoid = (obj: RuntimeValue | RuntimeObject): boolean => obj?.module?.fullyQualifiedName === VOID_WKO

Expand All @@ -462,6 +462,8 @@ export const getExpressionFor = (node: Expression): string =>
`message ${nodeSend.message}/${nodeSend.args.length}`),
when(If)(_ => 'if expression'),
when(Reference)(nodeRef => `reference '${nodeRef.name}'`),
when(Literal)(nodeLiteral => `literal ${nodeLiteral.value}`),
when(Self)(_ => 'self'),
when(Expression)(_ => 'expression'),
)

Expand Down
200 changes: 198 additions & 2 deletions test/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { should, use } from 'chai'
import { expect, should, use } from 'chai'
import sinonChai from 'sinon-chai'
import { BOOLEAN_MODULE, Body, Class, Describe, Environment, Evaluation, Field, Import, Interpreter, isError, LIST_MODULE, Literal, Method, methodByFQN, NUMBER_MODULE, New, OBJECT_MODULE, Package, Parameter, Reference, STRING_MODULE, Self, Send, Singleton, Test, Variable, WRENatives, allAvailableMethods, allScopedVariables, allVariables, implicitImport, isNamedSingleton, isNotImportedIn, link, linkSentenceInNode, literalValueToClass, mayExecute, parentModule, parse, projectPackages, hasNullValue, hasBooleanValue, projectToJSON, getNodeDefinition, ParameterizedType, sendDefinitions, Super, SourceMap } from '../src'
import { BOOLEAN_MODULE, Body, Class, Describe, Environment, Evaluation, Field, Import, Interpreter, isError, LIST_MODULE, Literal, Method, methodByFQN, NUMBER_MODULE, New, OBJECT_MODULE, Package, Parameter, Reference, STRING_MODULE, Self, Send, Singleton, Test, Variable, WRENatives, allAvailableMethods, allScopedVariables, allVariables, implicitImport, isNamedSingleton, isNotImportedIn, link, linkSentenceInNode, literalValueToClass, mayExecute, parentModule, parse, projectPackages, hasNullValue, hasBooleanValue, projectToJSON, getNodeDefinition, ParameterizedType, sendDefinitions, Super, SourceMap, isVoid, VOID_WKO, REPL, buildEnvironment, assertNotVoid, showParameter, getMethodContainer, Program, getExpressionFor, Expression, If, Return } from '../src'
import { WREEnvironment, environmentWithEntities } from './utils'
import { RuntimeObject } from '../src/interpreter/runtimeModel'

use(sinonChai)
should()
Expand Down Expand Up @@ -768,4 +769,199 @@ describe('Wollok helpers', () => {

})

describe('isVoid', () => {
const replEnvironment = buildEnvironment([{
name: REPL, content: `
object pajarito {
method volar() {
}
}
`,
}])
const evaluation = Evaluation.build(replEnvironment, WRENatives)

it('should return true for void singleton', () => {
isVoid(new RuntimeObject(replEnvironment.getNodeByFQN(VOID_WKO), evaluation.currentFrame, undefined)).should.be.true
})

it('should return false for Wollok elements', () => {
isVoid(new RuntimeObject(replEnvironment.getNodeByFQN(NUMBER_MODULE), evaluation.currentFrame, 42)).should.be.false
})

it('should return false for custom definitions', () => {
isVoid(new RuntimeObject(replEnvironment.getNodeByFQN(REPL + '.pajarito'), evaluation.currentFrame, undefined)).should.be.false
})

})

describe('assertNotVoid', () => {
const replEnvironment = buildEnvironment([{
name: REPL, content: `
object pajarito {
method volar() {
}
}
`,
}])
const evaluation = Evaluation.build(replEnvironment, WRENatives)

it('should throw error if value is void', () => {
expect(() => assertNotVoid(new RuntimeObject(replEnvironment.getNodeByFQN(VOID_WKO), evaluation.currentFrame, undefined), 'Something failed')).to.throw('Something failed')
})

it('should not throw error if value is not void', () => {
assertNotVoid(new RuntimeObject(replEnvironment.getNodeByFQN(NUMBER_MODULE), evaluation.currentFrame, 2), 'Something failed')
})

})

describe('showParameter', () => {
const replEnvironment = buildEnvironment([{
name: REPL, content: `
object pajarito {
method volar() {
}
}
`,
}])
const evaluation = Evaluation.build(replEnvironment, WRENatives)

it('should show a number', () => {
showParameter(new RuntimeObject(replEnvironment.getNodeByFQN(NUMBER_MODULE), evaluation.currentFrame, 2)).should.equal('"2"')
})

it('should show a string', () => {
showParameter(new RuntimeObject(replEnvironment.getNodeByFQN(STRING_MODULE), evaluation.currentFrame, 'pepita')).should.equal('"pepita"')
})

it('should show fqn for custom modules', () => {
showParameter(new RuntimeObject(replEnvironment.getNodeByFQN(REPL + '.pajarito'), evaluation.currentFrame, undefined)).should.equal(`"${REPL}.pajarito"`)
})

})

describe('getMethodContainer', () => {
const replEnvironment = buildEnvironment([{
name: REPL, content: `
object pajarito {
energia = 100
method volar() {
energia = energia + 10
}
}`,
}, {
name: 'test',
content: `
describe "some describe" {
test "some test" {
assert.equals(1, 1)
}
}
`,
}, {
name: 'program',
content: `
program Prueba {
const a = 1
const b = a + 1
console.println(a)
console.println(b)
}
`,
},
])

it('should find method container for a method', () => {
const birdSingleton = replEnvironment.getNodeByFQN(REPL + '.pajarito') as Singleton
const volarMethod = birdSingleton.allMethods[0] as Method
const volarSentence = volarMethod.sentences[0]
getMethodContainer(volarSentence)!.should.equal(volarMethod)
})

it('should find method container for a test', () => {
const firstDescribe = replEnvironment.getNodeByFQN('test."some describe"') as Describe
const firstTest = firstDescribe.allMembers[0] as Test
const assertSentence = firstTest.sentences[0]
getMethodContainer(assertSentence)!.should.equal(firstTest)
})

it('should find method container for a program', () => {
const program = replEnvironment.getNodeByFQN('program.Prueba') as Program
const anySentence = program.sentences()[3]
getMethodContainer(anySentence)!.should.equal(program)
})

})

describe('getExpression', () => {
const replEnvironment = buildEnvironment([{
name: REPL, content: `
object pajarito {
energia = 100
contenta = false
method jugar() {
contenta = true
}
method volar() {
if (energia > 100) {
self.jugar()
}
return energia
}
method valorBase() = 2
method bad() {
throw new Exception(message = "Do not call me!")
}
}`,
},
])

it('should show if expression', () => {
const birdSingleton = replEnvironment.getNodeByFQN(REPL + '.pajarito') as Singleton
const volarMethod = birdSingleton.allMethods[1] as Method
const ifExpression = volarMethod.sentences[0] as Expression
getExpressionFor(ifExpression)!.should.equal('if expression')
})

it('should show send expression', () => {
const birdSingleton = replEnvironment.getNodeByFQN(REPL + '.pajarito') as Singleton
const volarMethod = birdSingleton.allMethods[1] as Method
const sendExpression = (volarMethod.sentences[0] as If).thenBody.sentences[0] as Expression
getExpressionFor(sendExpression)!.should.equal('message jugar/0')
})

it('should show reference expression', () => {
const birdSingleton = replEnvironment.getNodeByFQN(REPL + '.pajarito') as Singleton
const volarMethod = birdSingleton.allMethods[1] as Method
const referenceExpression = (volarMethod.sentences[1] as Return).value as Expression
getExpressionFor(referenceExpression)!.should.equal('reference \'energia\'')
})

it('should show literal expression', () => {
const birdSingleton = replEnvironment.getNodeByFQN(REPL + '.pajarito') as Singleton
const valorBaseMethod = birdSingleton.allMethods[2] as Method
const literalExpression = (valorBaseMethod.sentences[0] as Return).value as Expression
getExpressionFor(literalExpression)!.should.equal('literal 2')
})

it('should show self expression', () => {
const birdSingleton = replEnvironment.getNodeByFQN(REPL + '.pajarito') as Singleton
const volarMethod = birdSingleton.allMethods[1] as Method
const selfExpression = ((volarMethod.sentences[0] as If).thenBody.sentences[0] as Send).receiver as Expression
getExpressionFor(selfExpression)!.should.equal('self')
})

it('should show default expression', () => {
const birdSingleton = replEnvironment.getNodeByFQN(REPL + '.pajarito') as Singleton
const badMethod = birdSingleton.allMethods[3] as Method
const throwException = badMethod.sentences[0] as Expression
getExpressionFor(throwException)!.should.equal('expression')
})

})

})

0 comments on commit a50362a

Please sign in to comment.