-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathIssue31SuperClass.cs
More file actions
105 lines (90 loc) · 2.95 KB
/
Copy pathIssue31SuperClass.cs
File metadata and controls
105 lines (90 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Linq;
using Chsword.Excel2Object.Internal;
using Chsword.Excel2Object.Options;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
namespace Chsword.Excel2Object.Tests;
[TestClass]
public class Issue31SuperClass
{
[TestMethod]
public void CheckModelA()
{
var excel = TypeConvert.ConvertObjectToExcelModel(GetExcel<SubClassA>()!,
new ExcelExporterOptions());
Assert.IsNotNull(excel);
Assert.AreEqual(1, excel.Sheets?.Count);
Assert.IsNotNull(excel.Sheets);
Assert.AreEqual("SuperClass", excel.Sheets[0].Title);
Console.WriteLine(JsonConvert.SerializeObject(excel));
Assert.IsTrue(excel.Sheets[0].Columns.Any(c => c.Title == "IdA"));
Assert.IsTrue(excel.Sheets[0].Columns.Any(c => c.Title == "P1"));
}
[TestMethod]
public void CheckModelB()
{
var excel = TypeConvert.ConvertObjectToExcelModel(
GetExcel<SubClassB>(),
new ExcelExporterOptions());
Assert.IsNotNull(excel);
Assert.IsNotNull(excel.Sheets);
Assert.AreEqual(1, excel.Sheets.Count);
Assert.AreEqual("SubClassB", excel.Sheets[0].Title);
Console.WriteLine(JsonConvert.SerializeObject(excel));
Assert.IsTrue(excel.Sheets[0].Columns
.Any(c => c.Title == "IdB"));
Assert.IsTrue(excel.Sheets[0]
.Columns.Any(c => c.Title == "P1"));
}
[TestMethod]
public void SuperClassTest()
{
var export = new ExcelExporter();
var bytes = export.ObjectToExcelBytes(GetExcel<SubClassA>());
var importer = new ExcelImporter();
Assert.IsNotNull(bytes);
Console.WriteLine(bytes.Length);
Assert.IsNotNull(importer);
// var model = ExcelImporter.
}
private List<T> GetExcel<T>() where T : SuperClass
{
if (typeof(T).Name == "SubClassA")
{
var list = new List<SubClassA>
{
new() {Id = 1, P = "x"},
new() {Id = 2, P = "x"}
};
return (list as List<T>)!;
}
if (typeof(T).Name == "SubClassB")
{
var list = new List<SubClassB>
{
new() {Id = 11, P = "x"},
new() {Id = 12, P = "x"}
};
return (list as List<T>)!;
}
throw new Exception("not support");
}
[ExcelTitle("SuperClass")]
public abstract class SuperClass
{
[ExcelColumn("Id1")] public int Id { get; set; }
// ReSharper disable once NotNullOrRequiredMemberIsNotInitialized
[ExcelColumn("P1")] public string P { get; set; }
}
public class SubClassA : SuperClass
{
[ExcelColumn("IdA")] public new int Id { get; set; }
}
[ExcelTitle("SubClassB")]
public class SubClassB : SuperClass
{
[ExcelColumn("IdB")] public new int Id { get; set; }
}
}