Skip to content

Commit

Permalink
Added temperature conversions now, turned out meh.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Oct 22, 2014
1 parent 47ebfb9 commit c238ca7
Show file tree
Hide file tree
Showing 27 changed files with 1,164 additions and 56 deletions.
36 changes: 36 additions & 0 deletions Gu.Units.Generator.Tests/ConversionFormulaTests.cs
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);
}
}
}
2 changes: 2 additions & 0 deletions Gu.Units.Generator.Tests/Gu.Units.Generator.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConversionFormulaTests.cs" />
<Compile Include="MockSettings.cs" />
<Compile Include="OperatorTests.cs" />
<Compile Include="QuantityTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringToFormulaConverterTests.cs" />
<Compile Include="UnitPartsConverterTests.cs" />
<Compile Include="UnitPartsTests.cs" />
</ItemGroup>
Expand Down
18 changes: 18 additions & 0 deletions Gu.Units.Generator.Tests/StringToFormulaConverterTests.cs
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);
}
}
}
34 changes: 25 additions & 9 deletions Gu.Units.Generator/Descriptors/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@
public class Conversion : TypeMetaData, IUnit
{
private readonly ObservableCollection<Conversion> _conversions = new ObservableCollection<Conversion>();
private readonly CodeDomProvider _provider = CodeDomProvider.CreateProvider("C#");
private readonly CodeDomProvider _codeDomProvider = CodeDomProvider.CreateProvider("C#");
private readonly ConversionFormula _formula;

private string _symbol;
private double _conversionFactor;
private Prefix _prefix;
private IUnit _baseUnit;

public Conversion()
{
_formula = new ConversionFormula(this);
}

public Conversion(string @namespace, string className, string symbol, double conversionFactor)
public Conversion(string @namespace, string className, string symbol)
: base(@namespace, className)
{
_symbol = symbol;
_conversionFactor = conversionFactor;
_formula = new ConversionFormula(this);
}

public string Symbol
Expand All @@ -49,19 +50,31 @@ public double ConversionFactor
{
get
{
return _conversionFactor;
return _formula.ConversionFactor;
}
set
{
if (value == _conversionFactor)
if (value == _formula.ConversionFactor)
{
return;
}
_conversionFactor = value;
_formula.ConversionFactor = value;
this.OnPropertyChanged();
}
}

public ConversionFormula Formula
{
get { return _formula; }
set
{
_formula.ConversionFactor = value.ConversionFactor;
_formula.Offset = value.Offset;
OnPropertyChanged();
OnPropertyChanged("ConversionFactor");
}
}

[XmlIgnore]
public string QuantityName
{
Expand All @@ -86,7 +99,6 @@ public Quantity Quantity
{
return BaseUnit.Quantity;
}

set { throw new NotImplementedException(); }
}

Expand Down Expand Up @@ -138,12 +150,16 @@ public ObservableCollection<Conversion> Conversions
{
get { return _conversions; }
}
public bool AnyOffsetConversion
{
get { return Conversions.Any(x => x.Formula.Offset != 0); }
}

public bool IsSymbolNameValid
{
get
{
return _provider.IsValidIdentifier(Symbol);
return _codeDomProvider.IsValidIdentifier(Symbol);
}
}

Expand Down
103 changes: 103 additions & 0 deletions Gu.Units.Generator/Descriptors/ConversionFormula.cs
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));
}
}
}
}
1 change: 1 addition & 0 deletions Gu.Units.Generator/Descriptors/IUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface IUnit : INotifyPropertyChanged
bool IsEmpty { get; }
string UiName { get; }
ObservableCollection<Conversion> Conversions { get; }
bool AnyOffsetConversion { get; }
bool IsSymbolNameValid { get; }
Settings Settings { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Gu.Units.Generator
{
public static class Methods
public static class Names
{
public static readonly string ToSiUnit = "ToSiUnit";
public static readonly string FromSiUnit = "FromSiUnit";
Expand Down
7 changes: 7 additions & 0 deletions Gu.Units.Generator/Descriptors/UnitBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System.CodeDom.Compiler;
using System.Collections.ObjectModel;
using System.Linq;
using System.Xml.Serialization;
using WpfStuff;

Expand Down Expand Up @@ -67,6 +68,12 @@ public ObservableCollection<Conversion> Conversions
}
}

[XmlIgnore]
public bool AnyOffsetConversion
{
get { return Conversions.Any(x => x.Formula.Offset != 0); }
}

[XmlIgnore]
public Quantity Quantity
{
Expand Down
Loading

0 comments on commit c238ca7

Please sign in to comment.