Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): add Assert.throws #4461

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions examples/tests/sdk_tests/bucket/delete.test.w
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
bring cloud;
bring "./../../valid/assertions.w" as assertions;

let b = new cloud.Bucket();
b.addObject("file2.txt", "Bar");

test "delete" {
let assertThrows = (expected: str, block: (): void) => {
let var error = false;
try {
block();
} catch actual {
assert(actual == expected);
error = true;
}
assert(error);
};

let OBJECT_DOES_NOT_EXIST_ERROR = "Object does not exist (key=file1.json).";
let jsonObj1 = Json { key1: "value1" };

Expand All @@ -26,7 +16,7 @@ test "delete" {

b.delete("file1.json", mustExist: true);

assertThrows(OBJECT_DOES_NOT_EXIST_ERROR, () => {
assertions.Assert.throws(OBJECT_DOES_NOT_EXIST_ERROR, () => {
b.delete("file1.json", mustExist: true);
});
assert(b.exists("file2.txt"));
Expand Down
14 changes: 2 additions & 12 deletions examples/tests/sdk_tests/bucket/public_url.test.w
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
bring cloud;
bring http;
bring util;
bring "./../../valid/assertions.w" as assertions;

let publicBucket = new cloud.Bucket(public: true) as "publicBucket";
let privateBucket = new cloud.Bucket() as "privateBucket";

test "publicUrl" {
let assertThrows = (expected: str, block: (): void) => {
let var error = false;
try {
block();
} catch actual {
assert(actual == expected);
error = true;
}
assert(error);
};

let BUCKET_NOT_PUBLIC_ERROR = "Cannot provide public url for a non-public bucket";

publicBucket.put("file1.txt", "Foo");
Expand All @@ -30,7 +20,7 @@ test "publicUrl" {
assert(http.get(publicUrl).body == "Foo");
}

assertThrows(BUCKET_NOT_PUBLIC_ERROR, () => {
assertions.Assert.throws(BUCKET_NOT_PUBLIC_ERROR, () => {
privateBucket.publicUrl("file2.txt");
});
}
11 changes: 4 additions & 7 deletions examples/tests/sdk_tests/bucket/signed_url.test.w
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
bring cloud;
bring http;
bring util;
bring "./../../valid/assertions.w" as assertions;

let testBucket = new cloud.Bucket(public: true) as "testBucket";

Expand All @@ -17,13 +18,9 @@ test "signedUrl" {
test "signedUrl for non existent key" {
let var error = "";

if (util.env("WING_TARGET") != "sim") {
try{
if (util.env("WING_TARGET") != "sim") {
assertions.Assert.throws("Cannot provide signed url for a non-existent key (key=file.txt)", () =>{
let signedUrl = testBucket.signedUrl("file.txt");
} catch e {
error = e;
}

assert(error == "Cannot provide signed url for a non-existent key (key=file.txt)");
});
}
}
27 changes: 3 additions & 24 deletions examples/tests/sdk_tests/http/url.test.w
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
bring http;
bring "./../../valid/assertions.w" as assertions;

test "parseUrl()" {
let assertThrows = (expected: str, block: (): void) => {
let var error = false;
try {
block();
} catch actual {
assert(actual == expected);
error = true;
}
assert(error);
};

let INVALID_URL_STRING = "hello world";
let INVALID_URL_ERROR = "Invalid URL: ${INVALID_URL_STRING}";
let URL_STRING = "http://username:[email protected]:3000/pathname?search=test#hash";
Expand All @@ -33,23 +23,12 @@ test "parseUrl()" {
let parsedUrlStruct = http.parseUrl(URL_STRING);
assert(urlStruct == parsedUrlStruct);

assertThrows(INVALID_URL_ERROR, () => {
assertions.Assert.throws(INVALID_URL_ERROR, () => {
let invalidUrlStruct = http.parseUrl(INVALID_URL_STRING);
});
}

test "formatUrl()" {
let assertThrows = (expected: str, block: (): void) => {
let var error = false;
try {
block();
} catch actual {
assert(actual == expected);
error = true;
}
assert(error);
};

let UNABLE_TO_FORMAT_URL_STRUCT_ERROR = "Unable to format URL Struct: Invalid URL";
let urlStruct = http.parseUrl("https://a:b@測試.com/path?query=1#fragment");

Expand Down Expand Up @@ -80,7 +59,7 @@ test "formatUrl()" {
password: urlStruct.password,
};

assertThrows(UNABLE_TO_FORMAT_URL_STRUCT_ERROR, () => {
assertions.Assert.throws(UNABLE_TO_FORMAT_URL_STRUCT_ERROR, () => {
http.formatUrl(invalidUrlStruct);
});
}
26 changes: 10 additions & 16 deletions examples/tests/sdk_tests/math/acos.test.w
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
bring math;
try {
bring "./../../valid/assertions.w" as assertions;

assertions.PreflightAssert.throws("Input value must be between -1 and 1, inclusive.", () => {
log("${math.acos(-2)}");
} catch e {
assert(e == "Input value must be between -1 and 1, inclusive.");
}
});
assert(math.acos(-1) == math.PI);
assert(math.acos(-0) == 1.5707963267948966);
assert(math.acos(0) == 1.5707963267948966);
assert(math.acos(0.5) == 1.0471975511965979);
assert(math.acos(1) == 0);
try {
assertions.PreflightAssert.throws("Input value must be between -1 and 1, inclusive.", () => {
log("${math.acos(2)}");
} catch e {
assert(e == "Input value must be between -1 and 1, inclusive.");
}
});

test "inflight arc cosine" {
try {
assertions.Assert.throws("Input value must be between -1 and 1, inclusive.", () => {
log("${math.acos(-2)}");
} catch e {
assert(e == "Input value must be between -1 and 1, inclusive.");
}
});
assert(math.acos(-1) == math.PI);
assert(math.acos(-0) == 1.5707963267948966);
assert(math.acos(0) == 1.5707963267948966);
assert(math.acos(0.5) == 1.0471975511965979);
assert(math.acos(1) == 0);
try {
assertions.Assert.throws("Input value must be between -1 and 1, inclusive.", () => {
log("${math.acos(2)}");
} catch e {
assert(e == "Input value must be between -1 and 1, inclusive.");
}
});
}
13 changes: 5 additions & 8 deletions examples/tests/sdk_tests/math/acsc.test.w
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
bring math;
bring "./../../valid/assertions.w" as assertions;

try {
assertions.PreflightAssert.throws("Input value must be equal or greater than |1|.", () => {
log("${math.acsc(0.5)}");
} catch e {
assert(e == "Input value must be equal or greater than |1|.");
}
});
assert(math.acsc(1) == 1.5707963267948966);
assert(math.acsc(math.PI / 2) == 0.69010709137454);
assert(math.acsc(math.PI) == 0.3239461069319807);
assert(math.acsc(math.TAU) == 0.15983462638513704);
assert(math.acsc(-1) == -1.5707963267948966);

test "inflight arc cosecant" {
try {
assertions.Assert.throws("Input value must be equal or greater than |1|.", () => {
log("${math.acsc(0.5)}");
} catch e {
assert(e == "Input value must be equal or greater than |1|.");
}
});
assert(math.acsc(1) == 1.5707963267948966);
assert(math.acsc(math.PI / 2) == 0.69010709137454);
assert(math.acsc(math.PI) == 0.3239461069319807);
Expand Down
13 changes: 5 additions & 8 deletions examples/tests/sdk_tests/math/asec.test.w
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
bring math;
bring "./../../valid/assertions.w" as assertions;

try {
assertions.PreflightAssert.throws("Input value must be equal or greater than |1|.", () => {
log("${math.asec(0.5)}");
} catch e {
assert(e == "Input value must be equal or greater than |1|.");
}
});
assert(math.asec(2) == 1.0471975511965979);
assert(math.asec(1) == 0);
assert(math.asec(math.PI) == 1.2468502198629159);
Expand All @@ -13,11 +12,9 @@ assert(math.asec(-1) == math.PI);
assert(math.asec(-2) == 2.0943951023931957);

test "inflight arc cosecant" {
try {
assertions.Assert.throws("Input value must be equal or greater than |1|.", () => {
log("${math.asec(0.5)}");
} catch e {
assert(e == "Input value must be equal or greater than |1|.");
}
});
assert(math.asec(2) == 1.0471975511965979);
assert(math.asec(1) == 0);
assert(math.asec(math.PI) == 1.2468502198629159);
Expand Down
26 changes: 10 additions & 16 deletions examples/tests/sdk_tests/math/asin.test.w
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
bring math;
try {
bring "./../../valid/assertions.w" as assertions;

assertions.PreflightAssert.throws("Input value must be between -1 and 1, inclusive.", () => {
log("${math.asin(-2)}");
} catch e {
assert(e == "Input value must be between -1 and 1, inclusive.");
}
});
assert(math.asin(-1) == -1.5707963267948966);
assert(math.asin(-0) == -0);
assert(math.asin(0) == 0);
assert(math.asin(0.5) == 0.5235987755982989);
assert(math.asin(1) == 1.5707963267948966);
try {
assertions.PreflightAssert.throws("Input value must be between -1 and 1, inclusive.", () => {
log("${math.asin(2)}");
} catch e {
assert(e == "Input value must be between -1 and 1, inclusive.");
}
});

test "inflight arc sine" {
try {
assertions.Assert.throws("Input value must be between -1 and 1, inclusive.", () => {
log("${math.asin(-2)}");
} catch e {
assert(e == "Input value must be between -1 and 1, inclusive.");
}
});
assert(math.asin(-1) == -1.5707963267948966);
assert(math.asin(-0) == -0);
assert(math.asin(0) == 0);
assert(math.asin(0.5) == 0.5235987755982989);
assert(math.asin(1) == 1.5707963267948966);
try {
assertions.Assert.throws("Input value must be between -1 and 1, inclusive.", () => {
log("${math.asin(2)}");
} catch e {
assert(e == "Input value must be between -1 and 1, inclusive.");
}
});
}
14 changes: 6 additions & 8 deletions examples/tests/sdk_tests/math/sqrt.test.w
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
bring math;
try {
bring "./../../valid/assertions.w" as assertions;

assertions.PreflightAssert.throws("Input value must be greater than or equal to 0.", () => {
log("${math.sqrt(-1)}");
} catch e {
assert(e == "Input value must be greater than or equal to 0.");
}
});
assert(math.sqrt(-0) == -0);
assert(math.sqrt(0) == 0);
assert(math.sqrt(1) == 1);
Expand All @@ -12,11 +12,9 @@ assert(math.sqrt(9) == 3);
assert(math.sqrt(math.INF) == math.INF);

test "inflight square root" {
try {
assertions.Assert.throws("Input value must be greater than or equal to 0.", () => {
log("${math.sqrt(-1)}");
} catch e {
assert(e == "Input value must be greater than or equal to 0.");
}
});
assert(math.sqrt(-0) == -0);
assert(math.sqrt(0) == 0);
assert(math.sqrt(1) == 1);
Expand Down
15 changes: 5 additions & 10 deletions examples/tests/sdk_tests/queue/push.test.w
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
bring cloud;
bring util;
bring "./../../valid/assertions.w" as assertions;

let q = new cloud.Queue();

Expand All @@ -13,19 +14,13 @@ new std.Test(inflight () => {
}
};

try {
assertions.Assert.throws("Empty messages are not allowed", () => {
q.push("");
assert(false);
} catch e {
assert(e == "Empty messages are not allowed");
}
});

try {
assertions.Assert.throws("Empty messages are not allowed", () => {
q.push("Foo", "");
assert(false);
} catch e {
assert(e == "Empty messages are not allowed");
}
});

q.push("Foo");

Expand Down
Loading
Loading