-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTaskSeq.DebugUtils.Tests.fs
More file actions
145 lines (121 loc) · 6.04 KB
/
TaskSeq.DebugUtils.Tests.fs
File metadata and controls
145 lines (121 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
module TaskSeq.Tests.DebugUtils
open Xunit
open FsUnit.Xunit
open System
open System.IO
open System.Threading
open FSharp.Control
//
// Tests for FSharp.Control.Debug logging functionality
//
module VerboseSettings =
[<Fact>]
let ``Debug verbose setting defaults to false when env var not set`` () =
// Clear environment variable
Environment.SetEnvironmentVariable("TASKSEQ_LOG_VERBOSE", null)
// Force reinit of the setting by accessing private field via reflection
let debugType = typeof<Debug>
let verboseField = debugType.GetField("verbose", System.Reflection.BindingFlags.NonPublic ||| System.Reflection.BindingFlags.Static)
verboseField.SetValue(null, None)
// Test by calling logInfo and verifying no output in release builds
Debug.logInfo("test message")
// In release builds, this should be a no-op
// In debug builds, we can't easily capture console output, but the method should not throw
Assert.True(true) // Test passes if no exception thrown
[<Theory>]
[<InlineData("1", true)>]
[<InlineData("true", true)>]
[<InlineData("TRUE", true)>]
[<InlineData("on", true)>]
[<InlineData("ON", true)>]
[<InlineData("yes", true)>]
[<InlineData("YES", true)>]
[<InlineData("0", false)>]
[<InlineData("false", false)>]
[<InlineData("off", false)>]
[<InlineData("no", false)>]
[<InlineData("invalid", false)>]
[<InlineData("", false)>]
[<InlineData(" 1 ", true)>]
let ``Debug verbose setting parses environment variable correctly`` envValue expectedResult =
// Set environment variable
Environment.SetEnvironmentVariable("TASKSEQ_LOG_VERBOSE", envValue)
try
// Force reinit of the setting by accessing private field via reflection
let debugType = typeof<Debug>
let verboseField = debugType.GetField("verbose", System.Reflection.BindingFlags.NonPublic ||| System.Reflection.BindingFlags.Static)
verboseField.SetValue(null, None)
// Test by calling getVerboseSetting via reflection
let getVerboseMethod = debugType.GetMethod("getVerboseSetting", System.Reflection.BindingFlags.NonPublic ||| System.Reflection.BindingFlags.Static)
let result = getVerboseMethod.Invoke(null, [||]) :?> bool
result |> should equal expectedResult
finally
// Clean up
Environment.SetEnvironmentVariable("TASKSEQ_LOG_VERBOSE", null)
[<Fact>]
let ``Debug verbose setting caches result after first call`` () =
// Set environment variable
Environment.SetEnvironmentVariable("TASKSEQ_LOG_VERBOSE", "1")
try
// Force reinit of the setting
let debugType = typeof<Debug>
let verboseField = debugType.GetField("verbose", System.Reflection.BindingFlags.NonPublic ||| System.Reflection.BindingFlags.Static)
verboseField.SetValue(null, None)
// Get verbose setting via reflection
let getVerboseMethod = debugType.GetMethod("getVerboseSetting", System.Reflection.BindingFlags.NonPublic ||| System.Reflection.BindingFlags.Static)
let result1 = getVerboseMethod.Invoke(null, [||]) :?> bool
// Change environment variable
Environment.SetEnvironmentVariable("TASKSEQ_LOG_VERBOSE", "0")
// Call again - should return cached result
let result2 = getVerboseMethod.Invoke(null, [||]) :?> bool
result1 |> should equal true
result2 |> should equal true // Should be cached value, not re-read from env
finally
// Clean up
Environment.SetEnvironmentVariable("TASKSEQ_LOG_VERBOSE", null)
module LoggingMethods =
[<Fact>]
let ``Debug logInfo with single parameter does not throw`` () =
Debug.logInfo("Simple log message")
// Test passes if no exception thrown
Assert.True(true)
[<Fact>]
let ``Debug logInfo with data parameter does not throw`` () =
Debug.logInfo("Log message with data: ", 42)
// Test passes if no exception thrown
Assert.True(true)
[<Fact>]
let ``Debug logInfo with null string does not throw`` () =
Debug.logInfo(null)
// Test passes if no exception thrown
Assert.True(true)
[<Fact>]
let ``Debug logInfo with empty string does not throw`` () =
Debug.logInfo("")
// Test passes if no exception thrown
Assert.True(true)
[<Fact>]
let ``Debug logInfo with complex data does not throw`` () =
let complexData = {| Name = "test"; Values = [1; 2; 3] |}
Debug.logInfo("Complex data: ", complexData)
// Test passes if no exception thrown
Assert.True(true)
module ExceptionHandling =
[<Fact>]
let ``Debug handles environment variable access exceptions gracefully`` () =
// This test verifies that if Environment.GetEnvironmentVariable throws,
// the Debug class handles it gracefully and defaults to false
// We can't easily force an exception here, but we can test the method doesn't throw
try
// Force reinit and call getVerboseSetting
let debugType = typeof<Debug>
let verboseField = debugType.GetField("verbose", System.Reflection.BindingFlags.NonPublic ||| System.Reflection.BindingFlags.Static)
verboseField.SetValue(null, None)
let getVerboseMethod = debugType.GetMethod("getVerboseSetting", System.Reflection.BindingFlags.NonPublic ||| System.Reflection.BindingFlags.Static)
let result = getVerboseMethod.Invoke(null, [||]) :?> bool
// Should not throw and should return a boolean value
result |> should be instanceOfType<bool>
with
| ex ->
// Test should fail if an exception is thrown
Assert.Fail($"Debug.getVerboseSetting should not throw exceptions: {ex.Message}")