Skip to content

Commit

Permalink
Allow changing the FormatOptions.Default values
Browse files Browse the repository at this point in the history
Fixes issue #993
  • Loading branch information
jstedfast committed Jan 27, 2024
1 parent 6e9322c commit 4b77a9f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 57 deletions.
49 changes: 4 additions & 45 deletions MimeKit/FormatOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,9 @@ public class FormatOptions
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="value"/> is out of range. It must be between 60 and 998.
/// </exception>
/// <exception cref="System.InvalidOperationException">
/// <see cref="Default"/> cannot be changed.
/// </exception>
public int MaxLineLength {
get { return maxLineLength; }
set {
if (this == Default)
throw new InvalidOperationException ("The default formatting options cannot be changed.");

if (value < MinimumLineLength || value > MaximumLineLength)
throw new ArgumentOutOfRangeException (nameof (value));

Expand All @@ -133,15 +127,9 @@ public int MaxLineLength {
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="value"> is not a valid <see cref="NewLineFormat"/>.</paramref>
/// </exception>
/// <exception cref="System.InvalidOperationException">
/// <see cref="Default"/> cannot be changed.
/// </exception>
public NewLineFormat NewLineFormat {
get { return newLineFormat; }
set {
if (this == Default)
throw new InvalidOperationException ("The default formatting options cannot be changed.");

switch (newLineFormat) {
case NewLineFormat.Unix:
case NewLineFormat.Dos:
Expand All @@ -165,17 +153,9 @@ public NewLineFormat NewLineFormat {
/// that writing the message back to a stream will always end with a new-line sequence.</para>
/// </remarks>
/// <value><c>true</c> in order to ensure that the message will end with a new-line sequence; otherwise, <c>false</c>.</value>
/// <exception cref="System.InvalidOperationException">
/// <see cref="Default"/> cannot be changed.
/// </exception>
public bool EnsureNewLine {
get { return ensureNewLine; }
set {
if (this == Default)
throw new InvalidOperationException ("The default formatting options cannot be changed.");

ensureNewLine = value;
}
set { ensureNewLine = value; }
}

internal IMimeFilter CreateNewLineFilter (bool ensureNewLine = false)
Expand Down Expand Up @@ -229,17 +209,9 @@ public HashSet<HeaderId> HiddenHeaders {
/// (<a href="https://tools.ietf.org/html/rfc6855">rfc6855</a>).</para>
/// </remarks>
/// <value><c>true</c> if the new internationalized formatting should be used; otherwise, <c>false</c>.</value>
/// <exception cref="System.InvalidOperationException">
/// <see cref="Default"/> cannot be changed.
/// </exception>
public bool International {
get { return international; }
set {
if (this == Default)
throw new InvalidOperationException ("The default formatting options cannot be changed.");

international = value;
}
set { international = value; }
}

/// <summary>
Expand All @@ -260,12 +232,7 @@ public bool International {
/// <value><c>true</c> if the formatter should be allowed to use us-ascii and/or iso-8859-1 when encoding headers; otherwise, <c>false</c>.</value>
public bool AllowMixedHeaderCharsets {
get { return allowMixedHeaderCharsets; }
set {
if (this == Default)
throw new InvalidOperationException ("The default formatting options cannot be changed.");

allowMixedHeaderCharsets = value;
}
set { allowMixedHeaderCharsets = value; }
}

/// <summary>
Expand All @@ -288,9 +255,6 @@ public bool AllowMixedHeaderCharsets {
public ParameterEncodingMethod ParameterEncodingMethod {
get { return parameterEncodingMethod; }
set {
if (this == Default)
throw new InvalidOperationException ("The default formatting options cannot be changed.");

switch (value) {
case ParameterEncodingMethod.Rfc2047:
case ParameterEncodingMethod.Rfc2231:
Expand All @@ -315,12 +279,7 @@ public ParameterEncodingMethod ParameterEncodingMethod {
/// <value><c>true</c> if Content-Type and Content-Disposition parameters should always be quoted; otherwise, <c>false</c>.</value>
public bool AlwaysQuoteParameterValues {
get { return alwaysQuoteParameterValues; }
set {
if (this == Default)
throw new InvalidOperationException ("The default formatting options cannot be changed.");

alwaysQuoteParameterValues = value;
}
set { alwaysQuoteParameterValues = value; }
}

static FormatOptions ()
Expand Down
12 changes: 0 additions & 12 deletions UnitTests/FormatOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,5 @@ public void TestArgumentExceptions ()

Assert.DoesNotThrow (() => format.MaxLineLength = 72);
}

[Test]
public void TestInvalidOperationExceptions ()
{
Assert.Throws<InvalidOperationException> (() => FormatOptions.Default.MaxLineLength = 998, "MaxLineLength");
Assert.Throws<InvalidOperationException> (() => FormatOptions.Default.EnsureNewLine = true, "EnsureNewLine");
Assert.Throws<InvalidOperationException> (() => FormatOptions.Default.International = true, "International");
Assert.Throws<InvalidOperationException> (() => FormatOptions.Default.NewLineFormat = NewLineFormat.Dos, "NewLineFormat");
Assert.Throws<InvalidOperationException> (() => FormatOptions.Default.AllowMixedHeaderCharsets = true, "AllowMixedHeaderCharsets");
Assert.Throws<InvalidOperationException> (() => FormatOptions.Default.ParameterEncodingMethod = ParameterEncodingMethod.Rfc2047, "ParameterEncodingMethod");
Assert.Throws<InvalidOperationException> (() => FormatOptions.Default.AlwaysQuoteParameterValues = true, "AlwaysQuoteParameterValues");
}
}
}

0 comments on commit 4b77a9f

Please sign in to comment.