Skip to content

Commit fa51472

Browse files
committed
Add Case property to FormatConfig
1 parent e6bcc48 commit fa51472

4 files changed

Lines changed: 229 additions & 7 deletions

File tree

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
using SQL.Formatter.Core;
2+
using SQL.Formatter.Language;
3+
using Xunit;
4+
5+
namespace SQL.Formatter.Test
6+
{
7+
public class FormatConfigTest
8+
{
9+
private static readonly SqlFormatter.Formatter Formatter = SqlFormatter.Of(Dialect.StandardSql);
10+
11+
[Fact]
12+
public void Upper_FormatsKeywordsInUppercase()
13+
{
14+
var cfg = FormatConfig.Builder().Case(CaseTypes.UPPER).Build();
15+
var result = Formatter.Format("select id from users where id = 1", cfg);
16+
17+
Assert.Equal(
18+
"SELECT\n"
19+
+ " id\n"
20+
+ "FROM\n"
21+
+ " users\n"
22+
+ "WHERE\n"
23+
+ " id = 1",
24+
result);
25+
}
26+
27+
[Fact]
28+
public void Uppercase_FormatsKeywordsInUppercase()
29+
{
30+
#pragma warning disable CS0618
31+
var cfg = FormatConfig.Builder().Uppercase(true).Build();
32+
#pragma warning restore CS0618
33+
var result = Formatter.Format("select id from users where id = 1", cfg);
34+
35+
Assert.Equal(
36+
"SELECT\n"
37+
+ " id\n"
38+
+ "FROM\n"
39+
+ " users\n"
40+
+ "WHERE\n"
41+
+ " id = 1",
42+
result);
43+
}
44+
45+
[Fact]
46+
public void Uppercase_False_PreservesKeywordCase()
47+
{
48+
#pragma warning disable CS0618
49+
var cfg = FormatConfig.Builder().Uppercase(false).Build();
50+
#pragma warning restore CS0618
51+
var result = Formatter.Format("select id FROM users", cfg);
52+
53+
Assert.Equal(
54+
"select\n"
55+
+ " id\n"
56+
+ "FROM\n"
57+
+ " users",
58+
result);
59+
}
60+
61+
[Fact]
62+
public void Lower_FormatsKeywordsInLowercase()
63+
{
64+
var cfg = FormatConfig.Builder().Case(CaseTypes.LOWER).Build();
65+
var result = Formatter.Format("SELECT id FROM Users WHERE id = 1", cfg);
66+
67+
Assert.Equal(
68+
"select\n"
69+
+ " id\n"
70+
+ "from\n"
71+
+ " Users\n"
72+
+ "where\n"
73+
+ " id = 1",
74+
result);
75+
}
76+
77+
[Fact]
78+
public void Upper_ClearsLower()
79+
{
80+
var cfg = FormatConfig.Builder()
81+
.Case(CaseTypes.LOWER)
82+
.Case(CaseTypes.UPPER)
83+
.Build();
84+
85+
Assert.Equal(CaseTypes.UPPER, cfg.Case);
86+
}
87+
88+
[Fact]
89+
public void Lower_ClearsUpper()
90+
{
91+
var cfg = FormatConfig.Builder()
92+
.Case(CaseTypes.UPPER)
93+
.Case(CaseTypes.LOWER)
94+
.Build();
95+
96+
Assert.Equal(CaseTypes.LOWER, cfg.Case);
97+
}
98+
99+
[Fact]
100+
public void Upper_ClearsLower_FormatsUppercase()
101+
{
102+
var cfg = FormatConfig.Builder()
103+
.Case(CaseTypes.LOWER)
104+
.Case(CaseTypes.UPPER)
105+
.Build();
106+
107+
var result = Formatter.Format("select id from users", cfg);
108+
109+
Assert.Equal(
110+
"SELECT\n"
111+
+ " id\n"
112+
+ "FROM\n"
113+
+ " users",
114+
result);
115+
}
116+
117+
[Fact]
118+
public void Lower_ClearsUpper_FormatsLowercase()
119+
{
120+
var cfg = FormatConfig.Builder()
121+
.Case(CaseTypes.UPPER)
122+
.Case(CaseTypes.LOWER)
123+
.Build();
124+
125+
var result = Formatter.Format("SELECT id FROM users", cfg);
126+
127+
Assert.Equal(
128+
"select\n"
129+
+ " id\n"
130+
+ "from\n"
131+
+ " users",
132+
result);
133+
}
134+
135+
[Fact]
136+
public void NeitherUppercaseNorLowercase_PreservesKeywordCase()
137+
{
138+
var cfg = FormatConfig.Builder().Build();
139+
var result = Formatter.Format("SeLeCt id FrOm users", cfg);
140+
141+
Assert.Equal(
142+
"SeLeCt\n"
143+
+ " id\n"
144+
+ "FrOm\n"
145+
+ " users",
146+
result);
147+
}
148+
149+
[Fact]
150+
public void None_PreservesKeywordCase()
151+
{
152+
var cfg = FormatConfig.Builder().Case(CaseTypes.NONE).Build();
153+
var result = Formatter.Format("SeLeCt id FrOm users", cfg);
154+
155+
Assert.Equal(
156+
"SeLeCt\n"
157+
+ " id\n"
158+
+ "FrOm\n"
159+
+ " users",
160+
result);
161+
}
162+
163+
[Theory]
164+
[InlineData(CaseTypes.NONE, false)]
165+
[InlineData(CaseTypes.UPPER, true)]
166+
[InlineData(CaseTypes.LOWER, false)]
167+
public void Case_SetsUppercaseCorrectly(CaseTypes caseType, bool expectedUppercase)
168+
{
169+
var cfg = FormatConfig.Builder().Case(caseType).Build();
170+
#pragma warning disable CS0618
171+
Assert.Equal(expectedUppercase, cfg.Uppercase);
172+
#pragma warning restore CS0618
173+
}
174+
}
175+
}

