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): adding std.Map tests #4336

Merged
merged 6 commits into from
Oct 2, 2023
Merged
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
4 changes: 4 additions & 0 deletions examples/tests/invalid/container_types.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ s1.someRandomMethod();

let a: Array<str> = MutArray<str>[];
// ^^^^^^^^^^^^^^^ Expected type to be "Array<str>", but got "MutArray<str>" instead

let mm1: MutMap<num> = { "a" => 1, "b" => 2, "c" => 3 };
let mm2 = MutMap<num> { "a" => 1, "b" => 2, "c" => 3 };
let mm3 = mm2.copyMut();
10 changes: 9 additions & 1 deletion examples/tests/invalid/immutable_container_types.test.w
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
let m1 = Map<str>{"a" => "hi"};

m1.set("a", "bye");
// ^^^ Unknown symbol "set" (TODO: better error message https://github.com/winglang/wing/issues/1660)
// ^^^ Unknown symbol "set" (TODO: better error message https://github.com/winglang/wing/issues/1660)

let m2: Map<str> = MutMap<str> {};
let m3 = Map<bool> { "a" => "A" };
let m4 = {"1" => 1, "2" => 2 };
m4.set("2", 3);
let m5 = m4.copy();
m4.delete("1");
m4.clear();
241 changes: 241 additions & 0 deletions examples/tests/sdk_tests/std/map.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,244 @@ let mvalues = m.values();
assert(mvalues.length == 2);
assert(mvalues.at(0) == 123);
assert(mvalues.at(1) == 99);

// container types are equal regardless of the mutability if they have the same
// content but in all scenraios the type is specified for better readability

assert(Map<str>{} == MutMap<str>{});

test "equality"{
assert(Map<str>{} == MutMap<str>{});
}


let apprentices = MutMap<str> { "plagueis" => "sidious", "dooku" => "ventress" };
apprentices.delete("plagueis");
assert(apprentices.size() == 1);
assert(apprentices.copy() == Map<str> {"dooku" => "ventress"});
apprentices.set("sidious", "dooku");
assert(apprentices.get("sidious") == "dooku");
apprentices.set("sidious", "maul");
assert(apprentices.get("sidious") == "maul");
apprentices.clear();
assert(apprentices == MutMap<str>{});

test "mutability" {
let apprentices = MutMap<str> { "plagueis" => "sidious", "dooku" => "ventress" };
apprentices.delete("plagueis");
assert(apprentices.size() == 1);
assert(apprentices.copy() == Map<str> {"dooku" => "ventress"});
apprentices.set("sidious", "dooku");
assert(apprentices.get("sidious") == "dooku");
apprentices.set("sidious", "maul");
assert(apprentices.get("sidious") == "maul");
apprentices.clear();
assert(apprentices == MutMap<str>{});
}


let trilogies = { "PT" => 2, "OT" => 1, "ST" => 3 };
assert(trilogies.size() == 3);

let mutTrilogies = MutMap<num> { "PT" => 2, "OT" => 1, "ST" => 3 };
assert(mutTrilogies.size() == 3);
mutTrilogies.delete("ST"); // One could argue that it's better if this does not exist
assert(mutTrilogies.size() == 2);
WeepingClown13 marked this conversation as resolved.
Show resolved Hide resolved

test "size()" {
let trilogies = { "PT" => 2, "OT" => 1, "ST" => 3 };
assert(trilogies.size() == 3);

let mutTrilogies = MutMap<num> { "PT" => 2, "OT" => 1, "ST" => 3 };
assert(mutTrilogies.size() == 3);
mutTrilogies.delete("ST");
assert(mutTrilogies.size() == 2);
}


let greeting = { "hello" => "there!" };
assert(greeting.get("hello") == "there!");

let general: str? = greeting.get("grievous");
assert(general == nil);

let mutGreeting = MutMap<str>{ "general" => "kenobi" };
assert(mutGreeting.get("general") == "kenobi");

let Viceroy: str? = mutGreeting.get("gunray");
assert(Viceroy == nil);

test "get()" {
let greeting = { "hello" => "there!" };
assert(greeting.get("hello") == "there!");

let general: str? = greeting.get("grievous");
assert(general == nil);

let mutGreeting = MutMap<str>{ "general" => "kenobi" };
assert(mutGreeting.get("general") == "kenobi");

let Viceroy: str? = mutGreeting.get("gunray");
assert(Viceroy == nil);
}


let legion501 = { "fives" => "CT-5555", "rex" => "CT-7567" , "appo" => "CC-1119", "jesse" => "CT-5597" };
assert(legion501.has("fives") == true);
assert(legion501.has("rex") == legion501.has("jesse"));
assert(legion501.has("cody") == false);

