/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.painless;

import org.hamcrest.Matcher;

import java.util.Arrays;

import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

/** Tests for working with lists. */
public class ListTests extends ArrayLikeObjectTestCase {
    @Override
    protected String declType(String valueType) {
        return "List";
    }

    @Override
    protected String valueCtorCall(String valueType, int size) {
        String[] fill = new String[size];
        Arrays.fill(fill, fillValue(valueType));
        return "[" + String.join(",", fill) + "]";
    }

    private String fillValue(String valueType) {
        switch (valueType) {
        case "int":    return "0";
        case "long":   return "0L";
        case "short":  return "(short) 0";
        case "byte":   return "(byte) 0";
        case "float":  return "0.0f";
        case "double": return "0.0"; // Double is implicit for decimal constants
        default:       return null;
        }
    }

    @Override
    protected Matcher<String> outOfBoundsExceptionMessageMatcher(int index, int size) {
        if ("1.8".equals(Runtime.class.getPackage().getSpecificationVersion())) {
            // 1.8 and below aren't as clean as 1.9+
            if (index > size) {
                return equalTo("Index: " + index + ", Size: " + size);
            } else {
                Matcher<String> m = equalTo(Integer.toString(index));
                // If we set -XX:-OmitStackTraceInFastThrow we wouldn't need this
                m = anyOf(m, nullValue());
                return m;
            }
        } else {
            // Starting with 1.9 it gets nicer
            return equalTo("Index " + index + " out-of-bounds for length " + size);
        }
    }

}

@@ -872,17 +872,68 @@
 il.Array
-s
+List;%0Aimport java.util.Arrays;%0Aimport java.util.List
 ;%0A%0Aimpor
@@ -2073,61 +2073,8 @@
 ) %7B%0A
-            // 1.8 and below aren't as clean as 1.9+%0A
     
@@ -2181,36 +2181,25 @@
            %7D
- else %7B%0A    
+%0A
             
@@ -2215,16 +2215,22 @@
 tring%3E m
+atcher
  = equal
@@ -2254,28 +2254,24 @@
 ng(index));%0A
-    
             
@@ -2352,13 +2352,15 @@
     
-    m
+matcher
  = a
@@ -2365,16 +2365,22 @@
  anyOf(m
+atcher
 , nullVa
@@ -2403,20 +2403,16 @@
     
-    
 return m
 ;%0A  
@@ -2407,16 +2407,22 @@
 return m
+atcher
 ;%0A      
@@ -2415,37 +2415,40 @@
 atcher;%0A        
-    %7D
+%7D else %7B
 %0A        %7D else 
@@ -2444,145 +2444,367 @@
     
-%7D else %7B%0A            // Starting with 1.9 it gets nicer%0A            return equalTo(%22Index %22 + index + %22 out-of-bounds for length %22 + size
+    // This exception is locale dependent so we attempt to reproduce it%0A            List%3CObject%3E list = new ArrayList%3C%3E();%0A            for (int i = 0; i %3C size; i++) %7B%0A                list.add(new Object());%0A            %7D%0A            Exception e = expectThrows(IndexOutOfBoundsException.class, () -%3E list.get(index));%0A            return equalTo(e.getMessage()
 );%0A 

Make painless's assertion about out of bound less brittle