2
2
using Microsoft . VisualStudio . TestTools . UnitTesting ;
3
3
using Searchlight . Query ;
4
4
using System . Linq ;
5
+ using System . Runtime . Serialization ;
5
6
using System . Threading . Tasks ;
6
7
using Searchlight . Exceptions ;
7
8
using Searchlight . Expressions ;
@@ -509,13 +510,23 @@ public enum TestEnumValueCategory
509
510
Generic = 2 ,
510
511
}
511
512
513
+ public enum TestEnumAttributeValueCategory
514
+ {
515
+ [ EnumMember ( Value = "Not Anything" ) ]
516
+ None = 0 ,
517
+ Special = 1 ,
518
+ Generic = 2 ,
519
+ }
520
+
512
521
[ SearchlightModel ( DefaultSort = "Name ascending" ) ]
513
522
public class TestClassWithEnumValues
514
523
{
515
524
[ SearchlightField ( OriginalName = "field_name" ) ]
516
525
public string Name { get ; set ; }
517
526
[ SearchlightField ( OriginalName = "field_category" ) ]
518
527
public TestEnumValueCategory Category { get ; set ; }
528
+ [ SearchlightField ( OriginalName = "field_category_attribute" ) ]
529
+ public TestEnumAttributeValueCategory AttributeCategory { get ; set ; }
519
530
}
520
531
521
532
@@ -524,11 +535,13 @@ public void TestValidEnumFilters()
524
535
{
525
536
var source = DataSource . Create ( null , typeof ( TestClassWithEnumValues ) , AttributeMode . Strict ) ;
526
537
var columns = source . GetColumnDefinitions ( ) . ToArray ( ) ;
527
- Assert . AreEqual ( 2 , columns . Length ) ;
538
+ Assert . AreEqual ( 3 , columns . Length ) ;
528
539
Assert . AreEqual ( "Name" , columns [ 0 ] . FieldName ) ;
529
540
Assert . AreEqual ( typeof ( string ) , columns [ 0 ] . FieldType ) ;
530
541
Assert . AreEqual ( "Category" , columns [ 1 ] . FieldName ) ;
531
542
Assert . AreEqual ( typeof ( TestEnumValueCategory ) , columns [ 1 ] . FieldType ) ;
543
+ Assert . AreEqual ( "AttributeCategory" , columns [ 2 ] . FieldName ) ;
544
+ Assert . AreEqual ( typeof ( TestEnumAttributeValueCategory ) , columns [ 2 ] . FieldType ) ;
532
545
533
546
// Query for a valid category
534
547
var syntax1 = source . ParseFilter ( "category = None" ) ;
@@ -537,12 +550,26 @@ public void TestValidEnumFilters()
537
550
// Query using the raw integer value, which is generally not advised but we accept it for historical reasons
538
551
var syntax2 = source . ParseFilter ( "category = 0" ) ;
539
552
Assert . IsNotNull ( syntax2 ) ;
553
+
554
+ // Query attribute value
555
+ var syntax3 = source . ParseFilter ( "attributecategory = 'Not Anything'" ) ;
556
+ Assert . IsNotNull ( syntax3 ) ;
557
+
558
+ // Query non attribute value in mixed enum
559
+ var syntax4 = source . ParseFilter ( "attributecategory = Special" ) ;
560
+ Assert . IsNotNull ( syntax4 ) ;
540
561
541
562
// Query for a non-valid category
542
563
var ex2 = Assert . ThrowsException < InvalidToken > ( ( ) => source . ParseFilter ( "category = InvalidValue" ) ) ;
543
564
Assert . AreEqual ( "InvalidValue" , ex2 . BadToken ) ;
544
565
CollectionAssert . AreEqual ( new string [ ] { "None" , "Special" , "Generic" } , ex2 . ExpectedTokens ) ;
545
566
Assert . AreEqual ( "The filter statement contained an unexpected token, 'InvalidValue'. Searchlight expects to find one of these next: None, Special, Generic" , ex2 . ErrorMessage ) ;
567
+
568
+ // Query for a non-valid category in attribute enum
569
+ var ex3 = Assert . ThrowsException < InvalidToken > ( ( ) => source . ParseFilter ( "attributecategory = InvalidValue" ) ) ;
570
+ Assert . AreEqual ( "InvalidValue" , ex3 . BadToken ) ;
571
+ CollectionAssert . AreEqual ( new string [ ] { "Not Anything" , "Special" , "Generic" } , ex3 . ExpectedTokens ) ;
572
+ Assert . AreEqual ( "The filter statement contained an unexpected token, 'InvalidValue'. Searchlight expects to find one of these next: Not Anything, Special, Generic" , ex3 . ErrorMessage ) ;
546
573
}
547
574
548
575
[ SearchlightModel ( DefaultSort = nameof ( Name ) ) ]
0 commit comments