示例#1
0
        /// <inheritdoc />
        public override void WriteTo(LuaTableTextWriter writer)
        {
            if (Name != null)
            {
                switch (Name.TokenType)
                {
                case LTokenType.Boolean:
                    writer.WriteKey((bool)Name);
                    break;

                case LTokenType.Integer:
                    writer.WriteKey((long)Name);
                    break;

                case LTokenType.Float:
                    writer.WriteKey((double)Name);
                    break;

                case LTokenType.String:
                    writer.WriteKey((string)Name);
                    break;

                default:
#if NETSTANDARD2_0
                    Debug.Fail("Invalid Name.TokenType.");
#else
                    Debug.Assert(false);
#endif
                    break;
                }
            }
            Value.WriteTo(writer);
        }
示例#2
0
        public void WriteKeyedTest()
        {
            using (var sw = new StringWriter())
                using (var tw = new LuaTableTextWriter(sw))
                {
                    tw.WriteStartTable();
                    tw.WriteKey("Test");
                    tw.WriteLiteral("value");
                    tw.WriteKey(123);
                    tw.WriteLiteral(456);
                    tw.WriteKey(TimeSpan.FromHours(3.2));
                    tw.WriteNil();
                    tw.WriteKey("while");
                    tw.WriteNil();
                    tw.WriteKey("function");
                    tw.WriteNil();

                    // Though it's meaningless…
                    tw.WriteStartKey();
                    tw.WriteStartTable();
                    tw.WriteLiteral(1);
                    tw.WriteLiteral(2);
                    tw.WriteEndTable();
                    tw.WriteEndKey();
                    tw.WriteLiteral(123);

                    tw.WriteEndTable();
                    tw.Flush();
                    Assert.Equal("{Test=\"value\",[123]=456,[\"03:12:00\"]=nil,[\"while\"]=nil,[\"function\"]=nil,[{1,2}]=123}", sw.ToString());
                }
        }
示例#3
0
        public void WriteNestedTableTest()
        {
            using (var sw = new StringWriter())
                using (var tw = new LuaTableTextWriter(sw))
                {
                    tw.WriteStartTable();

                    tw.WriteStartTable();
                    tw.WriteLiteral(1);
                    tw.WriteLiteral(2);
                    tw.WriteLiteral(3);
                    tw.WriteEndTable();

                    tw.WriteLiteral(4);
                    tw.WriteKey("Named5");
                    tw.WriteLiteral(5);

                    tw.WriteStartTable();
                    tw.WriteLiteral(6);
                    tw.WriteKey("Named7");
                    tw.WriteLiteral(7);
                    tw.WriteEndTable();

                    tw.WriteEndTable();
                    tw.Flush();
                    Assert.Equal("{{1,2,3},4,Named5=5{6,Named7=7}}", sw.ToString());
                }
        }
示例#4
0
 /// <inheritdoc />
 public override void WriteTo(LuaTableTextWriter writer)
 {
     if (writer == null)
     {
         throw new ArgumentNullException(nameof(writer));
     }
     writer.WriteLiteral(Value);
 }
示例#5
0
 /// <inheritdoc />
 public override void WriteTo(LuaTableTextWriter writer)
 {
     writer.WriteStartTable();
     foreach (var field in store)
     {
         field.WriteTo(writer);
     }
     writer.WriteEndTable();
 }
示例#6
0
        /// <summary>
        /// Gets the Lua expression string with the specified formatting options.
        /// </summary>
        /// <param name="formatting">The formatting options.</param>
        /// <returns>Lua expression.</returns>
        public string ToString(Formatting formatting)
        {
            using (var writer = new StringWriter())
            {
                using (var lw = new LuaTableTextWriter(writer)
                {
                    CloseWriter = false, Formatting = formatting
                })
                {
                    WriteTo(lw);
                }

                return(writer.ToString());
            }
        }
示例#7
0
        public void WriteOutsideTableTest()
        {
            using (var sw = new StringWriter())
                using (var tw = new LuaTableTextWriter(sw))
                {
                    tw.WriteLiteral(123);

                    tw.WriteStartKey();
                    tw.WriteLiteral(2);
                    tw.WriteEndKey();

                    tw.WriteLiteral(456);

                    tw.WriteKey("key");
                    tw.WriteLiteral("value");

                    tw.WriteLiteral("last");

                    tw.Flush();
                    Assert.Equal("123,[2]=456,key=\"value\",\"last\"", sw.ToString());
                }
        }
示例#8
0
 public void WriteLiteralTest()
 {
     using (var sw = new StringWriter())
         using (var tw = new LuaTableTextWriter(sw))
         {
             tw.WriteStartTable();
             tw.WriteLiteral('@');
             tw.WriteLiteral("\r\n\t\f\b?{\\r\\n\"\'");
             tw.WriteLiteral(true);
             tw.WriteLiteral(10);
             tw.WriteLiteral(10.99);
             tw.WriteLiteral(0.99);
             tw.WriteLiteral(0.000000000000000001d);
             tw.WriteLiteral(0.000000000000000001m);
             tw.WriteLiteral((string)null);
             tw.WriteLiteral((object)null);
             tw.WriteLiteral("This is a string.");
             tw.WriteNil();
             tw.WriteEndTable();
             tw.Flush();
             Assert.Equal("{64,\"\\r\\n\\t\\f\\b?{\\r\\n\\\"\'\",true,10,10.99,0.99,1E-18,0.000000000000000001,\"\",nil,\"This is a string.\",nil}",
                          sw.ToString());
         }
 }
示例#9
0
 /// <summary>
 /// Writes this token to a <see cref="LuaTableTextWriter"/>.
 /// </summary>
 /// <param name="writer">A <see cref="LuaTableTextWriter"/> into which this method will write.</param>
 public abstract void WriteTo(LuaTableTextWriter writer);