Skip to content

Commit

Permalink
test: add and delete .fpkg files
Browse files Browse the repository at this point in the history
  • Loading branch information
sockmaster27 committed Apr 16, 2024
1 parent 8fdcb60 commit 95f4b7a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
lib
**/lib/cache
**/lib/github
out
node_modules
client/server
Expand Down
45 changes: 34 additions & 11 deletions test/src/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,60 @@

import * as assert from 'assert'
import * as vscode from 'vscode'
import { activate, addFile, deleteFile, getTestDocUri, sleep } from './util'
import { activate, addFile, deleteFile, getTestDocUri } from './util'

suite('Source file manipulation', () => {
suite('File manipulation', () => {
const doc1Uri = getTestDocUri('src/Main.flix')

const doc2Uri = getTestDocUri('src/Area.flix')
let doc2Content: string
let doc2Content: Uint8Array

const jarUri = getTestDocUri('lib/external/SquareArea.jar')
let jarContent: Uint8Array

const fpkgUri = getTestDocUri('lib/circleArea.fpkg')
let fpkgContent: Uint8Array

suiteSetup(async () => {
await activate()
doc2Content = (await vscode.workspace.fs.readFile(doc2Uri)).toString()
doc2Content = await vscode.workspace.fs.readFile(doc2Uri)
jarContent = await vscode.workspace.fs.readFile(jarUri)
fpkgContent = await vscode.workspace.fs.readFile(fpkgUri)
})
teardown(async () => {
// Restore the original content of the file after each test
await addFile(doc2Uri, doc2Content)
await addFile(jarUri, jarContent)
await addFile(fpkgUri, fpkgContent)
})

async function docIsAdded() {
// If Area.flix is not present in the compiler, then Main.flix will contain a resolution error on the call to area()
const r = vscode.languages.getDiagnostics(doc1Uri)
async function workspaceValid() {
// If all files are not present in the compiler, then Main.flix will contain a resolution error
const r = [...vscode.languages.getDiagnostics(doc1Uri), ...vscode.languages.getDiagnostics(doc2Uri)]
return r.length === 0
}

test('Deleted file should be removed', async () => {
test('Deleted source-file should be removed', async () => {
await deleteFile(doc2Uri)
assert.strictEqual(await docIsAdded(), false)
assert.strictEqual(await workspaceValid(), false)
})

test('Created file should be added', async () => {
test('Created source-file should be added', async () => {
await deleteFile(doc2Uri)
await addFile(doc2Uri, doc2Content)
assert.strictEqual(await docIsAdded(), true)
assert.strictEqual(await workspaceValid(), true)
})

test('Deleted fpkg-file should be removed', async () => {
await deleteFile(fpkgUri)
assert.strictEqual(await workspaceValid(), false)
})

test('Created fpkg-file should be added', async () => {
await deleteFile(fpkgUri)
await addFile(fpkgUri, fpkgContent)
assert.strictEqual(await workspaceValid(), true)
})

// TODO: Test for jar-file. This file is locked by the process, so it cannot be deleted and added again.
})
4 changes: 2 additions & 2 deletions test/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async function processFileChange() {
/**
* Add a file with the given `uri` and `content`, and wait for the compiler to process this.
*/
export async function addFile(uri: vscode.Uri, content: string) {
export async function addFile(uri: vscode.Uri, content: string | Uint8Array) {
await vscode.workspace.fs.writeFile(uri, Buffer.from(content))
await processFileChange()
}
Expand All @@ -90,6 +90,6 @@ export async function copyFile(from: vscode.Uri, to: vscode.Uri) {
* Throws if the file does not exist.
*/
export async function deleteFile(uri: vscode.Uri) {
await vscode.workspace.fs.delete(uri)
await vscode.workspace.fs.delete(uri, { useTrash: true })
await processFileChange()
}
Binary file added test/testWorkspace/lib/circleArea.fpkg
Binary file not shown.
Binary file added test/testWorkspace/lib/external/SquareArea.jar
Binary file not shown.
11 changes: 7 additions & 4 deletions test/testWorkspace/src/Area.flix
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

/// Computes the area of the given shape using
/// pattern matching and basic arithmetic.
def area(s: Shape): Int32 = match s {
case Shape.Circle(r) => 3 * (r * r)
case Shape.Square(w) => w * w
case Shape.Rectangle(h, w) => h * w
def area(s: Shape): Int32 = {
import static dev.flix.SquareArea.squareArea(Int32): Int32 \ {};
match s {
case Shape.Circle(r) => circleArea(r)
case Shape.Square(w) => squareArea(w)
case Shape.Rectangle(h, w) => h * w
}
}

0 comments on commit 95f4b7a

Please sign in to comment.