-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added temperature conversions now, turned out meh.
- Loading branch information
1 parent
47ebfb9
commit c238ca7
Showing
27 changed files
with
1,164 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace Gu.Units.Generator.Tests | ||
{ | ||
using NUnit.Framework; | ||
|
||
public class ConversionFormulaTests | ||
{ | ||
private MockSettings _settings; | ||
[SetUp] | ||
public void SetUp() | ||
{ | ||
_settings = new MockSettings(); | ||
} | ||
|
||
[Test] | ||
public void TrivialConversion() | ||
{ | ||
var conversionFormula = new ConversionFormula(_settings.Metres) | ||
{ | ||
ConversionFactor = 1 | ||
}; | ||
Assert.AreEqual("Metres", conversionFormula.ToSi); | ||
Assert.AreEqual("Metres", conversionFormula.FromSi); | ||
} | ||
|
||
[Test] | ||
public void FactorConversion() | ||
{ | ||
var conversionFormula = new ConversionFormula(_settings.Metres) | ||
{ | ||
ConversionFactor = 1 | ||
}; | ||
Assert.AreEqual("Metres", conversionFormula.ToSi); | ||
Assert.AreEqual("Metres", conversionFormula.FromSi); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Gu.Units.Generator.Tests | ||
{ | ||
using NUnit.Framework; | ||
using WpfStuff; | ||
|
||
public class StringToFormulaConverterTests | ||
{ | ||
[TestCase("10*x")] | ||
public void SimpleFactorTest(string s) | ||
{ | ||
var converter = new StringToFormulaConverter(); | ||
Assert.True(converter.CanConvertFrom(null, typeof(string))); | ||
var formula = (ConversionFormula)converter.ConvertFrom(null, null, s); | ||
Assert.AreEqual(s, formula.ToSi); | ||
Assert.AreEqual("x/10", formula.FromSi); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
namespace Gu.Units.Generator | ||
{ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using System.Xml.Serialization; | ||
using Annotations; | ||
using WpfStuff; | ||
|
||
[TypeConverter(typeof(StringToFormulaConverter))] | ||
public class ConversionFormula : INotifyPropertyChanged | ||
{ | ||
private readonly IUnit _baseUnit; | ||
private ConversionFormula() | ||
{ | ||
} | ||
|
||
public ConversionFormula(IUnit baseUnit) | ||
{ | ||
_baseUnit = baseUnit; | ||
} | ||
|
||
public event PropertyChangedEventHandler PropertyChanged; | ||
|
||
public double ConversionFactor { get; set; } | ||
|
||
public double Offset { get; set; } | ||
|
||
[XmlIgnore] | ||
public string ToSi | ||
{ | ||
get | ||
{ | ||
var builder = new StringBuilder(); | ||
if (ConversionFactor != 1) | ||
{ | ||
builder.Append(ConversionFactor.ToString(CultureInfo.InvariantCulture) + "*"); | ||
} | ||
builder.Append(_baseUnit != null ? _baseUnit.Quantity.Unit.ClassName : "x"); | ||
if (Offset != 0) | ||
{ | ||
if (Offset > 0) | ||
{ | ||
builder.AppendFormat("+{0}", Offset.ToString(CultureInfo.InvariantCulture)); | ||
} | ||
else | ||
{ | ||
builder.AppendFormat(Offset.ToString(CultureInfo.InvariantCulture)); | ||
} | ||
} | ||
return builder.ToString(); | ||
} | ||
} | ||
|
||
[XmlIgnore] | ||
public string FromSi | ||
{ | ||
get | ||
{ | ||
var builder = new StringBuilder(); | ||
|
||
builder.Append(_baseUnit != null ? _baseUnit.Quantity.Unit.ClassName : "x"); | ||
if (ConversionFactor != 1) | ||
{ | ||
builder.Append( "/"+ConversionFactor.ToString(CultureInfo.InvariantCulture)); | ||
} | ||
if (Offset != 0) | ||
{ | ||
if (Offset < 0) | ||
{ | ||
builder.AppendFormat("+{0}",(-1* Offset).ToString(CultureInfo.InvariantCulture)); | ||
} | ||
else | ||
{ | ||
builder.AppendFormat((-1*Offset).ToString(CultureInfo.InvariantCulture)); | ||
} | ||
} | ||
return builder.ToString(); | ||
} | ||
} | ||
|
||
[XmlIgnore] | ||
public bool CanRountrip | ||
{ | ||
get | ||
{ | ||
throw new NotImplementedException("message"); | ||
} | ||
} | ||
|
||
[NotifyPropertyChangedInvocator] | ||
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) | ||
{ | ||
var handler = PropertyChanged; | ||
if (handler != null) | ||
{ | ||
handler(this, new PropertyChangedEventArgs(propertyName)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Gu.Units.Generator/Descriptors/Methods.cs → Gu.Units.Generator/Descriptors/Names.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.