blob: c1ca6b58f6a1aad53acaf4c7c47c055172bfc383 [file] [log] [blame]
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2022 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#endregion
using NUnit.Framework;
using System;
using System.Linq;
namespace Google.Protobuf.Test;
internal class ParsingPrimitivesTest
{
// Note: test cases use integers rather than bytes as they're easier
// to specify in attributes.
[Test]
[TestCase("\ufffd", 255)]
[TestCase("A\ufffd", 65, 255)]
[TestCase("A\ufffd\ufffdB", 65, 255, 255, 66)]
// Overlong form of "space"
[TestCase("\ufffd\ufffd", 0xc0, 0xa0)]
public void ReadRawString_NonUtf8(string expectedText, params int[] bytes)
{
var context = CreateContext(bytes);
string text = ParsingPrimitives.ReadRawString(ref context.buffer, ref context.state, bytes.Length);
Assert.AreEqual(expectedText, text);
}
private static ParseContext CreateContext(int[] bytes)
{
byte[] actualBytes = bytes.Select(b => (byte) b).ToArray();
ParseContext.Initialize(actualBytes.AsSpan(), out var context);
return context;
}
}