Пример #1
0
        public void It_Should_Implement_IDictionaryFunctions()
        {
            var dict = new LiquidHash();

            Assert.False(dict.IsReadOnly);
            Assert.NotNull(dict.GetEnumerator());
        }
Пример #2
0
        public void It_Should_Set_A_Value()
        {
            var dict = new LiquidHash();

            dict["key"] = new Some <ILiquidValue>(LiquidString.Create("test"));
            Assert.Equal(LiquidString.Create("test"), dict["key"].Value);
        }
Пример #3
0
        public void It_Should_Add_A_Value()
        {
            var dict = new LiquidHash();

            dict.Add("key", LiquidString.Create("test"));
            Assert.Equal(LiquidString.Create("test"), dict["key"].Value);
        }
Пример #4
0
        private Option <ILiquidValue> FromObject(Object obj)
        {
            var newHash = new LiquidHash();
            var kvps    = obj.GetType()
                          .GetProperties()
                          .Where(property => !(property.GetCustomAttributes <LiquidIgnoreAttribute>().Any() ||
                                               (property.GetCustomAttributes <LiquidIgnoreIfNullAttribute>().Any() &&
                                                ReferenceEquals(property.GetGetMethod().Invoke(obj, null), null))))
                          .Select(property => new KeyValuePair <String, Option <ILiquidValue> > (
                                      GetPropertyName(property),
                                      GetPropertyValue(obj, property)));

            foreach (var kvp in kvps)
            {
                // tolower may create a collision
                if (newHash.ContainsKey(kvp.Key))
                {
                    newHash[kvp.Key] = kvp.Value;
                }
                else
                {
                    newHash.Add(kvp);
                }
            }
            return(newHash);
        }
Пример #5
0
        public void It_Should_Implement_IDictionaryFunctions()
        {
            var dict = new LiquidHash();

            Assert.That(dict.IsReadOnly, Is.False);
            Assert.That(dict.GetEnumerator(), Is.Not.Null);
        }
Пример #6
0
        public void It_Should_Retrieve_Values()
        {
            var dict = new LiquidHash {
                { "key", LiquidString.Create("test") }
            };

            Assert.Equal(1, dict.Values.Count);
        }
Пример #7
0
        public void It_Should_Fail_When_Dereferencing_A_Missing_Property()
        {
            // Arrange
            LiquidHash dictValue = new LiquidHash();

            // Assert
            Assert.False(dictValue.ValueAt("string1").HasValue);
        }
Пример #8
0
        public void It_Should_Convert_Null_To_None()
        {
            var kvp  = new KeyValuePair <String, Option <ILiquidValue> >("key", null);
            var dict = new LiquidHash {
                kvp
            };

            Assert.False(dict["key"].HasValue);
        }
Пример #9
0
        public void It_Should_Remove_A_Value()
        {
            var dict = new LiquidHash {
                { "key", LiquidString.Create("test") }
            };

            dict.Remove("key");
            Assert.Empty(dict);
        }
Пример #10
0
        public void It_Should_Clear_Values()
        {
            var dict = new LiquidHash {
                { "key", LiquidString.Create("test") }
            };

            dict.Clear();
            Assert.Empty(dict);
        }
Пример #11
0
        public void It_Should_Clear_Values()
        {
            var dict = new LiquidHash {
                { "key", LiquidString.Create("test") }
            };

            dict.Clear();
            Assert.That(dict.Count, Is.EqualTo(0));
        }
Пример #12
0
        public void It_Should_Remove_A_Value()
        {
            var dict = new LiquidHash {
                { "key", LiquidString.Create("test") }
            };

            dict.Remove("key");
            Assert.That(dict.Count, Is.EqualTo(0));
        }
Пример #13
0
        public void It_Should_Remove_Values()
        {
            var dict = new LiquidHash {
                { "key", LiquidString.Create("test") }
            };

            dict.Remove("key");
            Assert.False(dict.Values.Any());
        }
Пример #14
0
        public void It_Should_Copy_Values()
        {
            var dict = new LiquidHash {
                { "key", LiquidString.Create("test") }
            };
            var x = new KeyValuePair <string, Option <ILiquidValue> > [1];

            dict.CopyTo(x, 0);
            Assert.That(x.Length, Is.EqualTo(1));
        }
