Skip to content

Commit

Permalink
adding tests for switch / pattern matching (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed Jul 15, 2024
1 parent 64e64f1 commit 5fc4008
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Changelog
## 1.5.6
* Added: Support for new core Api `haxe.Rest` abstract type
* Added: Support for core Api `haxe.Rest` & `Single` abstract types
* Improvement: better support for switch capture variables (wip)
* Bugfix: variable shadowing should now resolve to the correct declaration.
* Bugfix: Incorrect caching of returnType for recursive methods
* Bugfix: Allow uninitialized final fields in abstract enums
* Bugfix: Allow assign from underlying type inside abstract enums
* Bugfix: Fixed issue with autocompletion in lime/openFL project xmls
* Bugfix: fixed incorrect handling of for/while loop array initializers
* Changed: Renaming class will now also rename file/module if names are the same.

## 1.5.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,12 @@ public void testTypeParameterAnonymousStructure() throws Throwable {
doTestNoFixWithWarnings();
}
@Test
public void testSwitchEumVariableCapture() throws Throwable {
public void testSwitchPatternMatching() throws Throwable {
myFixture.enableInspections(HaxeUnresolvedSymbolInspection.class);
doTestNoFixWithWarnings();
}
@Test
public void testSwitchStatements() throws Throwable {
myFixture.enableInspections(HaxeUnresolvedSymbolInspection.class);
doTestNoFixWithWarnings();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class PatternMachingTest {
var enumVal = Test.TAny(myArray);
// correct
switch(enumVal) {
//TODO mlo : needs resolver work
case TString(x = <warning descr="Unresolved symbol">s</warning>): x.<warning descr="Unresolved symbol">toLowerCase</warning>();
case TString(s): s.toLowerCase();
case TInt(i): i * 2;
case TAny(a): a.indexOf("");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package ;

function add(a:Int, b:Int) return a + b;
function mul(a:Int, b:Int) return a * b;
function isEven(value:Float) return value % 2 == 0;


class PatternMachingTest {
public function testEumVariableCapture() {
var value = 1;
switch value {
// capture variable
case special = 1:
trace(special * 2);

// multiple values
case 2 | 4 | 6:
trace("2 or 4 or 6 number");

// match expression
case _ * 2 => 10:
trace("found 5");

// match expresion with function call
case add(_, 1) => result:
trace(result > 1);

//complex matching expressiom
case mul(add(_, 1), 3) => result:
trace(result > 1);

//chained matching expressiom
case add(_, 1) => mul(_, 3) => result:
trace(result > 1);

// matching expression with capture
//TODO mlo: needs work
case value = <error descr="Incompatible type: Bool should be Int">isEven(_)</error> => true:
trace("even number: " + value);

// match anything
case _: trace ("anything");

// capture variable
case var other:
trace("other: " + other);
}
}

public function testSwitchOnArray() {
var myArray = [1, 6] ;
switch (myArray) {
case [2, _]:
trace("0");
case [_, 6]:
trace("1");
case []:
trace("2");
case [_, _, _]:
trace("3");
case _:
trace("4");
}
}

public function testSwitchOnStructure() {
var person = { name: "Mark", age: 33 };

switch person {
// match person with age older than 50
case { age: _ > 50 => true}:
trace('found somebody older than 50');

// match on specific person named Jose who is 42
case { name: "Jose", age: 42 }:
trace('Found Jose, who is 42');

// match on name
//TODO mlo: needs resolver work
case { name: <warning descr="Unresolved symbol">name</warning> }:
trace('Found someone called $<warning descr="Unresolved symbol">name</warning>');

// matches anything
case _:
trace("unknown");
}
}

}

0 comments on commit 5fc4008

Please sign in to comment.