let padawans = MutMap<str> { "ahsoka" => "anakin", "anakin" => "kenobi", "kenobi" => "qui gon", "qui gon" => "dooku", "dooku" => "yoda" };
assert(padawans.has("anakin") == true);
assert(padawans.has("qui gon") == padawans.has("dooku") == padawans.has("anakin"));
assert(padawans.has("windu") == padawans.has("sifo dyas"));
assert(padawans.has("revan") == false);

test "has()" {
let legion501 = { "fives" => "CT-5555", "rex" => "CT-7567" , "appo" => "CC-1119", "jesse" => "CT-5597" };
assert(legion501.has("fives") == true);
assert(legion501.has("rex") == legion501.has("jesse"));
assert(legion501.has("cody") == false);

let padawans = MutMap<str> { "ahsoka" => "anakin", "anakin" => "kenobi", "kenobi" => "qui gon", "qui gon" => "dooku", "dooku" => "yoda" };
assert(padawans.has("anakin") == true);
assert(padawans.has("qui gon") == padawans.has("dooku") == padawans.has("anakin"));
assert(padawans.has("windu") == padawans.has("sifo dyas"));
assert(padawans.has("revan") == false);
}


let forceUsers = { "sith" => ["malak", "vader", "bane"], "jedi" => ["sunrider", "bastila", "bindo"]};
let userKeys = forceUsers.values();
let valArr = [["malak", "vader", "bane"], ["sunrider", "bastila", "bindo"]];
assert(userKeys.length == valArr.length);
for i in 0..userKeys.length {
assert(userKeys.at(i) == valArr.at(i));
}
WeepingClown13 marked this conversation as resolved.
Show resolved Hide resolved

let saberforms = MutMap<str> { "1st" => "shii-cho", "2nd" => "makashi", "3rd" => "soresu", "4th" => "ataru", "5th" => "shien", "6th" => "niman", "7th" => "juyo"};
let saberformNames = saberforms.values();
let nameArr = ["shii-cho", "makashi", "soresu", "ataru", "shien", "niman", "juyo"];
assert(saberformNames.length == nameArr.length);
for i in 0..saberformNames.length {
assert(saberformNames.at(i) == nameArr.at(i));
}

test "values()" {
let forceUsers = { "sith" => ["malak", "vader", "bane"], "jedi" => ["sunrider", "bastila", "bindo"]};
let userKeys = forceUsers.values();
let valArr = [["malak", "vader", "bane"], ["sunrider", "bastila", "bindo"]];
assert(userKeys.length == valArr.length);
for i in 0..userKeys.length {
assert(userKeys.at(i) == valArr.at(i));
}

let saberforms = MutMap<str> { "1st" => "shii-cho", "2nd" => "makashi", "3rd" => "soresu", "4th" => "ataru", "5th" => "shien", "6th" => "niman", "7th" => "juyo"};
let saberformNames = saberforms.values();
let nameArr = ["shii-cho", "makashi", "soresu", "ataru", "shien", "niman", "juyo"];
assert(saberformNames.length == nameArr.length);
for i in 0..saberformNames.length {
assert(saberformNames.at(i) == nameArr.at(i));
}
}


let lightsaberColorMap = { "red" => "sith", "blue" => "jedi" };
let lightsaberColors = lightsaberColorMap.keys();
let colorArr = ["red", "blue"];
assert(lightsaberColors.length == colorArr.length);
for i in 0..lightsaberColors.length {
assert(lightsaberColors.at(i) == colorArr.at(i));
}

let isMandalorianWarrior = MutMap<bool> { "bo katan" => true, "jango" => true, "satine" => false, "boba" => true};
let mandalorianKeys = isMandalorianWarrior.keys();
let keysArr = ["bo katan", "jango", "satine", "boba"];
assert(mandalorianKeys.length == keysArr.length);
for i in 0..mandalorianKeys.length {
assert(mandalorianKeys.at(i) == keysArr.at(i));
}

test "keys()" {
let lightsaberColorMap = { "red" => "sith", "blue" => "jedi" };
let lightsaberColors = lightsaberColorMap.keys();
let colorArr = ["red", "blue"];
assert(lightsaberColors.length == colorArr.length);
for i in 0..lightsaberColors.length {
assert(lightsaberColors.at(i) == colorArr.at(i));
}

let isMandalorianWarrior = MutMap<bool> { "bo katan" => true, "jango" => true, "satine" => false, "boba" => true};
let mandalorianKeys = isMandalorianWarrior.keys();
let keysArr = ["bo katan", "jango", "satine", "boba"];
assert(mandalorianKeys.length == keysArr.length);
for i in 0..mandalorianKeys.length {
assert(mandalorianKeys.at(i) == keysArr.at(i));
}

}


let ruleOfTwo = { "master" => "apprentice" };
let mutRuleOfTwo: MutMap<str> = ruleOfTwo.copyMut();
assert(mutRuleOfTwo.copy() == ruleOfTwo);
assert(mutRuleOfTwo.delete("master") == true);
assert(mutRuleOfTwo.size() == 0);

