Repository navigation
Expand file tree
/
Copy pathPropertyBasedTests.cs
More file actions
178 lines (158 loc) · 8.96 KB
/
Copy pathPropertyBasedTests.cs
File metadata and controls
178 lines (158 loc) · 8.96 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.Linq;
using FolderDiffIL4DotNet.Core.Text;
using FolderDiffIL4DotNet.Models;
using FsCheck;
using FsCheck.Fluent;
using FsCheck.Xunit;
using Xunit;
using FluentArbMap = FsCheck.Fluent.ArbMap;
using FluentGen = FsCheck.Fluent.Gen;
namespace FolderDiffIL4DotNet.Tests.PropertyBased
{
/// <summary>
/// Property-based tests using FsCheck to verify invariants across random inputs.
/// FsCheck を使用してランダム入力における不変条件を検証するプロパティベーステスト。
/// </summary>
[Trait("Category", "Unit")]
public sealed class PropertyBasedTests
{
// ── TextDiffer properties / TextDiffer プロパティ ──
[Property(MaxTest = 200, Arbitrary = new[] { typeof(NonNullStringArrayArbitrary) })]
public Property TextDiffer_ContextLineCount_NeverExceedsTotalLines(string[] oldLines, string[] newLines)
{
// Property: the number of context lines in a diff never exceeds max(|old|, |new|)
// プロパティ: 差分中のコンテキスト行数は max(|old|, |new|) を超えない
var diff = TextDiffer.Compute(oldLines, newLines, contextLines: 3, maxOutputLines: 50000, maxEditDistance: 500);
var contextCount = diff.Count(d => d.Kind == TextDiffer.Context);
var maxSource = Math.Max(oldLines.Length, newLines.Length);
return (contextCount <= maxSource).ToProperty();
}
[Property(MaxTest = 200, Arbitrary = new[] { typeof(NonNullStringArrayArbitrary) })]
public Property TextDiffer_IdenticalInputs_ProducesNoChanges(string[] lines)
{
// Property: diff of identical inputs has no additions or removals
// プロパティ: 同一入力の差分には追加/削除がない
var diff = TextDiffer.Compute(lines, lines, contextLines: 0, maxOutputLines: 50000, maxEditDistance: 500);
return diff.All(d => d.Kind != TextDiffer.Added && d.Kind != TextDiffer.Removed).ToProperty();
}
[Property(MaxTest = 200, Arbitrary = new[] { typeof(NonNullStringArrayArbitrary) })]
public Property TextDiffer_Diff_RemovedCountMatchesOldOnlyLines(string[] oldLines, string[] newLines)
{
// Property: applying diff should reconstruct new lines from old lines
// プロパティ: 差分を適用すると old lines から new lines を再構成できる
// Use a context window large enough to include every line so reconstruction is complete.
// 全行を含む十分なコンテキストウィンドウを使用して再構成の完全性を保証する。
var fullContext = oldLines.Length + newLines.Length;
var diff = TextDiffer.Compute(oldLines, newLines, contextLines: fullContext, maxOutputLines: 50000, maxEditDistance: 500);
// If diff was truncated, skip this check / 差分が打ち切られた場合はスキップ
if (diff.Any(d => d.Kind == TextDiffer.Truncated))
return true.ToProperty();
// Reconstruct new lines from diff: context and added lines form the new file
// 差分から新ファイルを再構成: コンテキスト行と追加行が新ファイルを構成
var reconstructed = diff
.Where(d => d.Kind == TextDiffer.Context || d.Kind == TextDiffer.Added)
.Select(d => d.Text)
.ToArray();
return reconstructed.SequenceEqual(newLines).ToProperty();
}
[Property(MaxTest = 100)]
public Property TextDiffer_MaxOutputLines_Respected(PositiveInt maxLines)
{
// Property: non-truncation output lines never exceed maxOutputLines (truncation marker may add 1)
// プロパティ: 非切り詰め出力行数は maxOutputLines を超えない(切り詰めマーカーは +1 される場合あり)
int limit = Math.Min(maxLines.Get, 10000);
var old = Enumerable.Range(0, 50).Select(i => $"old line {i}").ToArray();
var @new = Enumerable.Range(0, 50).Select(i => $"new line {i}").ToArray();
var diff = TextDiffer.Compute(old, @new, contextLines: 3, maxOutputLines: limit, maxEditDistance: 500);
var nonTruncatedCount = diff.Count(d => d.Kind != TextDiffer.Truncated);
return (nonTruncatedCount <= limit).ToProperty();
}
// ── TextSanitizer properties / TextSanitizer プロパティ ──
[Property(MaxTest = 200)]
public Property TextSanitizer_Sanitize_IsIdempotent(NonNull<string> input)
{
// Property: sanitizing twice produces the same result as sanitizing once
// プロパティ: 2回サニタイズしても1回と同じ結果
var once = TextSanitizer.Sanitize(input.Get);
var twice = TextSanitizer.Sanitize(once);
return (once == twice).ToProperty();
}
[Property(MaxTest = 200)]
public Property TextSanitizer_Sanitize_NeverContainsPathSeparators(NonNull<string> input)
{
// Property: sanitized output never contains path separators or colons
// プロパティ: サニタイズ結果にパス区切りやコロンが含まれない
var result = TextSanitizer.Sanitize(input.Get);
return (!result.Contains('\\') && !result.Contains('/') && !result.Contains(':') && !result.Contains("..")).ToProperty();
}
[Property(MaxTest = 200)]
public Property TextSanitizer_Sanitize_PreservesEmptyAndNull()
{
// Property: null and empty strings produce empty output
// プロパティ: null と空文字列は空の出力を返す
return (TextSanitizer.Sanitize(null!) == string.Empty
&& TextSanitizer.Sanitize(string.Empty) == string.Empty).ToProperty();
}
// ── FileDiffResultLists properties / FileDiffResultLists プロパティ ──
[Property(MaxTest = 100)]
public Property FileDiffResultLists_Statistics_SumEqualsTotal(
NonNegativeInt addedCount,
NonNegativeInt removedCount,
NonNegativeInt unchangedCount,
NonNegativeInt modifiedCount)
{
// Property: sum of all categories equals total compared
// プロパティ: 全カテゴリの合計 = 比較合計
int a = Math.Min(addedCount.Get, 100);
int r = Math.Min(removedCount.Get, 100);
int u = Math.Min(unchangedCount.Get, 100);
int m = Math.Min(modifiedCount.Get, 100);
var resultLists = new FileDiffResultLists();
for (int i = 0; i < a; i++) resultLists.AddAddedFileAbsolutePath($"added_{i}.dll");
for (int i = 0; i < r; i++) resultLists.AddRemovedFileAbsolutePath($"removed_{i}.dll");
for (int i = 0; i < u; i++) resultLists.AddUnchangedFileRelativePath($"unchanged_{i}.dll");
for (int i = 0; i < m; i++) resultLists.AddModifiedFileRelativePath($"modified_{i}.dll");
var stats = resultLists.SummaryStatistics;
return (stats.AddedCount == a
&& stats.RemovedCount == r
&& stats.UnchangedCount == u
&& stats.ModifiedCount == m).ToProperty();
}
// ── ConfigSettings round-trip properties / ConfigSettings ラウンドトリッププロパティ ──
[Property(MaxTest = 50)]
public Property ConfigSettings_MaxParallelism_ClampedToNonNegative(int value)
{
// Property: MaxParallelism is always >= 0 after Build (0 = auto-detect CPU cores)
// プロパティ: Build 後の MaxParallelism は常に >= 0(0 = CPUコア数自動検出)
var builder = new ConfigSettingsBuilder { MaxParallelism = value };
var config = builder.Build();
return (config.MaxParallelism == value).ToProperty();
}
[Property(MaxTest = 50)]
public Property ConfigSettings_MaxLogGenerations_DefaultIsPositive()
{
// Property: default MaxLogGenerations is always positive
// プロパティ: デフォルトの MaxLogGenerations は常に正の値
var config = new ConfigSettingsBuilder().Build();
return (config.MaxLogGenerations >= 1).ToProperty();
}
}
/// <summary>
/// Custom arbitrary for generating non-null string arrays with reasonable sizes.
/// 適度なサイズの非 null 文字列配列を生成するカスタム Arbitrary。
/// </summary>
public static class NonNullStringArrayArbitrary
{
public static Arbitrary<string[]> StringArray()
{
return FluentGen.Choose(0, 30)
.SelectMany(len =>
FluentGen.ArrayOf(
FluentArbMap.Default.GeneratorFor<NonNull<string>>().Select(s => s.Get),
len))
.ToArbitrary();
}
}
}