Exemplo n.º 1
0
        public void SetRuleTest()
        {
            var DefaultFile = new FileInfo(Path.Combine(MockEnvironment.WebRootPath, "css", "custom", "default.css"));
            var RuleList    = new StyleRuleList(@".hello{world:blah;}");
            var RuleListTwo = new StyleRuleList(@".hi{apple:pie;}");

            //Test Null pass in
            Controller.SetRule(null);
            Assert.Equal(string.Empty, File.ReadAllText(DefaultFile.FullName));

            //Test setting rule list
            Controller.SetRule(RuleList);
            Assert.Equal(@".hello{world:blah;}", File.ReadAllText(DefaultFile.FullName));

            //Test overwriting rule list
            Controller.SetRule(RuleListTwo);
            Assert.Equal(@".hi{apple:pie;}", File.ReadAllText(DefaultFile.FullName));

            //Mock a user
            var UserSpecificFile = new FileInfo(Path.Combine(MockEnvironment.WebRootPath, "css", "custom", string.Format("{0}.css", MockAUser())));

            //Test setting rules for a user
            Controller.SetRule(RuleList);
            Assert.Equal(@".hello{world:blah;}", File.ReadAllText(UserSpecificFile.FullName));

            //Test overwriting rules for a user
            Controller.SetRule(RuleListTwo);
            Assert.Equal(@".hi{apple:pie;}", File.ReadAllText(UserSpecificFile.FullName));
        }
Exemplo n.º 2
0
        public IActionResult AddRule([FromBody] StyleRuleList list)
        {
            StyleRuleList Output       = null;
            var           FileContents = GetCustomStyleContents();

            Output = new StyleRuleList(FileContents);
            Output.AddRules(list);
            SetCustomStyleContents(Output);
            return(RedirectToAction("Index"));
        }
Exemplo n.º 3
0
        private void SetCustomStyleContents(StyleRuleList rules)
        {
            var contents = string.Empty;

            if (rules != null)
            {
                contents = rules.ToString();
            }
            var file = new FileInfo(GetCustomStylePath());

            file.Directory.Create();                              //Create directory if it does not exist
            //Limit file size?
            System.IO.File.WriteAllText(file.FullName, contents); //this will create the file if it does not exist
        }
Exemplo n.º 4
0
        public void FromStringTest()
        {
            //The input string must look like this with comments and all white space removed
            TestingList = new StyleRuleList(@"
/*
what
do you
want?*/
.hello {
    world:blah;
}
#rule-two {
apple:sauce;
pecan:pie;
    .hello {
    world:blah;
    }
}");

            //Test that rules exists
            Assert.True(TestingList.Rules.Where(x => x.Selector == ".hello").Any());
            Assert.True(TestingList.Rules.Where(x => x.Selector == "#rule-two").Any());

            var ruleOne = TestingList.Rules.Where(x => x.Selector == ".hello").Single();
            var ruleTwo = TestingList.Rules.Where(x => x.Selector == "#rule-two").Single();

            //Test that nested rules exist
            Assert.True(ruleTwo.NestedRules.Rules.Any());

            var nestedRule = ruleTwo.NestedRules.Rules.First();

            //Test that properties are correct
            Assert.Equal("blah", ruleOne.Styles.Where(x => x.Property == "world").Single().Value);
            Assert.Equal("sauce", ruleTwo.Styles.Where(x => x.Property == "apple").Single().Value);
            Assert.Equal("pie", ruleTwo.Styles.Where(x => x.Property == "pecan").Single().Value);
            Assert.Equal("blah", nestedRule.Styles.Where(x => x.Property == "world").Single().Value);
        }
Exemplo n.º 5
0
        public void ToStringTest()
        {
            TestingList = new StyleRuleList();
            var RuleOne = new StyleRule()
            {
                Selector = ".hello",
                Styles   = new List <Style>()
                {
                    new Style()
                    {
                        Property = "world",
                        Value    = "blah"
                    }
                }
            };
            var RuleTwoA = new StyleRule()
            {
                Selector = "#rule-two",
                Styles   = new List <Style>()
                {
                    new Style()
                    {
                        Property = "apple",
                        Value    = "sauce"
                    }
                }
            };
            var RuleTwoStyleB = new Style()
            {
                Property = "pecan",
                Value    = "pie"
            };
            var ChainedRule = new StyleRule()
            {
                Selector = ".hello,#rule-two",
                Styles   = new List <Style>()
                {
                    new Style()
                    {
                        Property = "dummy",
                        Value    = "var"
                    }
                }
            };

            //Test one rule
            TestingList.AddRule(RuleOne);
            Assert.Equal(@".hello{world:blah;}", TestingList.ToString());

            //Test two rules
            TestingList.AddRule(RuleTwoA);
            Assert.Equal(@".hello{world:blah;}#rule-two{apple:sauce;}", TestingList.ToString());

            //Test merging rules
            TestingList.Rules.Where(x => x.Selector == "#rule-two").Single().AddStyle(RuleTwoStyleB);
            Assert.Equal(@".hello{world:blah;}#rule-two{apple:sauce;pecan:pie;}", TestingList.ToString());

            //Test nested rules
            TestingList.Rules.Where(x => x.Selector == "#rule-two").Single().NestedRules.AddRule(RuleOne);
            Assert.Equal(@".hello{world:blah;}#rule-two{apple:sauce;pecan:pie;.hello{world:blah;}}", TestingList.ToString());

            //Test chained rules
            TestingList.AddRule(ChainedRule);
            Assert.Equal(@".hello{world:blah;}#rule-two{apple:sauce;pecan:pie;.hello{world:blah;}}.hello,#rule-two{dummy:var;}", TestingList.ToString());
        }
