public void TestCopy() { XLWorkbook wb = new XLWorkbook(); IXLWorksheet ws = wb.AddWorksheet("Test"); var excelReport = Substitute.For <object>(); var templateProcessor = Substitute.For <ITemplateProcessor>(); IXLRange range = ws.Range(1, 1, 3, 4); IXLRange childRange = ws.Range(2, 1, 3, 4); IXLRange childOfChildRange = ws.Range(3, 1, 3, 4); var panel = new ExcelPanel(range, excelReport, templateProcessor) { BeforeRenderMethodName = "BeforeMethod", AfterRenderMethodName = "AfterRender", Type = PanelType.Horizontal, ShiftType = ShiftType.Row, RenderPriority = 1, Children = new List <IExcelPanel> { new ExcelPanel(childRange, excelReport, templateProcessor) { BeforeRenderMethodName = "BeforeMethod_child", AfterRenderMethodName = "AfterRender_child", Type = PanelType.Vertical, ShiftType = ShiftType.NoShift, RenderPriority = 2, Children = new List <IExcelPanel> { new ExcelPanel(childOfChildRange, excelReport, templateProcessor) { BeforeRenderMethodName = "BeforeMethod_child_child", AfterRenderMethodName = "AfterRender_child_child", Type = PanelType.Horizontal, ShiftType = ShiftType.Row, RenderPriority = 3, } } } } }; IExcelPanel copiedPanel = panel.Copy(ws.Cell(5, 5)); Assert.AreSame(excelReport, copiedPanel.GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel)); Assert.AreSame(templateProcessor, copiedPanel.GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel)); Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Range.FirstCell()); Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Range.LastCell()); Assert.IsNull(copiedPanel.Parent); Assert.AreEqual(panel.BeforeRenderMethodName, copiedPanel.BeforeRenderMethodName); Assert.AreEqual(panel.AfterRenderMethodName, copiedPanel.AfterRenderMethodName); Assert.AreEqual(panel.Type, copiedPanel.Type); Assert.AreEqual(panel.ShiftType, copiedPanel.ShiftType); Assert.AreEqual(panel.RenderPriority, copiedPanel.RenderPriority); Assert.AreEqual(1, copiedPanel.Children.Count()); Assert.AreSame(excelReport, copiedPanel.Children.First().GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel.Children.First())); Assert.AreSame(templateProcessor, copiedPanel.GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel.Children.First())); Assert.AreEqual(ws.Cell(6, 5), copiedPanel.Children.First().Range.FirstCell()); Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Children.First().Range.LastCell()); Assert.AreSame(copiedPanel, copiedPanel.Children.First().Parent); Assert.AreEqual(panel.Children.First().BeforeRenderMethodName, copiedPanel.Children.First().BeforeRenderMethodName); Assert.AreEqual(panel.Children.First().AfterRenderMethodName, copiedPanel.Children.First().AfterRenderMethodName); Assert.AreEqual(panel.Children.First().Type, copiedPanel.Children.First().Type); Assert.AreEqual(panel.Children.First().ShiftType, copiedPanel.Children.First().ShiftType); Assert.AreEqual(panel.Children.First().RenderPriority, copiedPanel.Children.First().RenderPriority); Assert.AreEqual(1, copiedPanel.Children.First().Children.Count()); Assert.AreSame(excelReport, copiedPanel.Children.First().Children.First().GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel.Children.First().Children.First())); Assert.AreSame(templateProcessor, copiedPanel.Children.First().Children.First().GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel.Children.First().Children.First())); Assert.AreEqual(ws.Cell(7, 5), copiedPanel.Children.First().Children.First().Range.FirstCell()); Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Children.First().Children.First().Range.LastCell()); Assert.AreSame(copiedPanel.Children.First(), copiedPanel.Children.First().Children.First().Parent); Assert.AreEqual(panel.Children.First().Children.First().BeforeRenderMethodName, copiedPanel.Children.First().Children.First().BeforeRenderMethodName); Assert.AreEqual(panel.Children.First().Children.First().AfterRenderMethodName, copiedPanel.Children.First().Children.First().AfterRenderMethodName); Assert.AreEqual(panel.Children.First().Children.First().Type, copiedPanel.Children.First().Children.First().Type); Assert.AreEqual(panel.Children.First().Children.First().ShiftType, copiedPanel.Children.First().Children.First().ShiftType); Assert.AreEqual(panel.Children.First().Children.First().RenderPriority, copiedPanel.Children.First().Children.First().RenderPriority); IExcelPanel globalParent = new ExcelPanel(ws.Range(1, 1, 20, 20), excelReport, templateProcessor); range = ws.Range(1, 1, 3, 4); IXLRange childRange1 = ws.Range(1, 1, 1, 4); IXLRange childRange2 = ws.Range(2, 1, 3, 4); childOfChildRange = ws.Range(3, 1, 3, 4); panel = new ExcelPanel(range, excelReport, templateProcessor) { Parent = globalParent, Children = new List <IExcelPanel> { new ExcelPanel(childRange1, excelReport, templateProcessor), new ExcelPanel(childRange2, excelReport, templateProcessor) { Children = new List <IExcelPanel> { new ExcelPanel(childOfChildRange, excelReport, templateProcessor) } }, }, }; copiedPanel = panel.Copy(ws.Cell(5, 5)); Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Range.FirstCell()); Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Range.LastCell()); Assert.AreSame(globalParent, copiedPanel.Parent); Assert.AreEqual(2, copiedPanel.Children.Count()); Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Children.First().Range.FirstCell()); Assert.AreEqual(ws.Cell(5, 8), copiedPanel.Children.First().Range.LastCell()); Assert.AreSame(copiedPanel, copiedPanel.Children.First().Parent); Assert.AreEqual(ws.Cell(6, 5), copiedPanel.Children.Last().Range.FirstCell()); Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Children.Last().Range.LastCell()); Assert.AreSame(copiedPanel, copiedPanel.Children.Last().Parent); Assert.AreEqual(1, copiedPanel.Children.Last().Children.Count()); Assert.AreEqual(ws.Cell(7, 5), copiedPanel.Children.Last().Children.First().Range.FirstCell()); Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Children.Last().Children.First().Range.LastCell()); Assert.AreSame(copiedPanel.Children.Last(), copiedPanel.Children.Last().Children.First().Parent); globalParent = new ExcelPanel(ws.Range(1, 1, 7, 7), excelReport, templateProcessor); range = ws.Range(1, 1, 3, 4); childRange1 = ws.Range(1, 1, 1, 4); panel = new ExcelPanel(range, excelReport, templateProcessor) { Parent = globalParent, Children = new List <IExcelPanel> { new ExcelPanel(childRange1, excelReport, templateProcessor) }, }; copiedPanel = panel.Copy(ws.Cell(5, 5)); Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Range.FirstCell()); Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Range.LastCell()); Assert.IsNull(copiedPanel.Parent); Assert.AreEqual(1, copiedPanel.Children.Count()); Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Children.First().Range.FirstCell()); Assert.AreEqual(ws.Cell(5, 8), copiedPanel.Children.First().Range.LastCell()); Assert.AreSame(copiedPanel, copiedPanel.Children.First().Parent); globalParent = new ExcelPanel(ws.Range(1, 1, 7, 8), excelReport, templateProcessor); panel.Parent = globalParent; copiedPanel = panel.Copy(ws.Cell(5, 5), false); Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Range.FirstCell()); Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Range.LastCell()); Assert.AreSame(globalParent, copiedPanel.Parent); Assert.AreEqual(0, copiedPanel.Children.Count()); //wb.SaveAs("test.xlsx"); }