<commit_before>package groovy

class SwitchTest extends GroovyTestCase {

    void testSwitch() {
        callSwitch("foo", "foo")
        callSwitch("bar", "barfoo")
        callSwitch("barbar", "barfoo")
        callSwitch("dummy", "d*")
        callSwitch("xyz", "xyzDefault")
        callSwitch("zzz", "Default")
        callSwitch(4, "List")
        callSwitch(5, "List")
        callSwitch(6, "List")
        callSwitch("inList", "List")
        callSwitch(1, "Integer")
        callSwitch(1.2, "Number")
        callSwitch(null, "null")
        callSwitch([1,2,3], "ListInterface")
    }

    def callSwitch(x, expected) {
        println("Calling switch with ${x}")

        def result = ""

        switch (x) {
        	case null:
        		result = "null"
                break

        	case ~/d.*/:
        		result = "d*"
                break

            case "barbar":
            case "bar":
                result = result + "bar"

            case "foo":
                result = result + "foo"
                break

            case [4, 5, 6, 'inList']:
                result = "List"
                break

            case Integer:
                result = "Integer"
                break

            case Number:
                result = "Number"
                break
                
            case List:
                result = "ListInterface"
                break

            case "xyz":
                result = result + "xyz"

            default:
                result = result + "Default"

                // unnecessary just testing compiler
                break;
        }
        println("Found result ${result}")

        assert result == expected , "when calling switch with ${x}"
    }
    
    // test the continue in switch, which should jump to the the while start
    void testSwitchScope() {
        def i = 0
        def j = 0
        while (true) {
            ++i;
            switch(i) {
                case 4:
                    continue
                case 5:
                    break;
                default:
                    j += i;
                    break;
            }
            if (i == 5) break;
        }
        assert j == 6
    }

    void testSwitchWithClosure(){
        switch(0){
            case {true}: break
            default: assert false
        }
        switch(0){
            case {false}: assert false
        }
        switch(0){
            case {it == 0}: break
            default: assert false
        }
        switch(0){
            case { candidate -> candidate == 0}: break
            default: assert false
        }
    }
    
}
<commit_msg>adds test for a switch with fall through<commit_after>package groovy

class SwitchTest extends GroovyTestCase {

    void testSwitch() {
        callSwitch("foo", "foo")
        callSwitch("bar", "barfoo")
        callSwitch("barbar", "barfoo")
        callSwitch("dummy", "d*")
        callSwitch("xyz", "xyzDefault")
        callSwitch("zzz", "Default")
        callSwitch(4, "List")
        callSwitch(5, "List")
        callSwitch(6, "List")
        callSwitch("inList", "List")
        callSwitch(1, "Integer")
        callSwitch(1.2, "Number")
        callSwitch(null, "null")
        callSwitch([1,2,3], "ListInterface")
    }

    def callSwitch(x, expected) {
        println("Calling switch with ${x}")

        def result = ""

        switch (x) {
        	case null:
        		result = "null"
                break

        	case ~/d.*/:
        		result = "d*"
                break

            case "barbar":
            case "bar":
                result = result + "bar"

            case "foo":
                result = result + "foo"
                break

            case [4, 5, 6, 'inList']:
                result = "List"
                break

            case Integer:
                result = "Integer"
                break

            case Number:
                result = "Number"
                break
                
            case List:
                result = "ListInterface"
                break

            case "xyz":
                result = result + "xyz"

            default:
                result = result + "Default"

                // unnecessary just testing compiler
                break;
        }
        println("Found result ${result}")

        assert result == expected , "when calling switch with ${x}"
    }
    
    // test the continue in switch, which should jump to the the while start
    void testSwitchScope() {
        def i = 0
        def j = 0
        while (true) {
            ++i;
            switch(i) {
                case 4:
                    continue
                case 5:
                    break;
                default:
                    j += i;
                    break;
            }
            if (i == 5) break;
        }
        assert j == 6
    }

    void testSwitchWithClosure(){
        switch(0){
            case {true}: break
            default: assert false
        }
        switch(0){
            case {false}: assert false
        }
        switch(0){
            case {it == 0}: break
            default: assert false
        }
        switch(0){
            case { candidate -> candidate == 0}: break
            default: assert false
        }
    }
    
    /** older versions of groovy produced a ListExpression for a
        fall through. the result was that it worked in some cases
        and in other cases not. For example not for patterns **/
    void testFallthrough() {
      def a = [ 'FileName.java', 'AnotherFileName.groovy' ]
      def i = 0
      a.each {
        switch( it ) {
  	      case ~/.*java$/:
  	      case ~/.*groovy$/:
  	        i++
            break;
  	      default:
	        assert false
	    }
      }
      assert i==2
    }
}

{'fraction_non_alphanumeric': 0.1039272851997942, 'fraction_numerical': 0.008574858514834506, 'mean_word_length': 1.1168784029038112, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5375478', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1483, 'n_words': 442}