Skip to content

Commit

Permalink
Fix test (#7366)
Browse files Browse the repository at this point in the history
* Fix test

* Improve translations
  • Loading branch information
iHiD authored Jan 22, 2025
1 parent bef3cc8 commit 7034396
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
12 changes: 7 additions & 5 deletions app/javascript/interpreter/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,10 +479,7 @@ export class Executor
callee = this.evaluate(expression.callee)
} catch (e) {
if (isRuntimeError(e) && e.type == 'CouldNotFindValueWithName') {
if (
expression.callee instanceof VariableExpression &&
e.context?.didYouMean?.function?.length > 0
) {
if (e.context?.didYouMean?.function?.length > 0) {
const alternative = e.context.didYouMean.function
this.error('CouldNotFindFunctionWithNameSuggestion', e.location, {
...e.context,
Expand All @@ -491,7 +488,12 @@ export class Executor
})
}

this.error('CouldNotFindFunctionWithName', e.location, e.context)
this.error('CouldNotFindFunctionWithName', e.location, {
...e.context,
...{
name: expression.callee.name.lexeme,
},
})
}

throw e
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/interpreter/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export abstract class Expression {

export class CallExpression extends Expression {
constructor(
public callee: Expression,
public callee: VariableExpression,
public paren: Token,
public args: Expression[],
public location: Location
Expand Down
9 changes: 5 additions & 4 deletions app/javascript/interpreter/locales/system/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,26 @@
},
"runtime": {
"CouldNotEvaluateFunction": "CouldNotEvaluateFunction",
"CouldNotFindFunctionWithName": "CouldNotFindFunctionWithName",
"CouldNotFindFunctionWithName": "CouldNotFindFunctionWithName: name: {{name}}",
"CouldNotFindFunctionWithNameSuggestion": "CouldNotFindFunctionWithNameSuggestion: name: {{name}}, suggestion: {{suggestion}}",
"CouldNotFindValueWithName": "CouldNotFindValueWithName: name: {{name}}",
"InfiniteLoop": "InfiniteLoop",
"InvalidBinaryExpression": "InvalidBinaryExpression",
"InvalidExpression": "InvalidExpression",
"InvalidIndexGetterTarget": "InvalidIndexGetterTarget",
"InvalidIndexSetterTarget": "InvalidIndexSetterTarget",
"InvalidNumberOfArguments": "InvalidNumberOfArguments: maxArity: {{maxArity}}, numberOfArgs: {{numberOfArgs}}",
"InvalidNumberOfArgumentsWithOptionalArguments": "InvalidNumberOfArgumentsWithOptionalArguments: minArity: {{minArity}}, maxArity: {{maxArity}}, numberOfArgs: {{numberOfArgs}}",
"InvalidUnaryOperator": "InvalidUnaryOperator",
"MissingParenthesesForFunctionCall": "MissingParenthesesForFunctionCall",
"MissingParenthesesForFunctionCall": "MissingParenthesesForFunctionCall: name: {{name}}",
"NonCallableTarget": "NonCallableTarget",
"OperandMustBeBoolean": "OperandMustBeBoolean",
"OperandMustBeNumber": "OperandMustBeNumber",
"OperandsMustBeNumber": "OperandsMustBeNumber",
"OperandsMustBeTwoNumbersOrTwoStrings": "OperandsMustBeTwoNumbersOrTwoStrings",
"RepeatCountMustBeGreaterThanZero": "RepeatCountMustBeGreaterThanZero",
"RepeatCountMustBeNumber": "RepeatCountMustBeNumber"
"RepeatCountMustBeNumber": "RepeatCountMustBeNumber",
"TooManyArguments": "TooManyArguments: arity: {{arity}}, numberOfArgs: {{numberOfArgs}}",
"TooFewArguments": "TooFewArguments: arity: {{arity}}, numberOfArgs: {{numberOfArgs}}"
},
"disabledLanguageFeature": {
"ExcludeListViolation": "ExcludeListViolation: tokenType: {{tokenType}}",
Expand Down
43 changes: 34 additions & 9 deletions test/javascript/interpreter/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import {
evaluateFunction,
} from '@/interpreter/interpreter'
import type { ExecutionContext } from '@/interpreter/executor'
import { changeLanguage } from '@/interpreter/translator'

beforeAll(() => {
changeLanguage('system')
})

afterAll(() => {
changeLanguage('en')
})

describe('statements', () => {
describe('expression', () => {
Expand Down Expand Up @@ -1141,6 +1150,9 @@ describe('errors', () => {
expect(frames[0].error).not.toBeNull()
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe('CouldNotFindFunctionWithName')
expect(frames[0].error!.message).toBe(
'CouldNotFindFunctionWithName: name: foo'
)
expect(error).toBeNull()
})

Expand All @@ -1162,6 +1174,9 @@ describe('errors', () => {
expect(frames[2].error).not.toBeNull()
expect(frames[2].error!.category).toBe('RuntimeError')
expect(frames[2].error!.type).toBe('CouldNotFindFunctionWithName')
expect(frames[2].error!.message).toBe(
'CouldNotFindFunctionWithName: name: foo'
)
expect(error).toBeNull()
})
})
Expand Down Expand Up @@ -1214,7 +1229,7 @@ describe('errors', () => {
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe('TooManyArguments')
expect(frames[0].error!.message).toBe(
'Did you add an extra argument? This function expects to be called with 0 arguments but you called it with 1.'
'TooManyArguments: arity: 0, numberOfArgs: 1'
)
expect(error).toBeNull()
})
Expand All @@ -1238,7 +1253,7 @@ describe('errors', () => {
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe('TooFewArguments')
expect(frames[0].error!.message).toBe(
'Did you forget an argument? This function expects to be called with 1 arguments but you called it with 0.'
'TooFewArguments: arity: 1, numberOfArgs: 0'
)
expect(error).toBeNull()
})
Expand All @@ -1255,6 +1270,9 @@ describe('errors', () => {
expect(frames[0].error).not.toBeNull()
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe('CouldNotFindFunctionWithName')
expect(frames[0].error!.message).toBe(
'CouldNotFindFunctionWithName: name: foo'
)
expect(error).toBeNull()
})

Expand All @@ -1272,7 +1290,7 @@ describe('errors', () => {
expect(frames[0].code).toBe('foobor()')
expect(frames[0].error).not.toBeNull()
expect(frames[0].error!.message).toBe(
"Jiki couldn't find a function with the name `foobor`. Maybe you meant to use the `foobar` function instead?"
'CouldNotFindFunctionWithNameSuggestion: name: foobor, suggestion: foobar'
)
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe(
Expand All @@ -1295,11 +1313,11 @@ describe('errors', () => {
expect(frames[0].status).toBe('ERROR')
expect(frames[0].code).toBe('foo')
expect(frames[0].error).not.toBeNull()
expect(frames[0].error!.message).toBe(
'Did you forget the parenthesis when trying to use this function?\n\nDid you mean:\n\n```foo()```'
)
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe('MissingParenthesesForFunctionCall')
expect(frames[0].error!.message).toBe(
'MissingParenthesesForFunctionCall: name: foo'
)
expect(error).toBeNull()
})

Expand All @@ -1315,11 +1333,12 @@ describe('errors', () => {
expect(frames[0].status).toBe('ERROR')
expect(frames[0].code).toBe('set x to y')
expect(frames[0].error).not.toBeNull()
expect(frames[0].error!.message).toBe(
'Did you forget the parenthesis when trying to use this function?\n\nDid you mean:\n\n```y()```'
)
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe('MissingParenthesesForFunctionCall')
expect(frames[0].error!.message).toBe(
'MissingParenthesesForFunctionCall: name: y'
)

expect(error).toBeNull()
})

Expand All @@ -1339,6 +1358,9 @@ describe('errors', () => {
expect(frames[1].error).not.toBeNull()
expect(frames[1].error!.category).toBe('RuntimeError')
expect(frames[1].error!.type).toBe('CouldNotFindFunctionWithName')
expect(frames[1].error!.message).toBe(
'CouldNotFindFunctionWithName: name: foo'
)
expect(error).toBeNull()
})

Expand All @@ -1354,6 +1376,9 @@ describe('errors', () => {
expect(frames[0].error).not.toBeNull()
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe('CouldNotFindFunctionWithName')
expect(frames[0].error!.message).toBe(
'CouldNotFindFunctionWithName: name: foo'
)
expect(error).toBeNull()
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class ViewsExerciseMentoringTest < ApplicationSystemTestCase

within(".mentoring-in-progress") { assert_text "yamikani" }

Mentor::Discussion::FinishByStudent.(discussion, 5)
::Mentor::Discussion::FinishByStudent.(discussion, 5)

# Reload page
visit track_exercise_mentor_discussions_path(track, exercise)
Expand Down

0 comments on commit 7034396

Please sign in to comment.