/// <summary> /// Converts the String input to the correct string value. /// </summary> /// <typeparam name="TInput"> The temperature type to be converted to. </typeparam> /// <param name="input"> The value to be converted. </param> /// <exception cref="ArgumentException"> The TInput type is not a valid type for this method. </exception> /// <returns> /// The result of the conversion. /// </returns> public static string To <TInput>(this StringBase input) where TInput : TemperatureBase { return(typeof(TInput).Name switch { nameof(Celsius) when input is CelsiusString castInput => StringParser(castInput.Temperature, CelsiusConverter.CelsiusToCelsius), nameof(Celsius) when input is FahrenheitString castInput => StringParser(castInput.Temperature, FahrenheitConverter.FahrenheitToCelsius), nameof(Celsius) when input is KelvinString castInput => StringParser(castInput.Temperature, KelvinConverter.KelvinToCelsius), nameof(Celsius) when input is GasString castInput => StringParser(castInput.Temperature, GasConverter.GasToCelsius), nameof(Celsius) when input is RankineString castInput => StringParser(castInput.Temperature, RankineConverter.RankineToCelsius), nameof(Fahrenheit) when input is CelsiusString castInput => StringParser(castInput.Temperature, CelsiusConverter.CelsiusToFahrenheit), nameof(Fahrenheit) when input is FahrenheitString castInput => StringParser(castInput.Temperature, FahrenheitConverter.FahrenheitToFahrenheit), nameof(Fahrenheit) when input is KelvinString castInput => StringParser(castInput.Temperature, KelvinConverter.KelvinToFahrenheit), nameof(Fahrenheit) when input is GasString castInput => StringParser(castInput.Temperature, GasConverter.GasToFahrenheit), nameof(Fahrenheit) when input is RankineString castInput => StringParser(castInput.Temperature, RankineConverter.RankineToFahrenheit), nameof(Kelvin) when input is CelsiusString castInput => StringParser(castInput.Temperature, CelsiusConverter.CelsiusToKelvin), nameof(Kelvin) when input is FahrenheitString castInput => StringParser(castInput.Temperature, FahrenheitConverter.FahrenheitToKelvin), nameof(Kelvin) when input is KelvinString castInput => StringParser(castInput.Temperature, KelvinConverter.KelvinToKelvin), nameof(Kelvin) when input is GasString castInput => StringParser(castInput.Temperature, GasConverter.GasToKelvin), nameof(Kelvin) when input is RankineString castInput => StringParser(castInput.Temperature, RankineConverter.RankineToKelvin), nameof(Gas) when input is CelsiusString castInput => StringParser(castInput.Temperature, CelsiusConverter.CelsiusToGas), nameof(Gas) when input is FahrenheitString castInput => StringParser(castInput.Temperature, FahrenheitConverter.FahrenheitToGas), nameof(Gas) when input is KelvinString castInput => StringParser(castInput.Temperature, KelvinConverter.KelvinToGas), nameof(Gas) when input is GasString castInput => StringParser(castInput.Temperature, GasConverter.GasToGas), nameof(Gas) when input is RankineString castInput => StringParser(castInput.Temperature, RankineConverter.RankineToGas), nameof(Rankine) when input is CelsiusString castInput => StringParser(castInput.Temperature, CelsiusConverter.CelsiusToRankine), nameof(Rankine) when input is FahrenheitString castInput => StringParser(castInput.Temperature, FahrenheitConverter.FahrenheitToRankine), nameof(Rankine) when input is KelvinString castInput => StringParser(castInput.Temperature, KelvinConverter.KelvinToRankine), nameof(Rankine) when input is GasString castInput => StringParser(castInput.Temperature, GasConverter.GasToRankine), nameof(Rankine) when input is RankineString castInput => StringParser(castInput.Temperature, RankineConverter.RankineToRankine), _ => throw new ArgumentException($"Invalid type: {typeof(TInput).Name}") });
public void Test_to_gas_generic_from_celsius_throws_out_of_range_exception_too_high() { // Arrange. var input = new CelsiusString("271"); // Act. var result = Assert.Throws <ArgumentOutOfRangeException>(() => input.To <Gas>()); // Assert. result.Message.Should().Contain("Temp too high for gas mark!"); }
public void Test_to_rankine_generic_from_celsius_with_invalid_parameter_throws_exception(double input) { // Arrange. var inputCelsius = new CelsiusString(input.ToString(CultureInfo.InvariantCulture)); // Act. var result = Assert.Throws <ArgumentOutOfRangeException>(() => inputCelsius.To <Rankine>()); // Assert. result.Message.Should().Contain("Value out of range for type."); }
public void Test_to_celsius_generic_from_celsius_returns_same_value() { // Arrange. var input = new CelsiusString("42"); // Act. var result = input.To <Celsius>(); // Assert. result.Should().Be(input.Temperature); }
public void Test_to_rankine_generic_from_celsius_returns_correct_value() { // Arrange. const string expected = "851.6699999999998"; var input = new CelsiusString("200"); // Act. var result = input.To <Rankine>(); // Assert. result.Should().Be(expected); }
public void Test_to_kelvin_generic_from_celsius_returns_correct_value() { // Arrange. const string expected = "473.15"; var input = new CelsiusString("200"); // Act. var result = input.To <Kelvin>(); // Assert. result.Should().Be(expected); }
public void Test_to_gas_from_celsius_returns_correct_value() { // Arrange. const string expected = "6"; var input = new CelsiusString("200"); // Act. var result = input.ToGas(); // Assert. result.Should().Be(expected); }
public void Test_to_fahrenheit_generic_from_celsius_returns_same_value() { // Arrange. const string expected = "53.6"; var input = new CelsiusString("12"); // Act. var result = input.To <Fahrenheit>(); // Assert. result.Should().Be(expected); }
/// <summary> /// Converts the Celsius <paramref name="input"/> to FahrenheitConverter /// </summary> /// <param name="input"> The value to be converted. </param> /// <exception cref="T:System.ArgumentOutOfRangeException">If calculated value is beyond the limits of the type.</exception> /// <returns> /// The FahrenheitConverter string result. /// </returns> public static string ToFahrenheit(this CelsiusString input) { return(StringParser(input.Temperature, CelsiusConverter.CelsiusToFahrenheit)); }
/// <summary> /// Converts the Celsius <paramref name="input"/> to RankineConverter /// </summary> /// <param name="input"> The value to be converted. </param> /// <exception cref="T:System.ArgumentOutOfRangeException">If calculated value is beyond the limits of the type.</exception> /// <returns> /// The RankineConverter string result. /// </returns> public static string ToRankine(this CelsiusString input) { return(StringParser(input.Temperature, CelsiusConverter.CelsiusToRankine)); }