Skip to content

Commit

Permalink
Primitive Records (#15)
Browse files Browse the repository at this point in the history
* convert Audis.Primitives to records

* make typeconverter only available for strings, add unit tests

* Update azure-pipelines.yml for Azure Pipelines

Use .NET 5

* Update azure-pipelines.yml for Azure Pipelines

Use .NET 5

* add test-task for Audis.Primitives to pipelines

* publish test results as TRX

* use TRX logger for dotnet test
  • Loading branch information
meinsiedler authored Nov 18, 2020
1 parent 4ff4dd2 commit d4e6f39
Show file tree
Hide file tree
Showing 13 changed files with 361 additions and 316 deletions.
15 changes: 15 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ variables:
buildConfiguration: 'Release'

steps:
- task: UseDotNet@2
displayName: 'Use .NET 5'
inputs:
packageType: sdk
version: '5.0.x'

- script: dotnet build --configuration $(buildConfiguration) src/Audis.Analyzer.Common/Audis.Analyzer.Common.sln
displayName: Build Audis.Analyzer.Common

Expand All @@ -33,6 +39,15 @@ steps:
- script: dotnet build --configuration $(buildConfiguration) src/Audis.Primitives/Audis.Primitives.sln
displayName: Build Audis.Primitives

- script: dotnet test --no-build --configuration $(buildConfiguration) --logger trx src/Audis.Primitives/Audis.Primitives.sln
displayName: Test Audis.Primitives

- task: PublishTestResults@2
condition: succeededOrFailed()
inputs:
testResultsFormat: VSTest
testResultsFiles: '**/*.trx'

- task: CopyFiles@2
displayName: Publish NuGet Packages to Artifacts
inputs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -15,11 +16,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.261">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Newtonsoft.Json;
using NUnit.Framework;

namespace Audis.Primitives.Tests
{
[TestFixture]
public class PrimitiveTypeConverterTests
{
[Test]
public void StringSerialization()
{
var knowledgeValue = new KnowledgeValue("asdf");
var dto = new StringDto { Primitive = knowledgeValue };

var json = JsonConvert.SerializeObject(dto);
var newDto = JsonConvert.DeserializeObject<StringDto>(json);

Assert.That(json, Is.EqualTo("{\"Primitive\":\"asdf\"}"));
Assert.That(newDto.Primitive, Is.EqualTo(dto.Primitive));
Assert.That(newDto.Primitive!.Value, Is.EqualTo(knowledgeValue.Value));
}

[Test]
public void KnowledgeValueWithDateTimeCanBeSerializedAndDeserialized()
{
var date = new DateTime(2020, 11, 11, 11, 11, 11);
var knowledgeValue = new KnowledgeValue(date.ToString("o"));
var json = JsonConvert.SerializeObject(knowledgeValue);

var deserializedKnowledegeValue = JsonConvert.DeserializeObject<KnowledgeValue>(json);

Assert.That(json, Is.EqualTo("\"2020-11-11T11:11:11.0000000\""));
Assert.That(deserializedKnowledegeValue, Is.EqualTo(knowledgeValue));
Assert.That(deserializedKnowledegeValue.Value, Is.EqualTo(date.ToString("o")));
}

public class StringDto
{
public KnowledgeValue? Primitive { get; set; }
}
}
}
81 changes: 53 additions & 28 deletions src/Audis.Primitives/Audis.Primitives.Tests/PrimitivesTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Newtonsoft.Json;
using NUnit.Framework;

namespace Audis.Primitives.Tests
Expand All @@ -12,7 +11,7 @@ public class PrimitivesTests
[TestCase("@rd4", "rd4")]
public void TestDispositionCode(string input, string expected)
{
Assert.AreEqual(expected, DispositionCode.From(input).Value);
Assert.AreEqual(expected, new DispositionCode(input).Value);
}

[TestCase(null)]
Expand All @@ -21,21 +20,21 @@ public void TestDispositionCode(string input, string expected)
[TestCase("@")]
public void TestEmptyDispositionCodeThrows(string input)
{
Assert.Throws<ArgumentNullException>(() => DispositionCode.From(input));
Assert.Throws<ArgumentNullException>(() => new DispositionCode(input));
}

[TestCase(null)]
[TestCase("")]
[TestCase(" ")]
public void TestEmptyKnowledgeIdentifierThrows(string input)
{
Assert.Throws<ArgumentNullException>(() => KnowledgeIdentifier.From(input));
Assert.Throws<ArgumentNullException>(() => new KnowledgeIdentifier(input));
}

[Test]
public void TestKnowledgeIdentifierInvalidFormat()
{
var ex = Assert.Throws<ArgumentException>(() => KnowledgeIdentifier.From("no-leading-hashtag"));
var ex = Assert.Throws<ArgumentException>(() => new KnowledgeIdentifier("no-leading-hashtag"));
Assert.AreEqual("The KnowledgeIdentifier has an invalid format: \"no-leading-hashtag\", Expected starting with #.", ex.Message);
}

Expand All @@ -44,15 +43,15 @@ public void TestKnowledgeIdentifierInvalidFormat()
[TestCase(" ")]
public void TestEmptyKnowledgeValueThrows(string input)
{
Assert.Throws<ArgumentNullException>(() => KnowledgeValue.From(input));
Assert.Throws<ArgumentNullException>(() => new KnowledgeValue(input));
}

[TestCase(null)]
[TestCase("")]
[TestCase(" ")]
public void TestEmptyQuestionIdThrows(string input)
{
Assert.Throws<ArgumentNullException>(() => QuestionId.From(input));
Assert.Throws<ArgumentNullException>(() => new QuestionId(input));
}

[TestCase("invalid-format", true)]
Expand All @@ -65,23 +64,32 @@ public void TestQuestionIdInvalidFormat(string input, bool throws)
{
if (throws)
{
var ex = Assert.Throws<ArgumentException>(() => QuestionId.From(input));
Assert.AreEqual($"The QuestionId has an invalid format: \"{input}\", Expected \"<configuration-name>:<lineNumber>\".", ex.Message);
var ex = Assert.Throws<ArgumentException>(() => new QuestionId(input));
Assert.AreEqual($"The QuestionId has an invalid format: \"{input}\", Expected \"<question-catalog-name>:<lineNumber>\".", ex.Message);
}
else
{
var questionId = QuestionId.From(input);
var questionId = new QuestionId(input);
Assert.AreEqual(input, questionId.Value);
Assert.AreEqual(QuestionCatalogName.From(input.Split(':')[0]), questionId.ConfigurationName);
Assert.AreEqual(new QuestionCatalogName(input.Split(':')[0]), questionId.QuestionCatalogName);
}
}

[Test]
public void TestQuestionIdQuestionCatalogAndLineNumberConstructor()
{
var questionId = new QuestionId(new QuestionCatalogName("question-catalog"), 10);
Assert.That(questionId.Value, Is.EqualTo("question-catalog:10"));
Assert.AreEqual(new QuestionCatalogName(questionId.Value.Split(':')[0]), questionId.QuestionCatalogName);
Assert.AreEqual(int.Parse(questionId.Value.Split(':')[1]), questionId.LineNumber);
}

[TestCase(null)]
[TestCase("")]
[TestCase(" ")]
public void TestEmptyAnswerIdThrows(string input)
{
Assert.Throws<ArgumentNullException>(() => AnswerId.From(input));
Assert.Throws<ArgumentNullException>(() => new AnswerId(input));
}

[TestCase("invalid-format", true)]
Expand All @@ -97,26 +105,35 @@ public void TestAnswerIdInvalidFormat(string input, bool throws)
{
if (throws)
{
var ex = Assert.Throws<ArgumentException>(() => AnswerId.From(input));
Assert.AreEqual($"The AnswerId has an invalid format: \"{input}\", Expected \"<configuration-name>:<questionLineNumber>/<answerLineNumber>\".", ex.Message);
var ex = Assert.Throws<ArgumentException>(() => new AnswerId(input));
Assert.AreEqual($"The AnswerId has an invalid format: \"{input}\", Expected \"<question-catalog-name>:<questionLineNumber>/<answerLineNumber>\".", ex.Message);
}
else
{
var answerId = AnswerId.From(input);
var answerId = new AnswerId(input);
Assert.AreEqual(input, answerId.Value);
var split = input.Split('/');
Assert.AreEqual(split[0], answerId.QuestionId.Value);
}
}

[Test]
public void TestAnswerIdQuestionIdAndLineNumberConstructor()
{
var answerId = new AnswerId(new QuestionId(new QuestionCatalogName("question-catalog"), 10), 12);
Assert.That(answerId.Value, Is.EqualTo("question-catalog:10/12"));
Assert.AreEqual(new QuestionId(answerId.Value.Split('/')[0]), answerId.QuestionId);
Assert.AreEqual(int.Parse(answerId.Value.Split('/')[1]), answerId.LineNumber);
}

[TestCase("#audis.schmerzen", "#audis.schmerzen", true)]
[TestCase("#audis.SCHMERZEN", "#audis.schmerzen", true)]
[TestCase("#audis.schmerzen", "#audis.SCHMERZEN", true)]
[TestCase("#audis.value1", "#audis.value2", false)]
public void AssertKnowledgeIdentifierCaseInsensitiveEqualsAndGetHashCode(string input1, string input2, bool equals)
{
var value1 = KnowledgeIdentifier.From(input1);
var value2 = KnowledgeIdentifier.From(input2);
var value1 = new KnowledgeIdentifier(input1);
var value2 = new KnowledgeIdentifier(input2);

Assert.AreEqual(equals, value1.Equals(value2));
Assert.AreEqual(equals, value1.GetHashCode() == value2.GetHashCode());
Expand All @@ -129,8 +146,9 @@ public void AssertKnowledgeIdentifierCaseInsensitiveEqualsAndGetHashCode(string
[TestCase("ja", "nein", false)]
public void AssertKnowledgeValueCaseInsensitiveEqualsAndGetHashCode(string input1, string input2, bool equals)
{
var value1 = KnowledgeValue.From(input1);
var value2 = KnowledgeValue.From(input2);
var v = new KnowledgeValue("asdf");
var value1 = new KnowledgeValue(input1);
var value2 = new KnowledgeValue(input2);

Assert.AreEqual(equals, value1.Equals(value2));
Assert.AreEqual(equals, value1.GetHashCode() == value2.GetHashCode());
Expand All @@ -145,20 +163,27 @@ public void AssertKnowledgeValueCaseInsensitiveEqualsAndGetHashCode(string input
[TestCase("@RD1", "@RD2", false)]
public void AssertDispositionCodeCaseInsensitiveEqualsAndGetHashCode(string input1, string input2, bool equals)
{
var value1 = DispositionCode.From(input1);
var value2 = DispositionCode.From(input2);
var value1 = new DispositionCode(input1);
var value2 = new DispositionCode(input2);

Assert.AreEqual(equals, value1.Equals(value2));
Assert.AreEqual(equals, value1.GetHashCode() == value2.GetHashCode());
}

[Test]
public void KnowledgeValueWithDateTimeCanBeSerializedAndDeserialized()
[TestCase("@Scenario", "@Scenario", true)]
[TestCase("@Scenario", "Scenario", true)]
[TestCase("Scenario", "@Scenario", true)]
[TestCase("Scenario", "Scenario", true)]
[TestCase("scenario", "SCENARIO", true)]
[TestCase("SCENARIO", "scenario", true)]
[TestCase("@Scenario1", "@Scenario2", false)]
public void AssertScenarioCaseInsensitiveEqualsAndGetHashCode(string input1, string input2, bool equals)
{
var knowledgeValue = KnowledgeValue.From(DateTime.Now.ToString("o"));
var json = JsonConvert.SerializeObject(knowledgeValue);
var deserializedKnowledegeValue = JsonConvert.DeserializeObject<KnowledgeValue>(json);
Assert.AreEqual(knowledgeValue, deserializedKnowledegeValue);
var value1 = new ScenarioIdentifier(input1);
var value2 = new ScenarioIdentifier(input2);

Assert.AreEqual(equals, value1.Equals(value2));
Assert.AreEqual(equals, value1.GetHashCode() == value2.GetHashCode());
}
}
}
}
15 changes: 8 additions & 7 deletions src/Audis.Primitives/Audis.Primitives/Audis.Primitives.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>softaware gmbh</Authors>
<Company>softaware gmbh</Company>
Expand All @@ -12,9 +12,10 @@
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/softawaregmbh/audis-public</RepositoryUrl>
<PackageTags>Audis</PackageTags>
<AssemblyVersion>1.0.1.0</AssemblyVersion>
<FileVersion>1.0.1.0</FileVersion>
<Version>1.0.1</Version>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<FileVersion>2.0.0.0</FileVersion>
<Version>2.0.0</Version>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -23,12 +24,12 @@
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>D:\_GIT\work\audis-public\src\Audis.Primitives\Audis.Primitives\Audis.Primitives.xml</DocumentationFile>
<DocumentationFile>Audis.Primitives.xml</DocumentationFile>
<NoWarn>1701;1702;1591</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>C:\Development\audis-public\src\Audis.Primitives\Audis.Primitives.xml</DocumentationFile>
<DocumentationFile>Audis.Primitives.xml</DocumentationFile>
<NoWarn>1701;1702;1591</NoWarn>
</PropertyGroup>

Expand All @@ -41,7 +42,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.261">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading

0 comments on commit d4e6f39

Please sign in to comment.