17
17
#include < QObject>
18
18
#include < QString>
19
19
#include < QStringList>
20
+ #include < QLocale>
20
21
21
22
#include < memory>
22
23
@@ -66,12 +67,12 @@ void TestQgsField::cleanupTestCase()
66
67
67
68
void TestQgsField::init ()
68
69
{
69
-
70
+ QLocale::setDefault ( QLocale::English );
70
71
}
71
72
72
73
void TestQgsField::cleanup ()
73
74
{
74
-
75
+ QLocale::setDefault ( QLocale::English );
75
76
}
76
77
77
78
void TestQgsField::create ()
@@ -315,12 +316,12 @@ void TestQgsField::displayString()
315
316
// test int value in int type
316
317
QgsField intField2 ( QStringLiteral ( " int" ), QVariant::Int, QStringLiteral ( " int" ) );
317
318
QCOMPARE ( intField2.displayString ( 5 ), QString ( " 5" ) );
318
- QCOMPARE ( intField2.displayString ( 599999898999LL ), QString ( " 599999898999 " ) );
319
+ QCOMPARE ( intField2.displayString ( 599999898999LL ), QString ( " 599,999,898,999 " ) );
319
320
320
321
// test long type
321
322
QgsField longField ( QStringLiteral ( " long" ), QVariant::LongLong, QStringLiteral ( " longlong" ) );
322
323
QCOMPARE ( longField.displayString ( 5 ), QString ( " 5" ) );
323
- QCOMPARE ( longField.displayString ( 599999898999LL ), QString ( " 599999898999 " ) );
324
+ QCOMPARE ( longField.displayString ( 599999898999LL ), QString ( " 599,999,898,999 " ) );
324
325
325
326
// test NULL int
326
327
QVariant nullInt = QVariant ( QVariant::Int );
@@ -332,12 +333,57 @@ void TestQgsField::displayString()
332
333
QgsField doubleFieldNoPrec ( QStringLiteral ( " double" ), QVariant::Double, QStringLiteral ( " double" ), 10 );
333
334
QCOMPARE ( doubleFieldNoPrec.displayString ( 5.005005 ), QString ( " 5.005005" ) );
334
335
QCOMPARE ( doubleFieldNoPrec.displayString ( 5.005005005 ), QString ( " 5.005005005" ) );
335
- QCOMPARE ( doubleFieldNoPrec.displayString ( 599999898999.0 ), QString ( " 599999898999" ) );
336
+ QCOMPARE ( QLocale ().numberOptions () & QLocale::NumberOption::OmitGroupSeparator, QLocale::NumberOption::DefaultNumberOptions );
337
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 599999898999.0 ), QString ( " 599,999,898,999" ) );
336
338
337
339
// test NULL double
338
340
QVariant nullDouble = QVariant ( QVariant::Double );
339
341
QCOMPARE ( doubleField.displayString ( nullDouble ), QString ( " TEST NULL" ) );
340
342
343
+ // test double value with German locale
344
+ QLocale::setDefault ( QLocale::German );
345
+ QCOMPARE ( doubleField.displayString ( 5.005005 ), QString ( " 5,005" ) );
346
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 5.005005 ), QString ( " 5,005005" ) );
347
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 5.005005005 ), QString ( " 5,005005005" ) );
348
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 599999898999.0 ), QString ( " 599.999.898.999" ) );
349
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 5999.123456 ), QString ( " 5.999,123456" ) );
350
+
351
+ // test value with custom German locale (OmitGroupSeparator)
352
+ QLocale customGerman ( QLocale::German );
353
+ customGerman.setNumberOptions ( QLocale::NumberOption::OmitGroupSeparator );
354
+ QLocale::setDefault ( customGerman );
355
+ QCOMPARE ( doubleField.displayString ( 5.005005 ), QString ( " 5,005" ) );
356
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 5.005005 ), QString ( " 5,005005" ) );
357
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 5.005005005 ), QString ( " 5,005005005" ) );
358
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 599999898999.0 ), QString ( " 599999898999" ) );
359
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 5999.123456 ), QString ( " 5999,123456" ) );
360
+
361
+ // test int value in int type with custom German locale (OmitGroupSeparator)
362
+ QCOMPARE ( intField2.displayString ( 5 ), QString ( " 5" ) );
363
+ QCOMPARE ( intField2.displayString ( 599999898999LL ), QString ( " 599999898999" ) );
364
+
365
+ // test long type with custom German locale (OmitGroupSeparator)
366
+ QCOMPARE ( longField.displayString ( 5 ), QString ( " 5" ) );
367
+ QCOMPARE ( longField.displayString ( 599999898999LL ), QString ( " 599999898999" ) );
368
+
369
+ // test value with custom english locale (OmitGroupSeparator)
370
+ QLocale customEnglish ( QLocale::English );
371
+ customEnglish.setNumberOptions ( QLocale::NumberOption::OmitGroupSeparator );
372
+ QLocale::setDefault ( customEnglish );
373
+ QCOMPARE ( doubleField.displayString ( 5.005005 ), QString ( " 5.005" ) );
374
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 5.005005 ), QString ( " 5.005005" ) );
375
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 5.005005005 ), QString ( " 5.005005005" ) );
376
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 599999898999.0 ), QString ( " 599999898999" ) );
377
+ QCOMPARE ( doubleFieldNoPrec.displayString ( 5999.123456 ), QString ( " 5999.123456" ) );
378
+
379
+ // test int value in int type with custom english locale (OmitGroupSeparator)
380
+ QCOMPARE ( intField2.displayString ( 5 ), QString ( " 5" ) );
381
+ QCOMPARE ( intField2.displayString ( 599999898999LL ), QString ( " 599999898999" ) );
382
+
383
+ // test long type with custom english locale (OmitGroupSeparator)
384
+ QCOMPARE ( longField.displayString ( 5 ), QString ( " 5" ) );
385
+ QCOMPARE ( longField.displayString ( 599999898999LL ), QString ( " 599999898999" ) );
386
+
341
387
}
342
388
343
389
void TestQgsField::convertCompatible ()
@@ -452,6 +498,44 @@ void TestQgsField::convertCompatible()
452
498
QCOMPARE ( longlong.type (), QVariant::LongLong );
453
499
QCOMPARE ( longlong, QVariant ( 99999999999999999LL ) );
454
500
501
+ // string representation of an int
502
+ QVariant stringInt ( " 123456" );
503
+ QVERIFY ( intField.convertCompatible ( stringInt ) );
504
+ QCOMPARE ( stringInt.type (), QVariant::Int );
505
+ QCOMPARE ( stringInt, QVariant ( 123456 ) );
506
+ // now with group separator for english locale
507
+ stringInt = QVariant ( " 123,456" );
508
+ QVERIFY ( intField.convertCompatible ( stringInt ) );
509
+ QCOMPARE ( stringInt.type (), QVariant::Int );
510
+ QCOMPARE ( stringInt, QVariant ( " 123456" ) );
511
+
512
+ // string representation of a longlong
513
+ QVariant stringLong ( " 99999999999999999" );
514
+ QVERIFY ( longlongField.convertCompatible ( stringLong ) );
515
+ QCOMPARE ( stringLong.type (), QVariant::LongLong );
516
+ QCOMPARE ( stringLong, QVariant ( 99999999999999999LL ) );
517
+ // now with group separator for english locale
518
+ stringLong = QVariant ( " 99,999,999,999,999,999" );
519
+ QVERIFY ( longlongField.convertCompatible ( stringLong ) );
520
+ QCOMPARE ( stringLong.type (), QVariant::LongLong );
521
+ QCOMPARE ( stringLong, QVariant ( 99999999999999999LL ) );
522
+
523
+
524
+ // string representation of a double
525
+ QVariant stringDouble ( " 123456.012345" );
526
+ QVERIFY ( doubleField.convertCompatible ( stringDouble ) );
527
+ QCOMPARE ( stringDouble.type (), QVariant::Double );
528
+ QCOMPARE ( stringDouble, QVariant ( 123456.012345 ) );
529
+ // now with group separator for english locale
530
+ stringDouble = QVariant ( " 1,223,456.012345" );
531
+ QVERIFY ( doubleField.convertCompatible ( stringDouble ) );
532
+ QCOMPARE ( stringDouble.type (), QVariant::Double );
533
+ QCOMPARE ( stringDouble, QVariant ( 1223456.012345 ) );
534
+ // This should not convert
535
+ stringDouble = QVariant ( " 1.223.456,012345" );
536
+ QVERIFY ( ! doubleField.convertCompatible ( stringDouble ) );
537
+
538
+
455
539
// double with precision
456
540
QgsField doubleWithPrecField ( QStringLiteral ( " double" ), QVariant::Double, QStringLiteral ( " double" ), 10 , 3 );
457
541
doubleVar = QVariant ( 10.12345678 );
@@ -466,6 +550,62 @@ void TestQgsField::convertCompatible()
466
550
QVERIFY ( !stringWithLen.convertCompatible ( stringVar ) );
467
551
QCOMPARE ( stringVar.type (), QVariant::String );
468
552
QCOMPARE ( stringVar.toString (), QString ( " lon" ) );
553
+
554
+
555
+ // ///////////////////////////////////////////////////////
556
+ // German locale tests
557
+
558
+ // double with ',' as decimal separator for German locale
559
+ QLocale::setDefault ( QLocale::German );
560
+ QVariant doubleCommaVar ( " 1,2345" );
561
+ QVERIFY ( doubleField.convertCompatible ( doubleCommaVar ) );
562
+ QCOMPARE ( doubleCommaVar.type (), QVariant::Double );
563
+ QCOMPARE ( doubleCommaVar.toString (), QString ( " 1.2345" ) );
564
+
565
+ // string representation of an int
566
+ stringInt = QVariant ( " 123456" );
567
+ QVERIFY ( intField.convertCompatible ( stringInt ) );
568
+ QCOMPARE ( stringInt.type (), QVariant::Int );
569
+ QCOMPARE ( stringInt, QVariant ( 123456 ) );
570
+ // now with group separator for german locale
571
+ stringInt = QVariant ( " 123.456" );
572
+ QVERIFY ( intField.convertCompatible ( stringInt ) );
573
+ QCOMPARE ( stringInt.type (), QVariant::Int );
574
+ QCOMPARE ( stringInt, QVariant ( " 123456" ) );
575
+
576
+ // string representation of a longlong
577
+ stringLong = QVariant ( " 99999999999999999" );
578
+ QVERIFY ( longlongField.convertCompatible ( stringLong ) );
579
+ QCOMPARE ( stringLong.type (), QVariant::LongLong );
580
+ QCOMPARE ( stringLong, QVariant ( 99999999999999999LL ) );
581
+ // now with group separator for german locale
582
+ stringLong = QVariant ( " 99.999.999.999.999.999" );
583
+ QVERIFY ( longlongField.convertCompatible ( stringLong ) );
584
+ QCOMPARE ( stringLong.type (), QVariant::LongLong );
585
+ QCOMPARE ( stringLong, QVariant ( 99999999999999999LL ) );
586
+
587
+ // string representation of a double
588
+ stringDouble = QVariant ( " 123456,012345" );
589
+ QVERIFY ( doubleField.convertCompatible ( stringDouble ) );
590
+ QCOMPARE ( stringDouble.type (), QVariant::Double );
591
+ QCOMPARE ( stringDouble, QVariant ( 123456.012345 ) );
592
+ // For doubles we also want to accept dot as a decimal point
593
+ stringDouble = QVariant ( " 123456.012345" );
594
+ QVERIFY ( doubleField.convertCompatible ( stringDouble ) );
595
+ QCOMPARE ( stringDouble.type (), QVariant::Double );
596
+ QCOMPARE ( stringDouble, QVariant ( 123456.012345 ) );
597
+ // now with group separator for german locale
598
+ stringDouble = QVariant ( " 1.223.456,012345" );
599
+ QVERIFY ( doubleField.convertCompatible ( stringDouble ) );
600
+ QCOMPARE ( stringDouble.type (), QVariant::Double );
601
+ QCOMPARE ( stringDouble, QVariant ( 1223456.012345 ) );
602
+ // Be are good citizens and we also accept english locale
603
+ stringDouble = QVariant ( " 1,223,456.012345" );
604
+ QVERIFY ( doubleField.convertCompatible ( stringDouble ) );
605
+ QCOMPARE ( stringDouble.type (), QVariant::Double );
606
+ QCOMPARE ( stringDouble, QVariant ( 1223456.012345 ) );
607
+
608
+
469
609
}
470
610
471
611
void TestQgsField::dataStream ()
0 commit comments