public void Get_DefectiveComponentCount_Success() { this.mockPersistentState.Setup(s => s.GetAddress(nameof(DefectiveComponentCounter.Manufacturer))).Returns(ManufacturerAddress); this.mockContractState.Setup(s => s.Message.Sender).Returns(ManufacturerAddress); var defectiveComponentsCounter = new DefectiveComponentCounter(this.mockContractState.Object, new byte[] { }); var startingCounts = new uint[12] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; this.mockPersistentState.Setup(s => s.GetArray <uint>("DefectiveComponentsCount")) .Returns(startingCounts); // Check every month. for (uint i = 0; i < 12; i++) { Assert.Equal(startingCounts[i], defectiveComponentsCounter.GetDefectiveComponentsCount(i)); this.mockPersistentState.Verify(s => s.GetArray <uint>("DefectiveComponentsCount"), Times.Once); // Clear invocations. this.mockPersistentState.Invocations.Clear(); } }
public void Set_DefectiveComponentCount_Success(uint componentCount) { this.mockPersistentState.Setup(s => s.GetAddress(nameof(DefectiveComponentCounter.Manufacturer))).Returns(ManufacturerAddress); this.mockContractState.Setup(s => s.Message.Sender).Returns(ManufacturerAddress); var defectiveComponentsCounter = new DefectiveComponentCounter(this.mockContractState.Object, new byte[] { }); var startingCounts = new uint[12] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; // Check every month. for (uint i = 0; i < 12; i++) { this.mockPersistentState.Setup(s => s.GetArray <uint>("DefectiveComponentsCount")) .Returns(startingCounts); var newCounts = new uint[12]; startingCounts.CopyTo(newCounts, 0); newCounts[i] = componentCount; defectiveComponentsCounter.SetDefectiveComponentsCount(i, componentCount); this.mockPersistentState.Verify(s => s.SetArray("DefectiveComponentsCount", newCounts), Times.Once); // Clear invocations. this.mockPersistentState.Invocations.Clear(); } }
public void ComputeTotal_Fails_Sender_Not_Manufacturer() { this.mockPersistentState.Setup(s => s.GetAddress(nameof(DefectiveComponentCounter.Manufacturer))).Returns(ManufacturerAddress); this.mockContractState.Setup(s => s.Message.Sender).Returns(ManufacturerAddress); var defectiveComponentsCounter = new DefectiveComponentCounter(this.mockContractState.Object, new byte[] { }); this.mockContractState.Setup(s => s.Message.Sender).Returns(Address.Zero); Assert.Throws <SmartContractAssertException>(() => defectiveComponentsCounter.ComputeTotal()); }
[InlineData(new uint[] { 4294967295 })] // uint.MaxValue public void Constructor_Sets_Initial_Values_ComponentCounts_Success(uint[] componentCounts) { this.mockPersistentState.Setup(s => s.GetAddress(nameof(DefectiveComponentCounter.Manufacturer))).Returns(ManufacturerAddress); this.mockContractState.Setup(s => s.Message.Sender).Returns(ManufacturerAddress); var arr = ToByteArray(componentCounts); var defectiveComponentsCounter = new DefectiveComponentCounter(this.mockContractState.Object, arr); var persistedComponents = ToFixedWidthArray(componentCounts); this.mockPersistentState.Verify(s => s.SetArray("DefectiveComponentsCount", persistedComponents), Times.Once); }
public void Get_DefectiveComponentCount_Failure_If_12() { this.mockPersistentState.Setup(s => s.GetAddress(nameof(DefectiveComponentCounter.Manufacturer))).Returns(ManufacturerAddress); this.mockContractState.Setup(s => s.Message.Sender).Returns(ManufacturerAddress); var defectiveComponentsCounter = new DefectiveComponentCounter(this.mockContractState.Object, new byte[] { }); this.mockPersistentState.Invocations.Clear(); Assert.Throws <SmartContractAssertException>(() => defectiveComponentsCounter.GetDefectiveComponentsCount(12)); this.mockPersistentState.Verify(s => s.GetArray <uint>("DefectiveComponentsCount"), Times.Never); }
[InlineData(new uint[] { 1, uint.MaxValue })] // 1, uint.MaxValue public void ComputeTotal_Overflow_Throws_Exception(uint[] components) { components = ToFixedWidthArray(components); this.mockPersistentState.Setup(s => s.GetAddress(nameof(DefectiveComponentCounter.Manufacturer))).Returns(ManufacturerAddress); this.mockContractState.Setup(s => s.Message.Sender).Returns(ManufacturerAddress); var defectiveComponentsCounter = new DefectiveComponentCounter(this.mockContractState.Object, new byte[] { }); this.mockPersistentState.Setup(s => s.GetArray <uint>("DefectiveComponentsCount")).Returns(components); Assert.Throws <OverflowException>(() => defectiveComponentsCounter.ComputeTotal()); }
public void Constructor_Sets_Initial_Values() { this.mockContractState.Setup(s => s.Message.Sender).Returns(ManufacturerAddress); var arr = ToByteArray(new uint[] { 1, 2, 3 }); var defectiveComponentsCounter = new DefectiveComponentCounter(this.mockContractState.Object, arr); this.mockPersistentState.Verify(s => s.SetAddress(nameof(DefectiveComponentCounter.Manufacturer), ManufacturerAddress), Times.Once); this.mockPersistentState.Verify(s => s.SetArray("DefectiveComponentsCount", new uint[12] { 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 }), Times.Once); this.mockPersistentState.Verify(s => s.SetUInt32(nameof(DefectiveComponentCounter.Total), 0), Times.Once); this.mockPersistentState.Verify(s => s.SetUInt32(nameof(DefectiveComponentCounter.State), (uint)DefectiveComponentCounter.StateType.Create), Times.Once); }
public void ComputeTotal_Computes_Correctly(uint[] components, uint expectedTotal) { components = ToFixedWidthArray(components); this.mockPersistentState.Setup(s => s.GetAddress(nameof(DefectiveComponentCounter.Manufacturer))).Returns(ManufacturerAddress); this.mockContractState.Setup(s => s.Message.Sender).Returns(ManufacturerAddress); var defectiveComponentsCounter = new DefectiveComponentCounter(this.mockContractState.Object, ToByteArray(components)); this.mockPersistentState.Setup(s => s.GetArray <uint>("DefectiveComponentsCount")).Returns(components); defectiveComponentsCounter.ComputeTotal(); this.mockPersistentState.Verify(s => s.SetUInt32(nameof(DefectiveComponentCounter.Total), expectedTotal)); }