Skip to content

Commit

Permalink
Fix test and bug found when doint tests :)
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed May 7, 2024
1 parent e61dee2 commit fe15b06
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ private static IEnumerable<MemberDeclarationSyntax> GenerateServiceMethod(string

if (serviceArguments is null)
{
targetParam = "object? data";
if (service.Target is not null)
{
targetParam = $"{targetParam}, object? data = null";
}
else
{
targetParam = "object? data = null";

Check warning on line 106 in src/HassModel/NetDaemon.HassModel.CodeGenerator/CodeGeneration/ServicesGenerator.cs

View check run for this annotation

Codecov / codecov/patch

src/HassModel/NetDaemon.HassModel.CodeGenerator/CodeGeneration/ServicesGenerator.cs#L106

Added line #L106 was not covered by tests
}
targetArg = "null, data";
// method without arguments
yield return ParseMemberDeclaration($$"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,61 @@ public void Run(IHaContext ha)
CodeGenTestHelper.AssertCodeCompiles(code.ToString(), appCode);
}

[Fact]
public void TestServicesGenerationWithAndWithoutProvidingDataForServicesWithoutTargetOrFields()
{
var readOnlyCollection = new HassState[] {
new() { EntityId = "script.script1" },
};

var hassServiceDomains = new HassServiceDomain[] {
new() {
Domain = "script",
Services = [
new() {
Service = "turn_off",
Target = new TargetSelector
{
Entity = [new EntitySelector { Domain = ["script"] }]
}

},
]
}
};

// Act:
var code = CodeGenTestHelper.GenerateCompilationUnit(_settings, readOnlyCollection, hassServiceDomains);

var appCode = """
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using RootNameSpace;

public class Root
{
public void Run(IHaContext ha)
{
var s = new RootNameSpace.Services(ha);

s.Script.TurnOff(new ServiceTarget());
s.Script.TurnOff(new ServiceTarget(), new { });

ScriptEntity script = new RootNameSpace.ScriptEntity(ha, "script.testScript");

script.TurnOff(new { });
script.TurnOff();

IScriptEntityCore scriptCore = script;
scriptCore.TurnOff();
scriptCore.TurnOff(new { });
}
}
""";

CodeGenTestHelper.AssertCodeCompiles(code.ToString(), appCode);
}

[Fact]
public void TestServiceWithoutAnyTargetEntity_ExtensionMethodSkipped()
{
Expand Down

0 comments on commit fe15b06

Please sign in to comment.