-
Notifications
You must be signed in to change notification settings - Fork 8
/
cli.test.js
83 lines (69 loc) · 3.32 KB
/
cli.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require('fs')
const { createKeywordsRegex, convertToJS, runMamaLang, translateKeywordToJS } = require('./cli')
describe('MamaLang Translator', () => {
it('should create a regex pattern that matches keywords', () => {
const regex = createKeywordsRegex()
expect(regex.test('mama aida hoilo')).toBe(true)
expect(regex.test('bol toh mama')).toBe(true)
expect(regex.test('blah blah')).toBe(false)
})
it('should translate a MamaLang keyword to JavaScript', () => {
expect(translateKeywordToJS('mama aida hoilo')).toBe('let')
expect(translateKeywordToJS('bol toh mama')).toBe('console.log')
expect(translateKeywordToJS('kisuina mama')).toBe('null')
expect(translateKeywordToJS('nonexistent')).toBe('nonexistent')
})
it('should convert MamaLang code to JavaScript', () => {
const sourceCode = 'mama aida hoilo a = 5; bol toh mama(a);'
const jsCode = convertToJS(sourceCode)
expect(jsCode).toBe('let a = 5; console.log(a);')
})
it('should execute MamaLang code from a file', () => {
const testFileName = 'test-mama-code.mama' // Path to the test file
const testCode = 'mama aida hoilo a = 5; bol toh mama(a);'
const expectedJsCode = 'let a = 5; console.log(a);' // Expected JavaScript code
// Write test code to the file
fs.writeFileSync(testFileName, testCode, 'utf8')
// Mock eval to verify execution
const evalSpy = jest.spyOn(global, 'eval')
// Run the function
runMamaLang(testFileName)
// Verify that eval was called with the correct JavaScript code
expect(evalSpy).toHaveBeenCalledWith(expectedJsCode)
// Clean up: remove the test file and restore eval
fs.unlinkSync(testFileName)
evalSpy.mockRestore()
})
it('should correctly translate complex MamaLang expressions', () => {
const sourceCode =
'mama aida hoilo x = 10; jodi mama (x > 5) { bol toh mama("x is greater than 5"); }'
const jsCode = convertToJS(sourceCode)
const expectedJsCode = 'let x = 10; if (x > 5) { console.log("x is greater than 5"); }'
expect(jsCode).toBe(expectedJsCode)
})
it('should handle control flow structures in MamaLang', () => {
const sourceCode =
'jodi mama (condition) { bol toh mama("True"); } akdom e nah hoile { bol toh mama("False"); }'
const jsCode = convertToJS(sourceCode)
const expectedJsCode = 'if (condition) { console.log("True"); } else { console.log("False"); }'
expect(jsCode).toBe(expectedJsCode)
})
it('should not translate keywords embedded in other words', () => {
const sourceCode = 'text containingmama aida hoilo not a keyword'
const jsCode = convertToJS(sourceCode)
expect(jsCode).toBe('text containingmama aida hoilo not a keyword')
})
it('should correctly handle mixed content', () => {
const sourceCode = 'This is a comment. mama aida hoilo x = 5; // This is another comment'
const jsCode = convertToJS(sourceCode)
const expectedJsCode = 'This is a comment. let x = 5; // This is another comment'
expect(jsCode).toBe(expectedJsCode)
})
// Writing tests for functions
it('should handle the function declaration and return value', () => {
const sourceCode = 'mama kam da hoilo jog(a,b){ de toh mama (a+b);}'
const jsCode = convertToJS(sourceCode)
const expectedJsCode = 'function jog(a,b){ return (a+b);}'
expect(jsCode).toBe(expectedJsCode)
})
})