1
+ using System . Linq . Expressions ;
2
+ using JasperFx . Core . Descriptions ;
3
+ using JasperFx . Core . Reflection ;
4
+ using Shouldly ;
5
+
6
+ namespace JasperFx . Core . Tests . Descriptions ;
7
+
8
+ public class reading_descriptions
9
+ {
10
+ public readonly Target theTarget = new ( ) ;
11
+
12
+ private OptionsValue read ( Expression < Func < Target , object > > expression )
13
+ {
14
+ return OptionsValue . Read ( theTarget , expression ) ;
15
+ }
16
+
17
+ [ Fact ]
18
+ public void read_a_string ( )
19
+ {
20
+ // Our dog's name who's sleeping in my office...
21
+ theTarget . Name = "Chewie" ;
22
+
23
+ var property = read ( x => x . Name ) ;
24
+ property . Name . ShouldBe ( "Name" ) ;
25
+ property . Type . ShouldBe ( PropertyType . Text ) ;
26
+ property . Subject = $ "{ typeof ( Target ) . FullNameInCode ( ) } .{ nameof ( Target . Name ) } ";
27
+ property . RawValue . ShouldBe ( theTarget . Name ) ;
28
+ property . Value . ShouldBe ( "Chewie" ) ;
29
+ }
30
+
31
+ [ Fact ]
32
+ public void read_a_null_value ( )
33
+ {
34
+ theTarget . Name = null ;
35
+
36
+ var property = read ( x => x . Name ) ;
37
+ property . Name . ShouldBe ( "Name" ) ;
38
+ property . Type . ShouldBe ( PropertyType . None ) ;
39
+ property . Subject = $ "{ typeof ( Target ) . FullNameInCode ( ) } .{ nameof ( Target . Name ) } ";
40
+ property . RawValue . ShouldBe ( null ) ;
41
+ property . Value . ShouldBe ( "None" ) ;
42
+ }
43
+
44
+ [ Fact ]
45
+ public void read_an_integer ( )
46
+ {
47
+ var property = read ( x => x . Age ) ;
48
+ property . Name . ShouldBe ( "Age" ) ;
49
+ property . Type . ShouldBe ( PropertyType . Numeric ) ;
50
+ property . Subject = $ "{ typeof ( Target ) . FullNameInCode ( ) } .{ nameof ( Target . Age ) } ";
51
+ property . RawValue . ShouldBe ( theTarget . Age ) ;
52
+ property . Value . ShouldBe ( theTarget . Age . ToString ( ) ) ;
53
+ }
54
+
55
+ [ Fact ]
56
+ public void read_an_enum ( )
57
+ {
58
+ var property = read ( x => x . Color ) ;
59
+ property . Name . ShouldBe ( "Color" ) ;
60
+ property . Type . ShouldBe ( PropertyType . Enum ) ;
61
+ property . Subject = $ "{ typeof ( Target ) . FullNameInCode ( ) } .{ nameof ( Target . Color ) } ";
62
+ property . RawValue . ShouldBe ( theTarget . Color ) ;
63
+ property . Value . ShouldBe ( theTarget . Color . ToString ( ) ) ;
64
+ }
65
+
66
+ [ Fact ]
67
+ public void read_a_boolean ( )
68
+ {
69
+ theTarget . IsTrue = true ;
70
+ var property = read ( x => x . IsTrue ) ;
71
+ property . Name . ShouldBe ( "IsTrue" ) ;
72
+ property . Type . ShouldBe ( PropertyType . Boolean ) ;
73
+ property . Subject = $ "{ typeof ( Target ) . FullNameInCode ( ) } .{ nameof ( Target . IsTrue ) } ";
74
+ property . RawValue . ShouldBe ( theTarget . IsTrue ) ;
75
+ property . Value . ShouldBe ( theTarget . IsTrue . ToString ( ) ) ;
76
+ }
77
+
78
+ [ Fact ]
79
+ public void read_a_uri ( )
80
+ {
81
+ theTarget . IsTrue = true ;
82
+ var property = read ( x => x . Uri ) ;
83
+ property . Name . ShouldBe ( "Uri" ) ;
84
+ property . Type . ShouldBe ( PropertyType . Uri ) ;
85
+ property . Subject = $ "{ typeof ( Target ) . FullNameInCode ( ) } .{ nameof ( Target . Uri ) } ";
86
+ property . RawValue . ShouldBe ( theTarget . Uri ) ;
87
+ property . Value . ShouldBe ( theTarget . Uri . ToString ( ) ) ;
88
+ }
89
+
90
+ [ Fact ]
91
+ public void read_a_time_span ( )
92
+ {
93
+ theTarget . IsTrue = true ;
94
+ var property = read ( x => x . Duration ) ;
95
+ property . Name . ShouldBe ( "Duration" ) ;
96
+ property . Type . ShouldBe ( PropertyType . TimeSpan ) ;
97
+ property . Subject = $ "{ typeof ( Target ) . FullNameInCode ( ) } .{ nameof ( Target . Duration ) } ";
98
+ property . RawValue . ShouldBe ( theTarget . Duration ) ;
99
+ property . Value . ShouldBe ( theTarget . Duration . ToDisplay ( ) ) ;
100
+ }
101
+
102
+ [ Fact ]
103
+ public void read_in_description ( )
104
+ {
105
+ theTarget . Name = "Shiner" ; // our previous family dog
106
+ var description = new OptionsDescription ( theTarget ) ;
107
+
108
+ description . Properties . Select ( x => x . Name )
109
+ . ToArray ( )
110
+ . ShouldBe ( new string [ ] { "Name" , "IsTrue" , "Age" , "Color" , "Uri" , "Duration" } ) ;
111
+
112
+ description . Children [ "YesThis" ] . Properties . Select ( x => x . Name )
113
+ . ShouldHaveTheSameElementsAs ( "Number" , "Suffix" ) ;
114
+ }
115
+ }
116
+
117
+ public enum Color
118
+ {
119
+ Red , Blue , Green
120
+ }
121
+
122
+ public class Target
123
+ {
124
+ public string Name { get ; set ; }
125
+ public bool IsTrue { get ; set ; }
126
+ public int Age { get ; set ; } = 51 ;
127
+ public Color Color { get ; set ; } = Color . Blue ;
128
+ public Uri Uri { get ; set ; } = "local://durable" . ToUri ( ) ;
129
+
130
+ public TimeSpan Duration { get ; set ; } = 25 . Milliseconds ( ) ;
131
+
132
+ // I want this skipped
133
+ public string [ ] Strings { get ; set ; }
134
+
135
+ [ IgnoreDescription ]
136
+ public Thing NotThis { get ; set ; }
137
+
138
+ [ ChildDescription ]
139
+ public Thing YesThis { get ; set ; } = new Thing
140
+ {
141
+ Number = 4 , Suffix = "Jr"
142
+ } ;
143
+ }
144
+
145
+ public class Thing
146
+ {
147
+ public int Number { get ; set ; } = 5 ;
148
+ public string Suffix { get ; set ; } = "Esq." ;
149
+ }
0 commit comments