forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringConverterUtilsTests.cs
More file actions
95 lines (84 loc) · 2.47 KB
/
Copy pathStringConverterUtilsTests.cs
File metadata and controls
95 lines (84 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
#if !MONOTOUCH
using ServiceStack.ServiceInterface.ServiceModel;
#endif
using ServiceStack.Text;
namespace ServiceStack.ServiceModel.Tests
{
[TestFixture]
public class StringConverterUtilsTests
{
public class StringEnumerable : IEnumerable<string>
{
public List<string> Items = new[] { "a", "b", "c" }.ToList();
public IEnumerator<string> GetEnumerator()
{
return Items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public static StringEnumerable ParseJsv(string value)
{
return new StringEnumerable {
Items = value.To<List<string>>()
};
}
}
#if !MONOTOUCH
[Test]
public void Create_super_list_type_of_int_from_string()
{
var textValue = "1,2,3";
var convertedValue = textValue.Split(',').ToList().ConvertAll(x => Convert.ToInt32(x));
var result = TypeSerializer.DeserializeFromString<ArrayOfIntId>(textValue);
Assert.That(result, Is.EquivalentTo(convertedValue));
}
#endif
[Test]
public void Create_guid_from_string()
{
var textValue = "40DFA5A2-8054-4b3e-B7F5-06E61FF387EF";
var convertedValue = new Guid(textValue);
var result = TypeSerializer.DeserializeFromString<Guid>(textValue);
Assert.That(result, Is.EqualTo(convertedValue));
}
[Test]
public void Create_int_from_string()
{
var textValue = "99";
var convertedValue = int.Parse(textValue);
var result = TypeSerializer.DeserializeFromString<int>(textValue);
Assert.That(result, Is.EqualTo(convertedValue));
}
[Test]
public void Create_bool_from_string()
{
var textValue = "True";
var convertedValue = bool.Parse(textValue);
var result = TypeSerializer.DeserializeFromString<bool>(textValue);
Assert.That(result, Is.EqualTo(convertedValue));
}
[Test]
public void Create_string_array_from_string()
{
var convertedValue = new[] { "Hello", "World" };
var textValue = string.Join(",", convertedValue);
var result = TypeSerializer.DeserializeFromString<string[]>(textValue);
Assert.That(result, Is.EqualTo(convertedValue));
}
[Test]
public void Create_from_StringEnumerable()
{
var value = StringEnumerable.ParseJsv("d,e,f");
var convertedValue = TypeSerializer.SerializeToString(value);
var result = TypeSerializer.DeserializeFromString<StringEnumerable>(convertedValue);
Assert.That(result, Is.EquivalentTo(value.Items));
}
}
}