-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { KipperCompileResult } from "@kipper/core"; | ||
import { KipperCompiler } from "@kipper/core"; | ||
Check warning on line 2 in test/module/core/core-functionality/arrays.test.ts
|
||
import { assert } from "chai"; | ||
import * as ts from "typescript"; | ||
import { KipperTypeScriptTarget } from "@kipper/target-ts"; | ||
Check warning on line 5 in test/module/core/core-functionality/arrays.test.ts
|
||
import { jsConfig } from "../errors"; | ||
Check warning on line 6 in test/module/core/core-functionality/arrays.test.ts
|
||
import { ScriptTarget } from "typescript"; | ||
import { compiler, defaultTarget } from "."; | ||
import { testPrintOutput } from ".."; | ||
|
||
describe("Arrays", () => { | ||
it("Simple array declaration", async () => { | ||
const fileContent = `var x: Array<num> = [1, 2, 3];`; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.deepEqual(instance.programCtx?.errors, [], "Expected no compilation errors"); | ||
|
||
const code = instance.write(); | ||
assert.include( | ||
code, | ||
"let x: Array<number> = __kipper.assignTypeMeta([1, 2, 3],__kipper.builtIn.Array.changeGenericTypeArguments({T: __kipper.builtIn.num}));", | ||
"Invalid TypeScript code (Expected different output)", | ||
); | ||
}); | ||
|
||
it("Assign array to array", async () => { | ||
const code = "var x: Array<num> = [1, 2, 3]; var y: Array<num> = x;"; | ||
const instance: KipperCompileResult = await compiler.compile(code, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.deepEqual(instance.programCtx?.errors, [], "Expected no compilation errors"); | ||
|
||
const jsCode = instance.write(); | ||
assert.include( | ||
jsCode, | ||
`let x: Array<number> = __kipper.assignTypeMeta([1, 2, 3],__kipper.builtIn.Array.changeGenericTypeArguments({T: __kipper.builtIn.num}));\nlet y: Array<number> = x;`, | ||
); | ||
}); | ||
|
||
it("Accessing array element", async () => { | ||
const code = "var x: Array<num> = [1, 2, 3]; print(x[1] as str);"; | ||
const instance: KipperCompileResult = await compiler.compile(code, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.deepEqual(instance.programCtx?.errors, [], "Expected no compilation errors"); | ||
|
||
const jsCode = ts.transpile(instance.write(), { target: ScriptTarget.ES2015 }); | ||
testPrintOutput((message: any) => assert.equal(message, "2", "Expected different output"), jsCode); | ||
}); | ||
|
||
it("Assigning one array element to another", async () => { | ||
const code = "var x: Array<num> = [1, 2, 3]; var y: num = x[1]; print(y as str);"; | ||
const instance: KipperCompileResult = await compiler.compile(code, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.deepEqual(instance.programCtx?.errors, [], "Expected no compilation errors"); | ||
|
||
const jsCode = ts.transpile(instance.write(), { target: ScriptTarget.ES2015 }); | ||
testPrintOutput((message: any) => assert.equal(message, "2", "Expected different output"), jsCode); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import type { KipperCompileResult } from "@kipper/core"; | ||
import { KipperCompiler } from "@kipper/core"; | ||
Check warning on line 2 in test/module/core/core-functionality/assignment.test.ts
|
||
import { assert } from "chai"; | ||
import * as ts from "typescript"; | ||
import { KipperTypeScriptTarget } from "@kipper/target-ts"; | ||
Check warning on line 5 in test/module/core/core-functionality/assignment.test.ts
|
||
import { jsConfig } from "../errors"; | ||
Check warning on line 6 in test/module/core/core-functionality/assignment.test.ts
|
||
import { ScriptTarget } from "typescript"; | ||
import { compiler, defaultTarget } from "."; | ||
import { testPrintOutput } from ".."; | ||
|
||
describe("Assignment", () => { | ||
it("to 'num'", async () => { | ||
const fileContent = "var x: num = 4;\nx = 5;"; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.deepEqual(instance.programCtx?.errors, [], "Expected no compilation errors"); | ||
assert( | ||
instance.write().includes("let x: number = 4;\nx = 5;"), | ||
"Invalid TypeScript code (Expected different output)", | ||
); | ||
}); | ||
|
||
it("to 'str'", async () => { | ||
const fileContent = 'var x: str = "4";\nx = "5";'; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.deepEqual(instance.programCtx?.errors, [], "Expected no compilation errors"); | ||
assert( | ||
instance.write().includes('let x: string = "4";\nx = "5";'), | ||
"Invalid TypeScript code (Expected different output)", | ||
); | ||
}); | ||
|
||
it("to 'bool'", async () => { | ||
const fileContent = "var x: bool = true;\nx = false;"; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.deepEqual(instance.programCtx?.errors, [], "Expected no compilation errors"); | ||
assert( | ||
instance.write().includes("let x: boolean = true;\nx = false;"), | ||
"Invalid TypeScript code (Expected different output)", | ||
); | ||
}); | ||
|
||
it("to 'array'", async () => { | ||
const fileContent = "var x: Array<num> = [1, 2, 3];\nx = [4, 5, 6];"; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.deepEqual(instance.programCtx?.errors, [], "Expected no compilation errors"); | ||
assert( | ||
instance | ||
.write() | ||
.includes( | ||
"let x: Array<number> = __kipper.assignTypeMeta([1, 2, 3],__kipper.builtIn.Array.changeGenericTypeArguments({T: __kipper.builtIn.num}));\nx = __kipper.assignTypeMeta([4, 5, 6],__kipper.builtIn.Array.changeGenericTypeArguments({T: __kipper.builtIn.num}));", | ||
), | ||
"Invalid TypeScript code (Expected different output)", | ||
); | ||
}); | ||
|
||
it("to 'func'", async () => { | ||
const fileContent = "var x: Func<str, void> = print;\nx = print;"; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.deepEqual(instance.programCtx?.errors, [], "Expected no compilation errors"); | ||
assert( | ||
instance.write().includes("let x: (arg0: string) => void = __kipper.print;\nx = __kipper.print;"), | ||
"Invalid TypeScript code (Expected different output)", | ||
); | ||
}); | ||
|
||
it("to 'array[]'", async () => { | ||
const fileContent = "var x: Array<num> = [1, 2, 3];\nx[0] = 4;\nprint(x[0]);"; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.deepEqual(instance.programCtx?.errors, [], "Expected no compilation errors"); | ||
assert( | ||
instance | ||
.write() | ||
.includes( | ||
"let x: Array<number> = __kipper.assignTypeMeta([1, 2, 3],__kipper.builtIn.Array.changeGenericTypeArguments({T: __kipper.builtIn.num}));\nx[0] = 4;", | ||
), | ||
"Invalid TypeScript code (Expected different output)", | ||
); | ||
|
||
const jsCode = ts.transpile(instance.write(), { target: ScriptTarget.ES2015 }); | ||
testPrintOutput((message: any) => assert.equal(message, "4", "Expected different output"), jsCode); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import type { KipperCompileResult } from "@kipper/core"; | ||
import { KipperCompiler } from "@kipper/core"; | ||
Check warning on line 2 in test/module/core/core-functionality/class.test.ts
|
||
import { assert } from "chai"; | ||
import * as ts from "typescript"; | ||
import { KipperTypeScriptTarget } from "@kipper/target-ts"; | ||
Check warning on line 5 in test/module/core/core-functionality/class.test.ts
|
||
import { jsConfig } from "../errors"; | ||
Check warning on line 6 in test/module/core/core-functionality/class.test.ts
|
||
import { ScriptTarget } from "typescript"; | ||
import { compiler, defaultTarget } from "."; | ||
import { testPrintOutput } from ".."; | ||
|
||
describe("Class", () => { | ||
it("should be able to create an empty class", async () => { | ||
const fileContent = "class Test { }"; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.equal(instance.programCtx!!.errors.length, 0, "Expected no compilation errors"); | ||
let written = instance.write(); | ||
assert.include(written, "class Test {\n}", "Invalid TypeScript code (Expected different output)"); | ||
}); | ||
|
||
it("should be able to create class with constructor", async () => { | ||
const fileContent = "class Test {constructor (a:num, b:str) {};};"; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.equal(instance.programCtx!!.errors.length, 0, "Expected no compilation errors"); | ||
let written = instance.write(); | ||
assert.include( | ||
written, | ||
"class Test {\n constructor(a: number, b: string)\n {\n }\n}", | ||
"Invalid TypeScript code (Expected different output)", | ||
); | ||
}); | ||
|
||
it("should be able to create class with members", async () => { | ||
const fileContent = `class Test {\n x: num;\n y: str;\ngreet(): void {\nprint("Kippa");\n};\n constructor(a: num, b: str)\n{\n};}`; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.equal(instance.programCtx!!.errors.length, 0, "Expected no compilation errors"); | ||
|
||
const written = instance.write(); | ||
assert.include( | ||
written, | ||
"class Test {\n" + | ||
" x: number;\n" + | ||
" y: string;\n" + | ||
" greet(): void\n" + | ||
" {\n" + | ||
' __kipper.print("Kippa");\n' + | ||
" }\n" + | ||
" constructor(a: number, b: string)\n" + | ||
" {\n" + | ||
" }\n" + | ||
"}", | ||
); | ||
}); | ||
|
||
it("should be able to instantiate a class with new", async () => { | ||
const fileContent = `class Test {a: str; constructor (b: str) {this.a = b;}}; var x: Test = new Test("3"); print(x.a);`; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.equal(instance.programCtx!!.errors.length, 0, "Expected no compilation errors"); | ||
|
||
const written = instance.write(); | ||
assert.include( | ||
written, | ||
"class Test {\n" + | ||
" a: string;\n" + | ||
" constructor(b: string)\n" + | ||
" {\n" + | ||
" this.a = b;\n" + | ||
" }\n" + | ||
"}\n" + | ||
'let x: Test = new Test("3");\n' + | ||
"__kipper.print(x.a);", | ||
"Invalid TypeScript code (Expected different output)", | ||
); | ||
|
||
const jsCode = ts.transpile(written); | ||
testPrintOutput((message: any) => assert.equal(message, "3", "Expected different output"), jsCode); | ||
}); | ||
|
||
it("should be able to instantiate a class with new and two properties", async () => { | ||
const fileContent = `class Test {x: str; y: num; constructor (a: str, b: num) {this.x = a; this.y = b;}}; var x: Test = new Test("hello", 42); print(x.x);`; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.equal(instance.programCtx!!.errors.length, 0, "Expected no compilation errors"); | ||
|
||
const written = instance.write(); | ||
assert.include( | ||
written, | ||
"class Test {\n" + | ||
" x: string;\n" + | ||
" y: number;\n" + | ||
" constructor(a: string, b: number)\n" + | ||
" {\n" + | ||
" this.x = a;\n" + | ||
" this.y = b;\n" + | ||
" }\n" + | ||
"}\n" + | ||
'let x: Test = new Test("hello", 42);\n' + | ||
"__kipper.print(x.x);", | ||
"Invalid TypeScript code (Expected different output)", | ||
); | ||
|
||
const jsCode = ts.transpile(written); | ||
testPrintOutput((message: any) => assert.equal(message, "hello", "Expected different output"), jsCode); | ||
}); | ||
|
||
it("should be able to access 'this' inside a class method", async () => { | ||
const fileContent = `class Test {x: str; constructor (a: str) {this.x = a;} greet(): void {print(this.x);}}; var x: Test = new Test("hello"); x.greet();`; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.equal(instance.programCtx!!.errors.length, 0, "Expected no compilation errors"); | ||
let written = instance.write(); | ||
assert.include( | ||
written, | ||
"class Test {\n" + | ||
" x: string;\n" + | ||
" greet(): void\n" + | ||
" {\n" + | ||
" __kipper.print(this.x);\n" + | ||
" }\n" + | ||
" constructor(a: string)\n" + | ||
" {\n" + | ||
" this.x = a;\n" + | ||
" }\n" + | ||
"}\n" + | ||
'let x: Test = new Test("hello");\n' + | ||
"x.greet();", | ||
"Invalid TypeScript code (Expected different output)", | ||
); | ||
|
||
const jsCode = ts.transpile(written); | ||
testPrintOutput((message: any) => assert.equal(message, "hello", "Expected different output"), jsCode); | ||
}); | ||
|
||
it("should be able to return a value inside a class method", async () => { | ||
const fileContent = `class Test {x: str; constructor (a: str) {this.x = a;} greet(): str {return this.x;}}; var x: Test = new Test("hello"); print(x.greet());`; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.equal(instance.programCtx!!.errors.length, 0, "Expected no compilation errors"); | ||
let written = instance.write(); | ||
assert.include( | ||
written, | ||
"class Test {\n" + | ||
" x: string;\n" + | ||
" greet(): string\n" + | ||
" {\n" + | ||
" return this.x;\n" + | ||
" }\n" + | ||
" constructor(a: string)\n" + | ||
" {\n" + | ||
" this.x = a;\n" + | ||
" }\n" + | ||
"}\n" + | ||
'let x: Test = new Test("hello");\n' + | ||
"__kipper.print(x.greet());", | ||
"Invalid TypeScript code (Expected different output)", | ||
); | ||
|
||
const jsCode = ts.transpile(written, { target: ScriptTarget.ES2015 }); | ||
testPrintOutput((message: any) => assert.equal(message, "hello", "Expected different output"), jsCode); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { KipperCompileResult } from "@kipper/core"; | ||
import { KipperCompiler } from "@kipper/core"; | ||
Check warning on line 2 in test/module/core/core-functionality/comment.test.ts
|
||
import { assert } from "chai"; | ||
import * as ts from "typescript"; | ||
import { KipperTypeScriptTarget } from "@kipper/target-ts"; | ||
import { jsConfig } from "../errors"; | ||
import { ScriptTarget } from "typescript"; | ||
import { compiler, defaultTarget } from "."; | ||
import { testPrintOutput } from ".."; | ||
|
||
describe("Comment", () => { | ||
it("Single line", async () => { | ||
const fileContent = 'var x: num = 5;\n// A comment\n print("");'; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.deepEqual(instance.programCtx?.errors, [], "Expected no compilation errors"); | ||
assert.include(instance.write(), "let x: number = 5;", "Expected variable declaration to be present in output"); | ||
assert.include(instance.write(), '__kipper.print("");', "Expected print call to be present in output"); | ||
}); | ||
|
||
it("Multi line", async () => { | ||
const fileContent = 'var x: num = 5;\n/* A comment\n ... \n end of comment */\n print("");'; | ||
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget }); | ||
|
||
assert.isDefined(instance.programCtx); | ||
assert.deepEqual(instance.programCtx?.errors, [], "Expected no compilation errors"); | ||
assert.include(instance.write(), "let x: number = 5;", "Expected variable declaration to be present in output"); | ||
assert.include(instance.write(), '__kipper.print("");', "Expected print call to be present in output"); | ||
}); | ||
}); |