Exemplo n.º 6
0
        public void AddRuleTest()
        {
            var TestStyleRule = new StyleRule()
            {
                Selector = ".hello",
                Styles   = new List <Style>()
                {
                    new Style()
                    {
                        Property = "differentWorld",
                        Value    = "newblah"
                    },
                    new Style()
                    {
                        Property = "world",
                        Value    = "newblah"
                    }
                }
            };
            var InitialStyleRuleList = new StyleRuleList()
            {
                Rules = new List <StyleRule>()
                {
                    new StyleRule()
                    {
                        Selector = ".hello",
                        Styles   = new List <Style>()
                        {
                            new Style()
                            {
                                Property = "world",
                                Value    = "blah"
                            }
                        }
                    }
                }
            };

            //Append the list to itself as a nested rule
            TestStyleRule.NestedRules.AddRule(TestStyleRule);
            InitialStyleRuleList.Rules.First().NestedRules.AddRules(InitialStyleRuleList);

            //Initialize a StyleRuleList
            TestingList = InitialStyleRuleList;

            //Add the test rules
            TestingList.AddRule(TestStyleRule);

            var GeneratedRuleStyle       = TestingList.Rules.Single();
            var GeneratedNestedRuleStyle = GeneratedRuleStyle.NestedRules.Rules.Single();

            //Test adding same selector different property
            Assert.Equal(@"newblah", GeneratedRuleStyle.Styles.Where(x => x.Property == "differentWorld").Single().Value);

            //Test adding same selector same property
            Assert.Equal(@"newblah", GeneratedRuleStyle.Styles.Where(x => x.Property == "world").Single().Value);

            //Test adding same nested selector different property
            Assert.Equal(@"newblah", GeneratedNestedRuleStyle.Styles.Where(x => x.Property == "differentWorld").Single().Value);

            //Test adding same nested selector same property
            Assert.Equal(@"newblah", GeneratedNestedRuleStyle.Styles.Where(x => x.Property == "world").Single().Value);
        }
Exemplo n.º 7
0
        public void AddRuleTest()
        {
            var DefaultFile = new FileInfo(Path.Combine(MockEnvironment.WebRootPath, "css", "custom", "default.css"));
            var RuleOne     = new StyleRule()
            {
                Selector = ".hello",
                Styles   = new List <Style>()
                {
                    new Style()
                    {
                        Property = "world",
                        Value    = "blah"
                    }
                }
            };
            var RuleTwo = new StyleRule()
            {
                Selector = ".hello",
                Styles   = new List <Style>()
                {
                    new Style()
                    {
                        Property = "apple",
                        Value    = "sauce"
                    }
                }
            };

            StyleRuleList RuleList = null;

            //Test file created with default
            Controller.AddRule(RuleList);
            Assert.True(DefaultFile.Exists);
            Assert.Equal(string.Empty, File.ReadAllText(DefaultFile.FullName));

            //Initialize rule list
            RuleList = new StyleRuleList();

            //Test file rules added
            RuleList.AddRule(RuleOne);
            Controller.AddRule(RuleList);
            Assert.Equal(@".hello{world:blah;}", File.ReadAllText(DefaultFile.FullName));

            //Test file rules appended
            RuleList.AddRule(RuleTwo);
            Controller.AddRule(RuleList);
            var content = File.ReadAllText(DefaultFile.FullName);

            Assert.Equal(@".hello{world:blah;apple:sauce;}", content);

            //Test file created with user name
            var UserOneSpecificFile = new FileInfo(Path.Combine(MockEnvironment.WebRootPath, "css", "custom", string.Format("{0}.css", MockAUser())));

            Controller.AddRule(RuleList);
            Assert.True(UserOneSpecificFile.Exists);

            //Test different files for diferent users
            var UserTwoSpecificFile = new FileInfo(Path.Combine(MockEnvironment.WebRootPath, "css", "custom", string.Format("{0}.css", MockAUser())));

            Controller.AddRule(RuleList);
            Assert.True(UserTwoSpecificFile.Exists);

            //Test that files for two different users are not the same
            Assert.NotEqual(UserOneSpecificFile.FullName, UserTwoSpecificFile.FullName);
        }
Exemplo n.º 8
0
 public IActionResult SetRule([FromBody] StyleRuleList list)
 {
     SetCustomStyleContents(list);
     return(RedirectToAction("Index"));
 }