easy.mecket.com | ||
ASP.NET Web PDF Document Viewer/Editor Control LibraryFigure 16-2. A spin box widget The QTEST_MAIN function macro treats unit tests intended to test widgets and those to test other aspects of an application equally. The project file doesn t have to be changed, either. By building and running the unit test shown previously, you get a list of passed test cases. var orderedOrders = orders.OrderBy(order => order.OrderDate); var orderedOrders = from order in orders orderby order.OrderDate select order; free barcode generator for excel 2007, microsoft barcode control 15.0 excel 2010, free barcode font for excel 2003, free barcode add in for excel 2010, how to add barcode font to excel 2007, barcode in excel 2010 freeware, free barcode generator add-in for excel, free barcode add in for excel 2010, barcode generator excel 2007, create barcode in excel,This doesn t execute the orders query. It just means we have two queries now the orders query that just filters, and then the orderedOrders query that filters and then sorts. You could think of this chained query as shorthand for Example 14-11, which explicitly combines the clauses from Example 14-9 and Example 14-10 into one query. You ve run into the same redundancy problem as with the QDate class the unit test of QSpinBox contains a lot of duplicated code. The solution is to convert the tests into data-driven tests, which is done in exactly the same way regardless of the class being tested. All test cases are converted in similar ways, so start by focusing on the testKeys slot. The new version of the slot is shown along with testKeys_data in Listing 16-19. Most of the source code shown in the listing should be clear. However, the two highlighted lines are important. When you add a column of the type Qt::Key, you see a compilation error if you do not declare it as a meta-type. The registration is made by using the Q_DECLARE_METATYPE macro. The test case works like all data-driven tests: It fetches data using QFETCH and uses the data before using QTEST to check the outcome of the test. Listing 16-19. Testing keyboard interaction using a data-driven test case Q_DECLARE_METATYPE( Qt::Key ) void SpinBoxTest::testKeys() { QSpinBox spinBox; spinBox.setRange( 1, 10 ); QFETCH( Qt::Key, key ); QFETCH( int, startValue ); spinBox.setValue( startValue ); QTest::keyClick( &spinBox, key ); QTEST( spinBox.value(), "endValue" ); } void SpinBoxTest::testKeys_data() { QTest::addColumn<Qt::Key>( "key" ); QTest::addColumn<int>( "startValue" ); QTest::addColumn<int>( "endValue" ); QTest::newRow( QTest::newRow( QTest::newRow( QTest::newRow( } void SpinBoxTest::testClicks() { QSpinBox spinBox; spinBox.setRange( 1, 10 ); "Up" ) << Qt::Key_Up << 5 << 6; "Down" ) << Qt::Key_Down << 5 << 4; "Up, limit" ) << Qt::Key_Up << 10 << 10; "Down, limit" ) << Qt::Key_Down << 1 << 1; var orderedOrders = from order in dbContext.SalesOrderHeaders where order.OrderDate == orderDate orderby order.OrderDate select order; Regardless of the various equivalent ways we would build the second query, the result of executing it (e.g., by iterating over it in a foreach loop) is, as you d expect, a SQL query that includes an ORDER BY clause as well as a WHERE clause. (And as it happens, that s not hugely useful because in this example database, all the orders have the exact same date. With slightly more realistic data, this would have the expected effect, though.) So LINQ to Entities queries work in a fundamentally different way from the LINQ to Objects queries we saw previously. In LINQ to Objects, the expression in a where clause is simply a delegate in disguise it s a method that the Where operator calls for each item in turn to work out whether to include that in the results. But with LINQ to Entities (and LINQ to SQL for that matter) the LINQ query s where clause has been translated into T-SQL and sent to the database the expression we wrote in C# ends up running in a different language, probably on a different machine. If you want to understand how these kinds of queries are able to work so differently for different providers, see the sidebar on the next page. This translation is obviously very useful for shifting the work to the database, but it brings some limitations. If you try to add arbitrary method calls into the middle of a LINQ query, it ll fail in LINQ to Entities. For example, suppose we have the following helper: bindings dataContext id accessKey associatedElement behaviors cssClass enabled style tabIndex visible visibilityMode static DateTime NextDay(DateTime dt) { return dt + TimeSpan.FromDays(1); }
|