Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IntegerPropertyViewModelTests.cs « Xamarin.PropertyEditing.Tests - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 23390df9e5c5aa05a795686e2b75cef9da9cbc41 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using Moq;
using NUnit.Framework;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Tests
{
	[TestFixture]
	internal class IntegerPropertyViewModelTests
		: NumericViewModelTests<long, long>
	{
		protected override Tuple<long, long> MaxMin => new Tuple<long, long> (Int64.MaxValue, Int64.MinValue);

		protected override long GetRandomTestValue (Random rand)
		{
			return rand.Next ();
		}

		protected override long GetConstrainedRandomValue (Random rand, out long max, out long min)
		{
			int value = rand.Next (2, Int32.MaxValue - 2);
			max = rand.Next (value + 1, Int32.MaxValue);
			min = rand.Next (0, value - 1);
			return value;
		}

		protected override long GetConstrainedRandomValueAboveBounds (Random rand, out long max, out long min)
		{
			int value = rand.Next (2, Int32.MaxValue - 2);
			min = rand.Next (0, value - 1);
			max = rand.Next ((int)min + 1, value - 1);

			return value;
		}

		protected override long GetConstrainedRandomValueBelowBounds (Random rand, out long max, out long min)
		{
			int value = rand.Next (2, Int32.MaxValue - 2);
			max = rand.Next (value + 1, Int32.MaxValue);
			min = rand.Next (value + 1, (int)max - 1);

			return value;
		}

		protected override NumericPropertyViewModel<long> GetViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors)
		{
			return new NumericPropertyViewModel<long> (platform, property, editors);
		}
	}

	[TestFixture]
	internal class NullableIntegerPropertyViewModelTests
		: NumericViewModelTests<long?, long>
	{
		// TODO: we can handle value disagreements with null as well

		[Test]
		[Description ("Even if we say its nullable, the IPropertyInfo type and source are the actual controllers")]
		public void ConstrainedToNonNull ()
		{
			var property = GetPropertyMock ();
			// As long as IPropertyInfo Type is non-nullable AND we report supporting the Default source, we should act non-nullable
			property.SetupGet (ip => ip.ValueSources).Returns (ValueSources.Local | ValueSources.Default);
			property.SetupGet (ip => ip.Type).Returns (typeof(long)); // override base long?

			var vm = GetViewModel (property.Object, new MockObjectEditor (property.Object));
			Assert.That (vm.Value, Is.EqualTo (0));

			vm.Value = null;
			Assert.That (vm.Value, Is.EqualTo (0));
		}

		[Test]
		public void NonNullableButUnset ()
		{
			var property = GetPropertyMock ();
			// Even if we say we're non-nullable, if we need to support Unset (!Default), we should act nullable.
			property.SetupGet (ip => ip.ValueSources).Returns (ValueSources.Local);
			property.SetupGet (ip => ip.Type).Returns (typeof(long)); // override base long?

			var vm = GetViewModel (property.Object, new MockObjectEditor (property.Object));
			Assert.That (vm.Value, Is.EqualTo (null));

			vm.Value = 5;
			Assume.That (vm.Value, Is.EqualTo (5));

			vm.Value = null;
			Assert.That (vm.Value, Is.EqualTo (null));
		}

		[Test]
		public void NullableEvenWithDefault ()
		{
			var property = GetPropertyMock ();
			// If the property says its nullable, its nullable.
			property.SetupGet (ip => ip.ValueSources).Returns (ValueSources.Local | ValueSources.Default);
			property.SetupGet (ip => ip.Type).Returns (typeof(long?)); // override base long?

			var vm = GetViewModel (property.Object, new MockObjectEditor (property.Object));
			Assert.That (vm.Value, Is.EqualTo (null));

			vm.Value = 5;
			Assume.That (vm.Value, Is.EqualTo (5));

			vm.Value = null;
			Assert.That (vm.Value, Is.EqualTo (null));
		}

		protected override Tuple<long?, long?> MaxMin => new Tuple<long?, long?> (Int64.MaxValue, Int64.MinValue);

		protected override long? GetRandomTestValue (Random rand)
		{
			return rand.Next ();
		}

		protected override long? GetConstrainedRandomValue (Random rand, out long? max, out long? min)
		{
			int value = rand.Next (2, Int32.MaxValue - 2);
			max = rand.Next (value + 1, Int32.MaxValue);
			min = rand.Next (0, value - 1);
			return value;
		}

		protected override long? GetConstrainedRandomValueAboveBounds (Random rand, out long? max, out long? min)
		{
			int value = rand.Next (2, Int32.MaxValue - 2);
			min = rand.Next (0, value - 1);
			max = rand.Next ((int)min + 1, value - 1);

			return value;
		}

		protected override long? GetConstrainedRandomValueBelowBounds (Random rand, out long? max, out long? min)
		{
			int value = rand.Next (2, Int32.MaxValue - 2);
			max = rand.Next (value + 1, Int32.MaxValue);
			min = rand.Next (value + 1, (int)max - 1);

			return value;
		}

		protected override NumericPropertyViewModel<long?> GetViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors)
		{
			return new NumericPropertyViewModel<long?> (platform, property, editors);
		}
	}
}