Skip to content

Commit

Permalink
Refactor: ShortTypeValue.Instance, IntegerTypeValue.Instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Jul 17, 2011
1 parent 129923a commit 26cd004
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
20 changes: 10 additions & 10 deletions AjVars.Tests/TypeValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TypeValueTests
[TestMethod]
public void IntegerTypeValueParseStrings()
{
TypeValue type = new IntegerTypeValue();
TypeValue type = IntegerTypeValue.Instance;

Assert.AreEqual(1, type.ParseString("1"));
Assert.AreEqual(1000, type.ParseString("1000"));
Expand All @@ -23,22 +23,22 @@ public void IntegerTypeValueParseStrings()
[ExpectedException(typeof(FormatException))]
public void RaiseIfTryParseFloatAsInteger()
{
TypeValue type = new IntegerTypeValue();
TypeValue type = IntegerTypeValue.Instance;
type.ParseString("1.2");
}

[TestMethod]
[ExpectedException(typeof(FormatException))]
public void RaiseIfTryParseTextAsInteger()
{
TypeValue type = new IntegerTypeValue();
TypeValue type = IntegerTypeValue.Instance;
type.ParseString("foo");
}

[TestMethod]
public void ShortTypeValueParseStrings()
{
TypeValue type = new ShortTypeValue();
TypeValue type = ShortTypeValue.Instance;

Assert.AreEqual((short) 1, type.ParseString("1"));
Assert.AreEqual((short) 1000, type.ParseString("1000"));
Expand All @@ -49,22 +49,22 @@ public void ShortTypeValueParseStrings()
[ExpectedException(typeof(FormatException))]
public void RaiseIfTryParseFloatAsShort()
{
TypeValue type = new ShortTypeValue();
TypeValue type = ShortTypeValue.Instance;
type.ParseString("1.2");
}

[TestMethod]
[ExpectedException(typeof(FormatException))]
public void RaiseIfTryParseTextAsShort()
{
TypeValue type = new ShortTypeValue();
TypeValue type = ShortTypeValue.Instance;
type.ParseString("foo");
}

[TestMethod]
public void IntegerToAndFromBytes()
{
BytesTypeValue type = new IntegerTypeValue();
BytesTypeValue type = IntegerTypeValue.Instance;

Assert.AreEqual(10, type.FromBytes(type.ToBytes(10)));
Assert.AreEqual(-1, type.FromBytes(type.ToBytes(-1)));
Expand All @@ -75,7 +75,7 @@ public void IntegerToAndFromBytes()
[TestMethod]
public void ShortToAndFromBytes()
{
BytesTypeValue type = new ShortTypeValue();
BytesTypeValue type = ShortTypeValue.Instance;

Assert.AreEqual((short)10, type.FromBytes(type.ToBytes((short)10)));
Assert.AreEqual((short)-1, type.FromBytes(type.ToBytes((short)-1)));
Expand All @@ -87,7 +87,7 @@ public void ShortToAndFromBytes()
public void IntegerToAndFromMemory()
{
ByteMemory memory = new ByteMemory();
TypeValue type = new IntegerTypeValue();
TypeValue type = IntegerTypeValue.Instance;

type.ToMemory(memory, 0, 10);
Assert.AreEqual(10, type.FromMemory(memory, 0));
Expand All @@ -103,7 +103,7 @@ public void IntegerToAndFromMemory()
public void ShortToAndFromMemory()
{
ByteMemory memory = new ByteMemory();
TypeValue type = new ShortTypeValue();
TypeValue type = ShortTypeValue.Instance;

type.ToMemory(memory, 0, (short) 10);
Assert.AreEqual((short) 10, type.FromMemory(memory, 0));
Expand Down
8 changes: 8 additions & 0 deletions AjVars/IntegerTypeValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

public class IntegerTypeValue : BytesTypeValue
{
private static IntegerTypeValue instance = new IntegerTypeValue();

private IntegerTypeValue()
{
}

public static IntegerTypeValue Instance { get { return instance; } }

public override short Size { get { return 4; } }

public override object ParseString(string text)
Expand Down
2 changes: 1 addition & 1 deletion AjVars/IntegerVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class IntegerVariable : Variable
{
public IntegerVariable(int address, ByteMemory memory)
: base(address, new IntegerTypeValue(), memory)
: base(address, IntegerTypeValue.Instance, memory)
{
}
}
Expand Down
8 changes: 8 additions & 0 deletions AjVars/ShortTypeValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

public class ShortTypeValue : BytesTypeValue
{
private static ShortTypeValue instance = new ShortTypeValue();

private ShortTypeValue()
{
}

public static ShortTypeValue Instance { get { return instance; } }

public override short Size { get { return 2; } }

public override object ParseString(string text)
Expand Down
2 changes: 1 addition & 1 deletion AjVars/ShortVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class ShortVariable : Variable
{
public ShortVariable(int address, ByteMemory memory)
: base(address, new ShortTypeValue(), memory)
: base(address, ShortTypeValue.Instance, memory)
{
}
}
Expand Down

0 comments on commit 26cd004

Please sign in to comment.