-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathContentstackRegionConverterUnitTests.cs
More file actions
146 lines (123 loc) · 4.99 KB
/
ContentstackRegionConverterUnitTests.cs
File metadata and controls
146 lines (123 loc) · 4.99 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
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
using System;
using System.ComponentModel;
using System.Globalization;
using Contentstack.Core.Configuration;
using Contentstack.Core.Internals;
using Xunit;
namespace Contentstack.Core.Unit.Tests
{
public class ContentstackRegionConverterUnitTests
{
private readonly ContentstackRegionConverter _converter = new ContentstackRegionConverter();
[Fact]
public void CanConvertFrom_String_ReturnsTrue()
{
// Act
var result = _converter.CanConvertFrom(null, typeof(string));
// Assert
Assert.True(result);
}
[Fact]
public void CanConvertFrom_NonString_ReturnsFalse()
{
// Act
var result = _converter.CanConvertFrom(null, typeof(int));
// Assert
Assert.False(result);
}
[Fact]
public void ConvertFrom_ValidRegionString_ThrowsNotSupportedException()
{
// Arrange
var regionString = "US";
// Act & Assert
// The converter only parses when nonDigitIndex > 0 (i.e., there's a prefix)
// For "US", nonDigitIndex is 0, so it doesn't parse and throws
Assert.Throws<NotSupportedException>(() =>
_converter.ConvertFrom(null, CultureInfo.InvariantCulture, regionString));
}
[Fact]
public void ConvertFrom_ValidRegionStringWithPrefix_ThrowsNotSupportedException()
{
// Arrange
var regionString = "eu-EU";
// Act & Assert
// The converter looks for first letter ('e') which is at index 0
// nonDigitIndex is 0, which is NOT > 0, so it doesn't parse and throws
Assert.Throws<NotSupportedException>(() =>
_converter.ConvertFrom(null, CultureInfo.InvariantCulture, regionString));
}
[Fact]
public void ConvertFrom_ValidRegionStringLowerCase_ThrowsNotSupportedException()
{
// Arrange
var regionString = "us";
// Act & Assert
// For "us", nonDigitIndex is 0, so it doesn't parse and throws
Assert.Throws<NotSupportedException>(() =>
_converter.ConvertFrom(null, CultureInfo.InvariantCulture, regionString));
}
[Fact]
public void ConvertFrom_ValidRegionStringMixedCase_ThrowsNotSupportedException()
{
// Arrange
var regionString = "Eu";
// Act & Assert
// For "Eu", nonDigitIndex is 0, so it doesn't parse and throws
Assert.Throws<NotSupportedException>(() =>
_converter.ConvertFrom(null, CultureInfo.InvariantCulture, regionString));
}
[Fact]
public void ConvertFrom_InvalidRegionString_ThrowsNotSupportedException()
{
// Arrange
var regionString = "INVALID";
// Act & Assert
// When nonDigitIndex <= 0 or parsing fails, it calls base.ConvertFrom which throws
Assert.Throws<NotSupportedException>(() =>
_converter.ConvertFrom(null, CultureInfo.InvariantCulture, regionString));
}
[Fact]
public void ConvertFrom_EmptyString_ThrowsNotSupportedException()
{
// Arrange
var regionString = "";
// Act & Assert
// Empty string results in null, which calls base.ConvertFrom and throws
Assert.Throws<NotSupportedException>(() =>
_converter.ConvertFrom(null, CultureInfo.InvariantCulture, regionString));
}
[Fact]
public void ConvertFrom_Null_ThrowsNotSupportedException()
{
// Act & Assert
Assert.Throws<NotSupportedException>(() =>
_converter.ConvertFrom(null, CultureInfo.InvariantCulture, null));
}
[Fact]
public void ConvertFrom_StringWithOnlyDigits_ThrowsNotSupportedException()
{
// Arrange
var regionString = "123";
// Act & Assert
Assert.Throws<NotSupportedException>(() =>
_converter.ConvertFrom(null, CultureInfo.InvariantCulture, regionString));
}
[Theory]
[InlineData("123-US", ContentstackRegion.US)]
[InlineData("123-EU", ContentstackRegion.EU)]
[InlineData("123-AZURE_EU", ContentstackRegion.AZURE_EU)]
[InlineData("123-AZURE_NA", ContentstackRegion.AZURE_NA)]
[InlineData("123-GCP_NA", ContentstackRegion.GCP_NA)]
[InlineData("123-AU", ContentstackRegion.AU)]
public void ConvertFrom_AllRegionsWithNumericPrefix_ReturnsCorrectRegion(string input, ContentstackRegion expected)
{
// Act
// The converter finds first letter which is after the digits
// nonDigitIndex > 0, so it parses from there
var result = _converter.ConvertFrom(null, CultureInfo.InvariantCulture, input);
// Assert
Assert.Equal(expected, result);
}
}
}