Skip to content

Commit

Permalink
Fixes for unit testing of components (#27)
Browse files Browse the repository at this point in the history
* working on: fixing unit testing generator component

* working on generator unit testing

* fixed unit testig for components

---------

Co-authored-by: blfuentes <[email protected]>
  • Loading branch information
blfuentes and blfuentes authored May 26, 2024
1 parent a37cdaa commit c8f2987
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/GSTC.bUnit/Components/GeneratorTestWrapper.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@using GuitarStringTensionCalculator.Components.Pages
@using MudBlazor

<MudPopoverProvider />

<Generator />

@code {

}
2 changes: 2 additions & 0 deletions src/GSTC.bUnit/FreqRepoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class FreqRepoTest
[Fact]
public void GetMusicalNote()
{
// Arrange
FreqRepo tmp = new FreqRepo();
var C1_test = tmp.GetMusicalNote("C", 1);
var D2_test = tmp.GetMusicalNote("D", 2);
Expand All @@ -18,6 +19,7 @@ public void GetMusicalNote()
var A6_test = tmp.GetMusicalNote("A", 6);
var B7_test = tmp.GetMusicalNote("B", 7);

// Assert
Assert.Equal(Math.Round(32.703, 1), Math.Round(C1_test.Frequency, 1));
Assert.Equal(Math.Round(73.416, 1), Math.Round(D2_test.Frequency, 1));
Assert.Equal(Math.Round(164.814, 1), Math.Round(E3_test.Frequency, 1));
Expand Down
71 changes: 60 additions & 11 deletions src/GSTC.bUnit/GeneratorComponentTest.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,71 @@
using Bunit;
using _pages = GuitarStringTensionCalculator.Components.Pages;
using Xunit;
using GSTC.bUnit.Components;
using MudBlazor;
using MudBlazor.Services;
using Shouldly;
using Microsoft.AspNetCore.Components;
using Xunit;

namespace GSTC.bUnit
{
public class GeneratorComponentTest : TestContext
{
[Fact]
public void InitialHtmlIsCorrect()
{
// Arrange - renders the Generator component
var cut = RenderComponent<_pages.Generator>();
cut.Find("#_result_numberofstrings").GetAttribute("value").ShouldBe("0");
cut.Find("select").Change(new ChangeEventArgs() { Value = "6" });
cut.Find("#_result_numberofstrings").GetAttribute("value").ShouldBe("6");
cut.Find("select").Change(new ChangeEventArgs() { Value = "" });
cut.Find("#_result_numberofstrings").GetAttribute("value").ShouldBe("6");
// Arrange
using var ctx = new Bunit.TestContext();

ctx.JSInterop.Mode = JSRuntimeMode.Loose;
ctx.Services.AddMudServices(config =>
{
config.SnackbarConfiguration.PreventDuplicates = true;
config.SnackbarConfiguration.NewestOnTop = false;
config.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.TopCenter;
config.PopoverOptions.ThrowOnDuplicateProvider = false;
});

// Act
var component = ctx.RenderComponent<GeneratorTestWrapper>();
var numOfStringsSelect = component.FindAll("#select-numofstrings");
var scaleLengthSelect = component.FindAll("#select-scalelength");
var semitonesSelect = component.FindAll("#select-semitones");
var tuningSelect = component.FindAll("#select-tuning");

// Assert
component.ShouldNotBeNull();
numOfStringsSelect.Count.ShouldBe(1);
scaleLengthSelect.Count.ShouldBe(1);
semitonesSelect.Count.ShouldBe(1);
tuningSelect.ShouldBeEmpty();
}

[Fact]
public void SelectNumOfStrings()
{
// Arrange
using var ctx = new Bunit.TestContext();

ctx.JSInterop.Mode = JSRuntimeMode.Loose;
ctx.Services.AddMudServices(config =>
{
config.SnackbarConfiguration.PreventDuplicates = true;
config.SnackbarConfiguration.NewestOnTop = false;
config.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.TopCenter;
config.PopoverOptions.ThrowOnDuplicateProvider = false;
});

// Act & Assert
var component = ctx.RenderComponent<GeneratorTestWrapper>();
var select = component.FindComponents<MudSelect<string>>()[0];
select.Instance.Value.ShouldBeNull();

var input = component.Find("div.mud-input-control");
input.Click();

var items = component.FindAll("div.mud-list-item");
items.Count.ShouldBeGreaterThan(0);
items[1].Click();
select.Instance.Value.ShouldBe("7");
}
}
}
}
2 changes: 2 additions & 0 deletions src/GSTC.bUnit/GuitarStringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ public void GuitarString()
//double d = (1.0 / 0.59) * Math.Sqrt(35) * (1.0 / tmp_gut.Note.Frequency) * (1.0 / Math.Sqrt(Math.PI * tmp_gut.Material.Density));
//double t = Math.Pow(d * 0.59 * tmp_gut.Note.Frequency * Math.Sqrt(Math.PI * tmp_gut.Material.Density), 2);

// Arrange
GuitarString tmp_ssteel_e4 = new GuitarString(1, "PL", 0.010, 25.5, (new FreqRepo()).GetMusicalNote("E", 4), new StringMaterial(MaterialType.STAINLESS_STEEL));
GuitarString tmp_ssteel_b3 = new GuitarString(2, "PL", 0.014, 25.5, (new FreqRepo()).GetMusicalNote("B", 3), new StringMaterial(MaterialType.STAINLESS_STEEL));
GuitarString tmp_ssteel_g3 = new GuitarString(3, "PL", 0.022, 25.5, (new FreqRepo()).GetMusicalNote("G", 3), new StringMaterial(MaterialType.STAINLESS_STEEL));
GuitarString tmp_ssteel_d3 = new GuitarString(4,"NW", 0.030, 25.5, (new FreqRepo()).GetMusicalNote("D", 3), new StringMaterial(MaterialType.STAINLESS_STEEL));
GuitarString tmp_ssteel_a2 = new GuitarString(5, "NW", 0.039, 25.5, (new FreqRepo()).GetMusicalNote("A", 2), new StringMaterial(MaterialType.STAINLESS_STEEL));
GuitarString tmp_ssteel_e2 = new GuitarString(6, "NW", 0.046, 25.5, (new FreqRepo()).GetMusicalNote("E", 2), new StringMaterial(MaterialType.STAINLESS_STEEL));

// Assert
Assert.Equal(16.2, Math.Round(tmp_ssteel_e4.StringTension_UW_LB, 1));
Assert.Equal(17.8, Math.Round(tmp_ssteel_b3.StringTension_UW_LB, 1));
Assert.Equal(27.7, Math.Round(tmp_ssteel_g3.StringTension_UW_LB, 1));
Expand Down
4 changes: 4 additions & 0 deletions src/GSTC.bUnit/MusicalNoteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class MusicalNoteTest
[Fact]
public void GetPrev()
{
// Arrange
var firstNote = new MusicalNote("C", 0).GetPrev();

var middleNote = new MusicalNote("A#/Bb", 5).GetPrev();
Expand All @@ -19,6 +20,7 @@ public void GetPrev()
var lastNote = new MusicalNote("B", 9).GetPrev();
var lastNoteCheck = new MusicalNote("A#/Bb", 9);

// Assert
Assert.Null(firstNote);
Assert.True(middleNote.Equals(middleNoteCheck));
Assert.True(middleNotePrevOctave.Equals(middleNotePrevOctaveCheck));
Expand All @@ -28,6 +30,7 @@ public void GetPrev()
[Fact]
public void GetNext()
{
// Arrange
var lastNote = new MusicalNote("B", 9).GetNext();

var middleNote = new MusicalNote("A#/Bb", 5).GetNext();
Expand All @@ -39,6 +42,7 @@ public void GetNext()
var firstNote = new MusicalNote("C", 0).GetNext();
var firstNoteCheck = new MusicalNote("C#/Db", 0);

// Assert
Assert.Null(lastNote);
Assert.True(middleNote.Equals(middleNoteCheck));
Assert.True(middleNoteNextOctave.Equals(middleNoteNextOctaveCheck));
Expand Down
5 changes: 3 additions & 2 deletions src/GSTC.bUnit/StringSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class StringSetTest
[Fact]
public void StringSet()
{
// Arrange
string[] prefixes = new string[6] { "PL", "PL", "PL", "NW", "NW", "NW" };
double[] diameters = new double[6] { 0.010, 0.013, 0.017, 0.026, 0.036, 0.048 };
MusicalNote[] notes = new MusicalNote[6]
Expand All @@ -23,15 +24,15 @@ public void StringSet()
var tmpStringSet_255 = new StringSet(6, prefixes, diameters, 25.5, notes);
var tmpStringSet_2475 = new StringSet(6, prefixes, diameters, 24.75, notes);

//
// Assert
Assert.Equal(Math.Round(16.2, 1), Math.Round(tmpStringSet_255.StringCollection[0].StringTension_UW_LB, 1));
Assert.Equal(Math.Round(15.4, 1), Math.Round(tmpStringSet_255.StringCollection[1].StringTension_UW_LB, 1));
Assert.Equal(Math.Round(16.6, 1), Math.Round(tmpStringSet_255.StringCollection[2].StringTension_UW_LB, 1));
Assert.Equal(Math.Round(18.4, 1), Math.Round(tmpStringSet_255.StringCollection[3].StringTension_UW_LB, 1));
Assert.Equal(Math.Round(19.5, 1), Math.Round(tmpStringSet_255.StringCollection[4].StringTension_UW_LB, 1));
Assert.Equal(Math.Round(18.9, 1), Math.Round(tmpStringSet_255.StringCollection[5].StringTension_UW_LB, 1));

//
// Assert
Assert.Equal(Math.Round(15.3, 1), Math.Round(tmpStringSet_2475.StringCollection[0].StringTension_UW_LB, 1));
Assert.Equal(Math.Round(14.5, 1), Math.Round(tmpStringSet_2475.StringCollection[1].StringTension_UW_LB, 1));
Assert.Equal(Math.Round(15.6, 1), Math.Round(tmpStringSet_2475.StringCollection[2].StringTension_UW_LB, 1));
Expand Down
8 changes: 6 additions & 2 deletions src/GSTC.bUnit/TuningTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,29 @@ public class TuningTest
[Fact]
public void DefaultTuning()
{
// Arrange
var tuning = new Tuning();

// Assert
Assert.Equal((new MusicalNote("E", 4)), tuning.MusicalNotes.First());
}

[Fact]
public void SpecificTuning()
{
// Arrange
var tuning_minus_2 = new Tuning(-2);

var tuning_plus_2 = new Tuning(2);

// Assert
Assert.Equal((new MusicalNote("E", 4).GetPrev().GetPrev()), tuning_minus_2.MusicalNotes[0]);
Assert.Equal((new MusicalNote("B", 3).GetPrev().GetPrev()), tuning_minus_2.MusicalNotes[1]);
Assert.Equal((new MusicalNote("G", 3).GetPrev().GetPrev()), tuning_minus_2.MusicalNotes[2]);
Assert.Equal((new MusicalNote("D", 3).GetPrev().GetPrev()), tuning_minus_2.MusicalNotes[3]);
Assert.Equal((new MusicalNote("A", 2).GetPrev().GetPrev()), tuning_minus_2.MusicalNotes[4]);
Assert.Equal((new MusicalNote("E", 2).GetPrev().GetPrev()), tuning_minus_2.MusicalNotes[5]);

var tuning_plus_2 = new Tuning(2);

Assert.Equal((new MusicalNote("E", 4).GetNext().GetNext()), tuning_plus_2.MusicalNotes[0]);
Assert.Equal((new MusicalNote("B", 3).GetNext().GetNext()), tuning_plus_2.MusicalNotes[1]);
Assert.Equal((new MusicalNote("G", 3).GetNext().GetNext()), tuning_plus_2.MusicalNotes[2]);
Expand Down
2 changes: 2 additions & 0 deletions src/GSTC.bUnit/UnitWeightRepoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ public class UnitWeightRepoTest
[Fact]
public void GetUnitWeight()
{
// Arrange
var tmp_PL007 = UnitWeightRepo.GetUnitWeight("PL", 0.007);
var tmp_PL013 = UnitWeightRepo.GetUnitWeight("PL", 0.013);
var tmp_PL017 = UnitWeightRepo.GetUnitWeight("PL", 0.017);
var tmp_NW022 = UnitWeightRepo.GetUnitWeight("NW", 0.022);
var tmp_NW034 = UnitWeightRepo.GetUnitWeight("NW", 0.034);
var tmp_NW046 = UnitWeightRepo.GetUnitWeight("NW", 0.046);

// Assert
Assert.Equal(0.00001085, tmp_PL007.Value);
Assert.Equal(0.00003744, tmp_PL013.Value);
Assert.Equal(0.00006402, tmp_PL017.Value);
Expand Down
2 changes: 1 addition & 1 deletion src/GuitarStringTensionCalculator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution items", "Solution
Directory.Packages.props = Directory.Packages.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GuitarStringTensionCalculator", "GuitarStringTensionCalculator\GuitarStringTensionCalculator.csproj", "{358B3CE8-073F-4C3C-9D5A-D5053EF17BA9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GuitarStringTensionCalculator", "GuitarStringTensionCalculator\GuitarStringTensionCalculator.csproj", "{358B3CE8-073F-4C3C-9D5A-D5053EF17BA9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

<h3>Calculator</h3>

<MudSelect @bind-Value="numberOfStrings" @bind-Value:after=@AfterNumberOfStringsChanged
<MudSelect id="select-numofstrings" @bind-Value="numberOfStrings" @bind-Value:after=@AfterNumberOfStringsChanged
Label="Number of strings" AdornmentIcon="@Icons.Material.Filled.RunCircle" AdornmentColor="Color.Primary">
@foreach (var idx in Enumerable.Range(6, 4))
{
<MudSelectItem Value="@(idx.ToString())">@idx</MudSelectItem>
}
</MudSelect>

<MudSelect @bind-Value="scaleLength" @bind-Value:after=@AfterScaleLengthChanged
<MudSelect id="select-scalelength" @bind-Value="scaleLength" @bind-Value:after=@AfterScaleLengthChanged
Label="Scale length" OpenIcon="@Icons.Material.Filled.Rule" AdornmentColor="Color.Secondary">
@foreach (var scale in Calculator.ScaleLengthRepo.Scales)
{
Expand All @@ -30,7 +30,7 @@
}
else
{
<MudSelect @bind-Value="semitones" @bind-Value:after=@AfterSemitonesChanged
<MudSelect id="select-semitones" @bind-Value="semitones" @bind-Value:after=@AfterSemitonesChanged
Label="Tuning" OpenIcon="@Icons.Material.Filled.MusicNote" AdornmentColor="Color.Tertiary">
@foreach (var tuning in Tunings)
{
Expand All @@ -55,7 +55,7 @@ else
else
{
<div class="col-10">
<TuningComponent NumberOfStrings="@_numOfStrings" ScaleLength="@_scaleLength" Tuning="@selectedTuning" />
<TuningComponent id="component-tuning" NumberOfStrings="@_numOfStrings" ScaleLength="@_scaleLength" Tuning="@selectedTuning" />
</div>
}

Expand Down

0 comments on commit c8f2987

Please sign in to comment.