1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-09 17:44:59 +09:00

#188: tests fixed

This commit is contained in:
Kirill 2020-06-24 17:28:56 +03:00
parent 6f6a90f245
commit 5630221e10
No known key found for this signature in database
GPG key ID: 52B176BB96D272ED
4 changed files with 34 additions and 68 deletions

View file

@ -2,7 +2,7 @@
{
"desc": "list with nested lists",
"html": "<ol><li>Google<ul><li>JavaScript</li><li>C++</li></ul></li><li>Facebook<ul><li>Erlang</li></ul></li> <li> Stuff </li></ol>",
"blocks": [{"id":"1","Content":{"text":{"text":"JavaScript","style":9,"marks":{}}}},{"id":"2","Content":{"text":{"text":"C++","style":9,"marks":{}}}},{"id":"3","childrenIds":["1","2",""],"Content":{"text":{"text":"Google","style":10,"marks":{}}}},{"id":"4","Content":{"text":{"text":"Erlang","style":9,"marks":{}}}},{"id":"5","childrenIds":["4"],"Content":{"text":{"text":"Facebook","style":10,"marks":{}}}},{"id":"6","Content":{"text":{"text":"Stuff","style":10,"marks":{}}}}]
"blocks": [{"id":"1","Content":{"text":{"text":"JavaScript","style":9,"marks":{}}}},{"id":"2","Content":{"text":{"text":"C++","style":9,"marks":{}}}},{"id":"3","childrenIds":["1","2"],"Content":{"text":{"text":"Google","style":10,"marks":{}}}},{"id":"4","Content":{"text":{"text":"Erlang","style":9,"marks":{}}}},{"id":"5","childrenIds":["4"],"Content":{"text":{"text":"Facebook","style":10,"marks":{}}}},{"id":"6","Content":{"text":{"text":"Stuff","style":10,"marks":{}}}}]
},
{
@ -120,62 +120,8 @@
{
"desc": "Nested List without spaces",
"html": "<ul><li>Coffee</li><li>Tea<ul><li>Black tea</li><li>Green tea</li></ul></li><li>Milk</li></ul>",
"blocks": [
{
"id": "1",
"Content": {
"text": {
"text": "Coffee",
"style": 9,
"marks": {}
}
}
},
{
"id": "2",
"Content": {
"text": {
"text": "Black tea",
"style": 9,
"marks": {}
}
}
},
{
"id": "3",
"Content": {
"text": {
"text": "Green tea",
"style": 9,
"marks": {}
}
}
},
{
"id": "4",
"childrenIds": [
"2",
"3"
],
"Content": {
"text": {
"text": "Tea",
"style": 9,
"marks": {}
}
}
},
{
"id": "5",
"Content": {
"text": {
"text": "Milk",
"style": 9,
"marks": {}
}
}
}
]
"blocks": [{"id": "1","Content": {"text": {"text": "Coffee","style": 9,"marks": {}}}},{"id": "2","Content": {"text": {"text": "Black tea","style": 9,"marks": {}}}},{"id": "3","Content": {"text": {"text": "Green tea","style": 9,"marks": {}}}},{"id": "4","childrenIds": ["2","3"],"Content": {"text": {"text": "Tea","style": 9,"marks": {}}}},{"id": "5","Content": {"text": {"text": "Milk","style": 9,"marks": {}}}}]
},
{
"desc": "Nested List wrapped",

View file

@ -185,7 +185,7 @@ func (rw *rWriter) GetRootBlockIDs() []string {
func (rw *rWriter) addChildrenId(cId string) {
for i, _ := range rw.blocks {
if rw.blocks[i].Id == rw.listParentId {
if rw.blocks[i].Id == rw.listParentId && len(cId) > 0 {
rw.blocks[i].ChildrenIds = append(rw.blocks[i].ChildrenIds, cId)
}
}

View file

@ -24,20 +24,31 @@ type TestCase struct {
Desc string `json:"desc"`
}
func replaceFakeIds(blocks []*model.Block) {
var m = make(map[string]string, len(blocks))
func replaceFakeIds(anySlot []*model.Block) (anySlotConverted []*model.Block) {
var oldToNew map[string]string
oldToNew = make(map[string]string)
for i, _ := range anySlot {
var oldId = make([]byte, len(anySlot[i].Id))
for i, block := range blocks {
newId := fmt.Sprintf("%d", i+1)
m[block.Id] = newId
block.Id = newId
copy(oldId, anySlot[i].Id)
oldToNew[string(oldId)] = newId
anySlot[i].Id = newId
}
for _, block := range blocks {
for j := range block.ChildrenIds {
block.ChildrenIds[j] = m[block.ChildrenIds[j]]
for i, _ := range anySlot {
cIds := []string{}
for _, cId := range anySlot[i].ChildrenIds {
if len(oldToNew[cId]) > 0 {
cIds = append(cIds, oldToNew[cId])
}
}
anySlot[i].ChildrenIds = cIds
}
return anySlot
}
func TestConvertHTMLToBlocks(t *testing.T) {
@ -61,7 +72,7 @@ func TestConvertHTMLToBlocks(t *testing.T) {
t.Run(testCase.Desc, func(t *testing.T) {
mdToBlocksConverter := New()
_, blocks, _ := mdToBlocksConverter.HTMLToBlocks([]byte(testCase.HTML))
replaceFakeIds(blocks)
blocks = replaceFakeIds(blocks)
actualJson, err := json.Marshal(blocks)
require.NoError(t, err)
@ -74,7 +85,8 @@ func TestConvertHTMLToBlocks(t *testing.T) {
require.NoError(t, err)
if !reflect.DeepEqual(testCase.Blocks, actual) {
fmt.Println("expected:\n", string(actualJson))
fmt.Println("real output:\n", string(actualJson))
fmt.Println("expected:\n", testCase.Blocks)
require.Equal(t, testCase.Blocks, actual)
}
})

View file

@ -39,6 +39,14 @@ func PreprocessBlocks(blocks []*model.Block) (blocksOut []*model.Block) {
blocksOut = append(blocksOut, CombineCodeBlocks(accum))
}
for _, b := range blocks {
for i, cId := range b.ChildrenIds {
if len(cId) == 0 {
b.ChildrenIds = append(b.ChildrenIds[:i], b.ChildrenIds[i+1:]...)
}
}
}
return blocksOut
}