This repository was archived by the owner on Dec 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +87
-3
lines changed
tests/ServiceStack.Text.Tests Expand file tree Collapse file tree 4 files changed +87
-3
lines changed Original file line number Diff line number Diff line change @@ -140,7 +140,11 @@ static Env()
140140 public static bool StrictMode
141141 {
142142 get => strictMode ;
143- set => JsConfig . ThrowOnError = strictMode = value ;
143+ set
144+ {
145+ strictMode = value ;
146+ if ( ! JsConfig . HasInit ) JsConfig . ThrowOnError = value ;
147+ }
144148 }
145149
146150 public static string ServerUserAgent { get ; set ; }
Original file line number Diff line number Diff line change @@ -25,6 +25,20 @@ static JsConfig()
2525 // force deterministic initialization of static constructor
2626 public static void InitStatics ( ) { }
2727
28+ /// <summary>
29+ /// Mark JsConfig global config as initialized and assert it's no longer mutated
30+ /// </summary>
31+ /// <param name="config"></param>
32+ public static void Init ( ) => Config . Init ( ) ;
33+
34+ /// <summary>
35+ /// Initialize global config and assert that it's no longer mutated
36+ /// </summary>
37+ /// <param name="config"></param>
38+ public static void Init ( Config config ) => Config . Init ( config ) ;
39+
40+ public static bool HasInit => Config . HasInit ;
41+
2842 public static JsConfigScope BeginScope ( )
2943 {
3044 return new JsConfigScope ( ) ; //Populated with Config.Instance
Original file line number Diff line number Diff line change @@ -51,9 +51,26 @@ public static Config AssertNotInit() => HasInit
5151 ? throw new NotSupportedException ( "JsConfig can't be mutated after JsConfig.Init(). Use BeginScope() or CreateScope() to use custom config after Init()." )
5252 : Instance ;
5353
54- public static void Init ( ) => HasInit = true ;
54+ private static string InitStackTrace = null ;
5555
56- internal static void Reset ( ) => Instance . Populate ( Defaults ) ;
56+ public static void Init ( ) => Init ( null ) ;
57+ public static void Init ( Config config )
58+ {
59+ if ( HasInit && Env . StrictMode )
60+ throw new NotSupportedException ( $ "JsConfig has already been initialized at: { InitStackTrace } ") ;
61+
62+ if ( config != null )
63+ instance = config ;
64+
65+ HasInit = true ;
66+ InitStackTrace = Environment . StackTrace ;
67+ }
68+
69+ internal static void Reset ( )
70+ {
71+ HasInit = false ;
72+ Instance . Populate ( Defaults ) ;
73+ }
5774
5875 public Config ( )
5976 {
Original file line number Diff line number Diff line change @@ -244,4 +244,53 @@ public void Does_create_scope_from_string_using_CamelCaseHumps()
244244 scope . Dispose ( ) ;
245245 }
246246 }
247+
248+ public class JsConfigInitTests
249+ {
250+ [ TearDown ] public void TearDown ( ) => JsConfig . Reset ( ) ;
251+
252+ [ Test ]
253+ public void Allows_setting_config_before_Init ( )
254+ {
255+ JsConfig . MaxDepth = 1 ;
256+ JsConfig . Init ( new Config {
257+ DateHandler = DateHandler . UnixTime
258+ } ) ;
259+ }
260+
261+ [ Test ]
262+ public void Does_not_allow_setting_JsConfig_after_Init ( )
263+ {
264+ JsConfig . Init ( new Config {
265+ DateHandler = DateHandler . UnixTime
266+ } ) ;
267+
268+ Assert . Throws < NotSupportedException > ( ( ) => JsConfig . MaxDepth = 1000 ) ;
269+ }
270+
271+ [ Test ]
272+ public void Does_not_allow_setting_multiple_inits_in_StrictMode ( )
273+ {
274+ JsConfig . Init ( ) ;
275+ JsConfig . Init ( new Config { MaxDepth = 1 } ) ;
276+
277+ Env . StrictMode = true ;
278+
279+ Assert . Throws < NotSupportedException > ( ( ) => JsConfig . Init ( ) ) ;
280+ }
281+
282+ [ Test ]
283+ public void Does_combine_global_configs_in_multiple_inits ( )
284+ {
285+ JsConfig . Init ( new Config { MaxDepth = 1 } ) ;
286+ JsConfig . Init ( new Config { DateHandler = DateHandler . UnixTime } ) ;
287+
288+ Assert . That ( JsConfig . MaxDepth , Is . EqualTo ( 1 ) ) ;
289+ Assert . That ( JsConfig . DateHandler , Is . EqualTo ( DateHandler . UnixTime ) ) ;
290+
291+ var newConfig = new Config ( ) ;
292+ Assert . That ( newConfig . MaxDepth , Is . EqualTo ( 1 ) ) ;
293+ Assert . That ( newConfig . DateHandler , Is . EqualTo ( DateHandler . UnixTime ) ) ;
294+ }
295+ }
247296}
You can’t perform that action at this time.
0 commit comments