public void Int64Test() { var parser1 = StringInputParsers.GetStringParserFor(typeof(long).FullName); var parser2 = StringInputParsers.GetStringParserFor(typeof(long).FullName.ToShorterName()); Assert.Equal((long)10, parser1("10")); Assert.Equal((long)10, parser2("10")); Assert.Equal((long)16, parser1("0x10")); Assert.Equal((long)16, parser2("0x10")); Assert.ThrowsAny <Exception>(() => parser1("invalid input")); Assert.ThrowsAny <Exception>(() => parser2("invalid input")); string max = long.MaxValue.ToString(); Assert.Equal(long.MaxValue, parser1(max)); Assert.Equal(long.MaxValue, parser2(max)); string min = long.MinValue.ToString(); Assert.Equal(long.MinValue, parser1(min)); Assert.Equal(long.MinValue, parser2(min)); string tooBig = (long.MaxValue).ToString() + "1"; Assert.ThrowsAny <Exception>(() => parser1(tooBig)); Assert.ThrowsAny <Exception>(() => parser2(tooBig)); }
public void BoolTest() { var parser1 = StringInputParsers.GetStringParserFor(typeof(bool).FullName); var parser2 = StringInputParsers.GetStringParserFor(typeof(bool).FullName.ToShorterName()); Assert.True((bool)parser1("true")); Assert.True((bool)parser2("true")); Assert.False((bool)parser1("false")); Assert.False((bool)parser2("false")); Assert.ThrowsAny <Exception>(() => parser1("invalid input")); Assert.ThrowsAny <Exception>(() => parser2("invalid input")); }
public void BytesTest() { var parser1 = StringInputParsers.GetStringParserFor(typeof(byte[]).FullName); var parser2 = StringInputParsers.GetStringParserFor(typeof(byte[]).FullName.ToShorterName()); Assert.Equal(new byte[] { 0x10, 0x10 }, parser1("1010")); Assert.Equal(new byte[] { 0x10, 0x10 }, parser2("1010")); Assert.Equal(new byte[] { 0x10, 0x10 }, parser1("0x1010")); Assert.Equal(new byte[] { 0x10, 0x10 }, parser2("0x1010")); Assert.ThrowsAny <Exception>(() => parser1("invalid input")); Assert.ThrowsAny <Exception>(() => parser2("invalid input")); }
public void HashTest() { var parser = StringInputParsers.GetStringParserFor(typeof(Address).FullName); // TODO: Value has to be a fixed length var hash = Address.FromRawBytes(Hash.Generate().ToByteArray()); var hashHex = BitConverter.ToString(hash.DumpByteArray()).Replace("-", ""); // Note: Hash has the same structure as BytesValue, hence using BytesValue for serialization. // So that we don't need dependency AElf.Kernel. Assert.Equal(hash.ToByteArray(), ((IMessage)parser(hashHex)).ToByteArray()); Assert.Equal(hash.ToByteArray(), ((IMessage)parser("0x" + hashHex)).ToByteArray()); // Lowercase Assert.Equal(hash.ToByteArray(), ((IMessage)parser(hashHex.ToLower())).ToByteArray()); Assert.Equal(hash.ToByteArray(), ((IMessage)parser("0x" + hashHex.ToLower())).ToByteArray()); var tooShort = "0x101010"; Assert.ThrowsAny <Exception>(() => parser(tooShort)); var tooLong = hashHex + "01"; Assert.ThrowsAny <Exception>(() => parser(tooLong)); }