Пример #15
0
        public void It_Should_Set_A_Value_Via_Key_Value_Pair()
        {
            var val  = new Some <ILiquidValue>(LiquidString.Create("test"));
            var kvp  = new KeyValuePair <String, Option <ILiquidValue> >("key", val);
            var dict = new LiquidHash {
                kvp
            };

            Assert.Equal(LiquidString.Create("test"), dict["key"].Value);
        }
Пример #16
0
        public void It_Should_Know_If_A_Value_Is_Contained()
        {
            var val  = new Some <ILiquidValue>(LiquidString.Create("test"));
            var kvp  = new KeyValuePair <String, Option <ILiquidValue> >("key", val);
            var dict = new LiquidHash {
                kvp
            };

            Assert.Contains(kvp, dict);
        }
Пример #17
0
        public void It_Should_Copy_Values()
        {
            var dict = new LiquidHash {
                { "key", LiquidString.Create("test") }
            };
            var x = new KeyValuePair <string, Option <ILiquidValue> > [1];

            dict.CopyTo(x, 0);
            Assert.Single(x);
        }
Пример #18
0
        public void It_Should_Initialize_The_LiquidHash_With_OptionSyntax()
        {
            // Arrange
            var dict = new LiquidHash
            {
                { "key1", Option <ILiquidValue> .Create(LiquidString.Create("test 1")) },
                { "key2", Option <ILiquidValue> .Create(LiquidString.Create("test 2")) },
            };

            // Assert
            Assert.Equal(LiquidString.Create("test 1"), dict.ValueAt("key1").Value);
        }
Пример #19
0
        public void It_Should_Try_To_Retrieve_Values()
        {
            var dict = new LiquidHash {
                { "key", LiquidString.Create("test") }
            };
            Option <ILiquidValue> opt;
            bool success = dict.TryGetValue("key", out opt);

            Assert.True(success);
            // ReSharper disable once PossibleNullReferenceException
            Assert.Equal("test", ((LiquidString)opt.Value).StringVal);
        }
Пример #20
0
        public void A_Dict_With_No_Value_Should_Have_Zero_Length()
        {
            // Arrange
            LiquidHash dictValue = new LiquidHash();
            var        filter    = new SizeFilter();

            // Act
            var result = filter.Apply(new TemplateContext(), dictValue).SuccessValue <LiquidNumeric>();

            // Assert
            Assert.Equal(0, result.Value);
        }
Пример #21
0
        public void It_Should_Dereference_A_LiquidHash()
        {
            // Arrange
            var dictValue = new LiquidHash
            {
                { "string1", LiquidString.Create("a string") },
                { "string2", LiquidNumeric.Create(123) },
                { "string3", LiquidNumeric.Create(456m) }
            };

            // Assert
            Assert.Equal("a string", dictValue.ValueAt("string1").Value.ToString());
        }
Пример #22
0
        public void It_Should_Initialize_The_LiquidHash_With_IExpressionConstant()
        {
            // Arrange
            var dict = new LiquidHash
            {
                { "key1", LiquidString.Create("test 1") },
                { "key2", LiquidString.Create("test 2") },
            };


            // Assert
            Assert.Equal(LiquidString.Create("test 1"), dict.ValueAt("key1").Value);
        }
Пример #23
0
        public static Option <ILiquidValue> Transform(JObject obj)
        {
            var result = new LiquidHash();
            var dict   = obj.Properties().ToDictionary(k => k.Name, v => (Option <ILiquidValue>)Transform((dynamic)v.Value));

            foreach (var kvp in dict)
            {
                result.Add(kvp);
//                result.Add(kvp.Key,kvp.Value != null?
//                    (Option<ILiquidValue>) new Some<ILiquidValue>(kvp.Value) :
//                    new None<ILiquidValue>());
            }
            return(result);
        }
Пример #24
0
        // SEE: https://github.com/Shopify/liquid/wiki/Liquid-for-Designers
        public void It_Should_Cast_KV_Pairs_In_A_Dictionary_To_An_Array_Of_Arrays_with_Two_Elements()
        {
            // Arrange
            var dictValue = new LiquidHash {
                { "one", LiquidString.Create("ONE") },
                { "two", LiquidString.Create("TWO") },
                { "three", LiquidString.Create("THREE") },
                { "four", LiquidString.Create("FOUR") }
            };

            // Act
            var result = ValueCaster.Cast <LiquidHash, LiquidCollection>(dictValue).SuccessValue <LiquidCollection>();

            // Assert

            Assert.Equal(4, result.Count);
        }
