Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Linq;
using System.Threading.Tasks;
using Azure.Provisioning.Resources;
using NUnit.Framework;

Expand All @@ -14,71 +15,68 @@ public class BicepValueTests
public void ValidateLiteralBicepValue()
{
// string value
AssertExpression("'test'", new BicepValue<string>("test"));
TestHelpers.AssertExpression("'test'", new BicepValue<string>("test"));

// int value
AssertExpression("42", new BicepValue<int>(42));
AssertExpression("-42", new BicepValue<int>(-42));
TestHelpers.AssertExpression("42", new BicepValue<int>(42));
TestHelpers.AssertExpression("-42", new BicepValue<int>(-42));

// long value
AssertExpression("42", new BicepValue<long>(42L));
AssertExpression("2147483647", new BicepValue<long>(2147483647L));
AssertExpression("json('2147483648')", new BicepValue<long>(2147483648));
AssertExpression("-2147483648", new BicepValue<long>(-2147483648L));
AssertExpression("json('-2147483649')", new BicepValue<long>(-2147483649));
AssertExpression("json('9223372036854775807')", new BicepValue<long>(9223372036854775807));
TestHelpers.AssertExpression("42", new BicepValue<long>(42L));
TestHelpers.AssertExpression("2147483647", new BicepValue<long>(2147483647L));
TestHelpers.AssertExpression("json('2147483648')", new BicepValue<long>(2147483648));
TestHelpers.AssertExpression("-2147483648", new BicepValue<long>(-2147483648L));
TestHelpers.AssertExpression("json('-2147483649')", new BicepValue<long>(-2147483649));
TestHelpers.AssertExpression("json('9223372036854775807')", new BicepValue<long>(9223372036854775807));

// bool value
AssertExpression("true", new BicepValue<bool>(true));
AssertExpression("false", new BicepValue<bool>(false));
TestHelpers.AssertExpression("true", new BicepValue<bool>(true));
TestHelpers.AssertExpression("false", new BicepValue<bool>(false));

// double value
AssertExpression("json('3.14')", new BicepValue<double>(3.14));
AssertExpression("json('-3.14')", new BicepValue<double>(-3.14));
TestHelpers.AssertExpression("json('3.14')", new BicepValue<double>(3.14));
TestHelpers.AssertExpression("json('-3.14')", new BicepValue<double>(-3.14));
// double value with whole numbers
AssertExpression("314", new BicepValue<double>(314d));
AssertExpression("2147483647", new BicepValue<double>(2147483647d));
AssertExpression("json('2147483648')", new BicepValue<double>(2147483648d));
AssertExpression("-2147483647", new BicepValue<double>(-2147483647d));
AssertExpression("-2147483648", new BicepValue<double>(-2147483648d));
AssertExpression("json('-2147483649')", new BicepValue<double>(-2147483649d));

static void AssertExpression(string expected, BicepValue bicepValue)
{
Assert.AreEqual(expected, bicepValue.ToString());
}
TestHelpers.AssertExpression("314", new BicepValue<double>(314d));
TestHelpers.AssertExpression("2147483647", new BicepValue<double>(2147483647d));
TestHelpers.AssertExpression("json('2147483648')", new BicepValue<double>(2147483648d));
TestHelpers.AssertExpression("-2147483647", new BicepValue<double>(-2147483647d));
TestHelpers.AssertExpression("-2147483648", new BicepValue<double>(-2147483648d));
TestHelpers.AssertExpression("json('-2147483649')", new BicepValue<double>(-2147483649d));
}

[Test]
public void ValidateTimeSpanPropertyWithFormat()
public async Task ValidateTimeSpanPropertyWithFormat()
{
var infra = new Infrastructure();
var powershell = new AzurePowerShellScript("script", "2023-08-01")
{
RetentionInterval = new TimeSpan(11, 22, 33),
AzPowerShellVersion = "10.0",
ScriptContent = "echo 'Hello, world!'",
};

infra.Add(powershell);
var plan = infra.Build();

var result = plan.Compile();
Assert.AreEqual(
"""
@description('The location for the resource(s) to be deployed.')
param location string = resourceGroup().location

resource script 'Microsoft.Resources/deploymentScripts@2023-08-01' = {
name: take('script${uniqueString(resourceGroup().id)}', 24)
location: location
kind: 'AzurePowerShell'
properties: {
azPowerShellVersion: '10.0'
retentionInterval: 'PT11H22M33S'
scriptContent: 'echo \'Hello, world!\''
}
}
""".NormalizeLineEndings(), result.Values.First().NormalizeLineEndings());
await using Trycep test = new();
test.Define(
ctx =>
{
var infra = new Infrastructure();
var powershell = new AzurePowerShellScript("script", "2023-08-01")
{
RetentionInterval = new TimeSpan(11, 22, 33),
AzPowerShellVersion = "10.0",
ScriptContent = "echo 'Hello, world!'",
};
infra.Add(powershell);
return infra;
})
.Compare(
"""
@description('The location for the resource(s) to be deployed.')
param location string = resourceGroup().location

resource script 'Microsoft.Resources/deploymentScripts@2023-08-01' = {
name: take('script${uniqueString(resourceGroup().id)}', 24)
location: location
kind: 'AzurePowerShell'
properties: {
azPowerShellVersion: '10.0'
retentionInterval: 'PT11H22M33S'
scriptContent: 'echo \'Hello, world!\''
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,44 @@ public async Task ValidateNormalProperties()
""");
}

[Test]
public async Task ValidateListProperties_Unset()
{
await using var test = new Trycep();

test.Define(
ctx =>
{
Infrastructure infra = new();

// Create our comprehensive test resource with various property types
var storageAccount = new StorageAccount("storageAccount")
{
// Test basic string property
Name = "test-storage",

// Test location property
Location = AzureLocation.WestUS2,

// Test enum property
StorageTier = StorageTier.Standard
};

infra.Add(storageAccount);
return infra;
})
.Compare(
"""
resource storageAccount 'Test.Provider/storageAccounts@2024-01-01' = {
name: 'test-storage'
location: 'westus2'
properties: {
tier: 'Standard'
}
}
""");
}

[Test]
public async Task ValidateResourceReference()
{
Expand Down
16 changes: 0 additions & 16 deletions sdk/provisioning/Azure.Provisioning/tests/StringHelpers.cs

This file was deleted.

28 changes: 28 additions & 0 deletions sdk/provisioning/Azure.Provisioning/tests/TestHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Text.RegularExpressions;
using Azure.Provisioning.Expressions;
using NUnit.Framework;

namespace Azure.Provisioning.Tests
{
internal static class TestHelpers
{
public static string NormalizeLineEndings(this string input)
{
// Normalize line endings to LF
return Regex.Replace(input, @"\r\n?", "\n");
}

public static void AssertExpression(string expected, IBicepValue bicepValue)
{
Assert.AreEqual(expected.NormalizeLineEndings(), bicepValue.ToString()?.NormalizeLineEndings());
}

public static void AssertExpression(string expected, BicepExpression expression)
{
Assert.AreEqual(expected.NormalizeLineEndings(), expression.ToString().NormalizeLineEndings());
}
}
}