<commit_before>////////////////////////////////////////////////////////////////////////////////
//
//  Licensed to the Apache Software Foundation (ASF) under one or more
//  contributor license agreements.  See the NOTICE file distributed with
//  this work for additional information regarding copyright ownership.
//  The ASF 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 spark.components {
    import mx.collections.ArrayCollection;
    import mx.collections.ArrayList;
    import mx.collections.IList;
    import mx.collections.ListCollectionView;

    import org.flexunit.asserts.assertEquals;
    import org.flexunit.asserts.assertNotNull;
    import org.fluint.uiImpersonation.UIImpersonator;

    import spark.components.gridClasses.GridColumn;
    import spark.formatters.DateTimeFormatter;

    public class DataGrid_FLEX_34837_Tests {
        private var _sut:DataGrid;

        [Before]
        public function setUp():void
        {
            _sut = new DataGrid();
        }

        [After]
        public function tearDown():void
        {
            _sut = null;
        }

        [Test]
        public function test_removing_selected_item_on_complex_field_sorted_grid_after_renaming_it():void
        {
            //given
            var streetColumn:GridColumn = new GridColumn("address.street");
            _sut.columns = new ArrayCollection([streetColumn]);

            const tenObjects:IList = generateVOs(10);
            const firstObject:FLEX_34837_VO = tenObjects.getItemAt(0) as FLEX_34837_VO;
            const dataProvider:ListCollectionView = new ListCollectionView(tenObjects);

            //when
            UIImpersonator.addChild(_sut);
            assertNotNull("UIImpersonator doesn't work correctly!", _sut.grid);
            _sut.dataProvider = dataProvider;
            _sut.sortByColumns(new <int>[0]); //sort by address.street
            firstObject.address.street = "zzz"; //should move it at the end of the list

            //then
            assertEquals("The object should have moved to the end of the list!", dataProvider.length - 1, dataProvider.getItemIndex(firstObject));
            dataProvider.removeItemAt(dataProvider.getItemIndex(firstObject)); //make sure there's no RTE
            assertEquals("The item wasn't removed!", 9, dataProvider.length);
        }

        [Test]
        public function test_removing_selected_item_on_complex_field_sorted_grid_with_formatter_after_renaming_it():void
        {
            //given
            var dateColumn:GridColumn = new GridColumn("address.dateMovedIn");
            var dateTimeFormatter:DateTimeFormatter = new DateTimeFormatter();
            dateTimeFormatter.dateTimePattern = "MMMM"; //Full month name
            dateColumn.formatter = dateTimeFormatter;

            _sut.columns = new ArrayCollection([dateColumn]);

            const tenObjects:IList = generateVOs(10);
            const dataProvider:ListCollectionView = new ListCollectionView(tenObjects);

            //when
            UIImpersonator.addChild(_sut);
            assertNotNull("UIImpersonator doesn't work correctly!", _sut.grid);
            _sut.dataProvider = dataProvider;
            _sut.sortByColumns(new <int>[0]); //sort by address.dateMovedIn, in effect by month name

            //then
            const aprilObject:FLEX_34837_VO = tenObjects.getItemAt(3) as FLEX_34837_VO;
            assertEquals(0, dataProvider.getItemIndex(aprilObject));

            //when
            const septemberObject:FLEX_34837_VO = tenObjects.getItemAt(8) as FLEX_34837_VO;
            septemberObject.address.dateMovedIn = new Date(2000, 3, 2); //"April"; should move it at the start of the list

            const firstObject:FLEX_34837_VO = tenObjects.getItemAt(0) as FLEX_34837_VO;
            firstObject.address.dateMovedIn = new Date(2000, 8, 2); //"September"; should move it at the end of the list

            //then
            assertEquals("The object should have moved to the end of the list!", dataProvider.length - 1, dataProvider.getItemIndex(firstObject));
            dataProvider.removeItemAt(dataProvider.getItemIndex(firstObject)); //make sure there's no RTE
            assertEquals("The item wasn't removed!", 9, dataProvider.length);
        }

        [Test]
        public function test_removing_selected_item_on_multiple_field_sorted_grid_with_formatter_changed_after_first_sort_and_after_renaming():void
        {
            //given
            var dateColumn:GridColumn = new GridColumn("address.dateMovedIn");
            _sut.columns = new ArrayCollection([dateColumn]);

            const tenObjects:IList = generateVOs(10);
            const dataProvider:ListCollectionView = new ListCollectionView(tenObjects);

            //when
            UIImpersonator.addChild(_sut);
            assertNotNull("UIImpersonator doesn't work correctly!", _sut.grid);
            _sut.dataProvider = dataProvider;

            var dateTimeFormatter:DateTimeFormatter = new DateTimeFormatter();
            dateTimeFormatter.dateTimePattern = "MMMM"; //Full month name
            dateColumn.formatter = dateTimeFormatter; //this should re-sort the items in the grid according to the month

            _sut.sortByColumns(new <int>[0]); //sort by address.dateMovedIn

            //then
            const aprilObject:FLEX_34837_VO = tenObjects.getItemAt(3) as FLEX_34837_VO;
            assertEquals(0, dataProvider.getItemIndex(aprilObject));

            //when
            const septemberObject:FLEX_34837_VO = tenObjects.getItemAt(8) as FLEX_34837_VO;
            septemberObject.address.dateMovedIn = new Date(2000, 3, 2); //"April"; should move it at the start of the list

            const firstObject:FLEX_34837_VO = tenObjects.getItemAt(0) as FLEX_34837_VO;
            firstObject.address.dateMovedIn = new Date(2000, 8, 2); //"September"; should move it at the end of the list

            //then
            assertEquals("The object should have moved to the end of the list!", dataProvider.length - 1, dataProvider.getItemIndex(firstObject));
            dataProvider.removeItemAt(dataProvider.getItemIndex(firstObject)); //make sure there's no RTE
            assertEquals("The item wasn't removed!", 9, dataProvider.length);
        }

        private static function generateVOs(no:int, reverse:Boolean = false):IList
        {
            return generateObjects(no, reverse, generateOneObject);
        }

        private static function generateObjects(no:int, reverse:Boolean, generator:Function):IList
        {
            var result:Array = [];
            for(var i:int = 0; i < no; i++)
            {
                result.push(generator(i));
            }

            if(reverse)
                result.reverse();

            return new ArrayList(result);
        }

        private static function generateOneObject(i:Number):FLEX_34837_VO
        {
            return new FLEX_34837_VO(i, "Object", "Street");
        }
    }
}

