コード例 #1
0
ファイル: TR2Tests.cs プロジェクト: erdincay/Cyotek.Data.Nbt
        public void TestName()
        {
            using (Stream stream = File.OpenRead(this.ComplexDataFileName))
            {
                using (Stream decompressorStream = new GZipStream(stream, CompressionMode.Decompress))
                {
                    // arrange
                    TR2                   target;
                    ComplexData           actual;
                    ComplexData           expected;
                    ComplextDataLoadState state;
                    ComplexData.Compound1 currentCompound1;
                    ComplexData.Compound2 currentCompound2;

                    actual   = new ComplexData();
                    expected = ComplexData.Default;

                    target = new TR2(decompressorStream);

                    state            = ComplextDataLoadState.NotInitialized;
                    currentCompound1 = null;
                    currentCompound2 = null;

                    // act
                    while (target.Read())
                    {
                        if (target.IsStartElement())
                        {
                            Trace.WriteLine(target.Type);
                            Trace.WriteLine(target.Name);
                            //Trace.WriteLine(target.Value)

                            switch (target.Name)
                            {
                            case "Level":
                                state = ComplextDataLoadState.Level;
                                break;

                            case "longTest":
                                if (state == ComplextDataLoadState.Level)
                                {
                                    actual.LongValue = target.ReadLong2();
                                }
                                else
                                {
                                    throw new InvalidDataException($"Unexpected token '{target.Name}'");
                                }
                                break;

                            case "shortTest":
                                if (state == ComplextDataLoadState.Level)
                                {
                                    actual.ShortValue = target.ReadShort2();
                                }
                                else
                                {
                                    throw new InvalidDataException($"Unexpected token '{target.Name}'");
                                }
                                break;

                            case "stringTest":
                                if (state == ComplextDataLoadState.Level)
                                {
                                    actual.StringValue = target.ReadString2();
                                }
                                else
                                {
                                    throw new InvalidDataException($"Unexpected token '{target.Name}'");
                                }
                                break;

                            case "floatTest":
                                if (state == ComplextDataLoadState.Level)
                                {
                                    actual.FloatValue = target.ReadFloat2();
                                }
                                else
                                {
                                    throw new InvalidDataException($"Unexpected token '{target.Name}'");
                                }
                                break;

                            case "intTest":
                                if (state == ComplextDataLoadState.Level)
                                {
                                    actual.IntegerValue = target.ReadInt2();
                                }
                                else
                                {
                                    throw new InvalidDataException($"Unexpected token '{target.Name}'");
                                }
                                break;

                            case "nested compound test":
                                state = ComplextDataLoadState.Compound;
                                break;

                            case "ham":
                            case "egg":
                                if (state == ComplextDataLoadState.Compound)
                                {
                                    currentCompound1 = new ComplexData.Compound1();
                                }
                                else
                                {
                                    throw new InvalidDataException($"Unexpected token '{target.Name}'");
                                }
                                break;

                            case "name":
                                if (state == ComplextDataLoadState.Compound)
                                {
                                    state = ComplextDataLoadState.CompoundItem;
                                    currentCompound1.Name = target.ReadString2();
                                    actual.CompoundValue.Add(currentCompound1.Name, currentCompound1);
                                }
                                else
                                {
                                    throw new InvalidDataException($"Unexpected token '{target.Name}'");
                                }
                                break;

                            case "value":
                                if (state == ComplextDataLoadState.CompoundItem)
                                {
                                    currentCompound1.Value = target.ReadFloat2();
                                }
                                else
                                {
                                    throw new InvalidDataException($"Unexpected token '{target.Name}'");
                                }
                                break;

                            case "listTest (long)":
                                state = ComplextDataLoadState.LongList;
                                break;
                            }
                        }
                        else if (target.IsEndElement())
                        {
                            switch (state)
                            {
                            case ComplextDataLoadState.NotInitialized:
                                break;

                            case ComplextDataLoadState.Level:
                                break;

                            case ComplextDataLoadState.CompoundItem:
                                state = ComplextDataLoadState.Compound;
                                break;

                            case ComplextDataLoadState.Compound:
                                state = ComplextDataLoadState.Level;
                                break;

                            case ComplextDataLoadState.LongList:
                                break;

                            case ComplextDataLoadState.CompoundList:
                                break;
                            }
                        }
                    }

                    // assert
                    NbtAssert.AreEqual(expected, actual);
                }
            }
        }
コード例 #2
0
ファイル: NbtAssert.cs プロジェクト: erdincay/Cyotek.Data.Nbt
 private static void AreEqual(ComplexData.Compound1 expected, ComplexData.Compound1 actual)
 {
     Assert.AreEqual(expected.Name, actual.Name);
     Assert.AreEqual(expected.Value, actual.Value);
 }