Replies: 3 comments 5 replies
-
Totally! This is a great idea 👍 |
Beta Was this translation helpful? Give feedback.
-
I think we should expose our compiler as a library API (I even suspect it already is exported), so spawning a child process is not needed. So basically, I'd want something like: import { compile } from '@winglang/wing';
import * as testing from '@winglang/sdk';
const wxpath = await compile('./hello.w', { target: 'sim' });
const sim new testing.Simulator({ simfile: wxpath });
// ... |
Beta Was this translation helpful? Give feedback.
-
I've been playing around with writing unit tests in Wing that can run on sim and on the clouds. I've created a not-so-real example, in order to display how this can work. bring cloud;
let all_tests = new TestSuite("My Example");
let test_counter = new cloud.Counter() as "test_counter";
let TEST_STARTED = "tests started";
let TEST_PASS = "tests pass";
let TEST_FAIL = "tests failed";
// Runs the following inflight function before any inflight test is running in this suite
all_tests.before("collect test statistics", inflight (test: Test) => {
test_counter.inc(1, key: TEST_STARTED);
});
// Runs the following inflight function after any inflight test is running in this suite
all_tests.after("collect test statistics", inflight (test: Test) => {
if test.test_result.pass {
test_counter.inc(1, key: TEST_PASS);
}
if (tests.test_result.fail) {
test_counter.inc(1, key: TEST_FAIL);
}
});
// Runs this inflight function after all tests has finished running in this suite
all_tests.after_all("report amount of tests runned", inflight () => {
print("tests: ${counter.get(key: TEST_STARTED)}\n"
+ "\tpass: ${counter.get(key: TEST_STARTED)}\n"
+ "\tfail:${counter.get(key: TEST_FAIL)}");
});
// (If we want to support running test in preflight)
// Adding a test for checking preflight behavior that has nothing to do with inflight
all_tests.add_test("Bucket public property", (test: Test): void => {
let bucket = new Cloud.bucket(public: true);
test.assert.is_true(bucket.public);
});
// Adding a test for checking inflight behavior that has nothing to do with preflight
all_tests.add_test("num + arithmetic", inflight (test: Test): void => {
let x = 23;
let y = 19;
test.assert.equal(42, x + y);
});
/**
* Adding a suite of inflight tests that have the same preflight setup
* This code will generate multiple instance of Test
* resources, having a single Test resource for every inflight test (leaf)
* that is added to the suite, so all inflight tests are isolated and can
* run simultaneously
**/
all_tests.add_suite("bucket", (bucket_suite: TestSuite): void => {
let bucket = new cloud.Bucket();
// this is a before inflight function under this test
bucket_suite.before("clean the bucket", inflight (test: Test): void => {
for f in bucket.list() {
bucket.delete(f);
}
});
// Adding another suite inside a suite is possible
bucket_suite.add_suite("CRUD", (crud_suite: TestSuite): void => {
let file_name = "file";
crud_suite.add_test("put and get", inflight (test: Test): void => {
let expected_content = "some data";
bucket.put(file_name, expected_content);
test.assert.equal(expected_content, bucket.get(file_name));
});
crud_suite.add_test("list and delete operation", inflight (test: Test): void => {
test.assert.equal(0, bucket.list().length);
bucket.put(file_name, "some content");
test.assert.equal(1, bucket.list().length);
bucket.delete(file_name);
test.assert.equal(0, bucket.list().length);
});
});
bucket_suite.add_suite("events", (event_suite: TestSuite): void => {
let counter = new cloud.Counter();
let uploaded_file_name = "uploaded";
bucket.on_upload(inflight (key: str): void => {
counter.inc();
// TestSuite.current_test is an inflight API of TestSuite resource
event_suite.current_test.assert.equals(uploaded_file_name, key);
});
events_suite.add_test("upload a file", inflight (test: Test): void => {
bucket.upload(uploaded_file_name);
});
});
});
|
Beta Was this translation helpful? Give feedback.
-
Wanted to start a discussion about the option to test the compiler using the sim api.
Here is a small example of how one test can look like (using jest)
code.w
basic.test.ts
Beta Was this translation helpful? Give feedback.
All reactions