Skip to content

Commit 911632e

Browse files
thomhurstclaude
andauthored
refactor: Consolidate test source code to eliminate DRY violations (#1683)
* refactor: consolidate test source code to eliminate DRY violations Extract duplicated test source code patterns across analyzer and serialization tests: - Create TestSourceConstants.cs with shared module header strings for analyzer tests - Create SerializationTestModels.cs with shared test model and values for JSON/XML/YAML tests - Update 7 analyzer test files to use shared TestSourceConstants - Update JsonTests, XmlTests, YamlTests to use shared SerializationTestModels Fixes #1634, fixes #1627 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: Exclude null Items from JSON serialization in test model Add JsonIgnore attribute with WhenWritingNull condition to prevent the Items property from appearing as null in serialized JSON output. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3fe70b4 commit 911632e

12 files changed

Lines changed: 426 additions & 751 deletions

src/ModularPipelines.Analyzers/ModularPipelines.Analyzers.Test/ModularPipelinesAnalyzersAsyncModulesUnitTests.cs

Lines changed: 55 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -7,181 +7,108 @@ namespace ModularPipelines.Analyzers.Test;
77
[TestClass]
88
public class ModularPipelinesAnalyzersAsyncModulesUnitTests
99
{
10-
private const string BadModuleSource = @"
11-
#nullable enable
12-
using System;
13-
using System.Linq;
14-
using System.Threading;
15-
using System.Threading.Tasks;
16-
using System.Collections.Generic;
17-
using ModularPipelines.Context;
18-
using ModularPipelines.Models;
19-
using ModularPipelines.Options;
20-
using ModularPipelines.Modules;
21-
using ModularPipelines.Attributes;
22-
23-
namespace ModularPipelines.Examples.Modules;
10+
private const string BadModuleSource = $@"
11+
{TestSourceConstants.StandardModuleHeaderWithOptions}
2412
2513
public class Module1 : Module<CommandResult>
26-
{
27-
{|#0:protected override Task<CommandResult?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
28-
{
14+
{{
15+
{{|#0:protected override Task<CommandResult?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
16+
{{
2917
return ExecuteCommand(context);
30-
}|}
18+
}}|}}
3119
3220
private async Task<CommandResult?> ExecuteCommand(IPipelineContext context)
33-
{
21+
{{
3422
return await context.Command.ExecuteCommandLineTool(new CommandLineToolOptions(""git""));
35-
}
36-
}
23+
}}
24+
}}
3725
";
38-
39-
private const string BadModuleSource2 = @"
40-
#nullable enable
41-
using System;
42-
using System.Linq;
43-
using System.Threading;
44-
using System.Threading.Tasks;
45-
using System.Collections.Generic;
46-
using ModularPipelines.Context;
47-
using ModularPipelines.Models;
48-
using ModularPipelines.Options;
49-
using ModularPipelines.Modules;
50-
using ModularPipelines.Attributes;
51-
52-
namespace ModularPipelines.Examples.Modules;
26+
27+
private const string BadModuleSource2 = $@"
28+
{TestSourceConstants.StandardModuleHeaderWithOptions}
5329
5430
public class Module1 : Module<string>
55-
{
56-
{|#0:protected override Task<string?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
57-
{
31+
{{
32+
{{|#0:protected override Task<string?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
33+
{{
5834
if (1 + ""n"" == ""1n"")
59-
{
35+
{{
6036
return ExecuteCommand(context);
61-
}
37+
}}
6238
6339
return Task.FromResult<string?>(""Foo!"");
64-
}|}
40+
}}|}}
6541
6642
private async Task<string?> ExecuteCommand(IPipelineContext context)
67-
{
43+
{{
6844
await Task.Yield();
6945
return ""Foo!"";
70-
}
71-
}
46+
}}
47+
}}
7248
";
7349

74-
private const string GoodModuleSource = @"
75-
#nullable enable
76-
using System;
77-
using System.Linq;
78-
using System.Threading;
79-
using System.Threading.Tasks;
80-
using System.Collections.Generic;
81-
using ModularPipelines.Context;
82-
using ModularPipelines.Models;
83-
using ModularPipelines.Options;
84-
using ModularPipelines.Modules;
85-
using ModularPipelines.Attributes;
86-
87-
namespace ModularPipelines.Examples.Modules;
50+
private const string GoodModuleSource = $@"
51+
{TestSourceConstants.StandardModuleHeaderWithOptions}
8852
8953
public class Module1 : Module<CommandResult>
90-
{
54+
{{
9155
protected override async Task<CommandResult?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
92-
{
56+
{{
9357
return await ExecuteCommand(context);
94-
}
58+
}}
9559
9660
private async Task<CommandResult?> ExecuteCommand(IPipelineContext context)
97-
{
61+
{{
9862
return await context.Command.ExecuteCommandLineTool(new CommandLineToolOptions(""git""));
99-
}
100-
}
63+
}}
64+
}}
10165
";
102-
103-
private const string GoodModuleSource2 = @"
104-
#nullable enable
105-
using System;
106-
using System.Linq;
107-
using System.Threading;
108-
using System.Threading.Tasks;
109-
using System.Collections.Generic;
110-
using ModularPipelines.Context;
111-
using ModularPipelines.Models;
112-
using ModularPipelines.Options;
113-
using ModularPipelines.Modules;
114-
using ModularPipelines.Attributes;
115-
116-
namespace ModularPipelines.Examples.Modules;
66+
67+
private const string GoodModuleSource2 = $@"
68+
{TestSourceConstants.StandardModuleHeaderWithOptions}
11769
11870
public class Module1 : Module<string>
119-
{
71+
{{
12072
protected override Task<string?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
121-
{
73+
{{
12274
return Task.FromResult<string?>(""Foo"");
123-
}
124-
}
75+
}}
76+
}}
12577
";
126-
127-
private const string GoodModuleSource3 = @"
128-
#nullable enable
129-
using System;
130-
using System.Linq;
131-
using System.Threading;
132-
using System.Threading.Tasks;
133-
using System.Collections.Generic;
134-
using ModularPipelines.Context;
135-
using ModularPipelines.Extensions;
136-
using ModularPipelines.Models;
137-
using ModularPipelines.Options;
138-
using ModularPipelines.Modules;
139-
using ModularPipelines.Attributes;
140-
141-
namespace ModularPipelines.Examples.Modules;
78+
79+
private const string GoodModuleSource3 = $@"
80+
{TestSourceConstants.StandardModuleHeaderWithExtensions}
14281
14382
public class Module1 : Module<string>
144-
{
83+
{{
14584
protected override Task<string?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
146-
{
85+
{{
14786
return ""Foo"".AsTask<string?>();
148-
}
149-
}
87+
}}
88+
}}
15089
";
151-
152-
private const string BadModuleSource2Fixed = @"
153-
#nullable enable
154-
using System;
155-
using System.Linq;
156-
using System.Threading;
157-
using System.Threading.Tasks;
158-
using System.Collections.Generic;
159-
using ModularPipelines.Context;
160-
using ModularPipelines.Models;
161-
using ModularPipelines.Options;
162-
using ModularPipelines.Modules;
163-
using ModularPipelines.Attributes;
164-
165-
namespace ModularPipelines.Examples.Modules;
90+
91+
private const string BadModuleSource2Fixed = $@"
92+
{TestSourceConstants.StandardModuleHeaderWithOptions}
16693
16794
public class Module1 : Module<string>
168-
{
169-
{|#0:protected override async Task<string?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
170-
{
95+
{{
96+
{{|#0:protected override async Task<string?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
97+
{{
17198
if (1 + ""n"" == ""1n"")
172-
{
99+
{{
173100
return await ExecuteCommand(context);
174-
}
101+
}}
175102
176103
return ""Foo!"";
177-
}|}
104+
}}|}}
178105
179106
private async Task<string?> ExecuteCommand(IPipelineContext context)
180-
{
107+
{{
181108
await Task.Yield();
182109
return ""Foo!"";
183-
}
184-
}
110+
}}
111+
}}
185112
";
186113

187114
[TestMethod]

0 commit comments

Comments
 (0)