Пример #25
0
        public void It_Should_Measure_The_Size_Of_A_Dictionary()
        {
            // Arrange

            LiquidHash dictValue = new LiquidHash {
                { "string1", LiquidString.Create("a string") },
                { "string2", LiquidNumeric.Create(123) },
                { "string3", LiquidNumeric.Create(456m) }
            };
            SizeFilter sizeFilter = new SizeFilter();

            // Act
            var result = sizeFilter.Apply(new TemplateContext(), dictValue).SuccessValue <LiquidNumeric>();

            // Assert
            Assert.Equal(3, result.Value);
        }
Пример #26
0
        private Option <ILiquidValue> CreateHash(IDictionary dict)
        {
            var newHash = new LiquidHash();

            foreach (var key in dict.Keys)
            {
                String keyAsStr = key.ToString();
                var    newValue = Convert(dict[key]); // may throw exception
                if (newHash.ContainsKey(keyAsStr))
                {
                    newHash[keyAsStr] = newValue;
                }
                else
                {
                    newHash.Add(keyAsStr, newValue);
                }
            }
            return(newHash);
        }
Пример #27
0
        public void Test_Simple_Hash()
        {
            ITemplateContext ctx = new TemplateContext();
            var nameHash         = new LiquidHash
            {
                { "first", LiquidString.Create("Tobias") },
                { "last", LiquidString.Create("Lütke") }
            };

            ctx.DefineLocalVariable("greeting", new LiquidHash
            {
                { "address", LiquidString.Create("Hello") },
                { "name", nameHash }
            });

            var parsingResult = LiquidTemplate.Create("You said '{{ greeting.address }} {{ greeting.name.first }} {{ greeting.name.last }}'");

            Assert.That(parsingResult.LiquidTemplate.Render(ctx).Result, Is.EqualTo("You said 'Hello Tobias Lütke'"));
        }
Пример #28
0
        public void It_Should_Allow_A_Variable_With_Index()
        {
            // Arrange
            ITemplateContext ctx  = new TemplateContext().WithAllFilters();
            LiquidHash       dict = new LiquidHash {
                { "foo", LiquidNumeric.Create(33) }
            };

            ctx.DefineLocalVariable("bar", dict);
            var template = LiquidTemplate.Create("{{ 1 | plus: bar.foo}}");

            // Act
            String result = template.LiquidTemplate.Render(ctx).Result;

            Logger.Log(result);

            // Assert
            Assert.That(result, Is.EqualTo("34"));
        }
Пример #29
0
        public void It_Should_Not_Quote_Numerics_In_Json_Dict()
        {
            // Arrange
            var dictValue = new LiquidHash
            {
                { "one", LiquidNumeric.Create(1) },
                { "two", LiquidNumeric.Create(2L) },
                { "three", LiquidNumeric.Create(3m) },
                { "four", LiquidNumeric.Create(new BigInteger(4)) }
            };

            // Act
            ITemplateContext ctx = new TemplateContext().DefineLocalVariable("dict1", dictValue);

            // Act
            var result = RenderingHelper.RenderTemplate("Result : {{ dict1 }}", ctx);

            // Assert
            Assert.Equal("Result : { \"one\" : 1, \"two\" : 2, \"three\" : 3.0, \"four\" : 4 }", result);
        }
Пример #30
0
        public void It_Should_Recursively_Render_Dictionaries_in_Json()
        {
            // Arrange
            var subDictValue = new LiquidHash
            {
                { "abc", LiquidString.Create("def") }
            };
            var dictValue = new LiquidHash {
                { "one", LiquidNumeric.Create(1) },
                { "two", subDictValue }
            };

            // Act
            ITemplateContext ctx = new TemplateContext().DefineLocalVariable("dict1", dictValue);

            // Act
            var result = RenderingHelper.RenderTemplate("Result : {{ dict1 }}", ctx);

            // Assert
            Assert.Equal("Result : { \"one\" : 1, \"two\" : { \"abc\" : \"def\" } }", result);
        }