[Bindable]
class FLEX_34837_VO
{
    public var name:String;
    public var address:FLEX_34837_AddressVO;
    public var index:Number;

    public function FLEX_34837_VO(index:Number, namePrefix:String, streetPrefix:String)
    {
        this.index = index;
        this.name = namePrefix + index;
        this.address = new FLEX_34837_AddressVO(streetPrefix + index, Math.floor(index), new Date(2000 + Math.floor(index), Math.floor(index), 1, 0, 0, 0, 1));
    }
}

[Bindable]
class FLEX_34837_AddressVO
{
    public var street:String;
    public var houseNumber:int;
    public var dateMovedIn:Date;

    public function FLEX_34837_AddressVO(street:String, houseNumber:int, dateMovedIn:Date)
    {
        this.street = street;
        this.houseNumber = houseNumber;
        this.dateMovedIn = dateMovedIn;
    }
}<commit_msg>try to fix these tests<commit_after>////////////////////////////////////////////////////////////////////////////////
//
//  Licensed to the Apache Software Foundation (ASF) under one or more
//  contributor license agreements.  See the NOTICE file distributed with
//  this work for additional information regarding copyright ownership.
//  The ASF 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 spark.components {
    import mx.collections.ArrayCollection;
    import mx.collections.ArrayList;
    import mx.collections.IList;
    import mx.collections.ListCollectionView;

    import org.flexunit.asserts.assertEquals;
    import org.flexunit.asserts.assertNotNull;
    import org.fluint.uiImpersonation.UIImpersonator;

    import spark.components.gridClasses.GridColumn;
    import spark.formatters.DateTimeFormatter;

    public class DataGrid_FLEX_34837_Tests {
        private var _sut:DataGrid;

        [Before]
        public function setUp():void
        {
            _sut = new DataGrid();
        }

        [After]
        public function tearDown():void
        {
            _sut = null;
        }

        [Test]
        public function test_removing_selected_item_on_complex_field_sorted_grid_after_renaming_it():void
        {
            //given
            var streetColumn:GridColumn = new GridColumn("address.street");
            _sut.columns = new ArrayCollection([streetColumn]);

            const tenObjects:IList = generateVOs(10);
            const firstObject:FLEX_34837_VO = tenObjects.getItemAt(0) as FLEX_34837_VO;
            const dataProvider:ListCollectionView = new ListCollectionView(tenObjects);

            //when
            UIImpersonator.addElement(_sut);
            assertNotNull("UIImpersonator doesn't work correctly!", _sut.grid);
            _sut.dataProvider = dataProvider;
            _sut.sortByColumns(new <int>[0]); //sort by address.street
            firstObject.address.street = "zzz"; //should move it at the end of the list

            //then
            assertEquals("The object should have moved to the end of the list!", dataProvider.length - 1, dataProvider.getItemIndex(firstObject));
            dataProvider.removeItemAt(dataProvider.getItemIndex(firstObject)); //make sure there's no RTE
            assertEquals("The item wasn't removed!", 9, dataProvider.length);
        }

        [Test]
        public function test_removing_selected_item_on_complex_field_sorted_grid_with_formatter_after_renaming_it():void
        {
            //given
            var dateColumn:GridColumn = new GridColumn("address.dateMovedIn");
            var dateTimeFormatter:DateTimeFormatter = new DateTimeFormatter();
            dateTimeFormatter.dateTimePattern = "MMMM"; //Full month name
            dateColumn.formatter = dateTimeFormatter;

            _sut.columns = new ArrayCollection([dateColumn]);

            const tenObjects:IList = generateVOs(10);
            const dataProvider:ListCollectionView = new ListCollectionView(tenObjects);

            //when
            UIImpersonator.addElement(_sut);
            assertNotNull("UIImpersonator doesn't work correctly!", _sut.grid);
            _sut.dataProvider = dataProvider;
            _sut.sortByColumns(new <int>[0]); //sort by address.dateMovedIn, in effect by month name

            //then
            const aprilObject:FLEX_34837_VO = tenObjects.getItemAt(3) as FLEX_34837_VO;
            assertEquals(0, dataProvider.getItemIndex(aprilObject));

            //when
            const septemberObject:FLEX_34837_VO = tenObjects.getItemAt(8) as FLEX_34837_VO;
            septemberObject.address.dateMovedIn = new Date(2000, 3, 2); //"April"; should move it at the start of the list

            const firstObject:FLEX_34837_VO = tenObjects.getItemAt(0) as FLEX_34837_VO;
            firstObject.address.dateMovedIn = new Date(2000, 8, 2); //"September"; should move it at the end of the list

            //then
            assertEquals("The object should have moved to the end of the list!", dataProvider.length - 1, dataProvider.getItemIndex(firstObject));
            dataProvider.removeItemAt(dataProvider.getItemIndex(firstObject)); //make sure there's no RTE
            assertEquals("The item wasn't removed!", 9, dataProvider.length);
        }

        [Test]
        public function test_removing_selected_item_on_multiple_field_sorted_grid_with_formatter_changed_after_first_sort_and_after_renaming():void
        {
            //given
            var dateColumn:GridColumn = new GridColumn("address.dateMovedIn");
            _sut.columns = new ArrayCollection([dateColumn]);

            const tenObjects:IList = generateVOs(10);
            const dataProvider:ListCollectionView = new ListCollectionView(tenObjects);

            //when
            UIImpersonator.addElement(_sut);
            assertNotNull("UIImpersonator doesn't work correctly!", _sut.grid);
            _sut.dataProvider = dataProvider;

            var dateTimeFormatter:DateTimeFormatter = new DateTimeFormatter();
            dateTimeFormatter.dateTimePattern = "MMMM"; //Full month name
            dateColumn.formatter = dateTimeFormatter; //this should re-sort the items in the grid according to the month

            _sut.sortByColumns(new <int>[0]); //sort by address.dateMovedIn

            //then
            const aprilObject:FLEX_34837_VO = tenObjects.getItemAt(3) as FLEX_34837_VO;
            assertEquals(0, dataProvider.getItemIndex(aprilObject));

            //when
            const septemberObject:FLEX_34837_VO = tenObjects.getItemAt(8) as FLEX_34837_VO;
            septemberObject.address.dateMovedIn = new Date(2000, 3, 2); //"April"; should move it at the start of the list

            const firstObject:FLEX_34837_VO = tenObjects.getItemAt(0) as FLEX_34837_VO;
            firstObject.address.dateMovedIn = new Date(2000, 8, 2); //"September"; should move it at the end of the list

            //then
            assertEquals("The object should have moved to the end of the list!", dataProvider.length - 1, dataProvider.getItemIndex(firstObject));
            dataProvider.removeItemAt(dataProvider.getItemIndex(firstObject)); //make sure there's no RTE
            assertEquals("The item wasn't removed!", 9, dataProvider.length);
        }

        private static function generateVOs(no:int, reverse:Boolean = false):IList
        {
            return generateObjects(no, reverse, generateOneObject);
        }

        private static function generateObjects(no:int, reverse:Boolean, generator:Function):IList
        {
            var result:Array = [];
            for(var i:int = 0; i < no; i++)
            {
                result.push(generator(i));
            }

            if(reverse)
                result.reverse();

            return new ArrayList(result);
        }

        private static function generateOneObject(i:Number):FLEX_34837_VO
        {
            return new FLEX_34837_VO(i, "Object", "Street");
        }
    }
}

[Bindable]
class FLEX_34837_VO
{
    public var name:String;
    public var address:FLEX_34837_AddressVO;
    public var index:Number;

    public function FLEX_34837_VO(index:Number, namePrefix:String, streetPrefix:String)
    {
        this.index = index;
        this.name = namePrefix + index;
        this.address = new FLEX_34837_AddressVO(streetPrefix + index, Math.floor(index), new Date(2000 + Math.floor(index), Math.floor(index), 1, 0, 0, 0, 1));
    }
}

[Bindable]
class FLEX_34837_AddressVO
{
    public var street:String;
    public var houseNumber:int;
    public var dateMovedIn:Date;

    public function FLEX_34837_AddressVO(street:String, houseNumber:int, dateMovedIn:Date)
    {
        this.street = street;
        this.houseNumber = houseNumber;
        this.dateMovedIn = dateMovedIn;
    }
}
{'fraction_non_alphanumeric': 0.10475117148110802, 'fraction_numerical': 0.020997686695533542, 'mean_word_length': 2.485631589828406, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3405702', 'n_tokens_mistral': 4829, 'n_tokens_neox': 4497, 'n_words': 1170}