Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotate API reference types for nullable awareness #176

Merged
merged 2 commits into from
Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@
"editor.formatOnType": true
},
"prettier.proseWrap": "always",
"cSpell.words": [
"Conv",
"cref",
"Dependee",
"finalizer",
"msgctxt",
"Msgid"
],
}
8 changes: 4 additions & 4 deletions src/Yarhl.Media/Text/Binary2Po.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Po Convert(IBinary source)
Po po = new Po();

// Read the header if any
PoEntry entry = ReadEntry(reader);
PoEntry? entry = ReadEntry(reader);
if (entry == null)
return po;

Expand All @@ -62,15 +62,15 @@ public Po Convert(IBinary source)
return po;
}

static PoEntry ReadEntry(TextDataReader reader)
static PoEntry? ReadEntry(TextDataReader reader)
{
// Skip all the blank lines before the block of text
string line = string.Empty;
while (reader.PeekLine()?.Trim().Length == 0)
while (!reader.Stream.EndOfStream && reader.PeekLine().Trim().Length == 0)
reader.ReadLine();

// If nothing to read, EOF
if (reader.Stream.Position >= reader.Stream.Length)
if (reader.Stream.EndOfStream)
return null;

PoEntry entry = new PoEntry();
Expand Down
6 changes: 3 additions & 3 deletions src/Yarhl.Media/Text/Encodings/EscapeOutRangeEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public override int GetCharCount(byte[] bytes, int index, int count)
/// <param name="charIndex">Index in the char array.</param>
/// <param name="charCount">Number of chars to convert.</param>
/// <param name="bytes">Output byte array.</param>
/// <param name="byteIndex">Indes in the byte array.</param>
/// <param name="byteIndex">Index in the byte array.</param>
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
if (chars == null)
Expand Down Expand Up @@ -206,7 +206,7 @@ static bool MatchSequence(IList<byte> buffer, int index, params byte[] sequence)
return true;
}

