@lcap/nasl-parser
Version:
Take Nasl text to Nasl AST with the help of generalized parsing.
75 lines (62 loc) • 1.75 kB
text/typescript
import { Parser, Grammar } from "nearley";
import grammar from "../../ts/nasl";
const testBoolean= `
logic testBoolean() {
let bool : Bool
let nl = null
let b = true
let b = false
}
`
const testBinary= `
logic testBinary() {
let nl = null * 3
let a = 2 + 3 * 5
let b = 5 / 5
let d = 10 % 2
}
`
const testComparison= `
logic testComparison() {
let a = 11 > 2
let b = f() < g()
let c = f(true) <= g(false)
let d = f(true) >= g(false)
let e = f(true) != g(false)
let f = f(true) == g(false)
}
`
const testBinaryMoreStr =`
logic testArith() => result {
let variable1
let variable2
let variable3
let variable4
variable1 = (1 + 2) + 3
variable2 = 1 - (2 - 3)
variable3 = variable1 * variable2
variable4 = variable3 / (variable3 % variable3)
result = variable4
}`
describe('Test Logical Operations', () => {
test('Logic Operation: booleans', async () => {
const parser = new Parser(Grammar.fromCompiled(grammar));
parser.feed(testBoolean);
expect(parser.results.length).toBe(1);
});
test('Logic Operation: arith', async () => {
const parser = new Parser(Grammar.fromCompiled(grammar));
parser.feed(testBinary);
expect(parser.results.length).toBe(1);
});
test('Logic Operation: comparison', async () => {
const parser = new Parser(Grammar.fromCompiled(grammar));
parser.feed(testComparison);
expect(parser.results.length).toBe(1);
});
test('Logic Operation: more binary', async () => {
const parser = new Parser(Grammar.fromCompiled(grammar));
parser.feed(testBinaryMoreStr);
expect(parser.results.length).toBe(1);
});
});