diff --git a/gui/test/bytearraytablecursortest.cpp b/gui/test/bytearraytablecursortest.cpp index 317df4a0..226d17ac 100644 --- a/gui/test/bytearraytablecursortest.cpp +++ b/gui/test/bytearraytablecursortest.cpp @@ -1,986 +1,984 @@ /* This file is part of the Okteta Gui library, made within the KDE community. Copyright 2008 Friedrich W. H. Kossebau This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "bytearraytablecursortest.h" // test object #include // lib #include // Qt #include namespace Okteta { // local variables static const Size NoOfBytesPerLine = 8; static const Address StartOffset = 22; static const Address FirstLineOffset = 10; static const Address RelativeStartOffset = StartOffset-FirstLineOffset; static const Address ByteArrayOffset = 9; static const Size Length = 250; static const Address FinalOffset = (StartOffset+Length-1); static const Address RelativeFinalOffset = FinalOffset-FirstLineOffset; static const Address FirstIndex = ByteArrayOffset; static const Address LastIndex = Length-1+ByteArrayOffset; static const Line StartLine = RelativeStartOffset / NoOfBytesPerLine; static const LinePosition StartLinePosition = RelativeStartOffset % NoOfBytesPerLine; static Coord StartCoord( StartLinePosition, StartLine ); static const Line FinalLine = RelativeFinalOffset/ NoOfBytesPerLine; static const LinePosition FinalLinePosition = RelativeFinalOffset % NoOfBytesPerLine; static Coord FinalCoord( FinalLinePosition, FinalLine ); -static const LineSize NoOfLinesPerPage = 5; - static const LinePosition Pos1 = 15; static const LinePosition Pos2 = 25; static const Line Line1 = 10; static const LineSize LineCount = 10; static const Line Line2 = Line1 + LineCount - 1; static Coord Start( Pos1, Line1 ); static Coord End( Pos2, Line2 ); void ByteArrayTableCursorTest::testConstructor() { const ByteArrayTableLayout layout( NoOfBytesPerLine, FirstLineOffset, StartOffset, ByteArrayOffset, Length ); ByteArrayTableCursor cursor( &layout ); QCOMPARE( cursor.index(), FirstIndex ); QCOMPARE( cursor.validIndex(), FirstIndex ); QCOMPARE( cursor.realIndex(), FirstIndex ); QCOMPARE( cursor.coord(), StartCoord ); QCOMPARE( cursor.pos(), StartCoord.pos() ); QCOMPARE( cursor.line(), StartCoord.line() ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.appendPosEnabled(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); } void ByteArrayTableCursorTest::testSetAppendPosEnabled() { const ByteArrayTableLayout layout( NoOfBytesPerLine, FirstLineOffset, StartOffset, ByteArrayOffset, Length ); const Coord finalCoord = layout.finalCoord(); ByteArrayTableCursor cursor( &layout ); // with append pos cursor.setAppendPosEnabled( true ); cursor.gotoEnd(); Coord nextToFinalCoord( finalCoord ); nextToFinalCoord.goCRight( layout.noOfBytesPerLine()-1 ); QCOMPARE( cursor.appendPosEnabled(), true ); QCOMPARE( cursor.atAppendPos(), true ); QCOMPARE( cursor.coord(), nextToFinalCoord ); // without append pos cursor.setAppendPosEnabled( false ); cursor.gotoEnd(); QCOMPARE( cursor.appendPosEnabled(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.coord(), finalCoord ); } void ByteArrayTableCursorTest::testGotoIndex() { const ByteArrayTableLayout layout( NoOfBytesPerLine, FirstLineOffset, StartOffset, ByteArrayOffset, Length ); ByteArrayTableCursor cursor( &layout ); // 2 before first valid, as (in)validIndex equals -1, see next test Address index = FirstIndex-2; Coord coord = layout.coordOfIndex( index ); cursor.gotoIndex( index ); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), -1 ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), coord ); // 1 before first valid index = FirstIndex-1; // hm coord = layout.coordOfIndex( index ); cursor.gotoIndex( index ); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), -1 ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), coord ); // at first valid index = FirstIndex; coord = layout.coordOfIndex( index ); cursor.gotoIndex( index ); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), coord ); // at last valid index = LastIndex; coord = layout.coordOfIndex( index ); cursor.gotoIndex( index ); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), coord ); // 1 after last valid index = LastIndex+1; coord = layout.coordOfIndex( index ); cursor.gotoIndex( index ); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), -1 ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), true ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), coord ); // 2 after last valid index = LastIndex + 2; coord = layout.coordOfIndex( index ); cursor.gotoIndex( index ); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), -1 ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), coord ); } void ByteArrayTableCursorTest::testGotoCIndex() { const ByteArrayTableLayout layout( NoOfBytesPerLine, FirstLineOffset, StartOffset, ByteArrayOffset, Length ); ByteArrayTableCursor cursor( &layout ); // 2 before first valid, as (in)validIndex equals -1, see next test Address index = FirstIndex-2; const Coord startCoord = layout.startCoord(); cursor.gotoCIndex( index ); QCOMPARE( cursor.index(), FirstIndex ); QCOMPARE( cursor.validIndex(), FirstIndex ); QCOMPARE( cursor.realIndex(), FirstIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), startCoord ); // 1 before first valid index = FirstIndex-1; // hm cursor.gotoCIndex( index ); QCOMPARE( cursor.index(), FirstIndex ); QCOMPARE( cursor.validIndex(), FirstIndex ); QCOMPARE( cursor.realIndex(), FirstIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), startCoord ); // at first valid index = FirstIndex; Coord coord = layout.coordOfIndex( index ); cursor.gotoCIndex( index ); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), coord ); // at last valid index = LastIndex; coord = layout.coordOfIndex( index ); cursor.gotoCIndex( index ); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), coord ); // 1 after last valid index = LastIndex + 1; const Coord finalCoord = layout.finalCoord(); cursor.gotoCIndex( index ); QCOMPARE( cursor.index(), LastIndex ); QCOMPARE( cursor.validIndex(), LastIndex ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), true ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), finalCoord ); // 2 after last valid index = LastIndex + 2; cursor.gotoCIndex( index ); QCOMPARE( cursor.index(), LastIndex ); QCOMPARE( cursor.validIndex(), LastIndex ); QCOMPARE( cursor.realIndex(), LastIndex+1 ); QCOMPARE( cursor.isBehind(), true ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), finalCoord ); } void ByteArrayTableCursorTest::testGotoRealIndex() { const ByteArrayTableLayout layout( NoOfBytesPerLine, FirstLineOffset, StartOffset, ByteArrayOffset, Length ); ByteArrayTableCursor cursor( &layout ); // 2 before first valid, as (in)validIndex equals -1, see next test Address index = FirstIndex; Coord coord = layout.coordOfIndex( index ); cursor.gotoIndex( index ); cursor.gotoRealIndex(); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.coord(), coord ); // after gotoLineEnd in first line (so lineEnd != end) cursor.gotoStart(); cursor.gotoLineEnd(); index = cursor.index() + 1; coord = layout.coordOfIndex( index ); cursor.gotoRealIndex(); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.coord(), coord ); // after gotoEnd with append pos disabled cursor.setAppendPosEnabled( false ); cursor.gotoEnd(); index = cursor.index(); coord = layout.coordOfIndex( index ); cursor.gotoRealIndex(); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index+1 ); QCOMPARE( cursor.isBehind(), true ); QCOMPARE( cursor.coord(), coord ); // after gotoEnd with append pos disabled cursor.setAppendPosEnabled( true ); cursor.gotoEnd(); index = cursor.index(); coord = layout.coordOfIndex( index ); cursor.gotoRealIndex(); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), -1 ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.coord(), coord ); } void ByteArrayTableCursorTest::testGotoStart() { const ByteArrayTableLayout layout( NoOfBytesPerLine, FirstLineOffset, StartOffset, ByteArrayOffset, Length ); const Coord startCoord = layout.startCoord(); ByteArrayTableCursor cursor( &layout ); cursor.gotoStart(); QCOMPARE( cursor.index(), FirstIndex ); QCOMPARE( cursor.validIndex(), FirstIndex ); QCOMPARE( cursor.realIndex(), FirstIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.coord(), startCoord ); // TODO: test for length == 0 } void ByteArrayTableCursorTest::testGotoEnd() { const ByteArrayTableLayout layout( NoOfBytesPerLine, FirstLineOffset, StartOffset, ByteArrayOffset, Length ); const Coord finalCoord = layout.finalCoord(); ByteArrayTableCursor cursor( &layout ); // without append pos cursor.setAppendPosEnabled( false ); cursor.gotoEnd(); QCOMPARE( cursor.index(), LastIndex ); QCOMPARE( cursor.validIndex(), LastIndex ); QCOMPARE( cursor.realIndex(), LastIndex+1 ); QCOMPARE( cursor.isBehind(), true ); QCOMPARE( cursor.coord(), finalCoord ); // without append pos cursor.setAppendPosEnabled( true ); Coord nextToFinalCoord( finalCoord ); nextToFinalCoord.goCRight( layout.noOfBytesPerLine()-1 ); cursor.gotoEnd(); QCOMPARE( cursor.index(), LastIndex+1 ); QCOMPARE( cursor.validIndex(), -1 ); QCOMPARE( cursor.realIndex(), LastIndex+1 ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.coord(), nextToFinalCoord ); // TODO: test for length == 0 } void ByteArrayTableCursorTest::testGotoNextByte() { const ByteArrayTableLayout layout( NoOfBytesPerLine, FirstLineOffset, StartOffset, ByteArrayOffset, Length ); ByteArrayTableCursor cursor( &layout ); // at start Address index = FirstIndex + 1; Coord coord = layout.startCoord(); coord.goCRight( layout.noOfBytesPerLine()-1 ); cursor.gotoStart(); cursor.gotoNextByte(); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), coord ); // one before end index = LastIndex; coord = layout.coordOfIndex( index ); cursor.gotoIndex( index-1 ); cursor.gotoNextByte(); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), coord ); // at end, append disabled -> noop cursor.setAppendPosEnabled( false ); cursor.gotoEnd(); index = cursor.index(); coord = cursor.coord(); bool isBehind = cursor.isBehind(); cursor.gotoNextByte(); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index+1 ); QCOMPARE( cursor.isBehind(), isBehind ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), coord ); // at end, append enabled -> noop cursor.setAppendPosEnabled( true ); cursor.gotoEnd(); index = cursor.index(); coord = cursor.coord(); isBehind = cursor.isBehind(); cursor.gotoNextByte(); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), -1 ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), isBehind ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), true ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), coord ); } void ByteArrayTableCursorTest::testGotoPreviousByte() { const ByteArrayTableLayout layout( NoOfBytesPerLine, FirstLineOffset, StartOffset, ByteArrayOffset, Length ); ByteArrayTableCursor cursor( &layout ); // at start cursor.gotoStart(); Address index = cursor.index(); Coord coord = cursor.coord(); cursor.gotoPreviousByte(); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), coord ); // one after start index = FirstIndex; coord = layout.coordOfIndex( index ); cursor.gotoIndex( index + 1 ); cursor.gotoPreviousByte(); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), coord ); // at end, append disabled -> noop cursor.setAppendPosEnabled( false ); cursor.gotoEnd(); index = cursor.realIndex() - 1; coord = layout.coordOfIndex( index ); cursor.gotoPreviousByte(); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), coord ); // at end, append enabled -> noop cursor.setAppendPosEnabled( true ); cursor.gotoEnd(); index = cursor.realIndex() - 1; coord = layout.coordOfIndex( index ); cursor.gotoPreviousByte(); QCOMPARE( cursor.index(), index ); QCOMPARE( cursor.validIndex(), index ); QCOMPARE( cursor.realIndex(), index ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), coord ); } void ByteArrayTableCursorTest::testGotoNextByteN() { const ByteArrayTableLayout layout( NoOfBytesPerLine, FirstLineOffset, StartOffset, ByteArrayOffset, Length ); ByteArrayTableCursor cursor( &layout ); // at start // one right Address indexSteps = 1; Address expectedIndex = FirstIndex + indexSteps; Coord expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoStart(); cursor.gotoNextByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); // one line indexSteps = layout.noOfBytesPerLine(); expectedIndex = FirstIndex + indexSteps; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoStart(); cursor.gotoNextByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); // to end, append disabled cursor.setAppendPosEnabled( false ); indexSteps = layout.length(); expectedIndex = FirstIndex + indexSteps -1; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoStart(); cursor.gotoNextByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex+1 ); QCOMPARE( cursor.isBehind(), true ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), expectedCoord ); // to end, append disabled cursor.setAppendPosEnabled( true ); indexSteps = layout.length(); expectedIndex = FirstIndex + indexSteps; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoStart(); cursor.gotoNextByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), -1 ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), true ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); // behind end, append disabled cursor.setAppendPosEnabled( false ); indexSteps = layout.length() + 1; expectedIndex = LastIndex; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoStart(); cursor.gotoNextByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex+1 ); QCOMPARE( cursor.isBehind(), true ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), expectedCoord ); // behind end, append disabled cursor.setAppendPosEnabled( true ); indexSteps = layout.length() + 1; expectedIndex = LastIndex + 1; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoStart(); cursor.gotoNextByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), -1 ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), true ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); // one before end // to end indexSteps = 1; expectedIndex = LastIndex; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoIndex( LastIndex-1 ); cursor.gotoNextByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), expectedCoord ); // behind end, append disabled cursor.setAppendPosEnabled( false ); indexSteps = 2; expectedIndex = LastIndex; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoIndex( LastIndex-1 ); cursor.gotoNextByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex+1 ); QCOMPARE( cursor.isBehind(), true ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), expectedCoord ); // behind end, append disabled cursor.setAppendPosEnabled( true ); indexSteps = 2; expectedIndex = LastIndex + 1; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoIndex( LastIndex-1 ); cursor.gotoNextByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), -1 ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), true ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); // at end // TODO: check for end == lineEnd // append disabled -> noop cursor.setAppendPosEnabled( false ); indexSteps = 1; expectedIndex = LastIndex; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoEnd(); cursor.gotoNextByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex+1 ); QCOMPARE( cursor.isBehind(), true ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), true ); QCOMPARE( cursor.coord(), expectedCoord ); // append enabled -> noop cursor.setAppendPosEnabled( true ); indexSteps = 1; expectedIndex = LastIndex + 1; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoEnd(); cursor.gotoNextByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), -1 ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), false ); QCOMPARE( cursor.atEnd(), true ); QCOMPARE( cursor.atAppendPos(), true ); QCOMPARE( cursor.atLineStart(), false ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); } void ByteArrayTableCursorTest::testGotoPreviousByteN() { const ByteArrayTableLayout layout( NoOfBytesPerLine, FirstLineOffset, StartOffset, ByteArrayOffset, Length ); ByteArrayTableCursor cursor( &layout ); // at start Address indexSteps = 1; Address expectedIndex = FirstIndex; Coord expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoStart(); cursor.gotoPreviousByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); // one behind start // to start indexSteps = 1; expectedIndex = FirstIndex; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoIndex( FirstIndex+1 ); cursor.gotoPreviousByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); // to one before start indexSteps = 2; expectedIndex = FirstIndex; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoIndex( FirstIndex+1 ); cursor.gotoPreviousByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); // at end // append disabled -> noop cursor.setAppendPosEnabled( false ); // to start indexSteps = layout.length(); expectedIndex = FirstIndex; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoEnd(); cursor.gotoPreviousByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); // to one before start indexSteps = layout.length() + 1; expectedIndex = FirstIndex; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoEnd(); cursor.gotoPreviousByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); // append disabled -> noop cursor.setAppendPosEnabled( true ); // to start indexSteps = layout.length(); expectedIndex = FirstIndex; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoEnd(); cursor.gotoPreviousByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); // to one before start indexSteps = layout.length() + 1; expectedIndex = FirstIndex; expectedCoord = layout.coordOfIndex( expectedIndex ); cursor.gotoEnd(); cursor.gotoPreviousByte( indexSteps ); QCOMPARE( cursor.index(), expectedIndex ); QCOMPARE( cursor.validIndex(), expectedIndex ); QCOMPARE( cursor.realIndex(), expectedIndex ); QCOMPARE( cursor.isBehind(), false ); QCOMPARE( cursor.atStart(), true ); QCOMPARE( cursor.atEnd(), false ); QCOMPARE( cursor.atAppendPos(), false ); QCOMPARE( cursor.atLineStart(), true ); QCOMPARE( cursor.atLineEnd(), false ); QCOMPARE( cursor.coord(), expectedCoord ); } } QTEST_MAIN( Okteta::ByteArrayTableCursorTest ) diff --git a/kasten/controllers/test/jsparsertest.cpp b/kasten/controllers/test/jsparsertest.cpp index a3600e9b..b0ab7cc5 100644 --- a/kasten/controllers/test/jsparsertest.cpp +++ b/kasten/controllers/test/jsparsertest.cpp @@ -1,341 +1,340 @@ /* * This file is part of the Okteta Kasten module, made within the KDE community. * * Copyright 2013 Alex Richardson * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ // TODO: find better way to work-around simple name creation for QTest::newRow #undef QT_USE_QSTRINGBUILDER #include #include #include "view/structures/script/scriptengineinitializer.h" #include "view/structures/parsers/scriptvalueconverter.h" #include "testutils.h" #include struct JsTestData { typedef std::function CheckCallback; JsTestData() {} JsTestData(const char* tag, const char* constructor, CheckCallback check) : tag(tag), constructorCall(QString::fromUtf8(constructor)), check(check) {} QByteArray tag; QString constructorCall; CheckCallback check; }; Q_DECLARE_METATYPE(JsTestData) Q_DECLARE_METATYPE(JsTestData::CheckCallback) class JsParserTest : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase(); void testValidationFunc(); void testValidationFunc_data(); void testUpdateFunc(); void testUpdateFunc_data(); void testByteOrder(); void testByteOrder_data(); void testName(); void testName_data(); void testCustomTypeName(); void testCustomTypeName_data(); void testImport(); void testImportPathTraversal(); private: /** data gets set to the parsed result. * This is needed since functions with QVERIFY/QCOMPARE must return void */ void testCommon(DataInformation** data); private: QScriptEngine engine; QVector primitiveData; QVector bitfieldData; QVector allData; }; static JsTestData::CheckCallback primitiveTypeCheck(PrimitiveDataTypeEnum type) { return [type](DataInformation* data) { QVERIFY(data->isPrimitive()); QCOMPARE(data->asPrimitive()->type().value, type); }; } static JsTestData::CheckCallback bitfieldCheck(AbstractBitfieldDataInformation::Type type) { return [type](DataInformation* data) { QVERIFY(data->isPrimitive()); QCOMPARE(data->asBitfield()->bitfieldType(), type); }; } void JsParserTest::initTestCase() { ScriptEngineInitializer::addFuctionsToScriptEngine(&engine); primitiveData << JsTestData("float", "float()", primitiveTypeCheck(Type_Float)) << JsTestData("double", "double()", primitiveTypeCheck(Type_Double)) << JsTestData("char", "char()", primitiveTypeCheck(Type_Char)) << JsTestData("uint8", "uint8()", primitiveTypeCheck(Type_UInt8)) << JsTestData("uint16", "uint16()", primitiveTypeCheck(Type_UInt16)) << JsTestData("uint32", "uint32()", primitiveTypeCheck(Type_UInt32)) << JsTestData("uint64", "uint64()", primitiveTypeCheck(Type_UInt64)) << JsTestData("int8", "int8()", primitiveTypeCheck(Type_Int8)) << JsTestData("int16", "int16()", primitiveTypeCheck(Type_Int16)) << JsTestData("int32", "int32()", primitiveTypeCheck(Type_Int32)) << JsTestData("int64", "int64()", primitiveTypeCheck(Type_Int64)) << JsTestData("bool8", "bool8()", primitiveTypeCheck(Type_Bool8)) << JsTestData("bool16", "bool16()", primitiveTypeCheck(Type_Bool16)) << JsTestData("bool32", "bool32()", primitiveTypeCheck(Type_Bool32)) << JsTestData("bool64", "bool64()", primitiveTypeCheck(Type_Bool64)); bitfieldData << JsTestData("signed bitfield", "bitfield(\"signed\", 5)", bitfieldCheck(AbstractBitfieldDataInformation::Signed)) << JsTestData("unsigned bitfield", "bitfield(\"unsigned\", 5)", bitfieldCheck(AbstractBitfieldDataInformation::Unsigned)) << JsTestData("bool bitfield", "bitfield(\"bool\", 5)", bitfieldCheck(AbstractBitfieldDataInformation::Boolean)); allData << primitiveData << bitfieldData; //TODO struct, union, taggedUnion, pointer, flags, enum, array, string //needed so that imports can be resolved QString resources = QFINDTESTDATA("resources"); QString examples = QFINDTESTDATA("../view/structures/examples"); QVERIFY2(!resources.isEmpty(), "Test data must exist!"); QVERIFY2(!examples.isEmpty(), "Test data must exist!"); qputenv("XDG_DATA_DIRS", QFile::encodeName(QFileInfo(resources).absoluteFilePath()) + ':' + QFile::encodeName(QFileInfo(examples).absoluteFilePath())); } void JsParserTest::testByteOrder_data() { QTest::addColumn("code"); QTest::addColumn("checkFunction"); QTest::addColumn("expectedByteOrder"); //verify that default is inherit - QString codeStr = QStringLiteral("%1"); Q_FOREACH(const JsTestData& data, allData) { //default should be inherit QString codeStr = QStringLiteral("%1;"); QTest::newRow(data.tag.constData()) << codeStr.arg(data.constructorCall) << data.check << (int)DataInformation::EndianessInherit; //use set() function to specify byteOrder codeStr = QStringLiteral("%1.set({byteOrder: \"inherit\"})"); QTest::newRow((data.tag + " set() inherit").constData()) << codeStr.arg(data.constructorCall) << data.check << (int)DataInformation::EndianessInherit; codeStr = QStringLiteral("%1.set({byteOrder: \"littleEndian\"})"); QTest::newRow((data.tag + " set() little endian").constData()) << codeStr.arg(data.constructorCall) << data.check << (int)DataInformation::EndianessLittle; codeStr = QStringLiteral("%1.set({byteOrder: \"bigEndian\"})"); QTest::newRow((data.tag + " set() big endian").constData()) << codeStr.arg(data.constructorCall) << data.check << (int)DataInformation::EndianessBig; codeStr = QStringLiteral("%1.set({byteOrder: \"fromSettings\"})"); QTest::newRow((data.tag + " set() from settings").constData()) << codeStr.arg(data.constructorCall) << data.check << (int)DataInformation::EndianessFromSettings; //direct property access to specify byteOrder codeStr = QStringLiteral("var obj = %1; obj.byteOrder = \"inherit\"; obj;"); QTest::newRow((data.tag + " property assign inherit").constData()) << codeStr.arg(data.constructorCall) << data.check << (int)DataInformation::EndianessInherit; codeStr = QStringLiteral("var obj = %1; obj.byteOrder = \"little-endian\"; obj;"); QTest::newRow((data.tag + " property assign little endian").constData()) << codeStr.arg(data.constructorCall) << data.check << (int)DataInformation::EndianessLittle; codeStr = QStringLiteral("var obj = %1; obj.byteOrder = \"big-endian\"; obj;"); QTest::newRow((data.tag + " property assign big endian").constData()) << codeStr.arg(data.constructorCall) << data.check << (int)DataInformation::EndianessBig; codeStr = QStringLiteral("var obj = %1; obj.byteOrder = \"from-settings\"; obj;"); QTest::newRow((data.tag + " property assign from settings").constData()) << codeStr.arg(data.constructorCall) << data.check << (int)DataInformation::EndianessFromSettings; } } void JsParserTest::testCommon(DataInformation** dataPtr) { QFETCH(QString, code); QFETCH(JsTestData::CheckCallback, checkFunction); QScriptValue value = engine.evaluate(code); QVERIFY(value.isValid()); QVERIFY(!value.isError()); QVERIFY(value.isObject()); ScriptLogger logger; QScopedPointer data (ScriptValueConverter::convert(value, QStringLiteral("converted"), &logger)); QVERIFY(logger.rowCount() == 0); QVERIFY(data); checkFunction(data.data()); *dataPtr = data.take(); } void JsParserTest::testByteOrder() { DataInformation* data = 0; testCommon(&data); if (QTest::currentTestFailed()) return; //Qt doesn't use exceptions, we must manually check after each call QFETCH(int, expectedByteOrder); QCOMPARE((int)data->byteOrder(), expectedByteOrder); } static const QString updateFunction = QStringLiteral("function () { /* do nothing*/; }"); void JsParserTest::testUpdateFunc_data() { QTest::addColumn("code"); QTest::addColumn("checkFunction"); Q_FOREACH(const JsTestData& data, allData) { QString codeStr = QStringLiteral("%1.setUpdate(") + updateFunction + QStringLiteral(");"); QTest::newRow((data.tag + "-setUpdate()").constData()) << codeStr.arg(data.constructorCall) << data.check; codeStr = QStringLiteral("%1.set({updateFunc: ") + updateFunction + QStringLiteral("});"); QTest::newRow((data.tag + "-set()").constData()) << codeStr.arg(data.constructorCall) << data.check; codeStr = QStringLiteral("var obj = %1; obj.updateFunc = ") + updateFunction + QStringLiteral("; obj;"); QTest::newRow((data.tag + "-property assign").constData()) << codeStr.arg(data.constructorCall) << data.check; } } void JsParserTest::testUpdateFunc() { DataInformation* data = 0; testCommon(&data); if (QTest::currentTestFailed()) return; //Qt doesn't use exceptions, we must manually check after each call QVERIFY(data); QScriptValue update = data->updateFunc(); QVERIFY(update.isValid()); QVERIFY(update.isFunction()); QCOMPARE(update.toString(), updateFunction); } static const QString validationFunction = QStringLiteral("function () { return true; }"); void JsParserTest::testValidationFunc_data() { QTest::addColumn("code"); QTest::addColumn("checkFunction"); Q_FOREACH(const JsTestData& data, allData) { QString codeStr = QStringLiteral("%1.setValidation(") + validationFunction + QStringLiteral(");"); QTest::newRow((data.tag + "-setUpdate()").constData()) << codeStr.arg(data.constructorCall) << data.check; codeStr = QStringLiteral("%1.set({validationFunc: ") + validationFunction + QStringLiteral("});"); QTest::newRow((data.tag + "-set()").constData()) << codeStr.arg(data.constructorCall) << data.check; codeStr = QStringLiteral("var obj = %1; obj.validationFunc = ") + validationFunction + QStringLiteral("; obj;"); QTest::newRow((data.tag + "-property assign").constData()) << codeStr.arg(data.constructorCall) << data.check; } } void JsParserTest::testValidationFunc() { DataInformation* data = 0; testCommon(&data); if (QTest::currentTestFailed()) return; //Qt doesn't use exceptions, we must manually check after each call QScriptValue validation = data->validationFunc(); QVERIFY(validation.isValid()); QVERIFY(validation.isFunction()); QCOMPARE(validation.toString(), validationFunction); } void JsParserTest::testName_data() { QTest::addColumn("code"); QTest::addColumn("checkFunction"); Q_FOREACH(const JsTestData& data, allData) { QString codeStr = QStringLiteral("%1.set({name: \"expectedName\"});"); QTest::newRow((data.tag + "-set()").constData()) << codeStr.arg(data.constructorCall) << data.check; codeStr = QStringLiteral("var obj = %1;obj.name = \"expectedName\"; obj;"); QTest::newRow((data.tag + "-property assignment").constData()) << codeStr.arg(data.constructorCall) << data.check; } } void JsParserTest::testName() { DataInformation* data = 0; testCommon(&data); if (QTest::currentTestFailed()) return; //Qt doesn't use exceptions, we must manually check after each call QCOMPARE(data->name(), QString(QStringLiteral("expectedName"))); } void JsParserTest::testCustomTypeName_data() { QTest::addColumn("code"); QTest::addColumn("checkFunction"); Q_FOREACH(const JsTestData& data, allData) { QString codeStr = QStringLiteral("%1.set({typeName: 'myCustomType'});"); QTest::newRow((data.tag + "-set()").constData()) << codeStr.arg(data.constructorCall) << data.check; codeStr = QStringLiteral("var obj = %1;obj.typeName = 'myCustomType'; obj;"); QTest::newRow((data.tag + "-property assignment").constData()) << codeStr.arg(data.constructorCall) << data.check; } } void JsParserTest::testCustomTypeName() { DataInformation* data = 0; testCommon(&data); if (QTest::currentTestFailed()) return; //Qt doesn't use exceptions, we must manually check after each call QCOMPARE(data->typeName(), QString(QStringLiteral("myCustomType"))); } void JsParserTest::testImport() { QScopedPointer eng(ScriptEngineInitializer::newEngine()); QScriptValue val = eng->evaluate(QStringLiteral("s = importScript('simpleImport.js');s.foo()")); QCOMPARE(val.toString(), QString(QStringLiteral("100"))); } void JsParserTest::testImportPathTraversal() { QScopedPointer eng(ScriptEngineInitializer::newEngine()); QScriptValue val = eng->evaluate(QStringLiteral("s = importScript('../../pathtraversal.js');s.foo()")); QVERIFY(val.isError()); QCOMPARE(val.toString(), QString(QStringLiteral("Error: importScript(): You may only access installed structure files! Path traversal detected."))); } QTEST_GUILESS_MAIN(JsParserTest) #include "jsparsertest.moc" diff --git a/libs/kasten/core/tests/documentmanagertest.cpp b/libs/kasten/core/tests/documentmanagertest.cpp index 223f633c..a1b2ceb0 100644 --- a/libs/kasten/core/tests/documentmanagertest.cpp +++ b/libs/kasten/core/tests/documentmanagertest.cpp @@ -1,117 +1,116 @@ /* This file is part of the Kasten Framework, made within the KDE community. Copyright 2007,2011 Friedrich W. H. Kossebau This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "documentmanagertest.h" // sut #include // test #include // Qt #include #include Q_DECLARE_METATYPE( QList ) void DocumentManagerTest::checkAdded( QSignalSpy* changedSpy, Kasten::AbstractDocument* document ) { QVERIFY( changedSpy->isValid() ); QCOMPARE( changedSpy->count(), 1 ); const QList arguments = changedSpy->takeFirst(); QCOMPARE( arguments.count(), 1 ); - const QVariant firstArgument = arguments.at(0); const QList documents = arguments.at(0).value >(); QCOMPARE( documents.count(), 1 ); QCOMPARE( documents.at(0), document ); } void DocumentManagerTest::checkRemoving( QSignalSpy* changedSpy, Kasten::AbstractDocument* document ) { QVERIFY( changedSpy->isValid() ); QCOMPARE( changedSpy->count(), 1 ); const QList arguments = changedSpy->takeFirst(); QCOMPARE( arguments.count(), 1 ); const QList documents = arguments.at(0).value >(); QCOMPARE( documents.count(), 1 ); QCOMPARE( documents.at(0), document ); } void DocumentManagerTest::initTestCase() { qRegisterMetaType >("QList"); } void DocumentManagerTest::testConstructor() { Kasten::DocumentManager* documentManager = new Kasten::DocumentManager(); delete documentManager; } void DocumentManagerTest::testAddRemove() { Kasten::TestDocument* doc1 = new Kasten::TestDocument(); Kasten::TestDocument* doc2 = new Kasten::TestDocument(); Kasten::TestDocument* doc3 = new Kasten::TestDocument(); Kasten::DocumentManager* documentManager = new Kasten::DocumentManager(); QSignalSpy* addedSpy = new QSignalSpy( documentManager, SIGNAL(added(QList)) ); QSignalSpy* closingSpy = new QSignalSpy( documentManager, SIGNAL(closing(QList)) ); documentManager->addDocument( doc1 ); checkAdded( addedSpy, doc1 ); documentManager->closeDocument( doc1 ); checkAdded( closingSpy, doc1 ); documentManager->addDocument( doc2 ); checkAdded( addedSpy, doc2 ); documentManager->addDocument( doc3 ); checkAdded( addedSpy, doc3 ); documentManager->closeDocument( doc3 ); checkAdded( closingSpy, doc3 ); documentManager->closeDocument( doc2 ); checkAdded( closingSpy, doc2 ); delete documentManager; delete addedSpy; delete closingSpy; } void DocumentManagerTest::testCanClose() { Kasten::TestDocument* doc = new Kasten::TestDocument(); Kasten::DocumentManager* documentManager = new Kasten::DocumentManager(); documentManager->addDocument( doc ); QVERIFY( documentManager->canClose(doc) ); // doc->setSyncStates( Kasten::LocalHasChanges ); // QVERIFY( !documentManager->canClose(doc) ); delete documentManager; } QTEST_MAIN( DocumentManagerTest ) diff --git a/libs/kasten/gui/controller/togglebutton_p.cpp b/libs/kasten/gui/controller/togglebutton_p.cpp index 44d1e94b..80a22cb2 100644 --- a/libs/kasten/gui/controller/togglebutton_p.cpp +++ b/libs/kasten/gui/controller/togglebutton_p.cpp @@ -1,85 +1,84 @@ /* This file is part of the Kasten Framework, made within the KDE community. Copyright 2009 Friedrich W. H. Kossebau This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "togglebutton_p.h" #include "togglebutton.h" namespace Kasten { ToggleButtonPrivate::ToggleButtonPrivate( ToggleButton* parent ) : p( parent ) { p->setCheckable( true ); p->setAutoRaise( true ); p->connect( p, SIGNAL(toggled(bool)), SLOT(onToggled(bool)) ); } void ToggleButtonPrivate::setOtherState( const QIcon& icon, const QString& text, const QString& toolTip ) { - const QFontMetrics metrics = p->fontMetrics(); mOtherIcon = icon; mOtherText = text; mOtherToolTip = toolTip; if( !text.isEmpty() ) { const QString currentText = p->text(); const int currentTextWidth = p->sizeHint().width(); p->setText( text ); const int otherTextWidth = p->sizeHint().width(); p->setText( currentText ); // TODO: this breaks on new font (size) or style change // better would be to reimplement sizeHint() p->setFixedWidth( qMax(currentTextWidth,otherTextWidth) ); } } void ToggleButtonPrivate::onToggled( bool ) { const QIcon otherIcon = mOtherIcon; if( !otherIcon.isNull() ) { mOtherIcon = p->icon(); p->setIcon( otherIcon ); } const QString otherText = mOtherText; if( !otherText.isEmpty() ) { mOtherText = p->text(); p->setText( otherText ); } const QString otherToolTip = mOtherToolTip; if( !otherToolTip.isEmpty() ) { mOtherToolTip = p->toolTip(); p->setToolTip( otherToolTip ); } } }