Browse Source

Test cases for the language processor

Lucas de Souza 6 years ago
parent
commit
58d5810bdc
5 changed files with 118 additions and 6 deletions
  1. 9 6
      js/processor/ivprogProcessor.js
  2. 26 0
      tests/test19.spec.js
  3. 26 0
      tests/test20.spec.js
  4. 31 0
      tests/test21.spec.js
  5. 26 0
      tests/test22.spec.js

+ 9 - 6
js/processor/ivprogProcessor.js

@@ -125,12 +125,15 @@ export class IVProgProcessor {
           case 2: {
             if (vl.lines > 0 && vl.columns > 0
               && vl.subtype === v.type) {
-                if(v.byRef) {
-                  const ref = new StoreObjectRef(vl.id, callerStore);
-                  calleeStore.insertStore(v.id, ref);
-                } else {
-                  calleeStore.insertStore(v.id, vl);
-                }
+              if(v.byRef && !vl.inStore) {
+                throw new Error('You must inform a variable as parameter');
+              }
+              if(v.byRef) {
+                const ref = new StoreObjectRef(vl.id, callerStore);
+                calleeStore.insertStore(v.id, ref);
+              } else {
+                calleeStore.insertStore(v.id, vl);
+              }
             } else {
               // TODO: Better error message
               throw new Error(`Parameter ${v.id} is not compatible with the value given.`);

+ 26 - 0
tests/test19.spec.js

@@ -0,0 +1,26 @@
+import Lexers from './../grammar/';
+import {Types} from './../js/ast/types';
+import { IVProgParser } from './../js/ast/ivprogParser';
+import { IVProgProcessor} from './../js/processor/ivprogProcessor'
+
+describe('Multi(*) operation', function (done) {
+
+  let input = `programa {
+
+    funcao inicio() {
+      inteiro a
+      a = -2 + 2 * 4
+    }
+  }`;
+
+  const lexer = Lexers['pt_br'];
+
+  it(`should have higher priority than Sum(+)`, function () {
+    const parser = new IVProgParser(input, lexer);
+    const exec = new IVProgProcessor(parser.parseTree());
+    exec.interpretAST().then(sto => {
+      expect(sto.applyStore('a').value).toEqual(6);
+      done();
+    });
+  });
+});

+ 26 - 0
tests/test20.spec.js

@@ -0,0 +1,26 @@
+import Lexers from './../grammar/';
+import {Types} from './../js/ast/types';
+import { IVProgParser } from './../js/ast/ivprogParser';
+import { IVProgProcessor} from './../js/processor/ivprogProcessor'
+
+describe('An Array initialization with expressions', function (done) {
+
+  const input = `programa {
+
+    funcao inicio() {
+      inteiro a[2] = {2+2,3*5}
+    }
+  }`;
+
+  const lexer = Lexers['pt_br'];
+  const result = [4,15];
+
+  it(`should produce a valid state`, function () {
+    const parser = new IVProgParser(input, lexer);
+    const exec = new IVProgProcessor(parser.parseTree());
+    exec.interpretAST().then(sto => {
+      expect([sto.applyStore('a').value[0].value,sto.applyStore('a').value[1].value]).toEqual(result);
+      done();
+    });
+  });
+});

+ 31 - 0
tests/test21.spec.js

@@ -0,0 +1,31 @@
+import Lexers from './../grammar/';
+import {Types} from './../js/ast/types';
+import { IVProgParser } from './../js/ast/ivprogParser';
+import { IVProgProcessor} from './../js/processor/ivprogProcessor'
+
+describe('A call to a function that returns a valid type', function (done) {
+
+  const input = `programa {
+
+    funcao inicio() {
+      inteiro a = soma(1, 1)
+    }
+
+    funcao inteiro soma(inteiro a, inteiro b)
+    {
+      retorne a + b
+    }
+  }`;
+
+  const lexer = Lexers['pt_br'];
+  const result = 2;
+
+  it(`should produce a valid state`, function () {
+    const parser = new IVProgParser(input, lexer);
+    const exec = new IVProgProcessor(parser.parseTree());
+    exec.interpretAST().then(sto => {
+      expect(sto.applyStore('a').value).toEqual(result);
+      done();
+    });
+  });
+});

+ 26 - 0
tests/test22.spec.js

@@ -0,0 +1,26 @@
+import Lexers from './../grammar/';
+import {Types} from './../js/ast/types';
+import { IVProgParser } from './../js/ast/ivprogParser';
+import { IVProgProcessor} from './../js/processor/ivprogProcessor'
+
+describe('An assignment to a variable', function (done) {
+
+  const input = `programa {
+
+    funcao inicio() {
+      inteiro a
+      a = 5.5
+    }
+  }`;
+
+  const lexer = Lexers['pt_br'];
+  const result = 2;
+
+  it(`should respect the variable type`, function () {
+    const parser = new IVProgParser(input, lexer);
+    const exec = new IVProgProcessor(parser.parseTree());
+    exec.interpretAST().then(sto => {
+      done(new Error('Should not have resolved'));
+    }).catch( _ => done());
+  });
+});