Skip to content

Commit

Permalink
Add snapshot tests to formatter (#545)
Browse files Browse the repository at this point in the history
* Add snapshot tests for formatter
* Add test runner to CI

---------

Co-authored-by: David Kincaid <[email protected]>
  • Loading branch information
sandygk and DaelonSuzuka authored Dec 22, 2023
1 parent ec1d9c3 commit 0a794eb
Show file tree
Hide file tree
Showing 43 changed files with 749 additions and 145 deletions.
33 changes: 30 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ name: Continuous integration
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-20.04
test:
name: Test
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -13,9 +17,32 @@ jobs:
with:
node-version: 16.x

- name: Install dependencies
run: npm install

- name: Run headless test
uses: coactions/setup-xvfb@v1
with:
run: |
npm run compile
npm test
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Node.js
uses: actions/[email protected]
with:
node-version: 16.x

- name: Install dependencies
run: npm install

- name: Lint and build extension
run: |
npm install
npm run lint
npm run package -- --out godot-tools.vsix
ls -l godot-tools.vsix
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Godot 3.2 or later.
- `ctrl+click` on any symbol to jump to its definition or **open its documentation**
- `ctrl+click` on `res://resource/path` links
- **hover previews on `res://resource/path` links**
- **builtin code formatter**
- autocompletions
- full typed GDScript support
- optional "Smart Mode" to improve productivity with dynamically typed scripts
Expand Down
95 changes: 95 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@
}
},
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/marked": "^4.0.8",
"@types/mocha": "^10.0.6",
"@types/node": "^18.15.0",
Expand All @@ -814,6 +815,7 @@
"@vscode/test-cli": "^0.0.4",
"@vscode/test-electron": "^2.3.8",
"@vscode/vsce": "^2.21.0",
"chai": "^4.3.10",
"esbuild": "^0.17.15",
"eslint": "^8.37.0",
"mocha": "^10.2.0",
Expand Down
36 changes: 30 additions & 6 deletions src/formatter/formatter.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
import * as assert from "assert";
import * as vscode from "vscode";
import * as path from "path";
import * as fs from "fs";

import { format_document } from "./textmate";

import * as chai from "chai";
const expect = chai.expect;

const dots = ["..", "..", ".."];
const basePath = path.join(__filename, ...dots);

suite("GDScript Formatter Tests", () => {
test("Test Formatting", async () => {
const uri = vscode.Uri.file(path.join(basePath, "src/formatter/tests/test1.in.gd"));
const document = await vscode.workspace.openTextDocument(uri);
const edits = format_document(document);
assert.strictEqual(4, edits.length);
// Search for all folders in the snapshots folder and run a test for each
// comparing the output of the formatter with the expected output.
// To add a new test, create a new folder in the snapshots folder
// and add two files, `in.gd` and `out.gd` for the input and expected output.
const snapshotsFolderPath = path.join(basePath, "src/formatter/snapshots");
const testFolders = fs.readdirSync(snapshotsFolderPath);

testFolders.forEach((testFolder) => {
const testFolderPath = path.join(snapshotsFolderPath, testFolder);
if (fs.statSync(testFolderPath).isDirectory()) {
test(`Snapshot Test: ${testFolder}`, async () => {
const uriIn = vscode.Uri.file(path.join(testFolderPath, "in.gd"));
const uriOut = vscode.Uri.file(path.join(testFolderPath, "out.gd"));
const documentIn = await vscode.workspace.openTextDocument(uriIn);
const documentOut = await vscode.workspace.openTextDocument(uriOut);
const edits = format_document(documentIn);

// Apply the formatting edits
const workspaceEdit = new vscode.WorkspaceEdit();
workspaceEdit.set(uriIn, edits);
await vscode.workspace.applyEdit(workspaceEdit);

// Compare the result with the expected output
expect(documentIn.getText()).to.equal(documentOut.getText());
});
}
});
});
35 changes: 35 additions & 0 deletions src/formatter/snapshots/arithmetic/in.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var a = 1
var b = 2

func f():
a = a+b
a = .1+ 2
a= 1. +2
a =1.0 + .2
a = 1.0+ 2.

a =a -b
a = .1- 2
a= 1.-2
a = 1.0 - .2
a =1.0- 2.

a= a/ b
a = .1 /2
a =1. /2
a = 1.0 / .2
a =1.0/2.

a = a *b
a=.1* 2
a = 1. *2
a= 1.0* .2
a =1.0 * 2.

a= a% b
a =1%2

a =-1
a= +1

a = ((-1 + 2) * (3-4) / 5 * 6%( -7 + 8-9 - 10)) * (- 11 + 12) / (13*14 % 15 +16)
35 changes: 35 additions & 0 deletions src/formatter/snapshots/arithmetic/out.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var a = 1
var b = 2

func f():
a = a + b
a = .1 + 2
a = 1. + 2
a = 1.0 + .2
a = 1.0 + 2.

a = a - b
a = .1 - 2
a = 1. - 2
a = 1.0 - .2
a = 1.0 - 2.

a = a / b
a = .1 / 2
a = 1. / 2
a = 1.0 / .2
a = 1.0 / 2.

a = a * b
a = .1 * 2
a = 1. * 2
a = 1.0 * .2
a = 1.0 * 2.

a = a % b
a = 1 % 2

a = -1
a = +1

a = ((-1 + 2) * (3 - 4) / 5 * 6 % (-7 + 8 - 9 - 10)) * (-11 + 12) / (13 * 14 % 15 + 16)
10 changes: 10 additions & 0 deletions src/formatter/snapshots/arrays/in.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var primes = [2, 3, 5, 7, 11, 13, 17 ]
var primes2 = [
2 ,
3 ,
5,
7 ,
11 ,
13,
17
]
10 changes: 10 additions & 0 deletions src/formatter/snapshots/arrays/out.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var primes = [2, 3, 5, 7, 11, 13, 17]
var primes2 = [
2,
3,
5,
7,
11,
13,
17
]
2 changes: 2 additions & 0 deletions src/formatter/snapshots/assert/in.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
func f():
assert (1!= 2)
2 changes: 2 additions & 0 deletions src/formatter/snapshots/assert/out.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
func f():
assert(1 != 2)
2 changes: 2 additions & 0 deletions src/formatter/snapshots/await/in.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
func f():
await delay(10)
2 changes: 2 additions & 0 deletions src/formatter/snapshots/await/out.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
func f():
await delay(10)
Loading

0 comments on commit 0a794eb

Please sign in to comment.