-Fixed tests

-Changed JTokenReader to provide line numbers for end tokens
This commit is contained in:
James Newton-King
2014-10-19 18:29:34 +13:00
parent 15906f1f36
commit c7f8d85cc4
8 changed files with 153 additions and 26 deletions
+1 -1
View File
@@ -162,7 +162,7 @@ function CoreClrTests($build)
Write-Host -ForegroundColor Green "Ensuring latest CoreCLR is installed for $name"
Write-Host
#exec { & $toolsDir\Kvm\kvm.ps1 upgrade -r CoreCLR | Out-Default }
exec { & $toolsDir\Kvm\kvm.ps1 upgrade -r CoreCLR -NoNative | Out-Default }
Write-Host -ForegroundColor Green "Restoring packages for $name"
Write-Host
+1
View File
@@ -1,3 +1,4 @@
cls
powershell -Command "& { [Console]::WindowWidth = 150; [Console]::WindowHeight = 50; Start-Transcript runbuild.txt; Import-Module ..\Tools\PSake\psake.psm1; Invoke-psake ..\Build\build.ps1 %*; Stop-Transcript; }"
pause
@@ -74,6 +74,52 @@ namespace Newtonsoft.Json.Tests.Bson
}
#endif
public class MyTest
{
public DateTime TimeStamp { get; set; }
public string UserName { get; set; }
public MemoryStream Blob { get; set; }
}
public void Bson_SupportMultipleContent()
{
MemoryStream myStream = new MemoryStream();
BsonWriter writer = new BsonWriter(myStream);
JsonSerializer serializer = new JsonSerializer();
MyTest tst1 = new MyTest
{
TimeStamp = new DateTime(2000, 12, 20, 12, 59, 59, DateTimeKind.Utc),
UserName = "Joe Doe"
};
MyTest tst2 = new MyTest
{
TimeStamp = new DateTime(2010, 12, 20, 12, 59, 59, DateTimeKind.Utc),
UserName = "Bob"
};
serializer.Serialize(writer, tst1);
serializer.Serialize(writer, tst2);
myStream.Seek(0, SeekOrigin.Begin);
BsonReader reader = new BsonReader(myStream)
{
SupportMultipleContent = true,
DateTimeKindHandling = DateTimeKind.Utc
};
MyTest tst1A = serializer.Deserialize<MyTest>(reader);
reader.Read();
MyTest tst2A = serializer.Deserialize<MyTest>(reader);
Assert.AreEqual(tst1.UserName, tst1A.UserName);
Assert.AreEqual(tst1.TimeStamp, tst1A.TimeStamp);
Assert.AreEqual(tst2.UserName, tst2A.UserName);
Assert.AreEqual(tst2.TimeStamp, tst2A.TimeStamp);
}
[Test]
public void CloseInput()
{
@@ -2360,9 +2360,27 @@ third line", jsonTextReader.Value);
[Test]
public void SupportMultipleContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"1 2 ""name"" [][]null {}{} 1.1"));
JsonTextReader reader = new JsonTextReader(new StringReader(@"{'prop1':[1]} 1 2 ""name"" [][]null {}{} 1.1"));
reader.SupportMultipleContent = true;
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
@@ -270,15 +270,15 @@ namespace Newtonsoft.Json.Tests.Linq
jsonReader.Read();
Assert.AreEqual(jsonReader.TokenType, JsonToken.EndArray);
Assert.AreEqual(0, lineInfo.LineNumber);
Assert.AreEqual(0, lineInfo.LinePosition);
Assert.AreEqual(false, lineInfo.HasLineInfo());
Assert.AreEqual(3, lineInfo.LineNumber);
Assert.AreEqual(12, lineInfo.LinePosition);
Assert.AreEqual(true, lineInfo.HasLineInfo());
jsonReader.Read();
Assert.AreEqual(jsonReader.TokenType, JsonToken.EndObject);
Assert.AreEqual(0, lineInfo.LineNumber);
Assert.AreEqual(0, lineInfo.LinePosition);
Assert.AreEqual(false, lineInfo.HasLineInfo());
Assert.AreEqual(1, lineInfo.LineNumber);
Assert.AreEqual(1, lineInfo.LinePosition);
Assert.AreEqual(true, lineInfo.HasLineInfo());
jsonReader.Read();
Assert.AreEqual(jsonReader.TokenType, JsonToken.None);
@@ -107,18 +107,19 @@ namespace Newtonsoft.Json.Tests.Schema
[Test]
public void ValidateFailureWithOutLineInfoBecauseOfEndToken()
{
// changed in 6.0.6 to now include line info!
JsonSchema schema = JsonSchema.Parse("{'properties':{'lol':{'required':true}}}");
JObject o = JObject.Parse("{}");
List<string> errors = new List<string>();
o.Validate(schema, (sender, args) => errors.Add(args.Message));
Assert.AreEqual("Required properties are missing from object: lol.", errors[0]);
Assert.AreEqual("Required properties are missing from object: lol. Line 1, position 1.", errors[0]);
Assert.AreEqual(1, errors.Count);
}
[Test]
public void ValidateFailureWithLineInfo()
public void ValidateRequiredFieldsWithLineInfo()
{
JsonSchema schema = JsonSchema.Parse("{'properties':{'lol':{'type':'string'}}}");
JObject o = JObject.Parse("{'lol':1}");
@@ -23,8 +23,9 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if !(PORTABLE || ASPNETCORE50 || PORTABLE40)
#if !(NET35 || NET20 || PORTABLE || ASPNETCORE50)
#if !(PORTABLE || PORTABLE40)
using System.Collections.ObjectModel;
#if !(NET35 || NET20)
using System.Dynamic;
#endif
using System.Text;
@@ -58,6 +59,71 @@ namespace Newtonsoft.Json.Tests.Serialization
[TestFixture]
public class TypeNameHandlingTests : TestFixtureBase
{
#if !(NET20 || NET35 || NET40)
public class KnownAutoTypes
{
public ICollection<string> Collection { get; set; }
public IList<string> List { get; set; }
public IDictionary<string, string> Dictionary { get; set; }
public ISet<string> Set { get; set; }
public IReadOnlyCollection<string> ReadOnlyCollection { get; set; }
public IReadOnlyList<string> ReadOnlyList { get; set; }
public IReadOnlyDictionary<string, string> ReadOnlyDictionary { get; set; }
}
[Test]
public void KnownAutoTypesTest()
{
KnownAutoTypes c = new KnownAutoTypes
{
Collection = new List<string> { "Collection value!" },
List = new List<string> { "List value!" },
Dictionary = new Dictionary<string, string>
{
{ "Dictionary key!", "Dictionary value!" }
},
ReadOnlyCollection = new ReadOnlyCollection<string>(new[] { "Read Only Collection value!" }),
ReadOnlyList = new ReadOnlyCollection<string>(new[] { "Read Only List value!" }),
Set = new HashSet<string> { "Set value!" },
ReadOnlyDictionary = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>
{
{ "Read Only Dictionary key!", "Read Only Dictionary value!" }
})
};
string json = JsonConvert.SerializeObject(c, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
Console.WriteLine(json);
StringAssert.AreEqual(@"{
""Collection"": [
""Collection value!""
],
""List"": [
""List value!""
],
""Dictionary"": {
""Dictionary key!"": ""Dictionary value!""
},
""Set"": [
""Set value!""
],
""ReadOnlyCollection"": [
""Read Only Collection value!""
],
""ReadOnlyList"": [
""Read Only List value!""
],
""ReadOnlyDictionary"": {
""Read Only Dictionary key!"": ""Read Only Dictionary value!""
}
}", json);
}
#endif
[Test]
public void DictionaryAuto()
{
@@ -1035,7 +1101,7 @@ namespace Newtonsoft.Json.Tests.Serialization
CollectionAssert.AreEquivalent(data, d);
}
#if !(NETFX_CORE)
#if !(NETFX_CORE || ASPNETCORE50)
[Test]
public void ISerializableTypeNameHandlingTest()
{
@@ -1132,10 +1198,10 @@ namespace Newtonsoft.Json.Tests.Serialization
public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
assemblyName = "AssemblyName";
#if !NETFX_CORE
#if !(NETFX_CORE || ASPNETCORE50)
typeName = ":::" + serializedType.Name.ToUpper(CultureInfo.InvariantCulture) + ":::";
#else
typeName = ":::" + serializedType.Name.ToUpper() + ":::";
typeName = ":::" + serializedType.Name.ToUpper() + ":::";
#endif
}
}
@@ -1577,7 +1643,7 @@ namespace Newtonsoft.Json.Tests.Serialization
}
#endif
#if !NETFX_CORE
#if !(NETFX_CORE || ASPNETCORE50)
[Test]
public void SerializeDeserialize_DictionaryContextContainsGuid_DeserializesItemAsGuid()
{
@@ -1772,7 +1838,7 @@ namespace Newtonsoft.Json.Tests.Serialization
#endif
}
#if !NETFX_CORE
#if !(NETFX_CORE || ASPNETCORE50)
public class ParentParent
{
[JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto)]
@@ -1840,7 +1906,7 @@ namespace Newtonsoft.Json.Tests.Serialization
public int Quantity { get; set; }
}
#if !(NETFX_CORE)
#if !(NETFX_CORE || ASPNETCORE50)
public class SerializableWrapper
{
public object Content { get; set; }
+3 -8
View File
@@ -169,11 +169,6 @@ namespace Newtonsoft.Json.Linq
return false;
}
private bool IsEndElement
{
get { return (_current == _parent); }
}
private JsonToken? GetEndToken(JContainer c)
{
switch (c.Type)
@@ -293,7 +288,7 @@ namespace Newtonsoft.Json.Linq
if (CurrentState == State.Start)
return false;
IJsonLineInfo info = IsEndElement ? null : _current;
IJsonLineInfo info = _current;
return (info != null && info.HasLineInfo());
}
@@ -304,7 +299,7 @@ namespace Newtonsoft.Json.Linq
if (CurrentState == State.Start)
return 0;
IJsonLineInfo info = IsEndElement ? null : _current;
IJsonLineInfo info = _current;
if (info != null)
return info.LineNumber;
@@ -319,7 +314,7 @@ namespace Newtonsoft.Json.Linq
if (CurrentState == State.Start)
return 0;
IJsonLineInfo info = IsEndElement ? null : _current;
IJsonLineInfo info = _current;
if (info != null)
return info.LinePosition;