Forráskód Böngészése

Implement some test cases for the language defined functions

-Fix a typo in syntaxErrorFactory class
Lucas de Souza 5 éve
szülő
commit
bcf4f8594f

+ 1 - 1
js/ast/error/syntaxErrorFactory.js

@@ -9,7 +9,7 @@ export const SyntaxErrorFactory = Object.freeze({
   },
   token_missing_list: (expectedList, token) => {
     const line = expectedList.join(LocalizedStrings.getOR());
-    return SyntaxErrorCodes.token_missing_one(line, token);
+    return SyntaxErrorFactory.token_missing_one(line, token);
   },
   id_missing: (token) => {
     const context = [token.text, token.line, token.column];

+ 32 - 0
tests/test53.spec.js

@@ -0,0 +1,32 @@
+import { IVProgParser } from './../js/ast/ivprogParser';
+import { SemanticAnalyser } from './../js/processor/semantic/semanticAnalyser';
+import { LanguageService } from '../js/services/languageService';
+import { OutputTest } from '../js/util/outputTest';
+import { IVProgProcessor } from '../js/processor/ivprogProcessor';
+
+describe('Math lib modulo function ', function () {
+
+  const code = `programa {
+
+    funcao inicio() {
+      real a = -1.0
+      escreva(Matematica.modulo(a))
+    }
+  }`;
+
+  localStorage.setItem('ivprog.lang', 'pt');
+
+  const lexer = LanguageService.getCurrentLexer();
+  const out = new OutputTest();
+
+  it(`should be executed with no exception`, function (done) {
+    const parser = new IVProgParser(code, lexer);
+    const sem = new SemanticAnalyser(parser.parseTree());
+    const exec = new IVProgProcessor(sem.analyseTree());
+    exec.registerOutput(out);
+    exec.interpretAST().then(_ => {
+      expect(out.list).toEqual(['1.0']);
+      done();
+    }).catch(err => done(err));
+  });
+});

+ 32 - 0
tests/test54.spec.js

@@ -0,0 +1,32 @@
+import { IVProgParser } from './../js/ast/ivprogParser';
+import { SemanticAnalyser } from './../js/processor/semantic/semanticAnalyser';
+import { LanguageService } from '../js/services/languageService';
+import { OutputTest } from '../js/util/outputTest';
+import { IVProgProcessor } from '../js/processor/ivprogProcessor';
+
+describe('A Math lib function ', function () {
+
+  const code = `programa {
+
+    funcao inicio() {
+      real a = 1.0
+      escreva(Matematica.trocar_sinal(a))
+    }
+  }`;
+
+  localStorage.setItem('ivprog.lang', 'pt');
+
+  const lexer = LanguageService.getCurrentLexer();
+  const out = new OutputTest();
+
+  it(`should be executed with no exception`, function (done) {
+    const parser = new IVProgParser(code, lexer);
+    const sem = new SemanticAnalyser(parser.parseTree());
+    const exec = new IVProgProcessor(sem.analyseTree());
+    exec.registerOutput(out);
+    exec.interpretAST().then(_ => {
+      expect(out.list).toEqual(['-1.0']);
+      done();
+    }).catch(err => done(err));
+  });
+});

+ 25 - 0
tests/test55.spec.js

@@ -0,0 +1,25 @@
+import { IVProgParser } from './../js/ast/ivprogParser';
+import { SemanticAnalyser } from './../js/processor/semantic/semanticAnalyser';
+import { LanguageService } from '../js/services/languageService';
+
+describe('Math lib with no Lib mention ', function () {
+
+  const code = `programa {
+
+    funcao inicio() {
+      real a = 1.0
+      escreva(trocar_sinal(a))
+    }
+  }`;
+
+  localStorage.setItem('ivprog.lang', 'pt');
+
+  const lexer = LanguageService.getCurrentLexer();
+
+  it(`should throw an exception`, function () {
+    const parser = new IVProgParser(code, lexer);
+    const sem = new SemanticAnalyser(parser.parseTree());
+    const fun = sem.analyseTree.bind(sem);
+    expect(fun).toThrow();
+  });
+});

+ 32 - 0
tests/test56.spec.js

@@ -0,0 +1,32 @@
+import { IVProgParser } from './../js/ast/ivprogParser';
+import { SemanticAnalyser } from './../js/processor/semantic/semanticAnalyser';
+import { LanguageService } from '../js/services/languageService';
+import { OutputTest } from '../js/util/outputTest';
+import { IVProgProcessor } from '../js/processor/ivprogProcessor';
+
+describe('Cast to int function ', function () {
+
+  const code = `programa {
+
+    funcao inicio() {
+      cadeia a = "1"
+      escreva(como_inteiro(a))
+    }
+  }`;
+
+  localStorage.setItem('ivprog.lang', 'pt');
+
+  const lexer = LanguageService.getCurrentLexer();
+  const out = new OutputTest();
+
+  it(`should be executed with no exception`, function (done) {
+    const parser = new IVProgParser(code, lexer);
+    const sem = new SemanticAnalyser(parser.parseTree());
+    const exec = new IVProgProcessor(sem.analyseTree());
+    exec.registerOutput(out);
+    exec.interpretAST().then(_ => {
+      expect(out.list).toEqual(['1']);
+      done();
+    }).catch(err => done(err));
+  });
+});

+ 32 - 0
tests/test57.spec.js

@@ -0,0 +1,32 @@
+import { IVProgParser } from './../js/ast/ivprogParser';
+import { SemanticAnalyser } from './../js/processor/semantic/semanticAnalyser';
+import { LanguageService } from '../js/services/languageService';
+import { OutputTest } from '../js/util/outputTest';
+import { IVProgProcessor } from '../js/processor/ivprogProcessor';
+
+describe('Is_Real function ', function () {
+
+  const code = `programa {
+
+    funcao inicio() {
+      cadeia a = "1.2"
+      escreva(e_real(a))
+    }
+  }`;
+
+  localStorage.setItem('ivprog.lang', 'pt');
+
+  const lexer = LanguageService.getCurrentLexer();
+  const out = new OutputTest();
+
+  it(`should return true if the string is valid real representation`, function (done) {
+    const parser = new IVProgParser(code, lexer);
+    const sem = new SemanticAnalyser(parser.parseTree());
+    const exec = new IVProgProcessor(sem.analyseTree());
+    exec.registerOutput(out);
+    exec.interpretAST().then(_ => {
+      expect(out.list).toEqual([true]);
+      done();
+    }).catch(err => done(err));
+  });
+});