public void TestCreateSimplePanel()
        {
            XLWorkbook   wb    = new XLWorkbook();
            IXLWorksheet ws    = wb.AddWorksheet("Test");
            IXLRange     range = ws.Range(ws.Cell(1, 1), ws.Cell(2, 2));

            range.AddToNamed("s_Test", XLScope.Worksheet);
            IXLNamedRange namedRange = ws.NamedRange("s_Test");

            var report            = new object();
            var templateProcessor = Substitute.For <ITemplateProcessor>();
            var parseSettings     = new PanelParsingSettings
            {
                SimplePanelPrefix    = "s",
                PanelPrefixSeparator = "_",
            };

            var factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);
            var panel   = (ExcelPanel)factory.Create(namedRange, new Dictionary <string, string>
            {
                [nameof(ExcelPanel.Type)]                   = PanelType.Horizontal.ToString(),
                [nameof(ExcelPanel.ShiftType)]              = ShiftType.Row.ToString(),
                [nameof(ExcelPanel.RenderPriority)]         = "5",
                [nameof(ExcelPanel.BeforeRenderMethodName)] = "BeforeRenderMethodName",
                [nameof(ExcelPanel.AfterRenderMethodName)]  = "AfterRenderMethodName",
            });

            Assert.AreEqual(PanelType.Horizontal, panel.Type);
            Assert.AreEqual(ShiftType.Row, panel.ShiftType);
            Assert.AreEqual(5, panel.RenderPriority);
            Assert.AreEqual("BeforeRenderMethodName", panel.BeforeRenderMethodName);
            Assert.AreEqual("AfterRenderMethodName", panel.AfterRenderMethodName);
            Assert.AreEqual(0, panel.Children.Count);
            Assert.IsNull(panel.Parent);
            Assert.AreEqual(namedRange.Ranges.First(), panel.Range);
            Assert.AreSame(report, panel.GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
            Assert.AreSame(templateProcessor, panel.GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));

            namedRange.Delete();
            range.AddToNamed("SS--Test", XLScope.Workbook);
            namedRange = wb.NamedRange("SS--Test");

            parseSettings.SimplePanelPrefix    = "ss";
            parseSettings.PanelPrefixSeparator = "--";
            factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);
            panel   = (ExcelPanel)factory.Create(namedRange, null);

            Assert.IsInstanceOf <ExcelPanel>(panel);
            Assert.AreEqual(PanelType.Vertical, panel.Type);
            Assert.AreEqual(ShiftType.Cells, panel.ShiftType);
            Assert.AreEqual(0, panel.RenderPriority);
            Assert.AreEqual(0, panel.Children.Count);
            Assert.IsNull(panel.BeforeRenderMethodName);
            Assert.IsNull(panel.AfterRenderMethodName);
            Assert.IsNull(panel.Parent);
            Assert.AreEqual(namedRange.Ranges.First(), panel.Range);
            Assert.AreSame(report, panel.GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
            Assert.AreSame(templateProcessor, panel.GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
        }
        public void TestCreateDataSourcePanel()
        {
            XLWorkbook   wb    = new XLWorkbook();
            IXLWorksheet ws    = wb.AddWorksheet("Test");
            IXLRange     range = ws.Range(ws.Cell(1, 1), ws.Cell(2, 2));

            range.AddToNamed("d_Test", XLScope.Worksheet);
            IXLNamedRange namedRange = ws.NamedRange("d_Test");

            var report            = new object();
            var templateProcessor = Substitute.For <ITemplateProcessor>();
            var parseSettings     = new PanelParsingSettings
            {
                DataSourcePanelPrefix = "d",
                PanelPrefixSeparator  = "_",
            };

            var factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);
            var panel   = (ExcelDataSourcePanel)factory.Create(namedRange, new Dictionary <string, string>
            {
                [nameof(ExcelDataSourcePanel.Type)]                           = PanelType.Horizontal.ToString(),
                [nameof(ExcelDataSourcePanel.ShiftType)]                      = ShiftType.Row.ToString(),
                [nameof(ExcelDataSourcePanel.RenderPriority)]                 = "5",
                [nameof(ExcelDataSourcePanel.BeforeRenderMethodName)]         = "BeforeRenderMethodName",
                [nameof(ExcelDataSourcePanel.AfterRenderMethodName)]          = "AfterRenderMethodName",
                [nameof(ExcelDataSourcePanel.BeforeDataItemRenderMethodName)] = "BeforeDataItemRenderMethodName",
                [nameof(ExcelDataSourcePanel.AfterDataItemRenderMethodName)]  = "AfterDataItemRenderMethodName",
                ["DataSource"] = "DS",
            });

            Assert.AreEqual(PanelType.Horizontal, panel.Type);
            Assert.AreEqual(ShiftType.Row, panel.ShiftType);
            Assert.AreEqual(5, panel.RenderPriority);
            Assert.AreEqual("BeforeRenderMethodName", panel.BeforeRenderMethodName);
            Assert.AreEqual("AfterRenderMethodName", panel.AfterRenderMethodName);
            Assert.AreEqual("BeforeDataItemRenderMethodName", panel.BeforeDataItemRenderMethodName);
            Assert.AreEqual("AfterDataItemRenderMethodName", panel.AfterDataItemRenderMethodName);
            Assert.AreEqual(0, panel.Children.Count);
            Assert.IsNull(panel.Parent);
            Assert.AreEqual(namedRange.Ranges.First(), panel.Range);
            Assert.AreSame("DS", panel.GetType().GetField("_dataSourceTemplate", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
            Assert.AreSame(report, panel.GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
            Assert.AreSame(templateProcessor, panel.GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));

            ExceptionAssert.Throws <InvalidOperationException>(() => factory.Create(namedRange, null), "Data source panel must have the property \"DataSource\"");
        }
        public void TestExcelPanelFactoryArgumentsCheck()
        {
            var report            = new object();
            var templateProcessor = Substitute.For <ITemplateProcessor>();
            var parseSettings     = new PanelParsingSettings();

            ExceptionAssert.Throws <ArgumentNullException>(() => new ExcelPanelFactory(null, templateProcessor, parseSettings));
            ExceptionAssert.Throws <ArgumentNullException>(() => new ExcelPanelFactory(report, null, parseSettings));
            ExceptionAssert.Throws <ArgumentNullException>(() => new ExcelPanelFactory(report, templateProcessor, null));

            var factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);

            ExceptionAssert.Throws <ArgumentNullException>(() => factory.Create(null, new Dictionary <string, string>()));
        }
        private IDictionary <string, (IExcelPanel, string)> GetPanelsFlatView(IEnumerable <IXLNamedRange> panelsNamedRanges)
        {
            var panelFactory = new ExcelPanelFactory(Report, TemplateProcessor, PanelParsingSettings);
            IDictionary <string, (IExcelPanel, string)> panels = new Dictionary <string, (IExcelPanel, string)>();

            foreach (IXLNamedRange namedRange in panelsNamedRanges)
            {
                IDictionary <string, string> panelProperties = PanelPropertiesParser.Parse(namedRange.Comment);
                IExcelPanel panel = panelFactory.Create(namedRange, panelProperties);
                panelProperties.TryGetValue("ParentPanel", out string parentPanelName);
                panels[namedRange.Name] = (panel, parentPanelName);
            }
            return(panels);
        }
        public void TestCreateUnsupportedPanelType()
        {
            XLWorkbook   wb    = new XLWorkbook();
            IXLWorksheet ws    = wb.AddWorksheet("Test");
            IXLRange     range = ws.Range(ws.Cell(1, 1), ws.Cell(2, 2));

            range.AddToNamed("b_Test", XLScope.Worksheet);
            IXLNamedRange namedRange = ws.NamedRange("b_Test");

            var report            = new object();
            var templateProcessor = Substitute.For <ITemplateProcessor>();
            var parseSettings     = new PanelParsingSettings {
                PanelPrefixSeparator = "_"
            };

            var factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);

            ExceptionAssert.Throws <NotSupportedException>(() => factory.Create(namedRange, null), "Panel type with prefix \"b\" is not supported");
        }
        public void TestCreatePanelWithBadName()
        {
            XLWorkbook   wb    = new XLWorkbook();
            IXLWorksheet ws    = wb.AddWorksheet("Test");
            IXLRange     range = ws.Range(ws.Cell(1, 1), ws.Cell(2, 2));

            range.AddToNamed("b_Test", XLScope.Worksheet);
            IXLNamedRange namedRange = ws.NamedRange("b_Test");

            var report            = new object();
            var templateProcessor = Substitute.For <ITemplateProcessor>();
            var parseSettings     = new PanelParsingSettings {
                PanelPrefixSeparator = "-"
            };

            var factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);

            ExceptionAssert.Throws <InvalidOperationException>(() => factory.Create(namedRange, null), "Panel name \"b_Test\" does not contain prefix separator \"-\"");
        }