SQL.Formatter/Core/AbstractFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ protected virtual void FormatQuerySeparator(Token token, StringBuilder query)
282282

283283
protected virtual string Show(Token token)
284284
{
285-
if (_cfg.Uppercase
285+
if (_cfg.Case > CaseTypes.NONE
286286
&& (token.Type == TokenTypes.RESERVED
287287
|| token.Type == TokenTypes.RESERVED_TOP_LEVEL
288288
|| token.Type == TokenTypes.RESERVED_TOP_LEVEL_NO_INDENT
@@ -291,7 +291,7 @@ protected virtual string Show(Token token)
291291
|| token.Type == TokenTypes.CLOSE_PAREN))
292292
{
293293
// Note: If memory is still tight, caching upper-case values at the token generation stage is even better.
294-
return token.Value.ToUpper();
294+
return _cfg.Case == CaseTypes.UPPER ? token.Value.ToUpper() : token.Value.ToLower();
295295
}
296296

297297
return token.Value;

SQL.Formatter/Core/CaseTypes.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace SQL.Formatter.Core
6+
{
7+
public enum CaseTypes
8+
{
9+
NONE,
10+
UPPER,
11+
LOWER,
12+
}
13+
}

SQL.Formatter/Core/FormatConfig.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace SQL.Formatter.Core
45
{
@@ -10,26 +11,51 @@ public class FormatConfig
1011
public readonly string Indent;
1112
public readonly int MaxColumnLength;
1213
public readonly Params Parameters;
14+
[Obsolete("Use Case instead of uppercase")]
1315
public readonly bool Uppercase;
16+
public readonly CaseTypes Case;
1417
public readonly int LinesBetweenQueries;
1518
public readonly bool SkipWhitespaceNearBlockParentheses;
1619

20+
[Obsolete("Use the constructor with caseType instead of uppercase")]
1721
public FormatConfig(
1822
string indent,
1923
int maxColumnLength,
2024
Params parameters,
2125
bool uppercase,
2226
int linesBetweenQueries,
27+
bool skipWhitespaceNearBlockParentheses)
28+
29+
: this(
30+
indent,
31+
maxColumnLength,
32+
parameters,
33+
uppercase ? CaseTypes.UPPER : CaseTypes.NONE,
34+
linesBetweenQueries,
35+
skipWhitespaceNearBlockParentheses)
36+
{
37+
}
38+
39+
public FormatConfig(
40+
string indent,
41+
int maxColumnLength,
42+
Params parameters,
43+
CaseTypes caseType,
44+
int linesBetweenQueries,
2345
bool skipWhitespaceNearBlockParentheses)
2446
{
2547
Indent = indent;
2648
MaxColumnLength = maxColumnLength;
2749
Parameters = parameters == null ? Params.Empty : parameters;
28-
Uppercase = uppercase;
50+
Case = caseType;
51+
#pragma warning disable CS0618
52+
Uppercase = caseType == CaseTypes.UPPER;
53+
#pragma warning restore CS0618
2954
LinesBetweenQueries = linesBetweenQueries;
3055
SkipWhitespaceNearBlockParentheses = skipWhitespaceNearBlockParentheses;
3156
}
3257

58+
3359
public static FormatConfigBuilder Builder()
3460
{
3561
return new FormatConfigBuilder();
@@ -40,7 +66,7 @@ public class FormatConfigBuilder
4066
private string _indent = DefaultIndent;
4167
private int _maxColumnLength = DefaultColumnMaxLength;
4268
private Params _parameters;
43-
private bool _uppercase;
69+
private CaseTypes _case;
4470
private int _linesBetweenQueries;
4571
private bool _skipWhitespaceNearBlockParentheses;
4672

@@ -76,9 +102,17 @@ public FormatConfigBuilder Params<T>(List<T> parameters)
76102
return Params(Core.Params.Of(parameters));
77103
}
78104

105+
[Obsolete("Use Case instead of Uppercase")]
79106
public FormatConfigBuilder Uppercase(bool uppercase)
80107
{
81-
_uppercase = uppercase;
108+
_case = uppercase ? CaseTypes.UPPER : CaseTypes.NONE;
109+
110+
return this;
111+
}
112+
113+
public FormatConfigBuilder Case(CaseTypes caseType)
114+
{
115+
_case = caseType;
82116
return this;
83117
}
84118

@@ -100,7 +134,7 @@ public FormatConfig Build()
100134
_indent,
101135
_maxColumnLength,
102136
_parameters,
103-
_uppercase,
137+
_case,
104138
_linesBetweenQueries,
105139
_skipWhitespaceNearBlockParentheses);
106140
}

0 commit comments

Comments
 (0)