1+ using System . IO ;
2+ using AET . SteamAbstraction . Vdf ;
3+ using AET . SteamAbstraction . Vdf . Linq ;
4+ using Xunit ;
5+
6+ namespace AET . SteamAbstraction . Test . Vdf ;
7+
8+ public class VdfConvertFacts
9+ {
10+ [ Fact ]
11+ public void EmptyStringThrowsException ( )
12+ {
13+ Assert . Throws < VdfException > ( ( ) => VdfConvert . Deserialize ( TextReader . Null ) ) ;
14+ }
15+
16+ [ Fact ]
17+ public void CommentsDeserializeCorrectly ( )
18+ {
19+ const string vdf =
20+ """
21+ // Comment type A (at the start of the file)
22+ "root"
23+ {
24+ // Comment type B (as a child to an object)
25+ key1 "value1"
26+ "key2" // Comment type C (to the right of a property name)
27+ {
28+ "key3" "value3" // Comment type D (to the right of a property value)
29+ }
30+ }
31+ // Comment type E (at the end of the file)
32+ """ ;
33+
34+ using TextReader sr = new StringReader ( vdf ) ;
35+
36+ var result = VdfConvert . Deserialize ( sr ) ;
37+
38+ var expected = new VProperty ( "root" , new VObject
39+ {
40+ VValue . CreateComment ( " Comment type B (as a child to an object)" ) ,
41+ new VProperty ( "key1" , new VValue ( "value1" ) ) ,
42+ new VProperty ( "key2" , new VObject
43+ {
44+ new VProperty ( "key3" , new VValue ( "value3" ) ) ,
45+ VValue . CreateComment ( " Comment type D (to the right of a property value)" ) ,
46+ } ) ,
47+ } ) ;
48+
49+ Assert . True ( VToken . DeepEquals ( result , expected ) ) ;
50+ }
51+
52+ [ Fact ]
53+ public void DoubleSlashInValueDeserializesCorrectly ( )
54+ {
55+ const string vdf =
56+ """
57+ "root"
58+ {
59+ "key1" "//"
60+ }
61+ """ ;
62+
63+ using TextReader sr = new StringReader ( vdf ) ;
64+
65+ var result = VdfConvert . Deserialize ( sr ) ;
66+
67+ var expected = new VProperty ( "root" , new VObject
68+ {
69+ new VProperty ( "key1" , new VValue ( "//" ) ) ,
70+ } ) ;
71+
72+ Assert . True ( VToken . DeepEquals ( result , expected ) ) ;
73+ }
74+
75+ [ Fact ]
76+ public void ObjectWithManyPropertiesDeserializesCorrectly ( )
77+ {
78+ const string vdf =
79+ """
80+ "result"
81+ {
82+ "status" "1"
83+ "items_game_url" "http://media.steampowered.com/apps/440/scripts/items/items_game.140f75a4dad99e92cd86e7092b1516fff18786a6.txt"
84+ "qualities"
85+ {
86+ "Normal" "0"
87+ "rarity1" "1"
88+ "rarity2" "2"
89+ }
90+ "originNames"
91+ {
92+ "vintage" "3"
93+ "rarity3" "4"
94+ "rarity4" "5"
95+ }
96+ }
97+ """ ;
98+
99+ using TextReader sr = new StringReader ( vdf ) ;
100+
101+ var result = VdfConvert . Deserialize ( sr ) ;
102+
103+ var expected = new VProperty ( "result" , new VObject
104+ {
105+ new VProperty ( "status" , new VValue ( "1" ) ) ,
106+ new VProperty ( "items_game_url" , new VValue ( "http://media.steampowered.com/apps/440/scripts/items/items_game.140f75a4dad99e92cd86e7092b1516fff18786a6.txt" ) ) ,
107+ new VProperty ( "qualities" , new VObject
108+ {
109+ new VProperty ( "Normal" , new VValue ( "0" ) ) ,
110+ new VProperty ( "rarity1" , new VValue ( "1" ) ) ,
111+ new VProperty ( "rarity2" , new VValue ( "2" ) ) ,
112+ } ) ,
113+ new VProperty ( "originNames" , new VObject
114+ {
115+ new VProperty ( "vintage" , new VValue ( "3" ) ) ,
116+ new VProperty ( "rarity3" , new VValue ( "4" ) ) ,
117+ new VProperty ( "rarity4" , new VValue ( "5" ) ) ,
118+ } )
119+ } ) ;
120+
121+ Assert . True ( VToken . DeepEquals ( result , expected ) ) ;
122+ }
123+
124+ [ Fact ]
125+ public void ConditionalsDeserializeCorrectly ( )
126+ {
127+ const string vdf =
128+ """
129+ "root"
130+ {
131+ "key1" "//" [!$WIN32&&!$OSX&&!$PS3]
132+ "key2" "some value containing [!$WIN32&&!$OSX&&!$PS3]" [$WIN32||$OSX||$PS3]
133+ "key3" "//"
134+ }
135+ """ ;
136+
137+ using TextReader sr = new StringReader ( vdf ) ;
138+
139+ var result = VdfConvert . Deserialize ( sr ) ;
140+
141+ var expected = new VProperty ( "root" , new VObject
142+ {
143+ new VProperty ( "key1" , new VValue ( "//" ) , new VConditional
144+ {
145+ new VConditional . Token ( VConditional . TokenType . Not ) ,
146+ new VConditional . Token ( VConditional . TokenType . Constant , "WIN32" ) ,
147+ new VConditional . Token ( VConditional . TokenType . And ) ,
148+ new VConditional . Token ( VConditional . TokenType . Not ) ,
149+ new VConditional . Token ( VConditional . TokenType . Constant , "OSX" ) ,
150+ new VConditional . Token ( VConditional . TokenType . And ) ,
151+ new VConditional . Token ( VConditional . TokenType . Not ) ,
152+ new VConditional . Token ( VConditional . TokenType . Constant , "PS3" ) ,
153+ } ) ,
154+ new VProperty ( "key2" , new VValue ( "some value containing [!$WIN32&&!$OSX&&!$PS3]" ) , new VConditional
155+ {
156+ new VConditional . Token ( VConditional . TokenType . Constant , "WIN32" ) ,
157+ new VConditional . Token ( VConditional . TokenType . Or ) ,
158+ new VConditional . Token ( VConditional . TokenType . Constant , "OSX" ) ,
159+ new VConditional . Token ( VConditional . TokenType . Or ) ,
160+ new VConditional . Token ( VConditional . TokenType . Constant , "PS3" ) ,
161+ } ) ,
162+ new VProperty ( "key3" , new VValue ( "//" ) ) ,
163+ } ) ;
164+
165+ Assert . True ( VToken . DeepEquals ( result , expected ) ) ;
166+ }
167+
168+ [ Fact ]
169+ public void ConditionalsEvaluateCorrectly ( )
170+ {
171+ const string vdf =
172+ """
173+ "root"
174+ {
175+ "key1" "//" [!$WIN32&&!$OSX&&!$PS3]
176+ "key2" "//" [$WIN32||$OSX||$PS3]
177+ "key3" "//"
178+ "key4" "//" [$X360]
179+ }
180+ """ ;
181+ var settings = new VdfSerializerSettings { UsesConditionals = true , DefinedConditionals = [ "X360" ] } ;
182+
183+ using TextReader sr = new StringReader ( vdf ) ;
184+
185+ var result = VdfConvert . Deserialize ( sr , settings ) ;
186+
187+ var expected = new VProperty ( "root" , new VObject
188+ {
189+ new VProperty ( "key1" , new VValue ( "//" ) , new VConditional
190+ {
191+ new VConditional . Token ( VConditional . TokenType . Not ) ,
192+ new VConditional . Token ( VConditional . TokenType . Constant , "WIN32" ) ,
193+ new VConditional . Token ( VConditional . TokenType . And ) ,
194+ new VConditional . Token ( VConditional . TokenType . Not ) ,
195+ new VConditional . Token ( VConditional . TokenType . Constant , "OSX" ) ,
196+ new VConditional . Token ( VConditional . TokenType . And ) ,
197+ new VConditional . Token ( VConditional . TokenType . Not ) ,
198+ new VConditional . Token ( VConditional . TokenType . Constant , "PS3" ) ,
199+ } ) ,
200+ new VProperty ( "key3" , new VValue ( "//" ) ) ,
201+ new VProperty ( "key4" , new VValue ( "//" ) , new VConditional
202+ {
203+ new VConditional . Token ( VConditional . TokenType . Constant , "X360" ) ,
204+ } ) ,
205+ } ) ;
206+
207+ Assert . True ( VToken . DeepEquals ( result , expected ) ) ;
208+ }
209+ }
0 commit comments