static string UnescapeText(string text, ICollection<byte> symbols = null)
static string UnescapeText(string text, ICollection<byte>? symbols = null)
{
StringBuilder transformed = new StringBuilder(text);
StringComparison culture = StringComparison.Ordinal;
Expand Down Expand Up @@ -265,7 +265,7 @@ public override DecoderFallbackBuffer CreateFallbackBuffer()
/// </summary>
internal sealed class EscapeOutRangeDecoderFallbackBuffer : DecoderFallbackBuffer
{
string replacement;
string replacement = string.Empty;
int currentPos;

/// <summary>
Expand Down
6 changes: 2 additions & 4 deletions src/Yarhl.Media/Text/Encodings/EucJpEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public override int GetByteCount(char[] chars, int index, int count)
/// <param name="charIndex">Index in the char array.</param>
/// <param name="charCount">Number of chars to convert.</param>
/// <param name="bytes">Output byte array.</param>
/// <param name="byteIndex">Indes in the byte array.</param>
/// <param name="byteIndex">Index in the byte array.</param>
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
if (chars == null)
Expand Down Expand Up @@ -358,15 +358,13 @@ public static Table FromResource(string path)
{
Table table = new Table();

Stream stream = null;
Stream? stream = null;
try {
stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(path);

using (var reader = new StreamReader(stream)) {
#pragma warning disable IDISP003
stream = null; // Avoid disposing twice
#pragma warning restore IDISP003

while (!reader.EndOfStream) {
string line = reader.ReadLine();
Expand Down
34 changes: 8 additions & 26 deletions src/Yarhl.Media/Text/Po.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public class Po : ICloneableFormat
readonly IList<PoEntry> entries;
readonly ReadOnlyCollection<PoEntry> readonlyEntries;
readonly IDictionary<string, PoEntry> searchEntries;
PoHeader header;

/// <summary>
/// Initializes a new instance of the <see cref="Po"/> class.
/// </summary>
public Po()
{
Header = new PoHeader();
entries = new List<PoEntry>();
readonlyEntries = new ReadOnlyCollection<PoEntry>(entries);
searchEntries = new Dictionary<string, PoEntry>();
Expand All @@ -59,24 +59,8 @@ public Po(PoHeader headerArg)
/// </summary>
/// <value>The header.</value>
public PoHeader Header {
get {
return header;
}

set {
if (value == null) {
header = null;
} else {
if (string.IsNullOrEmpty(value.ProjectIdVersion))
throw new FormatException(nameof(value.ProjectIdVersion) + " is empty");
if (string.IsNullOrEmpty(value.ReportMsgidBugsTo))
throw new FormatException(nameof(value.ReportMsgidBugsTo) + " is empty");
if (string.IsNullOrEmpty(value.Language))
throw new FormatException(nameof(value.Language) + " is empty");

header = value;
}
}
get;
set;
}

/// <summary>
Expand Down Expand Up @@ -125,7 +109,7 @@ public void Add(IEnumerable<PoEntry> items)
/// <param name="original">Original text from the entry.</param>
/// <param name="context">Context text from the entry.</param>
/// <returns>The found entry or null if not found.</returns>
public PoEntry FindEntry(string original, string context = null)
public PoEntry? FindEntry(string original, string? context = null)
{
if (string.IsNullOrEmpty(original))
throw new ArgumentNullException(nameof(original));
Expand All @@ -137,10 +121,8 @@ public PoEntry FindEntry(string original, string context = null)
/// <inheritdoc />
public virtual object DeepClone()
{
Po clone = new Po();
if (header != null) {
clone.header = new PoHeader(header);
}
var clonedHeader = new PoHeader(Header);
Po clone = new Po(clonedHeader);

foreach (PoEntry entry in entries)
{
Expand All @@ -155,7 +137,7 @@ static string GetKey(PoEntry entry)
return GetKey(entry.Original, entry.Context);
}

static string GetKey(string original, string context)
static string GetKey(string original, string? context)
{
return original + "||" + (context ?? string.Empty);
}
Expand All @@ -168,7 +150,7 @@ static void MergeEntry(PoEntry current, PoEntry newEntry)
"different translations.");
}

if (newEntry.Reference != null)
if (!string.IsNullOrEmpty(newEntry.Reference))
current.Reference += "," + newEntry.Reference;
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/Yarhl.Media/Text/Po2Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ public class Po2Binary : IConverter<Po, BinaryFormat>
/// <summary>
/// Convert the specified PO into a Binary stream.
/// </summary>
/// <returns>The converted stream.</returns>
/// <param name="source">Source PO.</param>
/// <returns>The converted stream.</returns>
/// <remarks>
/// It writes the header only if <see cref="PoHeader.ProjectIdVersion"/>
/// is not empty.
/// </remarks>
public BinaryFormat Convert(Po source)
{
if (source == null)
Expand All @@ -41,7 +45,7 @@ public BinaryFormat Convert(Po source)
BinaryFormat binary = new BinaryFormat();
TextDataWriter writer = new TextDataWriter(binary.Stream);

if (source.Header != null)
if (!string.IsNullOrEmpty(source.Header.ProjectIdVersion))
WriteHeader(source.Header, writer);

foreach (var entry in source.Entries) {
Expand Down
10 changes: 9 additions & 1 deletion src/Yarhl.Media/Text/PoEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ public PoEntry()
{
Original = string.Empty;
Translated = string.Empty;
Context = string.Empty;
TranslatorComment = string.Empty;
ExtractedComments = string.Empty;
Reference = string.Empty;
Flags = string.Empty;
PreviousContext = string.Empty;
PreviousOriginal = string.Empty;
}

/// <summary>
/// Initializes a new instance of the <see cref="PoEntry"/> class.
/// </summary>
/// <param name="original">Original text to translate.</param>
public PoEntry(string original)
: this()
{
Original = original;
Translated = string.Empty;
Expand Down Expand Up @@ -69,7 +77,7 @@ public PoEntry(PoEntry entry)
/// Gets or sets the original content to translate.
/// </summary>
/// <remarks>
/// <para>Entris with the same original content will be merged.</para>
/// <para>Entries with the same original content will be merged.</para>
/// </remarks>
/// <value>The original content.</value>
public string Original { get; set; }
Expand Down
9 changes: 8 additions & 1 deletion src/Yarhl.Media/Text/PoHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class PoHeader
/// </summary>
public PoHeader()
{
ProjectIdVersion = string.Empty;
ReportMsgidBugsTo = string.Empty;
Language = string.Empty;
CreationDate = DateTime.Now.ToShortDateString();
RevisionDate = string.Empty;
LastTranslator = string.Empty;
LanguageTeam = string.Empty;
PluralForms = string.Empty;
Extensions = new Dictionary<string, string>();
}

Expand All @@ -47,7 +55,6 @@ public PoHeader(string id, string reporter, string lang)
ProjectIdVersion = id;
ReportMsgidBugsTo = reporter;
Language = lang;
CreationDate = DateTime.Now.ToShortDateString();
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Yarhl.Media/Yarhl.Media.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<PropertyGroup>
<Description>Yarhl plugin with support of text converters.</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Yarhl.UnitTests/IO/DataWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@ public void WriteObjectsInvalidArguments()
string nullValue = null;
Assert.Throws<ArgumentNullException>(() => writer.WriteOfType(nullType, 1));
Assert.Throws<ArgumentNullException>(() => writer.WriteOfType(typeof(string), nullValue));
Assert.Throws<ArgumentNullException>(() => writer.WriteOfType<string>(nullValue));
}

[Test]
Expand Down
14 changes: 0 additions & 14 deletions src/Yarhl.UnitTests/IO/StreamFormat/StreamWrapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@ public void ConstructorSetProperties()
Assert.That(wrapper.BaseStream, Is.SameAs(stream));
}

[Test]
public void FeaturePropertiesDefaults()
{
using var wrapper = new StreamWrapperImpl();
Assert.That(wrapper.CanRead, Is.True);
Assert.That(wrapper.CanWrite, Is.True);
Assert.That(wrapper.CanSeek, Is.True);
Assert.That(wrapper.CanTimeout, Is.False);
}

[Test]
public void FeaturePropertiesFromBaseStream()
{
Expand Down Expand Up @@ -278,10 +268,6 @@ public void TestFlushThrowsIfDisposed()

private sealed class StreamWrapperImpl : StreamWrapper
{
public StreamWrapperImpl()
{
}

public StreamWrapperImpl(Stream stream)
: base(stream)
{
Expand Down
16 changes: 11 additions & 5 deletions src/Yarhl.UnitTests/IO/TextDataReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,12 @@ public void ReadToTokenWithPreamble()
}

[Test]
public void ReadToTokenWhenEOFReturnsNull()
public void ReadToTokenWhenEOFThrows()
{
var reader = new TextDataReader(stream);
Assert.IsNull(reader.ReadToToken("3"));
Assert.That(
() => reader.ReadToToken("3"),
Throws.InstanceOf<EndOfStreamException>());
}

[Test]
Expand Down Expand Up @@ -608,13 +610,17 @@ public void ReadLineWithoutNewLineDoesNotThrow()
}

[Test]
public void ReadLineWhenEOFReturnsNull()
public void ReadLineWhenEOFThrowsException()
{
var reader = new TextDataReader(stream);
Assert.IsNull(reader.ReadLine());
Assert.That(
() => reader.ReadLine(),
Throws.InstanceOf<EndOfStreamException>());

reader.AutoNewLine = false;
Assert.IsNull(reader.ReadLine());
Assert.That(
() => reader.ReadLine(),
Throws.InstanceOf<EndOfStreamException>());
}

[Test]
Expand Down
3 changes: 2 additions & 1 deletion src/Yarhl.UnitTests/Media/Text/Po2BinaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ public void ConvertNoHeaderEntries()
var newPo = ConvertStringToPo(text);

CompareText(ConvertFormat.To<BinaryFormat>(testPo), text);
Assert.IsNull(newPo.Header);
Assert.That(newPo.Header, Is.Not.Null);
Assert.That(newPo.Header.ProjectIdVersion, Is.Empty);
Assert.AreEqual(2, newPo.Entries.Count);
}

Expand Down
20 changes: 10 additions & 10 deletions src/Yarhl.UnitTests/Media/Text/PoEntryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ public class PoEntryTests
public void DefaultValues()
{
PoEntry entry = new PoEntry();
Assert.AreEqual(string.Empty, entry.Original);
Assert.AreEqual(string.Empty, entry.Translated);
Assert.IsNull(entry.Context);
Assert.IsNull(entry.TranslatorComment);
Assert.IsNull(entry.ExtractedComments);
Assert.IsNull(entry.Reference);
Assert.IsNull(entry.Flags);
Assert.IsNull(entry.PreviousContext);
Assert.IsNull(entry.PreviousOriginal);
Assert.That(entry.Original, Is.Empty);
Assert.That(entry.Translated, Is.Empty);
Assert.That(entry.Context, Is.Empty);
Assert.That(entry.TranslatorComment, Is.Empty);
Assert.That(entry.ExtractedComments, Is.Empty);
Assert.That(entry.Reference, Is.Empty);
Assert.That(entry.Flags, Is.Empty);
Assert.That(entry.PreviousContext, Is.Empty);
Assert.That(entry.PreviousOriginal, Is.Empty);

entry = new PoEntry("original");
Assert.AreEqual("original", entry.Original);
Assert.That(entry.Original, Is.EqualTo("original"));
}

[Test]
Expand Down