test "copyMut()" {
let ruleOfTwo = { "master" => "apprentice" };
let mutRuleOfTwo: MutMap<str> = ruleOfTwo.copyMut();
assert(mutRuleOfTwo.copy() == ruleOfTwo);
assert(mutRuleOfTwo.delete("master") == true);
assert(mutRuleOfTwo.size() == 0);
}


let authority = MutMap<str> {"republic" => "senate", "empire" => "emperor"};
let immutAuthority: Map<str> = authority.copy();
assert(immutAuthority.copyMut() == authority);

test "copy()" {
let authority = MutMap<str> {"republic" => "senate", "empire" => "emperor"};
let immutAuthority: Map<str> = authority.copy();
assert(immutAuthority.copyMut() == authority);
}


let senate = MutMap<str> {"chancellor" => "palpatine"}; // I love democracy
senate.set("senator", "organa");
assert(senate.get("senator") == "organa");
assert(senate.size() == 2);

test "set()" {
let senate = MutMap<str> {"chancellor" => "palpatine"};
senate.set("senator", "organa");
assert(senate.get("senator") == "organa");
assert(senate.size() == 2);
}


let position = MutMap<str> {"librarian" => "jocasta"};
position.clear();
assert(position.size() == 0);

test "clear()" {
let position = MutMap<str> {"librarian" => "jocasta"};
position.clear();
assert(position.size() == 0);
}


let sithTriumvirate = MutMap<str> { "traya" => "lord of betrayal", "nihilus" => "lord of hunger", "sion" => "lord of pain" };
sithTriumvirate.delete("nihilus");
assert(sithTriumvirate.size() == 2);
sithTriumvirate.delete("sion");
assert(sithTriumvirate == MutMap<str> { "traya" => "lord of betrayal" });

test "delete()" {
let sithTriumvirate = MutMap<str> { "traya" => "lord of betrayal", "nihilus" => "lord of hunger", "sion" => "lord of pain" };
sithTriumvirate.delete("nihilus");
assert(sithTriumvirate.size() == 2);
sithTriumvirate.delete("sion");
assert(sithTriumvirate == MutMap<str> { "traya" => "lord of betrayal" });
}
56 changes: 56 additions & 0 deletions tools/hangar/__snapshots__/invalid.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,20 @@ error: Expected type to be \\"Array<str>\\", but got \\"MutArray<str>\\" instead
| ^^^^^^^^^^^^^^^ Expected type to be \\"Array<str>\\", but got \\"MutArray<str>\\" instead


error: Expected type to be \\"MutMap<num>\\", but got \\"Map<num>\\" instead
--> ../../../examples/tests/invalid/container_types.test.w:30:24
|
30 | let mm1: MutMap<num> = { \\"a\\" => 1, \\"b\\" => 2, \\"c\\" => 3 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Expected type to be \\"MutMap<num>\\", but got \\"Map<num>\\" instead


error: Unknown symbol \\"copyMut\\"
--> ../../../examples/tests/invalid/container_types.test.w:32:15
|
32 | let mm3 = mm2.copyMut();
| ^^^^^^^ Unknown symbol \\"copyMut\\"




Tests 1 failed (1)
Expand Down Expand Up @@ -1164,6 +1178,48 @@ exports[`immutable_container_types.test.w 1`] = `
| ^^^ Unknown symbol \\"set\\"


error: Expected type to be \\"Map<str>\\", but got \\"MutMap<str>\\" instead
--> ../../../examples/tests/invalid/immutable_container_types.test.w:6:20
|
6 | let m2: Map<str> = MutMap<str> {};
| ^^^^^^^^^^^^^^ Expected type to be \\"Map<str>\\", but got \\"MutMap<str>\\" instead


error: Expected type to be \\"bool\\", but got \\"str\\" instead
--> ../../../examples/tests/invalid/immutable_container_types.test.w:7:29
|
7 | let m3 = Map<bool> { \\"a\\" => \\"A\\" };
| ^^^ Expected type to be \\"bool\\", but got \\"str\\" instead


error: Unknown symbol \\"set\\"
--> ../../../examples/tests/invalid/immutable_container_types.test.w:9:4
|
9 | m4.set(\\"2\\", 3);
| ^^^ Unknown symbol \\"set\\"


error: Unknown symbol \\"copy\\"
--> ../../../examples/tests/invalid/immutable_container_types.test.w:10:13
|
10 | let m5 = m4.copy();
| ^^^^ Unknown symbol \\"copy\\"


error: Unknown symbol \\"delete\\"
--> ../../../examples/tests/invalid/immutable_container_types.test.w:11:4
|
11 | m4.delete(\\"1\\");
| ^^^^^^ Unknown symbol \\"delete\\"


error: Unknown symbol \\"clear\\"
--> ../../../examples/tests/invalid/immutable_container_types.test.w:12:4
|
12 | m4.clear();
| ^^^^^ Unknown symbol \\"clear\\"




Tests 1 failed (1)
Expand Down
Loading
Loading